f241a21d576053a938a0d976446a6facb277919f
[anni] / test / pleroma / pagination_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.PaginationTest do
6   use Pleroma.DataCase, async: true
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
23       %{total: total, items: paginated} =
24         Pagination.fetch_paginated(Object, %{min_id: id, total: true})
25
26       assert length(paginated) == 2
27       assert total == 5
28     end
29
30     test "paginates by since_id", %{notes: notes} do
31       id = Enum.at(notes, 2).id |> Integer.to_string()
32
33       %{total: total, items: paginated} =
34         Pagination.fetch_paginated(Object, %{since_id: id, total: true})
35
36       assert length(paginated) == 2
37       assert total == 5
38     end
39
40     test "paginates by max_id", %{notes: notes} do
41       id = Enum.at(notes, 1).id |> Integer.to_string()
42
43       %{total: total, items: paginated} =
44         Pagination.fetch_paginated(Object, %{max_id: id, total: true})
45
46       assert length(paginated) == 1
47       assert total == 5
48     end
49
50     test "paginates by min_id & limit", %{notes: notes} do
51       id = Enum.at(notes, 2).id |> Integer.to_string()
52
53       paginated = Pagination.fetch_paginated(Object, %{min_id: id, limit: 1})
54
55       assert length(paginated) == 1
56     end
57
58     test "handles id gracefully", %{notes: notes} do
59       id = Enum.at(notes, 1).id |> Integer.to_string()
60
61       paginated =
62         Pagination.fetch_paginated(Object, %{
63           id: "9s99Hq44Cnv8PKBwWG",
64           max_id: id,
65           limit: 20,
66           offset: 0
67         })
68
69       assert length(paginated) == 1
70     end
71   end
72
73   describe "offset" do
74     setup do
75       notes = insert_list(5, :note)
76
77       %{notes: notes}
78     end
79
80     test "paginates by limit" do
81       paginated = Pagination.fetch_paginated(Object, %{limit: 2}, :offset)
82
83       assert length(paginated) == 2
84     end
85
86     test "paginates by limit & offset" do
87       paginated = Pagination.fetch_paginated(Object, %{limit: 2, offset: 4}, :offset)
88
89       assert length(paginated) == 1
90     end
91   end
92 end