From 70ca206b5d0430fcd36eaecf0d1ddd7ec98759d9 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Thu, 3 Oct 2024 23:10:55 +0100 Subject: [PATCH] Show artists for latest record --- .../controllers/stats_html/index.html.heex | 7 ++- .../controllers/stats_controller_test.exs | 44 +++++++++++++++++++ test/support/fixtures/records_fixtures.ex | 4 +- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/music_library_web/controllers/stats_controller_test.exs diff --git a/lib/music_library_web/controllers/stats_html/index.html.heex b/lib/music_library_web/controllers/stats_html/index.html.heex index c47c51e4..1effa597 100644 --- a/lib/music_library_web/controllers/stats_html/index.html.heex +++ b/lib/music_library_web/controllers/stats_html/index.html.heex @@ -46,7 +46,12 @@

Latest record

-

<%= @latest_record.title %>

+

+ + <%= Enum.map(@latest_record.artists, fn a -> a.name end) %> + + <%= @latest_record.title %> +

record_fixture() end) + %{records: records} + end + + defp escape(string) do + string + |> Phoenix.HTML.html_escape() + |> Phoenix.HTML.safe_to_string() + end + + describe "GET /" do + setup [:create_records] + + test "it shows the record count (total and by format)", %{conn: conn, records: records} do + conn = get(conn, "/") + + assert html_response(conn, 200) =~ records |> length() |> Integer.to_string() + + records + |> Enum.frequencies_by(& &1.format) + |> Enum.each(fn {format, count} -> + assert html_response(conn, 200) =~ "#{count} #{format}" + end) + end + + test "it shows the latest record", %{conn: conn, records: records} do + latest_record = Enum.max_by(records, & &1.inserted_at) + + conn = get(conn, "/") + + assert html_response(conn, 200) =~ escape(latest_record.title) + + for artist <- latest_record.artists do + assert html_response(conn, 200) =~ escape(artist["name"]) + end + end + end +end diff --git a/test/support/fixtures/records_fixtures.ex b/test/support/fixtures/records_fixtures.ex index b7656531..44c52ca5 100644 --- a/test/support/fixtures/records_fixtures.ex +++ b/test/support/fixtures/records_fixtures.ex @@ -4,6 +4,8 @@ defmodule MusicLibrary.RecordsFixtures do entities via the `MusicLibrary.Records` context. """ + alias MusicLibrary.Records.Record + @genres [ "progressive rock", "art rock", @@ -43,7 +45,7 @@ defmodule MusicLibrary.RecordsFixtures do musicbrainz_id: musicbrainz_id, title: Enum.random(@titles), type: :album, - format: :cd, + format: Record.formats() |> Enum.random(), release: Enum.random(1969..2024) |> Integer.to_string() }) |> MusicLibrary.Records.create_record()