def test_add_should_add_in_correct_order(self): SOME_CHOICES = Choices( ('ONE', 1, u'One'), ('TWO', 2, u'Two'), ) OTHER_CHOICES = Choices( ('THREE', 3, u'Three'), ('FOUR', 4, u'Four'), ) # Adding a choices to choices tup = SOME_CHOICES + OTHER_CHOICES self.assertEqual(tup, ((1, 'One'), (2, 'Two'), (3, 'Three'), (4, 'Four'))) # Adding a tuple to choices tup = SOME_CHOICES + ((3, 'Three'), (4, 'Four')) self.assertEqual(tup, ((1, 'One'), (2, 'Two'), (3, 'Three'), (4, 'Four')))
def test_retrocompatibility(self): MY_CHOICES = Choices(('TWO', 2, u'Deux'), ('FOUR', 4, u'Quatre'), name="EVEN") MY_CHOICES.add_choices( "ODD", ('ONE', 1, u'Un'), ('THREE', 3, u'Trois'), ) self.assertEqual(MY_CHOICES.CHOICES, ((2, u'Deux'), (4, u'Quatre'), (1, u'Un'), (3, u'Trois'))) self.assertEqual(MY_CHOICES.ODD, ((1, u'Un'), (3, u'Trois'))) self.assertEqual(MY_CHOICES.EVEN, ((2, u'Deux'), (4, u'Quatre')))
# -*- coding: utf-8 -*- from django.test import TestCase from django import forms from extended_choices.choices import Choices from extended_choices.fields import NamedExtendedChoiceFormField MY_CHOICES = Choices( ('ONE', 1, u'One for the money'), ('TWO', 2, u'Two for the show'), ('THREE', 3, u'Three to get ready'), ) MY_CHOICES.add_subset("ODD", ("ONE", "THREE")) class FieldsTests(TestCase): """ Testing the fields """ def test_named_extended_choice_form_field(self): """ Should return accept only string, and should return the integer value. """ field = NamedExtendedChoiceFormField(choices=MY_CHOICES) # Should work with lowercase self.assertEqual(field.clean("one"), 1) # Should word with uppercase self.assertEqual(field.clean("ONE"), 1) # Should not validate with wrong name self.assertRaises(forms.ValidationError, field.clean, "FOUR") # Should not validate with integer