Ejemplo n.º 1
0
 def test_choicefield(self):
     from dj._choicestestproject.app.models import Favourites, Color,\
             MusicGenre, Sports
     judy = Favourites.create(name='Judy')
     self.assertEqual(judy.color, Color.green)
     self.assertEqual(judy.music, MusicGenre.banjo)
     self.assertEqual(judy.sport, Sports.poker)
     self.assertEqual(judy.nullable, None)
     judy.color = Color.blue
     judy.music = MusicGenre.rock
     judy.sport = Sports.mountaineering
     judy.save()
     judy2 = Favourites.objects.get(name='Judy')
     self.assertEqual(judy2.color, Color.blue)
     self.assertEqual(judy2.music, MusicGenre.rock)
     self.assertEqual(judy2.sport, Sports.mountaineering)
     tom = Favourites.create(name='Tom', sport=Sports.mountaineering)
     self.assertEqual(Favourites.objects.filter(sport=102).count(), 2)
     self.assertEqual(
         Favourites.objects.filter(sport=Sports.mountaineering).count(), 2)
     self.assertEqual(Favourites.objects.filter(sport__gt=10).count(), 2)
     self.assertEqual(Favourites.objects.filter(color__lte=2).count(), 1)
     tom.color = 1
     tom.save()
     tom2 = Favourites.objects.get(name='Tom')
     self.assertEqual(tom2.color, Color.red)
     tom_and_judy = Favourites.objects.filter(color__in=(Color.blue,
                                                         Color.red))
     self.assertEqual(tom_and_judy.count(), 2)
     self.assertEqual(set(obj.name for obj in tom_and_judy),
                      set(['Judy', 'Tom']))
     self.assertEqual(tom2.get_sport_display(), 'Mountaineering')
Ejemplo n.º 2
0
 def test_choicefield(self):
     from dj._choicestestproject.app.models import Favourites, Color,\
             MusicGenre, Sports
     judy = Favourites.create(name='Judy')
     self.assertEqual(judy.color, Color.green)
     self.assertEqual(judy.music, MusicGenre.banjo)
     self.assertEqual(judy.sport, Sports.poker)
     self.assertEqual(judy.nullable, None)
     judy.color = Color.blue
     judy.music = MusicGenre.rock
     judy.sport = Sports.mountaineering
     judy.save()
     judy2 = Favourites.objects.get(name='Judy')
     self.assertEqual(judy2.color, Color.blue)
     self.assertEqual(judy2.music, MusicGenre.rock)
     self.assertEqual(judy2.sport, Sports.mountaineering)
     tom = Favourites.create(name='Tom', sport=Sports.mountaineering)
     self.assertEqual(Favourites.objects.filter(sport=102).count(), 2)
     self.assertEqual(Favourites.objects.filter(
         sport=Sports.mountaineering).count(), 2)
     self.assertEqual(Favourites.objects.filter(
         sport__gt=10).count(), 2)
     self.assertEqual(Favourites.objects.filter(
         color__lte=2).count(), 1)
     tom.color = 1
     tom.save()
     tom2 = Favourites.objects.get(name='Tom')
     self.assertEqual(tom2.color, Color.red)
     tom_and_judy = Favourites.objects.filter(color__in=(Color.blue, Color.red))
     self.assertEqual(tom_and_judy.count(), 2)
     self.assertEqual(set(obj.name for obj in tom_and_judy), set(['Judy', 'Tom']))
     self.assertEqual(tom2.get_sport_display(), 'Mountaineering')
