Django:第一次测试通过,但在第二次测试添加时失败

时间:2012-07-21 23:10:15

标签: python django unit-testing django-models

我的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)
  • 这里有什么问题?它的代码相同吗?
  • 我是否应该在每次测试后重置数据库?我该怎么办?

1 个答案:

答案 0 :(得分:2)

问题是测试数据库在每次测试后没有重置

在阅读了Django测试和SO之后,我意识到我需要TransactionTestCase

相关链接
Django Doc
SO question

这样做之后我所有的测试都很高兴:)

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'...