예제 #1
0
    def test_deletion(self):
        with revision:
            queen = Artist(name='Queen')
            queen.save()

            a_kind_of_magic = Album(artist=queen, title='A Kind of Magic')
            a_kind_of_magic.save()

            dont_lose_your_head = Song(album=a_kind_of_magic, title="Don't Lose Your Head")
            dont_lose_your_head.save()

        first_revision = revision.latest_transactions['default']

        with revision:
            princes_of_the_universe = Song(album=a_kind_of_magic, title='Princes of the Universe')
            princes_of_the_universe.save()

            dont_lose_your_head.delete()

        second_revision = revision.latest_transactions['default']

        with revision:
            friends_will_be_friends = Song(album=a_kind_of_magic, title='Friends Will Be Friends')
            friends_will_be_friends.save()

        third_revision = revision.latest_transactions['default']

        self.assertEqual(Song.objects.version(first_revision).get(pk=dont_lose_your_head.pk), dont_lose_your_head)
        self.assertRaises(Song.DoesNotExist, Song.objects.version(second_revision).get, pk=dont_lose_your_head.pk)
        self.assertRaises(Song.DoesNotExist, Song.objects.version(third_revision).get, pk=dont_lose_your_head.pk)
        self.assertRaises(Song.DoesNotExist, Song.objects.get, pk=dont_lose_your_head.pk)
        self.assertEqual(list(Album.objects.version(first_revision).get(pk=a_kind_of_magic.pk).songs.all()), [dont_lose_your_head])
        self.assertEqual(list(Album.objects.version(second_revision).get(pk=a_kind_of_magic.pk).songs.all()), [princes_of_the_universe])
        self.assertEqual(list(Album.objects.version(third_revision).get(pk=a_kind_of_magic.pk).songs.all()), [princes_of_the_universe, friends_will_be_friends])
예제 #2
0
    def test_field_include(self):
        with revision:
            queen = Artist(name='Queen')
            queen.save()

            a_kind_of_magic = Album(artist=queen, title='A Kind of Magic')
            a_kind_of_magic.save()

        data = revision.data(a_kind_of_magic)
        self.assertEqual(data['field'].keys(), ['_versions_status', 'title'])
예제 #3
0
    def test_staged_edits_new(self):
        with revision:
            queen = Artist(name='Queen')
            queen.save()

            a_kind_of_magic = Album(artist=queen, title='A Kind of Magic')
            a_kind_of_magic.save()

            dont_lose_your_head = Song(album=a_kind_of_magic, title="Don't Lose Your Head")
            dont_lose_your_head.save()

            princes_of_the_universe = Song(album=a_kind_of_magic, title='Princes of the Universe')
            princes_of_the_universe.save()

            original_lyrics = Lyrics(song=princes_of_the_universe, text="Dont lose your head")
            original_lyrics.stage()

        first_revision = revision.latest_transactions['default']

        with revision:
            original_lyrics.song = dont_lose_your_head
            original_lyrics.stage()

        second_revision = revision.latest_transactions['default']

        # Verify that the lyrics object does not exist from the published perspective.
        self.assertRaises(Lyrics.DoesNotExist, Lyrics.objects.get, pk=original_lyrics.pk)
        # Vefify that the the published Song object does not know that the new lyrics exist.
        self.assertEquals(list(Song.objects.get(pk=princes_of_the_universe.pk).lyrics.all()), [])

        # Verify that the first staged version of the Song object knows that the lyrics exist.
        self.assertEquals(list(Song.objects.version(first_revision).get(pk=princes_of_the_universe.pk).lyrics.all()), [original_lyrics])
        # Verify that the first staged version of the Song object knows that the lyrics exist.
        self.assertEquals(list(Song.objects.version(first_revision).get(pk=dont_lose_your_head.pk).lyrics.all()), [])

        # Verify that the second staged version of the Song object knows that the lyrics do not exist because they were altered to point to the correct song.
        self.assertEquals(list(Song.objects.version(second_revision).get(pk=princes_of_the_universe.pk).lyrics.all()), [])
        # Verify that the second staged version of the Song object knows that the lyrics do not exist because they were altered to point to the correct song.
        self.assertEquals(list(Song.objects.version(second_revision).get(pk=dont_lose_your_head.pk).lyrics.all()), [original_lyrics])

        with revision:
            original_lyrics.commit()

        third_revision = revision.latest_transactions['default']

        self.assertEquals(Lyrics.objects.get(pk=original_lyrics.pk), original_lyrics)
