コード例 #1
0
 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"),
         ],
     )
コード例 #2
0
    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')
コード例 #3
0
    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)
コード例 #4
0
    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)
コード例 #5
0
    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')
コード例 #6
0
    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')
コード例 #7
0
    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
コード例 #8
0
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"))
コード例 #9
0
ファイル: enums.py プロジェクト: lamby/31bananas
class StateEnum(Enumeration):
    DRAFT = Item(10, 'draft', "Draft")
    TASTED = Item(20, 'tasted', "Tasted")
    PUBLISHED = Item(30, 'published', "Published")
コード例 #10
0
ファイル: enums.py プロジェクト: danpalmer/common_sense
from django_enumfield import Enum, Item


ConsultationStateEnum = Enum('ConsultationStateEnum',
    Item(10, 'open', 'Open consultation'),
    Item(20, 'closed', 'Closed consultation'),
    Item(30, 'outcome', 'Consultation outcome'),
)
コード例 #11
0
 def test_lazy_translation_in_display(self):
     item = Item(10, 'slug', _("Display"))
     self.assertEqual(item.display, "Display")
コード例 #12
0
 def test_hash(self):
     self.assertEqual(hash(Item(10, 'slug', "display")), 10)
コード例 #13
0
 def test_repr(self):
     self.assertEqual(
         repr(Item(10, 'slug', "display")),
         "<enum.Item: 10 slug 'display'>",
     )
コード例 #14
0
 def test_str(self):
     self.assertEqual(str(Item(10, 'slug', "display")), 'slug')
コード例 #15
0
    def test_item(self):
        item = Item(10, 'slug', "Display")

        self.assertEqual(item.value, 10)
        self.assertEqual(item.slug, 'slug')
        self.assertEqual(item.display, "Display")
コード例 #16
0
    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"))