From f704d3834b99bc82fccd2639ed2d57611da4b9fd Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sun, 10 Nov 2024 21:12:09 +0000 Subject: [PATCH] Add collection context tests --- test/music_library/collection_test.exs | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 test/music_library/collection_test.exs diff --git a/test/music_library/collection_test.exs b/test/music_library/collection_test.exs new file mode 100644 index 00000000..dbcd8925 --- /dev/null +++ b/test/music_library/collection_test.exs @@ -0,0 +1,70 @@ +defmodule MusicLibrary.CollectionTest do + use MusicLibrary.DataCase + + alias MusicLibrary.Collection + import MusicLibrary.RecordsFixtures + + defp fill_collection(_) do + records = [ + record_fixture(%{title: "Brave", format: :cd, type: :album}), + record_fixture(%{title: "Brave", format: :vinyl, type: :live}), + record_fixture(%{format: :vinyl, type: :ep}), + record_fixture(%{title: "Brave", format: :dvd, purchased_at: nil, type: :live}) + ] + + %{collection: records} + end + + describe "search_records/2" do + setup [:fill_collection] + + test "returns matching records, excluding wishlisted records" do + assert [%{title: "Brave"}, %{title: "Brave"}] = Collection.search_records("brave") + end + + test "it respects limit" do + assert [%{title: "Brave"}] = Collection.search_records("brave", limit: 1) + end + + test "it respects offset" do + [first_match] = Collection.search_records("brave", limit: 1) + [second_match] = Collection.search_records("brave", limit: 1, offset: 1) + assert first_match !== second_match + end + end + + describe "search_records_count/1" do + setup [:fill_collection] + + test "returns the count of matching records, excluding wishlisted records" do + assert 2 = Collection.search_records_count("brave") + end + end + + describe "count_records_by_format/0" do + setup [:fill_collection] + + test "returns the count of records in the collection by format, excluding wishlisted records" do + assert [vinyl: 2, cd: 1] = Collection.count_records_by_format() + end + end + + describe "count_records_by_type/0" do + setup [:fill_collection] + + test "returns the count of records in the collection by type, excluding wishlisted records" do + assert [live: 1, ep: 1, album: 1] = Collection.count_records_by_type() + end + end + + describe "get_latest_record!/0" do + setup [:fill_collection] + + test "returns the most recently purchased record" do + expected_record = record_fixture(%{purchased_at: DateTime.utc_now()}) + most_recent_purchase = Collection.get_latest_record!() + + assert expected_record.id == most_recent_purchase.id + end + end +end