0

modelsのように見えます

class PlaylistVideoManager(models.Manager):
    def add_video_to_playlist(self, video, playlist):
        """
        if video is already added to playlist, don't add it again
        """
        search_video = self.get_video_for_playlist(playlist, video)
        if search_video:
            logging.info('video is already added to playlist, will not re-add - ' + repr(playlist) + ', ' + repr(video))
        else:
            playlist_video = PlaylistVideo(video=video, playlist=playlist)
            playlist_video.save()
        return video

    def get_all_videos_for_playlist(self, playlist):
        videos_queryset = self.get_query_set().filter(playlist=playlist)
        videos = []
        if videos_queryset:
            for video in videos_queryset:
                videos.append(video)
        return videos

    def get_video_for_playlist(self, playlist, video):
        all_videos = self.get_all_videos_for_playlist(playlist)
        if all_videos:
            for vid in all_videos:
                if vid.pk == video.pk:
                    return video    

class PlaylistVideo(models.Model):
    playlist = models.ForeignKey(Playlist)
    video = models.ForeignKey(Video)
    objects = PlaylistVideoManager()

    class Meta:
        db_table = 'playlists_videos'

と私のようにfirst test見えます

def test_add_same_video_twice_to_one_playlist(self):
    """
    desired: same video added twice to one playlist should not add it twice in database
    """
    video = Utility.create_video()
    playlist = Utility.create_playlist()
    PlaylistVideo.objects.add_video_to_playlist(video, playlist)
    PlaylistVideo.objects.add_video_to_playlist(video, playlist)
    self.assertEqual(PlaylistVideo.objects.count(), 1, msg='playlist video count is not 1, it is ' + str(PlaylistVideo.objects.count()))
    self.assertEqual(len(PlaylistVideo.objects.get_all_videos_for_playlist(playlist)), 1, msg='videos count in playlist is not 1, it is ' + str(len(PlaylistVideo.objects.get_all_videos_for_playlist(playlist))))

このテストを実行すると、正常に実行されます

Creating test database for alias 'default'...
....INFO:root:adding video title - title, url - url
INFO:root:video is already added to playlist, will not re-add - <Playlist id:7, name:playlist, date_created:2012-07-21 23:08:23.677941+00:00, deleted:False>, <Video id:1, title:title, url:url>
.INFO:root:adding video title - title, url - url

しかし、別のテストを実行するとすぐに、同じテストが失敗します

def test_add_same_video_twice_to_different_playlist(self):
    video = Utility.create_video()
    playlist1 = Utility.create_playlist('playlist1')
    playlist2 = Utility.create_playlist('playlist2')
    PlaylistVideo.objects.add_video_to_playlist(video, playlist1)
    PlaylistVideo.objects.add_video_to_playlist(video, playlist2)
    self.assertEqual(PlaylistVideo.objects.count(), 2, msg='playlist video count is not 2, it is ' + str(PlaylistVideo.objects.count()))
    self.assertEqual(len(PlaylistVideo.objects.get_all_videos_for_playlist(playlist1)), 1, msg='videos count in playlist1 is not 1, it is ' + str(len(PlaylistVideo.objects.get_all_videos_for_playlist(playlist1))))
    self.assertEqual(len(PlaylistVideo.objects.get_all_videos_for_playlist(playlist2)), 1, msg='videos count in playlist2 is not 1, it is ' + str(len(PlaylistVideo.objects.get_all_videos_for_playlist(playlist2))))


======================================================================
FAIL: test_add_same_video_twice_to_one_playlist (App.apps.playlists.tests.PlaylistVideoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/hhimanshu/code/p/Apps/App/apps/playlists/tests.py", line 96, in test_add_same_video_twice_to_one_playlist
    self.assertEqual(PlaylistVideo.objects.count(), 1, msg='playlist video count is not 1, it is ' + str(PlaylistVideo.objects.count()))
AssertionError: playlist video count is not 1, it is 2

----------------------------------------------------------------------
Ran 10 tests in 1.315s

FAILED (failures=1)
Destroying test database for alias 'default'...

Utilityのクラスは

PLAYLIST = 'playlist'
class Utility():
    @staticmethod
    def create_playlist(playlist_name=PLAYLIST):
        return Playlist.objects.add_playlist(playlist_name)

    @staticmethod
    def create_user(name='test', email='test', passwd='test'):
        return User.objects.create_user(name, email, passwd)

    @staticmethod
    def create_video(title='title', url='url'):
        return Video.objects.get_or_create_video(title, url)
  • ここで何が悪いのでしょうか?その同じコード?
  • テストのたびにデータベースをリセットする必要がありますか?どうやってやるの?
4

1 に答える 1

2

問題は、各テスト後にテストデータベースがリセットされなかったことです。

DjangoテストとSOを読んだ後、私は必要だと気づきましたTransactionTestCase

関連リンク
DjangoDocSOの
質問

これを行った後、私のテストはすべて幸せでした:)

Creating test database for alias 'default'...
....INFO:root:adding video title - title, url - url1
.INFO:root:adding video title - title, url - myurl
INFO:root:video is already added to playlist, will not re-add - <Playlist id:1, name:playlist, date_created:2012-07-21 23:25:15.411728+00:00, deleted:False>, <Video id:1, title:title, url:myurl>
.INFO:root:adding video title - title, url - url
.INFO:root:playlist created: <Playlist id:1, name:playlist, date_created:2012-07-21 23:25:15.904582+00:00, deleted:False>, <User: test>
INFO:root:playlist created: <Playlist id:2, name:playlist1, date_created:2012-07-21 23:25:15.912512+00:00, deleted:False>, <User: test>
.INFO:root:playlist created: <Playlist id:1, name:playlist, date_created:2012-07-21 23:25:16.230842+00:00, deleted:False>, <User: test>
INFO:root:Playlist already exists: <Playlist id:1, name:playlist, date_created:2012-07-21 23:25:16.230842+00:00, deleted:False>, <User: test>
.INFO:root:playlist created: <Playlist id:1, name:playlist, date_created:2012-07-21 23:25:16.861322+00:00, deleted:False>, <User: user1>
INFO:root:playlist created: <Playlist id:2, name:playlist, date_created:2012-07-21 23:25:16.866924+00:00, deleted:False>, <User: user2>
.
----------------------------------------------------------------------
Ran 10 tests in 2.427s

OK
Destroying test database for alias 'default'...
于 2012-07-21T23:26:03.927 に答える