refactor: apply review comments
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Simon 2026-01-28 14:42:16 +01:00
parent 050ca4a13c
commit ea3bdcaa65
Signed by: simon
GPG key ID: 40E7A58C4AA1EDB2
5 changed files with 81 additions and 13 deletions

View file

@ -114,17 +114,36 @@ defmodule MvWeb.GroupLive.IndexTest do
end
end
@moduletag :slow
describe "performance" do
@describetag :slow
test "page loads efficiently with many groups", %{conn: conn} do
# Create multiple groups
Enum.each(1..10, fn _ -> Fixtures.group_fixture() end)
# Count queries using Telemetry
query_count_agent = Agent.start_link(fn -> 0 end) |> elem(1)
handler = fn _event, _measurements, _metadata, _config ->
Agent.update(query_count_agent, &(&1 + 1))
end
handler_id = "test-query-counter-#{System.unique_integer([:positive])}"
:telemetry.attach(handler_id, [:ash, :query, :start], handler, nil)
# Should load without N+1 queries
{:ok, _view, html} = live(conn, "/groups")
final_count = Agent.get(query_count_agent, & &1)
:telemetry.detach(handler_id)
# Verify all groups are displayed
assert html =~ gettext("Groups")
# Verify query count is reasonable (should avoid N+1 queries)
# Expected: 1 query for groups list + possible count queries
# Allow some overhead for LiveView setup queries
assert final_count <= 5,
"Expected max 5 queries (groups list + possible counts + LiveView setup), got #{final_count}. This suggests N+1 query problem."
end
test "member count is loaded efficiently via calculation", %{conn: conn} do
@ -143,10 +162,29 @@ defmodule MvWeb.GroupLive.IndexTest do
actor: system_actor
)
# Count queries using Telemetry
query_count_agent = Agent.start_link(fn -> 0 end) |> elem(1)
handler = fn _event, _measurements, _metadata, _config ->
Agent.update(query_count_agent, &(&1 + 1))
end
handler_id = "test-query-counter-#{System.unique_integer([:positive])}"
:telemetry.attach(handler_id, [:ash, :query, :start], handler, nil)
{:ok, _view, html} = live(conn, "/groups")
final_count = Agent.get(query_count_agent, & &1)
:telemetry.detach(handler_id)
# Member count should be displayed (should be 2)
assert html =~ "2" or html =~ gettext("Members") or html =~ "Mitglieder"
# Verify query count is reasonable (member count should be calculated efficiently)
# Expected: 1 query for groups + 1 query for member counts (aggregated)
# Allow some overhead for LiveView setup queries
assert final_count <= 5,
"Expected max 5 queries (groups + member counts + LiveView setup), got #{final_count}. This suggests inefficient member count calculation."
end
end
end

View file

@ -219,8 +219,8 @@ defmodule MvWeb.GroupLive.ShowTest do
end
end
@moduletag :slow
describe "performance" do
@describetag :slow
test "member list is loaded efficiently (no N+1 queries)", %{conn: conn} do
group = Fixtures.group_fixture()
@ -236,12 +236,31 @@ defmodule MvWeb.GroupLive.ShowTest do
)
end)
# Count queries using Telemetry
query_count_agent = Agent.start_link(fn -> 0 end) |> elem(1)
handler = fn _event, _measurements, _metadata, _config ->
Agent.update(query_count_agent, &(&1 + 1))
end
handler_id = "test-query-counter-#{System.unique_integer([:positive])}"
:telemetry.attach(handler_id, [:ash, :query, :start], handler, nil)
{:ok, _view, html} = live(conn, "/groups/#{group.slug}")
final_count = Agent.get(query_count_agent, & &1)
:telemetry.detach(handler_id)
# All members should be displayed
Enum.each(members, fn member ->
assert html =~ member.first_name or html =~ member.last_name
end)
# Verify query count is reasonable (should avoid N+1 queries)
# Expected: 1 query for group lookup + 1 query for members (with preload)
# Allow some overhead for LiveView setup queries
assert final_count <= 5,
"Expected max 5 queries (group + members preload + LiveView setup), got #{final_count}. This suggests N+1 query problem."
end
test "slug lookup is efficient (uses unique_slug index)", %{conn: conn} do