我的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)
- 这里应该有什么问题?它的代码相同吗?
- 每次测试后我都应该重置我的数据库吗?我怎样才能做到这一点?