Ejemplo n.º 1
0
    def test_get(self):
        subscription = SubscriptionFactory(user=self.user)
        video = VideoFactory(user=self.user, subscription=subscription)
        video = video.add_titles()

        response = self.client.get(
            reverse("subscription-video-api",
                    kwargs={"subscription": subscription.pk}))
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(
            data, {
                "next":
                "{}?after={}".format(
                    reverse("subscription-video-api",
                            kwargs={"subscription": subscription.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,
                }],
            })
Ejemplo n.º 2
0
    def test_ordering(self):
        VideoFactory.create_batch(3)

        qs = Video.objects.all()
        forward, _, _ = queryset_to_json(qs, "pk", {"id": "id"})
        backward, _, _ = queryset_to_json(qs, "-pk", {"id": "id"})
        self.assertEqual(forward, list(reversed(backward)))
Ejemplo n.º 3
0
    def test_pagination_options(self):
        VideoFactory.create_batch(3)
        videos = list(Video.objects.all().order_by("pk").add_titles())

        qs = Video.objects.all()
        items, first, last = queryset_to_json(qs, "pk", {"id": "id"})
        self.assertEqual(items, [{"id": v.id} for v in videos])
        self.assertEqual(first, videos[0].pk)
        self.assertEqual(last, videos[2].pk)
Ejemplo n.º 4
0
    def test_model_method(self):
        vid = VideoFactory(youtube_id="123")
        with self.assertRaises(AttributeError):
            vid.title
        with self.assertRaises(AttributeError):
            vid.description

        vid.add_titles()

        self.assertEqual(vid.title, "henlo")
        self.assertEqual(vid.description, "bluh bluh")
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    def test_queryset_method(self):
        VideoFactory(youtube_id="123")
        VideoFactory(youtube_id="456")

        vids = Video.objects.all()
        for obj in vids:
            with self.assertRaises(AttributeError):
                obj.title
            with self.assertRaises(AttributeError):
                obj.description

        titled_vids = vids.add_titles()
        for obj in titled_vids:
            self.assertNotEqual(obj.title, None)
            self.assertNotEqual(obj.description, None)
Ejemplo n.º 7
0
    def test_post(self):
        video = VideoFactory(user=self.user,
                             subscription=SubscriptionFactory(user=self.user))
        data = {"id": video.youtube_id}
        response = self.client.post(reverse(
            "subscription-video-viewed-api",
            kwargs={"subscription": video.subscription_id}),
                                    data=data)
        self.assertEqual(response.status_code, 200)

        video.refresh_from_db()
        video.subscription.refresh_from_db()
        self.assertEqual(video.viewed, True)
        self.assertEqual(video.subscription.last_watched_video,
                         video.ordering_key)
Ejemplo n.º 8
0
 def test_bucket_not_found(self):
     video = VideoFactory(user=self.user)
     data = {"id": video.youtube_id}
     response = self.client.post(reverse("bucket-video-viewed-api",
                                         kwargs={"bucket": "1"}),
                                 data=data)
     self.assertEqual(response.status_code, 404)
Ejemplo n.º 9
0
 def test_bad_subscription_id(self):
     video = VideoFactory(user=self.user)
     data = {"id": video.youtube_id}
     response = self.client.post(reverse(
         "subscription-video-viewed-api",
         kwargs={"subscription": SubscriptionFactory(user=self.user).pk}),
                                 data=data)
     self.assertEqual(response.status_code, 404)
Ejemplo n.º 10
0
    def test_property_map(self):
        video = VideoFactory().add_titles()

        qs = Video.objects.all()
        items, _, _ = queryset_to_json(qs, "pk", {"bob": "title"})

        self.assertEqual(items[0].keys(), ["bob"])
        self.assertEqual(items[0]["bob"], video.title)
Ejemplo n.º 11
0
    def test_start(self):
        videos = VideoFactory.create_batch(3)
        videos = list(Video.objects.all().order_by("pk").add_titles())

        qs = Video.objects.all()
        items, first, last = queryset_to_json(qs,
                                              "pk", {"id": "id"},
                                              start=videos[1].pk)
        self.assertEqual(items, [{"id": v.id} for v in videos[1:]])
        self.assertEqual(first, videos[1].pk)
        self.assertEqual(last, videos[2].pk)
Ejemplo n.º 12
0
    def test_end(self):
        videos = VideoFactory.create_batch(3)
        videos = list(Video.objects.all().order_by("pk").add_titles())

        qs = Video.objects.all()
        items, first, last = queryset_to_json(qs,
                                              "pk", {"id": "id"},
                                              end=videos[1].pk)
        self.assertEqual(items, [{"id": v.id} for v in reversed(videos[:2])])
        self.assertEqual(first, videos[1].pk)
        self.assertEqual(last, videos[0].pk)
Ejemplo n.º 13
0
    def test_get_with_after(self):
        subscription = SubscriptionFactory(user=self.user)
        VideoFactory.create_batch(3, user=self.user, subscription=subscription)
        videos = list(
            Video.objects.all().order_by("ordering_key").add_titles())

        response = self.client.get("{}?after={}".format(
            reverse("subscription-video-api",
                    kwargs={"subscription": subscription.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("subscription-video-api",
                            kwargs={"subscription": subscription.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,
                    },
                ],
            })
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    def test_video_from_subscription(self):
        user = UserFactory()
        sub1 = SubscriptionFactory(user=user)
        sub2 = SubscriptionFactory(user=user)
        video = VideoFactory(user=user, subscription=sub2)

        videos1 = Video.objects.from_subscription(user=user, subscription=sub1)
        videos2 = Video.objects.from_subscription(user=user, subscription=sub2)

        self.assertEqual(len(videos1), 0)
        self.assertEqual(len(videos2), 1)
        self.assertEqual(videos2[0], video)
Ejemplo n.º 16
0
    def test_cache_populated(self):
        vid = VideoFactory.build(youtube_id="123")

        cache.set(VIDEO_TITLE_CACHE_PREFIX + "123", {
            "title": "henlo",
            "description": "bluh bluh"
        })

        results = list(video_add_titles([vid]))
        self.assertEqual(results, [vid])

        self.assertEqual(vid.title, "henlo")
        self.assertEqual(vid.description, "bluh bluh")

        self.assertEqual(self.video_mock.call_count, 0)
Ejemplo n.º 17
0
    def test_clean_cache(self):
        vid = VideoFactory.build(youtube_id="123")

        cached_data = cache.get(VIDEO_TITLE_CACHE_PREFIX + "123")
        self.assertEqual(cached_data, None)

        results = list(video_add_titles([vid]))
        self.assertEqual(results, [vid])

        self.assertEqual(vid.title, "henlo")
        self.assertEqual(vid.description, "bluh bluh")

        self.assertEqual(self.video_mock.call_count, 1)
        self.assertEqual(self.video_mock.call_args[1]["id"], "123")

        cached_data = cache.get(VIDEO_TITLE_CACHE_PREFIX + "123")
        self.assertEqual(cached_data, {
            "title": "henlo",
            "description": "bluh bluh"
        })
Ejemplo n.º 18
0
    def test_property_map_value_error(self):
        VideoFactory()

        qs = Video.objects.all()
        with self.assertRaises(ValueError):
            queryset_to_json(qs, "pk", (("id", "id"), ("title", "title")))
Ejemplo n.º 19
0
    def test_default_video_ordering(self):
        VideoFactory(published_at=datetime(1997,
                                           8,
                                           16,
                                           19,
                                           20,
                                           30,
                                           450000,
                                           tzinfo=UTC),
                     youtube_id="1")
        VideoFactory(published_at=datetime(1997,
                                           6,
                                           16,
                                           19,
                                           20,
                                           30,
                                           450000,
                                           tzinfo=UTC),
                     youtube_id="4")
        VideoFactory(published_at=datetime(1997,
                                           7,
                                           16,
                                           19,
                                           20,
                                           30,
                                           450000,
                                           tzinfo=UTC),
                     youtube_id="2")
        VideoFactory(published_at=datetime(1997,
                                           7,
                                           16,
                                           19,
                                           20,
                                           30,
                                           450000,
                                           tzinfo=UTC),
                     youtube_id="3")

        videos = Video.objects.all()

        self.assertEqual([i.published_at for i in videos], [
            datetime(1997, 6, 16, 19, 20, 30, 450000, tzinfo=UTC),
            datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC),
            datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC),
            datetime(1997, 8, 16, 19, 20, 30, 450000, tzinfo=UTC),
        ])

        self.assertEqual([i.ordering_key for i in videos], [
            create_composite_key(
                str(datetime(1997, 6, 16, 19, 20, 30, 450000, tzinfo=UTC)),
                "4"),
            create_composite_key(
                str(datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC)),
                "2"),
            create_composite_key(
                str(datetime(1997, 7, 16, 19, 20, 30, 450000, tzinfo=UTC)),
                "3"),
            create_composite_key(
                str(datetime(1997, 8, 16, 19, 20, 30, 450000, tzinfo=UTC)),
                "1"),
        ])
Ejemplo n.º 20
0
    def test_page_size(self):
        VideoFactory.create_batch(3)

        qs = Video.objects.all()
        items, _, _ = queryset_to_json(qs, "pk", {"id": "id"})
        self.assertEqual(len(items), 2)