def test_meta_ordering(self): beatles = Band(name='The Beatles', albums=[ Album(name='Please Please Me', sort_order=2), Album(name='With The Beatles', sort_order=1), Album(name='Abbey Road', sort_order=3), ]) # in the absence of an explicit order_by clause, it should use the ordering as defined # in Album.Meta, which is 'sort_order' albums = [album.name for album in beatles.albums.all()] self.assertEqual(['With The Beatles', 'Please Please Me', 'Abbey Road'], albums)
def setUp(self): self.beatles = Band(name='The Beatles', members=[ BandMember(name='John Lennon'), BandMember(name='Paul McCartney'), ], albums=[ Album(name='Please Please Me', sort_order=1), Album(name='With The Beatles', sort_order=2), Album(name='Abbey Road', sort_order=3), ])
def test_order_by_with_multiple_fields(self): beatles = Band(name='The Beatles', albums=[ Album(name='Please Please Me', sort_order=2), Album(name='With The Beatles', sort_order=1), Album(name='Abbey Road', sort_order=2), ]) albums = [album.name for album in beatles.albums.order_by('sort_order', 'name')] self.assertEqual(['With The Beatles', 'Abbey Road', 'Please Please Me'], albums) albums = [album.name for album in beatles.albums.order_by('sort_order', '-name')] self.assertEqual(['With The Beatles', 'Please Please Me', 'Abbey Road'], albums)
def setUp(self): """ Create 3 Album instances. """ titles = ['foo', 'bar', 'baz'] for title in titles: album = Album(title=title) album.save() self.detail_view = AlbumDetail.as_view() self.data = { 'foo': {'title': 'foo', 'url': 'http://testserver/albums/foo/'}, 'bar': {'title': 'bar', 'url': 'http://testserver/albums/bar/'}, 'baz': {'title': 'baz', 'url': 'http://testserver/albums/baz/'} }
def test_queryset_filter_with_nulls(self): tmbg = Band(name="They Might Be Giants", albums=[ Album(name="Flood", release_date=datetime.date(1990, 1, 1)), Album(name="John Henry", release_date=datetime.date(1994, 7, 21)), Album(name="Factory Showroom", release_date=datetime.date(1996, 3, 30)), Album(name="", release_date=None), Album(name=None, release_date=None), ]) self.assertEqual(tmbg.albums.get(name="Flood").name, "Flood") self.assertEqual(tmbg.albums.get(name="").name, "") self.assertEqual(tmbg.albums.get(name=None).name, None) self.assertEqual(tmbg.albums.get(name__exact="Flood").name, "Flood") self.assertEqual(tmbg.albums.get(name__exact="").name, "") self.assertEqual(tmbg.albums.get(name__exact=None).name, None) self.assertEqual(tmbg.albums.get(name__iexact="flood").name, "Flood") self.assertEqual(tmbg.albums.get(name__iexact="").name, "") self.assertEqual(tmbg.albums.get(name__iexact=None).name, None) self.assertEqual(tmbg.albums.get(name__contains="loo").name, "Flood") self.assertEqual(tmbg.albums.get(name__icontains="LOO").name, "Flood") self.assertEqual(tmbg.albums.get(name__startswith="Flo").name, "Flood") self.assertEqual( tmbg.albums.get(name__istartswith="flO").name, "Flood") self.assertEqual(tmbg.albums.get(name__endswith="ood").name, "Flood") self.assertEqual(tmbg.albums.get(name__iendswith="Ood").name, "Flood") self.assertEqual(tmbg.albums.get(name__lt="A").name, "") self.assertEqual(tmbg.albums.get(name__lte="A").name, "") self.assertEqual(tmbg.albums.get(name__gt="J").name, "John Henry") self.assertEqual(tmbg.albums.get(name__gte="J").name, "John Henry") self.assertEqual( tmbg.albums.get(name__in=["Flood", "Mink Car"]).name, "Flood") self.assertEqual(tmbg.albums.get(name__in=["", "Mink Car"]).name, "") self.assertEqual( tmbg.albums.get(name__in=[None, "Mink Car"]).name, None) self.assertEqual(tmbg.albums.filter(name__isnull=True).count(), 1) self.assertEqual(tmbg.albums.filter(name__isnull=False).count(), 4) self.assertEqual(tmbg.albums.get(name__regex=r'l..d').name, "Flood") self.assertEqual(tmbg.albums.get(name__iregex=r'f..o').name, "Flood")
def test_child_updates_without_ids(self): first_song = Song(name='I Saw Her Standing There') album = Album(name='Please Please Me', songs=[first_song]) beatles = Band(name='The Beatles', albums=[album]) beatles.save() first_song_id = first_song.id second_song = Song(name='Misery') album.songs.add(second_song) AlbumsFormset = childformset_factory(Band, Album, form=ClusterForm, extra=3) albums_formset = AlbumsFormset({ 'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1, 'form-MAX_NUM_FORMS': 1000, 'form-0-name': 'Please Please Me', 'form-0-id': album.id, 'form-0-songs-TOTAL_FORMS': 2, 'form-0-songs-INITIAL_FORMS': 2, 'form-0-songs-MAX_NUM_FORMS': 1000, 'form-0-songs-0-name': 'I Saw Her Standing There', 'form-0-songs-0-id': first_song_id, 'form-0-songs-1-name': 'Misery', 'form-0-songs-1-id': '', }, instance=beatles) self.assertTrue(albums_formset.is_valid()) albums_formset.save(commit=False) self.assertEqual(2, beatles.albums.first().songs.count())
def test_incoming_form_data(self): class BandForm(ClusterForm): class Meta: model = Band fields = ['name'] beatles = Band(name='The Beatles', albums=[ Album(name='Please Please Me', songs=[ Song(name='I Saw Her Standing There') ]), ]) form = BandForm({ 'name': 'The Beatles', 'members-TOTAL_FORMS': 0, 'members-INITIAL_FORMS': 0, 'members-MAX_NUM_FORMS': 1000, 'albums-TOTAL_FORMS': 1, 'albums-INITIAL_FORMS': 0, 'albums-MAX_NUM_FORMS': 1000, 'albums-0-name': 'Please Please Me', 'albums-0-id': '', 'albums-0-ORDER': 1, 'albums-0-songs-TOTAL_FORMS': 2, 'albums-0-songs-INITIAL_FORMS': 1, 'albums-0-songs-MAX_NUM_FORMS': 1000, 'albums-0-songs-0-name': 'I Saw Her Standing There', 'albums-0-songs-0-DELETE': 'albums-0-songs-0-DELETE', 'albums-0-songs-0-id': '', 'albums-0-songs-1-name': 'Misery', 'albums-0-songs-1-id': '', }, instance=beatles) self.assertTrue(form.is_valid()) result = form.save(commit=False) self.assertEqual(result, beatles) self.assertEqual(1, beatles.albums.count()) self.assertEqual('Please Please Me', beatles.albums.first().name) self.assertEqual(1, beatles.albums.first().songs.all().count()) self.assertEqual('Misery', beatles.albums.first().songs.first().name) # should not exist in the database yet self.assertFalse(Album.objects.filter(name='Please Please Me').exists()) self.assertFalse(Song.objects.filter(name='Misery').exists()) beatles.save() # this should create database entries self.assertTrue(Band.objects.filter(name='The Beatles').exists()) self.assertTrue(Album.objects.filter(name='Please Please Me').exists()) self.assertTrue(Song.objects.filter(name='Misery').exists()) self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
def test_ignore_validation_on_deleted_items(self): class BandForm(ClusterForm): class Meta: model = Band fields = ['name'] please_please_me = Album(name='Please Please Me', release_date=datetime.date(1963, 3, 22)) beatles = Band(name='The Beatles', albums=[please_please_me]) beatles.save() form = BandForm( { 'name': "The Beatles", 'members-TOTAL_FORMS': 0, 'members-INITIAL_FORMS': 0, 'members-MAX_NUM_FORMS': 1000, 'albums-TOTAL_FORMS': 1, 'albums-INITIAL_FORMS': 1, 'albums-MAX_NUM_FORMS': 1000, 'albums-0-name': 'With The Beatles', 'albums-0-release_date': '1963-02-31', # invalid date 'albums-0-id': please_please_me.id, 'albums-0-ORDER': 1, 'albums-0-songs-TOTAL_FORMS': 0, 'albums-0-songs-INITIAL_FORMS': 0, 'albums-0-songs-MAX_NUM_FORMS': 1000, }, instance=beatles) self.assertFalse(form.is_valid()) form = BandForm( { 'name': "The Beatles", 'members-TOTAL_FORMS': 0, 'members-INITIAL_FORMS': 0, 'members-MAX_NUM_FORMS': 1000, 'albums-TOTAL_FORMS': 1, 'albums-INITIAL_FORMS': 1, 'albums-MAX_NUM_FORMS': 1000, 'albums-0-name': 'With The Beatles', 'albums-0-release_date': '1963-02-31', # invalid date 'albums-0-id': please_please_me.id, 'albums-0-ORDER': 1, 'albums-0-DELETE': 'albums-0-DELETE', 'albums-0-songs-TOTAL_FORMS': 0, 'albums-0-songs-INITIAL_FORMS': 0, 'albums-0-songs-MAX_NUM_FORMS': 1000, }, instance=beatles) self.assertTrue(form.is_valid()) form.save(commit=False) self.assertEqual(0, beatles.albums.count()) self.assertEqual(1, Band.objects.get(id=beatles.id).albums.count()) beatles.save() self.assertEqual(0, Band.objects.get(id=beatles.id).albums.count())
def test_save_commit_false(self): first_song = Song(name='I Saw Her Standing There') second_song = Song(name='Mystery') album = Album(name='Please Please Me', songs=[first_song, second_song]) beatles = Band(name='The Beatles', albums=[album]) beatles.save() first_song_id, second_song_id = first_song.id, second_song.id AlbumsFormset = childformset_factory(Band, Album, form=ClusterForm, extra=3) albums_formset = AlbumsFormset({ 'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1, 'form-MAX_NUM_FORMS': 1000, 'form-0-name': 'Please Please Me', 'form-0-id': album.id, 'form-0-songs-TOTAL_FORMS': 4, 'form-0-songs-INITIAL_FORMS': 2, 'form-0-songs-MAX_NUM_FORMS': 1000, 'form-0-songs-0-name': 'I Saw Her Standing There', 'form-0-songs-0-DELETE': 'form-0-songs-0-DELETE', 'form-0-songs-0-id': first_song_id, 'form-0-songs-1-name': 'Misery', # changing data of an existing record 'form-0-songs-1-id': second_song_id, 'form-0-songs-2-name': '', 'form-0-songs-2-id': '', 'form-0-songs-3-name': 'Chains', # adding a record 'form-0-songs-3-id': '', }, instance=beatles) self.assertTrue(albums_formset.is_valid()) updated_albums = albums_formset.save(commit=False) # updated_members should only include the items that have been changed and not deleted self.assertEqual(1, len(updated_albums)) self.assertEqual('Please Please Me', updated_albums[0].name) self.assertEqual(2, updated_albums[0].songs.count()) self.assertEqual('Misery', updated_albums[0].songs.first().name) self.assertEqual(second_song_id, updated_albums[0].songs.first().id) self.assertEqual('Chains', updated_albums[0].songs.all()[1].name) self.assertEqual(None, updated_albums[0].songs.all()[1].id) # Changes should not be committed to the db yet self.assertTrue(Song.objects.filter(name='I Saw Her Standing There', id=first_song_id).exists()) self.assertEqual('Mystery', Song.objects.get(id=second_song_id).name) self.assertFalse(Song.objects.filter(name='Chains').exists()) beatles.albums.first().songs.commit() # this should create/update/delete database entries self.assertEqual('Misery', Song.objects.get(id=second_song_id).name) self.assertTrue(Song.objects.filter(name='Chains').exists()) self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
def test_base_instance_relationship_add_child(db_session): """Test adding a child when loading with a base instance.""" track = db_session.query(Track).filter(Track.track_id == 1).first() instance = Album(album_id=9999) instance.tracks.append(track) data = {"album_id": 1, "tracks": [{"track_id": 1}]} schema = AlbumSchema(session=db_session, partial=True) result = schema.load(data, instance=instance) assert result.tracks[0].track_id == 1
def test_saved_items(self): class BandForm(ClusterForm): class Meta: model = Band fields = ['name'] first_song = Song(name='I Saw Her Standing There') second_song = Song(name='Misery') album = Album(name='Please Please Me', songs=[first_song, second_song]) beatles = Band(name='The Beatles', albums=[album]) beatles.save() self.assertTrue(album.id) self.assertTrue(first_song.id) self.assertTrue(second_song.id) form = BandForm({ 'name': 'The Beatles', 'members-TOTAL_FORMS': 0, 'members-INITIAL_FORMS': 0, 'members-MAX_NUM_FORMS': 1000, 'albums-TOTAL_FORMS': 1, 'albums-INITIAL_FORMS': 1, 'albums-MAX_NUM_FORMS': 1000, 'albums-0-name': album.name, 'albums-0-id': album.id, 'albums-0-ORDER': 1, 'albums-0-songs-TOTAL_FORMS': 4, 'albums-0-songs-INITIAL_FORMS': 2, 'albums-0-songs-MAX_NUM_FORMS': 1000, 'albums-0-songs-0-name': first_song.name, 'albums-0-songs-0-DELETE': 'albums-0-songs-0-DELETE', 'albums-0-songs-0-id': first_song.id, 'albums-0-songs-1-name': second_song.name, 'albums-0-songs-1-id': second_song.id, 'albums-0-songs-2-name': 'Anna', 'albums-0-songs-2-id': '', 'albums-0-songs-3-name': '', 'albums-0-songs-3-id': '', }, instance=beatles) self.assertTrue(form.is_valid()) form.save() self.assertTrue(Song.objects.filter(name='Anna').exists()) self.assertTrue(Song.objects.filter(name='Misery').exists()) self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
def test_saved_items_with_non_db_relation(self): class BandForm(ClusterForm): class Meta: model = Band fields = ['name'] first_song = Song(name='I Saw Her Standing There') second_song = Song(name='Misery') album = Album(name='Please Please Me', songs=[first_song, second_song]) beatles = Band(name='The Beatles', albums=[album]) beatles.save() # pack and unpack the record so that we're working with a non-db-backed queryset new_beatles = Band.from_json(beatles.to_json()) form = BandForm({ 'name': 'The Beatles', 'members-TOTAL_FORMS': 0, 'members-INITIAL_FORMS': 0, 'members-MAX_NUM_FORMS': 1000, 'albums-TOTAL_FORMS': 1, 'albums-INITIAL_FORMS': 1, 'albums-MAX_NUM_FORMS': 1000, 'albums-0-name': album.name, 'albums-0-id': album.id, 'albums-0-ORDER': 1, 'albums-0-songs-TOTAL_FORMS': 4, 'albums-0-songs-INITIAL_FORMS': 2, 'albums-0-songs-MAX_NUM_FORMS': 1000, 'albums-0-songs-0-name': first_song.name, 'albums-0-songs-0-DELETE': 'albums-0-songs-0-DELETE', 'albums-0-songs-0-id': first_song.id, 'albums-0-songs-1-name': second_song.name, 'albums-0-songs-1-id': second_song.id, 'albums-0-songs-2-name': 'Anna', 'albums-0-songs-2-id': '', 'albums-0-songs-3-name': '', 'albums-0-songs-3-id': '', }, instance=new_beatles) self.assertTrue(form.is_valid()) form.save() self.assertTrue(Song.objects.filter(name='Anna').exists()) self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
def test_serialize_json_with_dates(self): beatles = Band(name='The Beatles', members=[ BandMember(name='John Lennon'), BandMember(name='Paul McCartney'), ], albums=[ Album(name='Rubber Soul', release_date=datetime.date(1965, 12, 3)) ]) beatles_json = beatles.to_json() self.assertTrue("John Lennon" in beatles_json) self.assertTrue("1965-12-03" in beatles_json) unpacked_beatles = Band.from_json(beatles_json) self.assertEqual(datetime.date(1965, 12, 3), unpacked_beatles.albums.all()[0].release_date)
def test_save_with_update_fields(self): beatles = Band(name='The Beatles', members=[ BandMember(name='John Lennon'), BandMember(name='Paul McCartney'), ], albums=[ Album(name='Please Please Me', sort_order=1), Album(name='With The Beatles', sort_order=2), Album(name='Abbey Road', sort_order=3), ]) beatles.save() # modify both relations, but only commit the change to members beatles.members.clear() beatles.albums.clear() beatles.name = 'The Rutles' beatles.save(update_fields=['name', 'members']) updated_beatles = Band.objects.get(pk=beatles.pk) self.assertEqual(updated_beatles.name, 'The Rutles') self.assertEqual(updated_beatles.members.count(), 0) self.assertEqual(updated_beatles.albums.count(), 3)
def test_can_create_formset(self): beatles = Band(name='The Beatles', albums=[ Album(name='Please Please Me', songs=[ Song(name='I Saw Her Standing There'), Song(name='Misery') ]) ]) AlbumsFormset = childformset_factory(Band, Album, form=ClusterForm, extra=3) albums_formset = AlbumsFormset(instance=beatles) self.assertEqual(4, len(albums_formset.forms)) self.assertEqual('Please Please Me', albums_formset.forms[0].instance.name) self.assertEqual(5, len(albums_formset.forms[0].formsets['songs'].forms)) self.assertEqual( 'I Saw Her Standing There', albums_formset.forms[0].formsets['songs'].forms[0].instance.name )
def test_can_omit_formset_from_submission(self): """ If no explicit `formsets` parameter has been given, any formsets missing from the submission should be skipped over. https://github.com/wagtail/wagtail/issues/5414#issuecomment-567468127 """ class BandForm(ClusterForm): class Meta: model = Band fields = ['name'] john = BandMember(name='John Lennon') paul = BandMember(name='Paul McCartney') abbey_road = Album(name='Abbey Road') beatles = Band(name='The Beatles', members=[john, paul], albums=[abbey_road]) beatles.save() form = BandForm( { 'name': "The Beatles", 'members-TOTAL_FORMS': 3, 'members-INITIAL_FORMS': 2, 'members-MAX_NUM_FORMS': 1000, 'members-0-name': john.name, 'members-0-DELETE': 'members-0-DELETE', 'members-0-id': john.id, 'members-1-name': paul.name, 'members-1-id': paul.id, 'members-2-name': 'George Harrison', 'members-2-id': '', }, instance=beatles) self.assertTrue(form.is_valid()) form.save() beatles = Band.objects.get(id=beatles.id) self.assertEqual(1, beatles.albums.count()) self.assertTrue( BandMember.objects.filter(name='George Harrison').exists()) self.assertFalse( BandMember.objects.filter(name='John Lennon').exists())
def test_nested_formsets(self): class BandForm(ClusterForm): class Meta: model = Band fields = ['name'] self.assertTrue(BandForm.formsets) beatles = Band(name='The Beatles', albums=[ Album(name='Please Please Me', songs=[ Song(name='I Saw Her Standing There'), Song(name='Misery') ]), ]) form = BandForm(instance=beatles) self.assertEqual(4, len(form.formsets['albums'].forms)) self.assertEqual(5, len(form.formsets['albums'].forms[0].formsets['songs'])) self.assertTrue('songs' in form.as_p())
def test_cannot_omit_explicit_formset_from_submission(self): """ If an explicit `formsets` parameter has been given, formsets missing from a form submission should raise a ValidationError as normal """ class BandForm(ClusterForm): class Meta: model = Band fields = ['name'] formsets = ['members', 'albums'] john = BandMember(name='John Lennon') paul = BandMember(name='Paul McCartney') abbey_road = Album(name='Abbey Road') beatles = Band(name='The Beatles', members=[john, paul], albums=[abbey_road]) beatles.save() form = BandForm({ 'name': "The Beatles", 'members-TOTAL_FORMS': 3, 'members-INITIAL_FORMS': 2, 'members-MAX_NUM_FORMS': 1000, 'members-0-name': john.name, 'members-0-DELETE': 'members-0-DELETE', 'members-0-id': john.id, 'members-1-name': paul.name, 'members-1-id': paul.id, 'members-2-name': 'George Harrison', 'members-2-id': '', }, instance=beatles) with self.assertRaises(ValidationError): form.is_valid()
def test_date_filters(self): tmbg = Band(name="They Might Be Giants", albums=[ Album(name="Flood", release_date=datetime.date(1990, 1, 1)), Album(name="John Henry", release_date=datetime.date(1994, 7, 21)), Album(name="Factory Showroom", release_date=datetime.date(1996, 3, 30)), Album(name="The Complete Dial-A-Song", release_date=None), ]) logs = FakeQuerySet(Log, [ Log(time=datetime.datetime(1979, 7, 1, 1, 1, 1), data="nobody died"), Log(time=datetime.datetime(1980, 2, 2, 2, 2, 2), data="one person died"), Log(time=None, data="nothing happened") ]) self.assertEqual( tmbg.albums.get( release_date__range=(datetime.date(1994, 1, 1), datetime.date(1994, 12, 31))).name, "John Henry") self.assertEqual( logs.get(time__range=( datetime.datetime(1980, 1, 1, 1, 1, 1), datetime.datetime(1980, 12, 31, 23, 59, 59))).data, "one person died") self.assertEqual( tmbg.albums.get( release_date__date=datetime.date(1994, 7, 21)).name, "John Henry") self.assertEqual( logs.get(time__date=datetime.date(1980, 2, 2)).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__year='1994').name, "John Henry") self.assertEqual(logs.get(time__year=1980).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__month=7).name, "John Henry") self.assertEqual(logs.get(time__month='2').data, "one person died") self.assertEqual( tmbg.albums.get(release_date__day='21').name, "John Henry") self.assertEqual(logs.get(time__day=2).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__week=29).name, "John Henry") self.assertEqual(logs.get(time__week='5').data, "one person died") self.assertEqual( tmbg.albums.get(release_date__week_day=5).name, "John Henry") self.assertEqual(logs.get(time__week_day=7).data, "one person died") self.assertEqual( tmbg.albums.get(release_date__quarter=3).name, "John Henry") self.assertEqual(logs.get(time__quarter=1).data, "one person died") self.assertEqual( logs.get(time__time=datetime.time(2, 2, 2)).data, "one person died") self.assertEqual(logs.get(time__hour=2).data, "one person died") self.assertEqual(logs.get(time__minute='2').data, "one person died") self.assertEqual(logs.get(time__second=2).data, "one person died")