048ab6f3c0942e15bac8ca9dc0fbd61703c0dee5
[akkoma] / test / pagination_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.PaginationTest do
6 use Pleroma.DataCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Object
11 alias Pleroma.Pagination
12
13 describe "keyset" do
14 setup do
15 notes = insert_list(5, :note)
16
17 %{notes: notes}
18 end
19
20 test "paginates by min_id", %{notes: notes} do
21 id = Enum.at(notes, 2).id |> Integer.to_string()
22 %{total: total, items: paginated} = Pagination.fetch_paginated(Object, %{"min_id" => id})
23
24 assert length(paginated) == 2
25 assert total == 5
26 end
27
28 test "paginates by since_id", %{notes: notes} do
29 id = Enum.at(notes, 2).id |> Integer.to_string()
30 %{total: total, items: paginated} = Pagination.fetch_paginated(Object, %{"since_id" => id})
31
32 assert length(paginated) == 2
33 assert total == 5
34 end
35
36 test "paginates by max_id", %{notes: notes} do
37 id = Enum.at(notes, 1).id |> Integer.to_string()
38 %{total: total, items: paginated} = Pagination.fetch_paginated(Object, %{"max_id" => id})
39
40 assert length(paginated) == 1
41 assert total == 5
42 end
43
44 test "paginates by min_id & limit", %{notes: notes} do
45 id = Enum.at(notes, 2).id |> Integer.to_string()
46
47 %{total: total, items: paginated} =
48 Pagination.fetch_paginated(Object, %{"min_id" => id, "limit" => 1})
49
50 assert length(paginated) == 1
51 assert total == 5
52 end
53 end
54
55 describe "offset" do
56 setup do
57 notes = insert_list(5, :note)
58
59 %{notes: notes}
60 end
61
62 test "paginates by limit" do
63 %{total: total, items: paginated} =
64 Pagination.fetch_paginated(Object, %{"limit" => 2}, :offset)
65
66 assert length(paginated) == 2
67 assert total == 5
68 end
69
70 test "paginates by limit & offset" do
71 %{total: total, items: paginated} =
72 Pagination.fetch_paginated(Object, %{"limit" => 2, "offset" => 4}, :offset)
73
74 assert length(paginated) == 1
75 assert total == 5
76 end
77 end
78 end