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:
Claudio Ortolina
2024-11-11 10:28:17 +00:00
parent f704d3834b
commit 0cbece6ed4
2 changed files with 113 additions and 1 deletions
+63
View File
@@ -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
+50 -1
View File
@@ -55,7 +55,7 @@ defmodule MusicLibrary.RecordsFixtures do
artists_attrs = [ artists_attrs = [
%{ %{
name: artist_name, name: artist_name,
musicbrainz_id: Ecto.UUID.generate(), musicbrainz_id: artist_uuid(artist_name),
sort_name: artist_name, sort_name: artist_name,
disambiguation: artist_name disambiguation: artist_name
} }
@@ -79,4 +79,53 @@ defmodule MusicLibrary.RecordsFixtures do
record record
end end
def record_fixture_with_artist(artist_name, record_attrs \\ %{}) do
artists_attrs = [
%{
name: artist_name,
musicbrainz_id: artist_uuid(artist_name),
sort_name: artist_name,
disambiguation: artist_name
}
]
record_attrs
|> Map.put(:artists, artists_attrs)
|> record_fixture()
end
defp artist_uuid(name) do
<<u0::48, _::4, u1::12, _::2, u2::62>> = :crypto.hash(:md5, name)
encode(<<u0::48, 4::4, u1::12, 2::2, u2::62>>)
end
defp encode(
<<a1::4, a2::4, a3::4, a4::4, a5::4, a6::4, a7::4, a8::4, b1::4, b2::4, b3::4, b4::4,
c1::4, c2::4, c3::4, c4::4, d1::4, d2::4, d3::4, d4::4, e1::4, e2::4, e3::4, e4::4,
e5::4, e6::4, e7::4, e8::4, e9::4, e10::4, e11::4, e12::4>>
) do
<<e(a1), e(a2), e(a3), e(a4), e(a5), e(a6), e(a7), e(a8), ?-, e(b1), e(b2), e(b3), e(b4), ?-,
e(c1), e(c2), e(c3), e(c4), ?-, e(d1), e(d2), e(d3), e(d4), ?-, e(e1), e(e2), e(e3), e(e4),
e(e5), e(e6), e(e7), e(e8), e(e9), e(e10), e(e11), e(e12)>>
end
@compile {:inline, e: 1}
defp e(0), do: ?0
defp e(1), do: ?1
defp e(2), do: ?2
defp e(3), do: ?3
defp e(4), do: ?4
defp e(5), do: ?5
defp e(6), do: ?6
defp e(7), do: ?7
defp e(8), do: ?8
defp e(9), do: ?9
defp e(10), do: ?a
defp e(11), do: ?b
defp e(12), do: ?c
defp e(13), do: ?d
defp e(14), do: ?e
defp e(15), do: ?f
end end