예제 #4
0
    def test_related_model_edits(self):
        with revision:
            queen = Artist(name='Queen')
            queen.save()

            a_kind_of_magic = Album(artist=queen, title='A Kind of Magic')
            a_kind_of_magic.save()

            dont_lose_your_head = Song(album=a_kind_of_magic, title="Don't Lose Your Head")
            dont_lose_your_head.save()

        first_revision = revision.latest_transactions['default']

        with revision:
            princes_of_the_universe = Song(album=a_kind_of_magic, title='Princes of the Universe')
            princes_of_the_universe.save()

            dont_lose_your_head.seconds = 278
            dont_lose_your_head.save()

        second_revision = revision.latest_transactions['default']

        with revision:
            princes_of_the_universe.seconds = 212
            princes_of_the_universe.save()

            friends_will_be_friends = Song(album=a_kind_of_magic, title='Friends Will Be Friends')
            friends_will_be_friends.save()

        third_revision = revision.latest_transactions['default']

        with revision:
            # the a_kind_of_magic album was not modified after the initial commit. Verify that we can retrieve the a_kind_of_magic model from the various versions
            first_a_kind_of_magic = Album.objects.version(first_revision).get(pk=a_kind_of_magic.pk)
            second_a_kind_of_magic = Album.objects.version(second_revision).get(pk=a_kind_of_magic.pk)
            third_a_kind_of_magic = Album.objects.version(third_revision).get(pk=a_kind_of_magic.pk)

            # Verify that the data is the same.
            self.assertEqual(revision.data(first_a_kind_of_magic)['field'], revision.data(second_a_kind_of_magic)['field'])
            self.assertEqual(revision.data(second_a_kind_of_magic)['field'], revision.data(third_a_kind_of_magic)['field'])

            # Verify that princes_of_the_universe does not exist at the first_revision (it was created on the second revision)
            self.assertRaises(ObjectDoesNotExist, first_a_kind_of_magic.songs.get, pk=princes_of_the_universe.pk)

            # Verify that retrieving the object from the reverse relationship and directly from the Song objects yield the same result.
            self.assertEqual(second_a_kind_of_magic.songs.get(pk=princes_of_the_universe.pk).__dict__, Song.objects.version(second_revision).get(pk=princes_of_the_universe.pk).__dict__)

            # Verify that retrieval of the object from the reverse relationship return the correct versions of the objects.
            second_princes_of_the_universe = second_a_kind_of_magic.songs.get(pk=princes_of_the_universe.pk)
            self.assertEqual(second_princes_of_the_universe.seconds, None)

            third_princes_of_the_universe = third_a_kind_of_magic.songs.get(pk=princes_of_the_universe.pk)
            self.assertEqual(third_princes_of_the_universe.seconds, 212)

            # Verify that the first revision of a_kind_of_magic has one song
            self.assertEquals(len(first_a_kind_of_magic.songs.all()), 1)

            # Verify that the second revision of a_kind_of_magic has two songs
            self.assertEquals(len(second_a_kind_of_magic.songs.all()), 2)

            # Verify that the third revision of a_kind_of_magic has three songs
            self.assertEquals(len(third_a_kind_of_magic.songs.all()), 3)
예제 #5
0
    def test_staged_edits(self):
        with revision:
            queen = Artist(name='Queen')
            queen.save()

            a_kind_of_magic = Album(artist=queen, title='A Kind of Magic')
            a_kind_of_magic.save()

            princes_of_the_universe = Song(album=a_kind_of_magic, title='Princes of the Universe')
            princes_of_the_universe.save()

            dont_lose_your_head = Song(album=a_kind_of_magic, title="Don't Lose Your Head")
            dont_lose_your_head.save()

            original_lyrics = Lyrics(song=dont_lose_your_head, text="Dont lose your head")
            original_lyrics.save()

        first_revision = revision.latest_transactions['default']

        with revision:
            queen.stage()
            a_kind_of_magic.stage()
            princes_of_the_universe.stage()
            dont_lose_your_head.stage()

            new_lyrics = """Dont lose your head
Dont lose your head
Dont lose your head
No dont lose you head
"""

            staged_edits_lyrics = Lyrics.objects.version('tip').get(pk=original_lyrics.pk)
            staged_edits_lyrics.text = new_lyrics
            staged_edits_lyrics.stage()

            princes_of_the_universe.delete()

        second_revision = revision.latest_transactions['default']

        # Ensure the database version still points to the old lyrics.
        self.assertEquals(Lyrics.objects.get(pk=original_lyrics.pk).text, "Dont lose your head")
        self.assertEquals(list(Album.objects.get(pk=a_kind_of_magic.pk).songs.all()), [princes_of_the_universe, dont_lose_your_head])
        # Ensure that the versions contain the correct information.
        self.assertEquals(Lyrics.objects.version(first_revision).get(pk=original_lyrics.pk).text, "Dont lose your head")
        self.assertEquals(Lyrics.objects.version(second_revision).get(pk=original_lyrics.pk).text, new_lyrics)

        with revision:
            queen.commit()
            a_kind_of_magic.commit()
            Album.objects.version('tip').get(pk=a_kind_of_magic.pk).songs.commit()

            new_lyrics = """Dont lose your head
Dont lose your head
Dont lose your head
Dont lose your head
No dont lose you head
Dont lose you head
Hear what I say
Dont lose your way - yeah
Remember loves stronger remember love walks tall
"""

            published_lyrics = Lyrics.objects.version('tip').get(pk=original_lyrics.pk)
            published_lyrics.text = new_lyrics
            published_lyrics.commit()

        third_revision = revision.latest_transactions['default']

        # Ensure the database version points to the new lyrics.
        self.assertEquals(Lyrics.objects.get(pk=original_lyrics.pk).text, new_lyrics)
        # Ensure the database version only has on song. Princess of the universe has been deleted.
        self.assertEquals(list(Album.objects.get(pk=a_kind_of_magic.pk).songs.all()), [dont_lose_your_head])
        # Ensure that the versions contain the correct information.
        self.assertEquals(Lyrics.objects.version(third_revision).get(pk=original_lyrics.pk).text, new_lyrics)
예제 #6
0
    def test_reverse_foreign_keys(self):
        with revision:
            queen = Artist(name='Queen')
            queen.save()

            a_kind_of_magic = Album(artist=queen, title='A Kind of Magic')
            a_kind_of_magic.save()

            journey_album = Album(artist=queen, title='Journey')
            journey_album.save()

        first_revision = revision.latest_transactions['default']

        with revision:
            journey = Artist(name='Journey')
            journey.save()

            journey_album.artist = journey
            journey_album.save()

        second_revision = revision.latest_transactions['default']

        self.assertEqual(list(Artist.objects.version(first_revision).get(pk=queen.pk).albums.all()), [a_kind_of_magic, journey_album])
        self.assertEqual(list(Artist.objects.version(second_revision).get(pk=queen.pk).albums.all()), [a_kind_of_magic])