I wrote the following for a Enum class
class AbstractBaseQuotation(object):
@total_ordering
class STATE(namedtuple("State", "human_readable path order_in_path description"), Enum):
DRAFT = "Draft", "happy", 1, "Anything that is not sent to vendor"
PENDING_VENDOR = (
"Pending Vendor",
"happy",
2,
"Anything that is in vendor inbox",
)
CANCELLED = (
"Cancelled",
"cancel",
None,
"Any Order past the Draft stage can be cancelled",
)
@property
def db_value(self):
"""
Opinionated decision to use the member name as the database value to store
"""
return self.name
def _is_cancelled(self):
return self == AbstractBaseQuotation.STATE.CANCELLED
def __lt__(self, other):
if (
self.__class__ is AbstractBaseQuotation.STATE.DRAFT.__class__
and self._is_cancelled()
):
raise ValueError("Cannot compare CANCELLED with other states")
if (
other.__class__ is AbstractBaseQuotation.STATE.DRAFT.__class__
and other._is_cancelled()
):
raise ValueError("Cannot compare CANCELLED with other states")
if self.__class__ is other.__class__:
return self.order_in_path < other.order_in_path
return NotImplemented
This is my unit test
with self.assertRaises(ValueError) as raised:
AbstractBaseQuotation.STATE.CANCELLED > AbstractBaseQuotation.STATE.DRAFT
I know what assertRaises expects a callable. but I am not sure how to test
I have also tried this:
def test_compare_across_states(self):
with self.assertRaises(ValueError) as raised:
def compare():
return AbstractBaseQuotation.STATE.CANCELLED > AbstractBaseQuotation.STATE.DRAFT
compare()
Both times I get AssertionError: ValueError not raised
How do I test for ValueError in that case?