def test_default_bucket_ordering(self): BucketFactory(title="test2") BucketFactory(title="test1") BucketFactory(title="test3") buckets = Bucket.objects.all() self.assertEqual([i.title for i in buckets], ["test1", "test2", "test3"])
def test_video_from_bucket(self): user = UserFactory() bucket1 = BucketFactory(user=user, title="test1") bucket2 = BucketFactory(user=user, title="test2") video = VideoFactory(user=user, buckets=[bucket2]) videos1 = Video.objects.from_bucket(user=user, bucket=bucket1) videos2 = Video.objects.from_bucket(user=user, bucket=bucket2) self.assertEqual(len(videos1), 0) self.assertEqual(len(videos2), 1) self.assertEqual(videos2[0], video)
def test_overview_has_items(self): subscriptions = SubscriptionFactory.create_batch(2, user=self.user) buckets = [ BucketFactory(user=self.user), BucketFactory(user=self.user, subs=[subscriptions[0]]), BucketFactory(), ] response = self.client.get(reverse('overview')) self.assertEqual(response.status_code, 200) self.assertIn(buckets[0].title, response.content) self.assertIn(buckets[1].title, response.content) self.assertNotIn(buckets[2].title, response.content)
def test_bucket(self): response = self.client.get(reverse('bucket', kwargs={'bucket': 1})) self.assertEqual(response.status_code, 404) bucket = BucketFactory(title='Cheese') response = self.client.get( reverse('bucket', kwargs={'bucket': bucket.pk})) self.assertEqual(response.status_code, 404) bucket.user = self.user bucket.save() response = self.client.get( reverse('bucket', kwargs={'bucket': bucket.pk})) self.assertEqual(response.status_code, 200)
def test_video_not_found(self): data = {"id": "12"} response = self.client.post(reverse( "bucket-video-viewed-api", kwargs={"bucket": BucketFactory(user=self.user).pk}), data=data) self.assertEqual(response.status_code, 404)
def test_import_videos_only_first_page(self, service_mock): playlistitems_mock = service_mock.return_value.playlistItems.return_value.list playlistitems_mock.return_value.execute = MockExecute([ { 'items': [], 'nextPageToken': '123', }, { 'items': [], }, ]) user = get_user_model().objects.create(username='******') OauthToken.objects.create(user=user, data={}) subscription = Subscription.objects.create(user=user, channel_id="123", last_update=timezone.now()) bucket = BucketFactory(user=user, subs=[subscription]) import_videos(user.id, subscription.id, "upload123", [bucket.id], only_first_page=True) self.assertEqual(playlistitems_mock.call_count, 1) self.assertEqual(playlistitems_mock.call_args_list, [ ((), {'part': 'contentDetails', 'playlistId': 'upload123', 'fields': 'items(contentDetails(videoId))', 'maxResults': API_MAX_RESULTS, 'pageToken': None}), ])
def test_get_empty(self): bucket = BucketFactory() response = self.client.get( reverse("bucket-video-api", kwargs={"bucket": bucket.id})) self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual(data, {"videos": []})
def test_bucket_edit_subs_qs(self): bucket = BucketFactory() other_user = UserFactory() my_subs = SubscriptionFactory.create_batch(2, user=bucket.user) SubscriptionFactory.create_batch(2, user=other_user) # other subs form = BucketEditForm(instance=bucket) subs = list(form.fields["subs"].queryset) self.assertEqual(len(subs), 2) self.assertCountEqual(subs, list(my_subs))
def test_bucket_edit(self): subscription = SubscriptionFactory(user=self.user) bucket = BucketFactory(user=self.user, title='Cheese') self.assertEqual(len(bucket.subs_ids), 0) data = { 'title': 'Games', 'subs': [subscription.pk], } response = self.client.post( reverse('bucket-edit', kwargs={'bucket': bucket.pk}), data) bucket.refresh_from_db() self.assertRedirects(response, reverse('bucket', kwargs={'bucket': bucket.pk})) self.assertEqual(bucket.title, 'Games') self.assertEqual(bucket.subs_ids, set([subscription.pk])) new_bucket = BucketFactory(title='Cheese', user=self.user) response = self.client.post( reverse('bucket-edit', kwargs={'bucket': new_bucket.pk}), data) self.assertEqual(response.status_code, 200)
def test_post(self): video = VideoFactory(user=self.user, buckets=[BucketFactory(user=self.user)]) data = {"id": video.youtube_id} response = self.client.post(reverse( "bucket-video-viewed-api", kwargs={"bucket": video.buckets.first().pk}), data=data) self.assertEqual(response.status_code, 200) video.refresh_from_db() self.assertEqual(video.viewed, True) self.assertEqual(video.buckets.all()[0].last_watched_video, video.ordering_key)
def test_bucket_start(self): bucket = BucketFactory(title='Cheese', user=self.user) response = self.client.get( reverse('bucket', kwargs={'bucket': bucket.pk})) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["start"], None) self.assertNotIn("?start=", response.content) video_id = "bluhbluhvideo" response = self.client.get("{}?start={}".format( reverse('bucket', kwargs={'bucket': bucket.pk}), video_id)) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["start"], video_id) self.assertIn("?start={}".format(video_id), response.content)
def test_get_with_after(self): bucket = BucketFactory(user=self.user) VideoFactory.create_batch(3, user=self.user, buckets=[bucket]) videos = list( Video.objects.all().order_by("ordering_key").add_titles()) response = self.client.get("{}?after={}".format( reverse("bucket-video-api", kwargs={"bucket": bucket.pk}), videos[0].ordering_key)) self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual(len(data["videos"]), 2) self.assertEqual( data, { "next": "{}?after={}".format( reverse("bucket-video-api", kwargs={"bucket": bucket.pk}), videos[2].ordering_key), "videos": [ { "id": videos[1].youtube_id, "title": videos[1].title, "description": videos[1].description, "published": datetime_to_js_iso( videos[1].published_at), "html_snippet": videos[1].html_snippet, "ordering_key": videos[1].ordering_key, }, { "id": videos[2].youtube_id, "title": videos[2].title, "description": videos[2].description, "published": datetime_to_js_iso( videos[2].published_at), "html_snippet": videos[2].html_snippet, "ordering_key": videos[2].ordering_key, }, ], })
def test_import_videos_runtime_exceeded(self, service_mock, defer_mock): playlistitems_mock = service_mock.return_value.playlistItems.return_value.list playlistitems_mock.return_value.execute = MockExecute([ { 'items': [], 'nextPageToken': '123', }, RuntimeExceededError(), ]) user = get_user_model().objects.create(username='******') OauthToken.objects.create(user=user, data={}) subscription = Subscription.objects.create(user=user, channel_id="123", last_update=timezone.now()) bucket = BucketFactory(user=user, subs=[subscription]) import_videos(user.id, subscription.id, "upload123", [bucket.id]) self.assertEqual(playlistitems_mock.call_count, 2) self.assertEqual(defer_mock.defer.call_count, 1) self.assertEqual(defer_mock.defer.call_args, ( (import_videos, user.id, subscription.id, "upload123", [bucket.id]), {"page_token": "123", "only_first_page": False}, ))
def test_get(self): bucket = BucketFactory(user=self.user) video = VideoFactory(user=self.user, buckets=[bucket]) video = video.add_titles() response = self.client.get( reverse("bucket-video-api", kwargs={"bucket": bucket.pk})) self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual( data, { "next": "{}?after={}".format( reverse("bucket-video-api", kwargs={"bucket": bucket.pk}), video.ordering_key), "videos": [{ "id": video.youtube_id, "title": video.title, "description": video.description, "published": datetime_to_js_iso(video.published_at), "html_snippet": video.html_snippet, "ordering_key": video.ordering_key, }], })
def test_missing_oauth_token(self): user = get_user_model().objects.create(username='******') subscription = Subscription.objects.create(user=user, channel_id="123", last_update=timezone.now()) bucket = BucketFactory(user=user, subs=[subscription]) import_videos(user.id, subscription.id, "upload123", [bucket.id])
def test_bucket_new_inconsistent(self): with inconsistent_db(): bucket = BucketFactory(title='Cheese', user=self.user) response = self.client.get( reverse('bucket', kwargs={'bucket': bucket.pk})) self.assertEqual(response.status_code, 200)
def test_import_videos(self, service_mock, defer_mock): playlistitems_mock = service_mock.return_value.playlistItems.return_value.list videos_mock = service_mock.return_value.videos.return_value.list playlistitems_mock.return_value.execute.return_value = { 'items': [ {'contentDetails': {'videoId': 'video123'}}, {'contentDetails': {'videoId': 'video456'}}, ], } videos_mock.return_value.execute.return_value = { 'items': [ { 'id': 'video123', 'snippet': { 'title': 'my video', 'description': 'this is my video', 'thumbnails': {}, 'publishedAt': '1997-07-16T19:20:30.45Z', }, }, { 'id': 'video456', 'snippet': { 'title': 'my other video', 'description': 'this is my other video', 'thumbnails': {}, 'publishedAt': '1997-07-16T19:20:30.45Z', }, }, ], } user = get_user_model().objects.create(username='******') OauthToken.objects.create(user=user, data={}) subscription = Subscription.objects.create(user=user, channel_id="123", last_update=timezone.now()) bucket = BucketFactory(user=user, subs=[subscription]) import_videos(user.id, subscription.id, "upload123", [bucket.id]) self.assertEqual(playlistitems_mock.call_count, 1) self.assertEqual(videos_mock.call_count, 1) self.assertEqual(defer_mock.call_count, 0) self.assertEqual(playlistitems_mock.call_args, ( (), {'playlistId': 'upload123', 'part': 'contentDetails', 'fields': 'items(contentDetails(videoId))', 'maxResults': API_MAX_RESULTS, 'pageToken': None} )) self.assertEqual(videos_mock.call_args, ( (), {'id': 'video123,video456', 'part': 'snippet', 'fields': 'items(snippet(publishedAt,thumbnails))', 'maxResults': API_MAX_RESULTS} )) self.assertEqual(Video.objects.count(), 2) video1 = Video.objects.get(youtube_id="video123") self.assertEqual(video1.published_at, datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC)) self.assertEqual(video1.ordering_key, create_composite_key(str(datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC)), "video123")) video2 = Video.objects.get(youtube_id="video456") self.assertEqual(video2.published_at, datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC)) self.assertEqual(video2.ordering_key, create_composite_key(str(datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC)), "video456"))