Small optimizations to speed up the test suite

Shaves off 1 second on M1
This commit is contained in:
Claudio Ortolina
2026-03-07 09:13:02 +00:00
parent 6a89d31d4c
commit f75485e5db
2 changed files with 79 additions and 84 deletions
@@ -12,7 +12,7 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
alias MusicLibrary.Records.Record alias MusicLibrary.Records.Record
# make it a multiple of 4 for easier calculations # make it a multiple of 4 for easier calculations
@default_records_page_size 8 @default_records_page_size 4
@total_records @default_records_page_size + div(@default_records_page_size, 2) @total_records @default_records_page_size + div(@default_records_page_size, 2)
defp fill_collection(_) do defp fill_collection(_) do
@@ -20,18 +20,11 @@ defmodule MusicLibraryWeb.CollectionLive.IndexTest do
%{collection: records} %{collection: records}
end end
defp fill_wishlist(_) do
records = Enum.map(1..@total_records, fn _ -> record(%{purchased_at: nil}) end)
%{wishlist: records}
end
describe "Collection" do describe "Collection" do
setup [:fill_collection, :fill_wishlist] setup [:fill_collection]
test "does not show wishlist records", %{ test "does not show wishlist records", %{conn: conn} do
conn: conn, wishlist_records = Enum.map(1..3, fn _ -> record(%{purchased_at: nil}) end)
wishlist: wishlist_records
} do
session = visit(conn, ~p"/collection") session = visit(conn, ~p"/collection")
for record <- wishlist_records do for record <- wishlist_records do
@@ -71,6 +71,81 @@ defmodule MusicLibraryWeb.StatsLive.IndexTest do
|> assert_has("dd", wishlist |> length() |> Integer.to_string()) |> assert_has("dd", wishlist |> length() |> Integer.to_string())
end end
test "it displays records for the current date in the 'On This Day' section", %{
conn: conn,
collection: collection
} do
today = Date.utc_today()
yesterday = Date.add(today, -1)
record_today =
List.first(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(today),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
record_yesterday =
List.last(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(yesterday),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
session = conn |> visit("/")
assert_has(session, "h1", "On This day")
assert_has(session, "##{record_today.id} h2", escape(record_today.title))
refute_has(session, "##{record_yesterday.id} h2", escape(record_yesterday.title))
end
test "it updates the 'On This Day' records when the date is changed", %{
conn: conn,
collection: collection
} do
today = Date.utc_today()
yesterday = Date.add(today, -1)
record_today =
List.first(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(today),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
record_yesterday =
List.last(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(yesterday),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
session = conn |> visit("/")
assert_has(session, "##{record_today.id} h2", escape(record_today.title))
refute_has(session, "##{record_yesterday.id} h2", escape(record_yesterday.title))
session
|> unwrap(fn view ->
view
|> form("[phx-change='set_current_date']", %{"current_date" => Date.to_iso8601(yesterday)})
|> render_change()
end)
refute_has(session, "##{record_today.id} h2", escape(record_today.title))
assert_has(session, "##{record_yesterday.id} h2", escape(record_yesterday.title))
end
end
describe "Scrobble activity" do
test "it shows the scrobble activity", %{conn: conn} do test "it shows the scrobble activity", %{conn: conn} do
# In test we don't run the LastFm.Refresh worker, # In test we don't run the LastFm.Refresh worker,
# so we need to interact directly with the LastFm.Feed to have some data # so we need to interact directly with the LastFm.Feed to have some data
@@ -241,78 +316,5 @@ defmodule MusicLibraryWeb.StatsLive.IndexTest do
assert [wishlisted_record] = Wishlist.search_records("mbid:#{release_group_id}") assert [wishlisted_record] = Wishlist.search_records("mbid:#{release_group_id}")
assert_path(session, ~p"/wishlist/#{wishlisted_record.id}") assert_path(session, ~p"/wishlist/#{wishlisted_record.id}")
end end
test "it displays records for the current date in the 'On This Day' section", %{
conn: conn,
collection: collection
} do
today = Date.utc_today()
yesterday = Date.add(today, -1)
record_today =
List.first(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(today),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
record_yesterday =
List.last(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(yesterday),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
session = conn |> visit("/")
assert_has(session, "h1", "On This day")
assert_has(session, "##{record_today.id} h2", escape(record_today.title))
refute_has(session, "##{record_yesterday.id} h2", escape(record_yesterday.title))
end
test "it updates the 'On This Day' records when the date is changed", %{
conn: conn,
collection: collection
} do
today = Date.utc_today()
yesterday = Date.add(today, -1)
record_today =
List.first(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(today),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
record_yesterday =
List.last(collection)
|> Records.change_record(%{
release_date: Date.to_iso8601(yesterday),
purchased_at: DateTime.utc_now()
})
|> Repo.update!()
session = conn |> visit("/")
assert_has(session, "##{record_today.id} h2", escape(record_today.title))
refute_has(session, "##{record_yesterday.id} h2", escape(record_yesterday.title))
session
|> unwrap(fn view ->
view
|> form("[phx-change='set_current_date']", %{"current_date" => Date.to_iso8601(yesterday)})
|> render_change()
end)
refute_has(session, "##{record_today.id} h2", escape(record_today.title))
assert_has(session, "##{record_yesterday.id} h2", escape(record_yesterday.title))
end
end end
end end