def test_cached(self): """If the view count is in the cache, return that count instead of the count in the database. """ with build_video(self.user, views=10) as video: self.cache.set(VIEWS_KEY % video.id, 12) eq_(cached_viewcount(video.id), 12)
def test_success(self): """A successful upload should mark the video as pending.""" with build_video(self.user, shortlink='') as video: eq_(video.shortlink, '') video = self._send(video, 'asdf') eq_(video.shortlink, 'asdf') eq_(video.state, 'pending')
def test_viewcount_updated(self): """Test that videos with viewcounts in the cache are updated.""" with build_video(self.user, views=10) as video: cache.set(VIEWS_KEY % video.id, 14) eq_(Video.objects.get(id=video.id).views, 10) persist_viewcounts() eq_(Video.objects.get(id=video.id).views, 14)
def test_basic(self): """Calling add_vote increases the vote count.""" with build_video(self.user, votes=0) as video: votes = video.votes add_vote(video) video = Video.objects.get(pk=video.pk) eq_(video.votes, votes + 1)
def test_success(self, generate): """If bit.ly is successful, save and return shortened link.""" generate.return_value = 'asdf' with build_video(self.user) as video: eq_(video.bitly_link, 'asdf') eq_(generate.called, True) eq_(Video.objects.get(pk=video.pk).bitly_link_db, 'asdf')
def test_success(self): """A successful upload should mark the video as pending.""" with build_video(self.user, shortlink="") as video: eq_(video.shortlink, "") video = self._send(video, "asdf") eq_(video.shortlink, "asdf") eq_(video.state, "pending")
def test_add_view(self): """If a video with the given ID exists, increment it's view count.""" with build_video(self.build_user(), views=10) as video: eq_(cached_viewcount(video.id), 10) response = self._post(video.id) eq_(response.status_code, 200) eq_(cached_viewcount(video.id), 11)
def test_uncached(self): """If the view count isn't in the cache, pull it from the database and store it in the cache. """ with build_video(self.user, views=10) as video: eq_(self.cache.get(VIEWS_KEY % video.id), None) eq_(cached_viewcount(video.id), 10) eq_(self.cache.get(VIEWS_KEY % video.id), 10)
def test_nonstaff_no_mark(self): """Test that normal users cannot mark videos for later judging.""" with build_video(self.user, judge_mark=False) as video: # Log in as normal user self.build_user(salt='staff', login=True, is_staff=False) eq_(Video.objects.get(pk=video.pk).judge_mark, False) self._post(video, admin_mark='asdf') eq_(Video.objects.get(pk=video.pk).judge_mark, False)
def test_staff_mark(self): """Test that staff members can mark videos for later judging.""" with build_video(self.user, judge_mark=False) as video: # Log in as staff user self.build_user(salt="staff", login=True, is_staff=True) eq_(Video.objects.get(pk=video.pk).judge_mark, False) self._post(video, admin_mark="asdf") eq_(Video.objects.get(pk=video.pk).judge_mark, True)
def test_valid_notification(self): """A valid notification updates a video's state.""" with build_video(self.user, state='pending') as video: self._post(video.shortlink) video = Video.objects.get(pk=video.pk) # Refresh eq_(video.state, 'complete') # Check for sent notification email eq_(len(mail.outbox), 1) with self.activate('en-US'): video_url = reverse('flicks.videos.details', kwargs={'video_id': video.id}) ok_(video_url in mail.outbox[0].body) eq_(mail.outbox[0].to, [self.user.email])
def test_store_at_intervals(self): """Test that the view count is written to the DB at certain intervals based on the view count. """ # Test each pair of (viewcount, # of view per save) for viewcount, per_save in [(0, 1), (100, 10)]: with build_video(self.user, views=viewcount) as video: # Add per_save - 1 views and ensure each time that the DB # hasn't been written to. for i in range(per_save - 1): add_view(video.id) eq_(self._get_views(video), viewcount) # The last view should trigger a write. add_view(video.id) eq_(self._get_views(video), viewcount + per_save)
def test_invalid_key_forbidden(self): """If an invalid key is provided in the URL, return a 403 and do not modify the video. """ xml = """<?xml version="1.0"?> <Response><Result><Task> <MediaShortLink>%(shortlink)s</MediaShortLink> <Status>Finished</Status> </Task></Result></Response> """ with build_video(self.user, state="pending") as video: response = self._post(xml % {"shortlink": video.shortlink}, "invalid_key") video = Video.objects.get(pk=video.pk) # Refresh eq_(response.status_code, 403) eq_(video.state, "pending")
def test_error_notification(self): """An error notification updates the video's state.""" xml = """<?xml version="1.0"?> <Response><Errors><Error> <ErrorCode>4.1</ErrorCode> <ErrorName>Wrong!</ErrorName> <Description>Desc</Description> <MediaShortLink>%(shortlink)s</MediaShortLink> </Error></Errors></Response>""" with build_video(self.user, state='pending') as video: self._post(xml % {'shortlink': video.shortlink}) video = Video.objects.get(pk=video.pk) # Refresh eq_(video.state, 'error') # Check for sent notification email eq_(len(mail.outbox), 1) ok_('error' in mail.outbox[0].body) eq_(mail.outbox[0].to, [self.user.email])
def test_valid_notification(self): """A valid notification updates a video's state.""" xml = """<?xml version="1.0"?> <Response><Result><Task> <MediaShortLink>%(shortlink)s</MediaShortLink> <Status>Finished</Status> </Task></Result></Response> """ with build_video(self.user, state="pending") as video: self._post(xml % {"shortlink": video.shortlink}) video = Video.objects.get(pk=video.pk) # Refresh eq_(video.state, "complete") # Check for sent notification email eq_(len(mail.outbox), 1) with self.activate("en-US"): video_url = reverse("flicks.videos.details", kwargs={"video_id": video.id}) ok_(video_url in mail.outbox[0].body) eq_(mail.outbox[0].to, [self.user.email])
def test_error(self): """If there's an error, the video's state should change to error.""" with build_video(self.user) as video: video = self._send(video, None) eq_(video.state, 'error')
def test_exists(self): """If no exceptions occur, return the matched object.""" with build_video(self.user, title='exists') as video: value = get_object_or_none(Video, title='exists') eq_(value, video)
def test_fallback(self, generate): """If bit.ly fails, return the long URL as a fallback.""" generate.return_value = None with build_video(self.user) as video: eq_(video.bitly_link, self.long_url % video.id) eq_(generate.called, True)
def test_no_generate_dev(self, generate): """If this is a dev environment, don't generate a link.""" with build_video(self.user) as video: eq_(video.bitly_link, self.long_url % video.id) eq_(generate.called, False)
def test_existing_link(self, generate): """If a link is already in the DB, don't generate a new one.""" with build_video(self.user, bitly_link_db='asdf') as video: eq_(video.bitly_link, 'asdf') eq_(generate.called, False)
def test_viewcount_unmodified(self): """Test that videos without viewcounts in the cache are not updated.""" with build_video(self.user, views=10) as video: eq_(Video.objects.get(id=video.id).views, 10) persist_viewcounts() eq_(Video.objects.get(id=video.id).views, 10)
def test_invalid_user_id(self): """Returning with an invalid user id fails.""" with build_video(self.user, state='pending') as video: self._post(video.shortlink, '9999') eq_(video.state, 'pending')
def test_store_in_cache(self): """The most-up-to-date view count should be in the cache.""" with build_video(self.user, views=10) as video: add_view(video.id) eq_(self.cache.get(VIEWS_KEY % video.id), 11)
def test_category_filter(self): """Test that search results can be filtered by category.""" with nested(build_video(self.user, category='animation'), build_video(self.user, category='psa')) as (v1, v2): eq_(list(self._videos(category='psa')), [v2])
def test_search(self): """Test that basic title searching works.""" with nested(build_video(self.user, title='Honey badger'), build_video(self.user, title='Lolcats')) as (v1, v2): eq_(list(self._videos(search='badger')), [v1])
def setUp(self): self._build_video = build_video(self.build_user()) self.video = self._build_video.__enter__()
def test_region_filter(self): """Test that search results can be filtered by region.""" with nested(build_video(self.user, region='america'), build_video(self.user, region='europe')) as (v1, v2): eq_(list(self._videos(region='europe')), [v2])
def test_valid_notification(self): """A valid notification updates a video's state.""" with build_video(self.user, state='pending') as video: self._post(video.shortlink) video = Video.objects.get(pk=video.pk) # Refresh eq_(video.state, 'complete')