Skip to content

Commit

Permalink
adjust query params for cursor based pagination and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mebareksaf committed Feb 28, 2024
1 parent 11258da commit 482a73e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ defmodule ExNVRWeb.API.RecordingController do
{:ok, {recordings, meta}} ->
meta =
Map.take(meta, [
:current_page,
:start_cursor,
:page_size,
:total_count,
:total_pages
:end_cursor
])

recordings = Enum.map(recordings, &Map.drop(&1, [:device_name, :timezone]))
Expand Down
6 changes: 4 additions & 2 deletions apps/ex_nvr_web/lib/ex_nvr_web/live/recordings_list_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,17 @@ defmodule ExNVRWeb.RecordingListLive do
@impl true
def handle_event("paginate", pagination_params, socket) do
after_cursor = Map.get(pagination_params, "after-cursor", nil)
first = Map.get(pagination_params, "first", 100)
before_cursor = Map.get(pagination_params, "before-cursor", nil)
last = Map.get(pagination_params, "last", 100)

cursor_params =
if not is_nil(after_cursor) do
old_pagination_params = Map.drop(socket.assigns.pagination_params, ["before", "before-cursor", "last"])
Map.merge(old_pagination_params, %{"first" => 100, "after" => after_cursor})
Map.merge(old_pagination_params, %{"first" => first, "after" => after_cursor})
else
old_pagination_params = Map.drop(socket.assigns.pagination_params, ["after", "after-cursor", "first"])
Map.merge(old_pagination_params, %{"last" => 100, "before" => before_cursor})
Map.merge(old_pagination_params, %{"last" => last, "before" => before_cursor})
end

pagination_params = Map.merge(pagination_params, cursor_params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ defmodule ExNVRWeb.API.RecordingControllerTest do
assert Enum.map(response["data"], & &1["id"]) |> MapSet.new() ==
Enum.map(recordings, & &1.id) |> MapSet.new()

assert %{
"total_count" => 4,
"total_pages" => 1,
"current_page" => 1,
"page_size" => 100
} = response["meta"]
assert Map.get(response["meta"], "page_size") == 100
assert Map.has_key?(response["meta"], "start_cursor")
assert Map.has_key?(response["meta"], "end_cursor")
end

test "filter recordings chunks", %{conn: conn, device: device, recordings: recordings} do
Expand Down
24 changes: 6 additions & 18 deletions apps/ex_nvr_web/test/ex_nvr_web/live/recordings_list_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do
{:ok, lv, html} =
conn
|> log_in_user(user_fixture())
|> live(~p"/recordings?page_size=1")
|> live(~p"/recordings?first=1")

# check pagination
assert lv
Expand All @@ -74,14 +74,6 @@ defmodule ExNVRWeb.RecordingListLiveTest do
|> element("a", "Next")
|> has_element?()

assert lv
|> element("a", "1")
|> has_element?()

for page_label <- ["1", "2", "...", "4", "5", "6", "...", "9", "10"] do
assert html =~ page_label
end

assert_recording_info(lv, html, device, List.last(recordings))
end

Expand All @@ -91,14 +83,10 @@ defmodule ExNVRWeb.RecordingListLiveTest do
|> log_in_user(user_fixture())
|> live(~p"/recordings?page_size=1")

html = element(lv, "a", "2") |> render_click(%{page_size: 1})

for page_label <- ["1", "2", "...", "4", "5", "6", "...", "9", "10"] do
assert html =~ page_label
end
html = element(lv, "a", "Next") |> render_click(%{last: 1})

# check recording
recording = Enum.at(recordings, length(recordings) - 2)
recording = Enum.at(recordings, 0)
assert_recording_info(lv, html, device, recording)
end

Expand Down Expand Up @@ -240,7 +228,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do
{:ok, lv, html} =
logged_in_conn
|> live(
~p"/recordings?page_size=1&order_by[]=device_name&order_by[]=start_date&order_by[]=end_date&order_directions[]=asc&order_directions[]=desc"
~p"/recordings?first=1&order_by[]=device_name&order_by[]=start_date&order_by[]=end_date&order_directions[]=asc&order_directions[]=desc"
)

assert_recording_info(lv, html, device, List.last(recordings))
Expand All @@ -249,7 +237,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do
{:ok, lv, html} =
logged_in_conn
|> live(
~p"/recordings?page_size=1&order_by[]=start_date&order_by[]=device_name&order_directions[]=asc&order_directions[]=asc"
~p"/recordings?first=1&order_by[]=start_date&order_by[]=device_name&order_directions[]=asc&order_directions[]=asc"
)

assert_recording_info(lv, html, new_device, List.first(new_recordings))
Expand All @@ -258,7 +246,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do
{:ok, lv, html} =
logged_in_conn
|> live(
~p"/recordings?page_size=1&order_by[]=end_date&order_by[]=device_name&order_directions[]=desc&order_directions[]=desc"
~p"/recordings?first=1&order_by[]=end_date&order_by[]=device_name&order_directions[]=desc&order_directions[]=desc"
)

assert_recording_info(lv, html, new_device, List.last(new_recordings))
Expand Down

0 comments on commit 482a73e

Please sign in to comment.