Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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)