Add Records search tests
Important note: to help, we use a deterministic implementation of artist UUID generation based on the artist name.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
defmodule MusicLibrary.RecordsTest do
|
||||
use MusicLibrary.DataCase
|
||||
|
||||
alias MusicLibrary.Records
|
||||
alias MusicLibrary.Records.Record
|
||||
import MusicLibrary.RecordsFixtures
|
||||
|
||||
defp create_records(_) do
|
||||
records = [
|
||||
record_fixture_with_artist("Marillion", %{title: "Brave", format: :vinyl}),
|
||||
record_fixture_with_artist("Marillion", %{title: "Brave (Live)", format: :cd, type: :live}),
|
||||
record_fixture_with_artist("Marillion", %{title: "Afraid of Sunlight"}),
|
||||
record_fixture_with_artist("Airbag", %{title: "The Greatest Show on Earth"}),
|
||||
record_fixture_with_artist("Airbag (AU)", %{title: "Libertad"})
|
||||
]
|
||||
|
||||
%{records: records}
|
||||
end
|
||||
|
||||
# using record ids as records contain autogenerated fields which are not loaded when the record is created
|
||||
defp search(query, limit, offset) do
|
||||
Record
|
||||
|> Records.search_records(query, limit: limit, offset: offset)
|
||||
|> Enum.map(& &1.id)
|
||||
end
|
||||
|
||||
describe "search_records/2" do
|
||||
setup [:create_records]
|
||||
|
||||
test "untagged search (with limit and offset)", %{
|
||||
records: [brave_vinyl, brave_live_cd | _rest]
|
||||
} do
|
||||
assert [brave_vinyl.id, brave_live_cd.id] == search("brave", 10, 0)
|
||||
assert [brave_vinyl.id] == search("brave", 1, 0)
|
||||
assert [brave_live_cd.id] == search("brave", 1, 1)
|
||||
end
|
||||
|
||||
test "tagged search - album", %{records: [brave_vinyl, brave_live_cd | _rest]} do
|
||||
assert [brave_live_cd.id] == search(~s(album:"Brave \(Live\)"), 10, 0)
|
||||
end
|
||||
|
||||
test "tagged search - artist", %{records: [_, _, _, greatest_show_on_earth, libertad]} do
|
||||
assert [greatest_show_on_earth.id, libertad.id] == search("artist:airbag", 10, 0)
|
||||
assert [libertad.id] == search(~s(artist:"airbag \(AU\)"), 10, 0)
|
||||
end
|
||||
|
||||
test "tagged search - format", %{records: [_brave_vinyl, brave_live_cd | _rest]} do
|
||||
assert [brave_live_cd.id] == search("brave format:cd", 10, 0)
|
||||
end
|
||||
|
||||
test "tagged search - type", %{records: [_brave_vinyl, brave_live_cd | _rest]} do
|
||||
assert [brave_live_cd.id] == search("brave type:live", 10, 0)
|
||||
end
|
||||
|
||||
test "tagged search - mbid", %{records: [_, _, _, greatest_show_on_earth, libertad]} do
|
||||
[airbag_mbid] = Enum.map(greatest_show_on_earth.artists, fn a -> a.musicbrainz_id end)
|
||||
[airbag_au_mbid] = Enum.map(libertad.artists, fn a -> a.musicbrainz_id end)
|
||||
|
||||
assert [greatest_show_on_earth.id] == search("mbid:#{airbag_mbid}", 10, 0)
|
||||
assert [libertad.id] == search("mbid:#{airbag_au_mbid}", 10, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user