def test_get(self): self.assertTrue( isinstance(PersonStatus.get(PersonStatus.ALIVE), Enum.Value)) self.assertTrue( isinstance(PersonStatus.get(six.text_type('ALIVE')), Enum.Value)) self.assertEqual(PersonStatus.get(PersonStatus.ALIVE), PersonStatus.get(six.text_type('ALIVE')))
def test_get_name(self): self.assertEqual(PersonStatus.get_name(PersonStatus.ALIVE), "ALIVE") self.assertEqual(PersonStatus.get_name(PersonStatus.ALIVE.value), "ALIVE") self.assertEqual(PersonStatus.get_name(PersonStatus.ALIVE.name), "ALIVE") self.assertIsNone(PersonStatus.get_name(89))
def test_choices(self): self.assertEqual(len(PersonStatus.choices()), len(PersonStatus)) for value, member in PersonStatus.choices(): self.assertTrue(isinstance(value, int)) self.assertTrue(isinstance(member, PersonStatus)) self.assertTrue(PersonStatus.get(value) == member) blank = PersonStatus.choices(blank=True)[0] self.assertEqual(blank, (BlankEnum.BLANK.value, BlankEnum.BLANK))
def test_name(self): self.assertEqual(PersonStatus.ALIVE.name, six.text_type("ALIVE")) self.assertEqual(LabelBeer.STELLA.name, six.text_type("STELLA")) # Check that the old classmethod still works. Kept for backward compatibility. self.assertEqual(LabelBeer.name(LabelBeer.STELLA.value), six.text_type("STELLA")) self.assertEqual(PersonStatus.name("ALIVE"), six.text_type("ALIVE")) self.assertEqual(PersonStatus.name(PersonStatus.ALIVE), six.text_type("ALIVE"))
def test_enum_field_init(self): field = EnumField(PersonStatus) self.assertEqual(field.default, PersonStatus.UNBORN) self.assertEqual(len(PersonStatus.choices()), len(field.choices)) field = EnumField(PersonStatus, default=PersonStatus.ALIVE) self.assertEqual(field.default, PersonStatus.ALIVE) field = EnumField(PersonStatus, default=None) self.assertEqual(field.default, None)
def test_enum_form_field_not_required(self): class CustomPersonForm(forms.Form): status = EnumChoiceField(PersonStatus, required=False) form = CustomPersonForm(data={"status": None}, initial={"status": PersonStatus.DEAD.value}) self.assertEqual(form.fields["status"].choices, PersonStatus.choices(blank=True)) self.assertIn(u'<option value="" selected', six.text_type(form["status"])) self.assertTrue(form.is_valid(), form.errors) self.assertEqual(form.cleaned_data["status"], six.text_type())
def test_get(self): self.assertTrue(isinstance(PersonStatus.get(PersonStatus.ALIVE), Enum)) self.assertTrue( isinstance(PersonStatus.get(six.text_type("ALIVE")), Enum)) self.assertEqual( PersonStatus.get(PersonStatus.ALIVE), PersonStatus.get(six.text_type("ALIVE")), ) # Returns `default` if not found self.assertEqual(PersonStatus.get("ALIVEISH", "?"), "?") self.assertEqual(PersonStatus.get(99, "??"), "??")
def test_enum_form_field(self): class CustomPersonForm(forms.Form): status = EnumChoiceField(PersonStatus) form = CustomPersonForm(initial={"status": PersonStatus.DEAD}) self.assertEqual(form["status"].initial, PersonStatus.DEAD.value) self.assertIn( u'<option value="{}" selected'.format(PersonStatus.DEAD.value), six.text_type(form["status"]), ) self.assertEqual(form.fields["status"].choices, PersonStatus.choices()) # Test validation form = CustomPersonForm( data={"status": six.text_type(PersonStatus.ALIVE.value)}, initial={"status": PersonStatus.DEAD.value}, ) self.assertTrue(form.is_valid(), form.errors) self.assertEqual(form.cleaned_data["status"], PersonStatus.ALIVE)
def test_name(self): self.assertEqual(PersonStatus.name(PersonStatus.ALIVE), six.text_type('ALIVE'))
def test_label(self): self.assertEqual(PersonStatus.label(PersonStatus.ALIVE), six.text_type('ALIVE'))
def test_get(self): self.assertTrue( isinstance(PersonStatus.get(PersonStatus.ALIVE), Enum.Value)) self.assertTrue(isinstance(PersonStatus.get(u'ALIVE'), Enum.Value))
def test_equal(self): self.assertTrue(PersonStatus.ALIVE == PersonStatus.ALIVE) self.assertFalse(PersonStatus.ALIVE == PersonStatus.DEAD) self.assertEqual(PersonStatus.get(PersonStatus.ALIVE), PersonStatus.get(PersonStatus.ALIVE))
def test_default(self): self.assertEqual(PersonStatus.default(), PersonStatus.UNBORN)
def test_name(self): self.assertEqual(PersonStatus.name(PersonStatus.ALIVE), u'ALIVE')
def test_label(self): self.assertEqual(PersonStatus.label(PersonStatus.ALIVE), u'Alive')
def test_labels(self): self.assertIsInstance( dict(LabelBeer.choices())[0].label, (Promise, str)) self.assertIsInstance( dict(PersonStatus.choices())[0].label, (Promise, str))
def test_get(self): self.assertTrue(isinstance(PersonStatus.get(PersonStatus.ALIVE), Enum.Value)) self.assertTrue(isinstance(PersonStatus.get(six.text_type('ALIVE')), Enum.Value)) self.assertEqual(PersonStatus.get(PersonStatus.ALIVE), PersonStatus.get(six.text_type('ALIVE')))
def test_choices(self): self.assertEqual(len(PersonStatus.choices()), len(list(PersonStatus.items()))) self.assertTrue(all(key in PersonStatus.__dict__ for key in dict(list(PersonStatus.items()))))
def test_field(self): self.assertTrue(isinstance(PersonStatus.field(), EnumField))
def test_get(self): self.assertTrue(isinstance(PersonStatus.get(PersonStatus.ALIVE), Enum.Value)) self.assertTrue(isinstance(PersonStatus.get(u'ALIVE'), Enum.Value)) self.assertEqual(unicode(PersonStatus.get(PersonStatus.ALIVE)), PersonStatus.label(PersonStatus.ALIVE))
def test_labels(self): self.assertIsInstance(dict(LabelBeer.choices())[0].label, (Promise, str)) self.assertIsInstance(dict(PersonStatus.choices())[0].label, (Promise, str))
def test_items(self): self.assertEqual(len(PersonStatus.items()), len(PersonStatus)) for name, value in PersonStatus.items(): self.assertTrue(isinstance(value, int)) self.assertTrue(isinstance(name, six.string_types)) self.assertEqual(PersonStatus.get(value), PersonStatus.get(name))
def test_get(self): self.assertTrue(isinstance(PersonStatus.get(PersonStatus.ALIVE), Enum.Value)) self.assertTrue(isinstance(PersonStatus.get(u"ALIVE"), Enum.Value))