def test_get_choices(self): self.assertEqual( self.enum.get_choices(), [ (Item(10, 'a', "Item A"), "Item A"), (Item(20, 'b', "Item B"), "Item B"), ], )
def test_dynamic_enum(self): FooEnum = Enum('FooEnum') FooEnum.add_item(Item(10, 'a', "Item A")) FooEnum.add_item(Item(20, 'b', "Item B")) self.assertEqual(len(FooEnum), 2) self.assertEqual(FooEnum.A.slug, 'a') self.assertEqual(FooEnum.B.display, "Item B") self.assertEqual(FooEnum.from_value(10).slug, 'a')
def test_comparison(self): item1 = Item(10, 'slug1', "display1") item2 = Item(20, 'slug2', "display2") item2_copy = Item(20, 'slug2', "display2") self.assertLess(item1, item2) self.assertGreater(item2, item1) self.assertGreaterEqual(item2, item2_copy) self.assertLessEqual(item2, item2_copy)
def test_invalid_types(self): with self.assertRaises(TypeError): Item('not an int', 'slug', "display") with self.assertRaises(TypeError): Item(10, 999, "display") with self.assertRaises(TypeError): Item(10, 'slug', 999)
def test_instance_based_enum(self): FooEnum = Enum( 'FooEnum', Item(10, 'a', "Item A"), Item(20, 'b', "Item B"), ) self.assertEqual(len(FooEnum), 2) self.assertEqual(FooEnum.A.slug, 'a') self.assertEqual(FooEnum.B.display, "Item B") self.assertEqual(FooEnum.from_value(10).slug, 'a')
def test_eq(self): item1 = Item(10, 'slug', "display") item2 = Item(10, 'slug', "display") item3 = Item(20, 'slug3', "display") self.assertEqual(item1, item2) self.assertNotEqual(item1, item3) self.assertEqual('slug', item1) self.assertEqual(item3, 'slug3') self.assertNotEqual(item2, 'slug2')
def setUp(self): super(EnumTests, self).setUp() FooEnum = Enum( 'FooEnum', Item(10, 'a', "Item A"), Item(20, 'b', "Item B"), ) LargeEnum = Enum( 'LargeEnum', Item(10, 'item_a', "Item A"), Item(20, 'item_b', "Item B"), Item(30, 'item_c', "Item C"), Item(40, 'item_d', "Item D"), Item(50, 'item_e', "Item E"), ) self.enum = FooEnum self.large_enum = LargeEnum
from django_enumfield import Enum, Item SecurityEventEnum = Enum( 'SecurityEventEnum', Item(10, 'possible_replay_attack', "An expired HMAC was used"), Item(20, 'useragent', "An incorrect useragent accessed the API"), Item(30, 'sentry_ipspoof', "An incorrect IP used the Sentry useragent"), Item(40, 'gameip_missmatch', "An incorrect IP accessed the game server API"), Item(50, 'no_body', "A request was received with no body"), Item(60, 'associated_reverse_engineer', "An account was marked as an RE by association"), Item(70, 'alt_detected', "An account was detected as an alternate account"), Item(80, 'invalid_data', "Invalid data was sent to the server"))
class StateEnum(Enumeration): DRAFT = Item(10, 'draft', "Draft") TASTED = Item(20, 'tasted', "Tasted") PUBLISHED = Item(30, 'published', "Published")
from django_enumfield import Enum, Item ConsultationStateEnum = Enum('ConsultationStateEnum', Item(10, 'open', 'Open consultation'), Item(20, 'closed', 'Closed consultation'), Item(30, 'outcome', 'Consultation outcome'), )
def test_lazy_translation_in_display(self): item = Item(10, 'slug', _("Display")) self.assertEqual(item.display, "Display")
def test_hash(self): self.assertEqual(hash(Item(10, 'slug', "display")), 10)
def test_repr(self): self.assertEqual( repr(Item(10, 'slug', "display")), "<enum.Item: 10 slug 'display'>", )
def test_str(self): self.assertEqual(str(Item(10, 'slug', "display")), 'slug')
def test_item(self): item = Item(10, 'slug', "Display") self.assertEqual(item.value, 10) self.assertEqual(item.slug, 'slug') self.assertEqual(item.display, "Display")
def test_dynamic_enum_rejects_duplicate_slug(self): FooEnum = Enum('FooEnum') FooEnum.add_item(Item(10, 'a', "Item A")) with self.assertRaises(ValueError): FooEnum.add_item(Item(20, 'a', "Item B"))