Ejemplo n.º 3
0
 def test_form_with_choicefields(self):
     from dj._choicestestproject.app.models import Favourites, Color,\
             MusicGenre, Sports
     from dj._choicestestproject.app.forms import FavouritesForm
     empty_form = FavouritesForm()
     self.assertFalse(empty_form.is_valid())
     self.assertEqual(empty_form._bound_value('color'), Color.green.id)
     self.assertEqual(empty_form._bound_value('music'), None)
     self.assertEqual(empty_form._bound_value('sport'), None)
     self.assertEqual(empty_form._bound_value('name'), None)
     self.assertEqual(empty_form._bound_value('nullable'), None)
     empty_form_data = FavouritesForm(data={
             'color': 1, 'music': 2, 'sport': 3, 'name': 'Richard'})
     self.assertTrue(empty_form_data.is_valid())
     self.assertEqual(empty_form_data._bound_value('color'), Color.red.id)
     self.assertEqual(empty_form_data._bound_value('music'),
             MusicGenre.country.id)
     self.assertEqual(empty_form_data._bound_value('sport'), Sports.baseball.id)
     self.assertEqual(empty_form_data._bound_value('name'), 'Richard')
     self.assertEqual(empty_form_data._bound_value('nullable'), None)
     judy = Favourites.create(name='Judy')
     judy_form = FavouritesForm(instance=judy)
     self.assertFalse(judy_form.is_valid()) # because it's not bound
     self.assertEqual(judy_form._bound_value('color'), Color.green.id)
     self.assertEqual(judy_form._bound_value('music'), MusicGenre.banjo.id)
     self.assertEqual(judy_form._bound_value('sport'), Sports.poker.id)
     self.assertEqual(judy_form._bound_value('name'), 'Judy')
     self.assertEqual(judy_form._bound_value('nullable'), None)
     data_for_form = dict(judy_form.initial)
     data_for_form.update({'nullable': Color.red})
     judy_form_data = FavouritesForm(instance=judy, data=data_for_form)
     self.assertTrue(judy_form_data.is_valid())
     self.assertEqual(judy_form_data._bound_value('color'), Color.green.id)
     self.assertEqual(judy_form_data._bound_value('music'), MusicGenre.banjo.id)
     self.assertEqual(judy_form_data._bound_value('sport'), Sports.poker.id)
     self.assertEqual(judy_form_data._bound_value('name'), 'Judy')
     self.assertEqual(judy_form_data._bound_value('nullable'), Color.red.id)
     invalid_data_for_form = dict(judy_form.initial)
     invalid_data_for_form.update({'color': -1, 'music': '', 'sport': None,
             'nullable': ''})
     judy_form_invalid_data = FavouritesForm(instance=judy,
             data=invalid_data_for_form)
     self.assertFalse(judy_form_invalid_data.is_valid())
     self.assertEqual(set(judy_form_invalid_data.errors.keys()),
             set(['color', 'music', 'sport']))
     self.assertTrue('not a valid choice' in
             judy_form_invalid_data.errors['color'][0])
     self.assertTrue('cannot be null' in
             judy_form_invalid_data.errors['music'][0])
     self.assertTrue('cannot be null' in
             judy_form_invalid_data.errors['sport'][0])
Ejemplo n.º 4
0
 def test_form_with_choicefields(self):
     from dj._choicestestproject.app.models import Favourites, Color,\
             MusicGenre, Sports
     from dj._choicestestproject.app.forms import FavouritesForm
     empty_form = FavouritesForm()
     self.assertFalse(empty_form.is_valid())
     self.assertEqual(empty_form._bound_value('color'), Color.green.id)
     self.assertEqual(empty_form._bound_value('music'), None)
     self.assertEqual(empty_form._bound_value('sport'), None)
     self.assertEqual(empty_form._bound_value('name'), None)
     self.assertEqual(empty_form._bound_value('nullable'), None)
     empty_form_data = FavouritesForm(data={
             'color': 1, 'music': 2, 'sport': 3, 'name': 'Richard'})
     self.assertTrue(empty_form_data.is_valid())
     self.assertEqual(empty_form_data._bound_value('color'), Color.red.id)
     self.assertEqual(empty_form_data._bound_value('music'),
             MusicGenre.country.id)
     self.assertEqual(empty_form_data._bound_value('sport'), Sports.baseball.id)
     self.assertEqual(empty_form_data._bound_value('name'), 'Richard')
     self.assertEqual(empty_form_data._bound_value('nullable'), None)
     judy = Favourites.create(name='Judy')
     judy_form = FavouritesForm(instance=judy)
     self.assertFalse(judy_form.is_valid()) # because it's not bound
     self.assertEqual(judy_form._bound_value('color'), Color.green.id)
     self.assertEqual(judy_form._bound_value('music'), MusicGenre.banjo.id)
     self.assertEqual(judy_form._bound_value('sport'), Sports.poker.id)
     self.assertEqual(judy_form._bound_value('name'), 'Judy')
     self.assertEqual(judy_form._bound_value('nullable'), None)
     data_for_form = dict(judy_form.initial)
     data_for_form.update({'nullable': Color.red})
     judy_form_data = FavouritesForm(instance=judy, data=data_for_form)
     self.assertTrue(judy_form_data.is_valid())
     self.assertEqual(judy_form_data._bound_value('color'), Color.green.id)
     self.assertEqual(judy_form_data._bound_value('music'), MusicGenre.banjo.id)
     self.assertEqual(judy_form_data._bound_value('sport'), Sports.poker.id)
     self.assertEqual(judy_form_data._bound_value('name'), 'Judy')
     self.assertEqual(judy_form_data._bound_value('nullable'), Color.red.id)
     invalid_data_for_form = dict(judy_form.initial)
     invalid_data_for_form.update({'color': -1, 'music': '', 'sport': None,
             'nullable': ''})
     judy_form_invalid_data = FavouritesForm(instance=judy,
             data=invalid_data_for_form)
     self.assertFalse(judy_form_invalid_data.is_valid())
     self.assertEqual(set(judy_form_invalid_data.errors.keys()),
             set(['color', 'music', 'sport']))
     self.assertTrue('not a valid choice' in
             judy_form_invalid_data.errors['color'][0])
     self.assertTrue('cannot be null' in
             judy_form_invalid_data.errors['music'][0])
     self.assertTrue('cannot be null' in
             judy_form_invalid_data.errors['sport'][0])