Use built-in Req stub for LastFm tests
This commit is contained in:
+6
-1
@@ -35,6 +35,11 @@ config :phoenix_live_view,
|
|||||||
|
|
||||||
config :music_library, monitoring_routes: true
|
config :music_library, monitoring_routes: true
|
||||||
|
|
||||||
config :music_library, LastFm, auto_refresh: false
|
config :music_library, LastFm,
|
||||||
|
auto_refresh: false,
|
||||||
|
req_options: [
|
||||||
|
plug: {Req.Test, LastFm.API},
|
||||||
|
max_retries: 0
|
||||||
|
]
|
||||||
|
|
||||||
config :phoenix_test, :endpoint, MusicLibraryWeb.Endpoint
|
config :phoenix_test, :endpoint, MusicLibraryWeb.Endpoint
|
||||||
|
|||||||
+4
-4
@@ -2,7 +2,7 @@ defmodule LastFm do
|
|||||||
def get_artist_info(musicbrainz_id, name) do
|
def get_artist_info(musicbrainz_id, name) do
|
||||||
last_fm_config = last_fm_config()
|
last_fm_config = last_fm_config()
|
||||||
|
|
||||||
case last_fm_config.api.get_artist_info(
|
case LastFm.APIImpl.get_artist_info(
|
||||||
{:musicbrainz_id, musicbrainz_id},
|
{:musicbrainz_id, musicbrainz_id},
|
||||||
last_fm_config
|
last_fm_config
|
||||||
) do
|
) do
|
||||||
@@ -12,7 +12,7 @@ defmodule LastFm do
|
|||||||
{:error, :invalid_parameters} ->
|
{:error, :invalid_parameters} ->
|
||||||
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
|
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
|
||||||
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
|
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
|
||||||
last_fm_config.api.get_artist_info({:name, name}, last_fm_config)
|
LastFm.APIImpl.get_artist_info({:name, name}, last_fm_config)
|
||||||
|
|
||||||
error ->
|
error ->
|
||||||
error
|
error
|
||||||
@@ -22,7 +22,7 @@ defmodule LastFm do
|
|||||||
def get_similar_artists(musicbrainz_id, name) do
|
def get_similar_artists(musicbrainz_id, name) do
|
||||||
last_fm_config = last_fm_config()
|
last_fm_config = last_fm_config()
|
||||||
|
|
||||||
case last_fm_config.api.get_similar_artists(
|
case LastFm.APIImpl.get_similar_artists(
|
||||||
{:musicbrainz_id, musicbrainz_id},
|
{:musicbrainz_id, musicbrainz_id},
|
||||||
last_fm_config
|
last_fm_config
|
||||||
) do
|
) do
|
||||||
@@ -32,7 +32,7 @@ defmodule LastFm do
|
|||||||
{:error, :invalid_parameters} ->
|
{:error, :invalid_parameters} ->
|
||||||
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
|
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
|
||||||
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
|
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
|
||||||
last_fm_config.api.get_similar_artists({:name, name}, last_fm_config)
|
LastFm.APIImpl.get_similar_artists({:name, name}, last_fm_config)
|
||||||
|
|
||||||
error ->
|
error ->
|
||||||
error
|
error
|
||||||
|
|||||||
@@ -102,6 +102,7 @@ defmodule LastFm.APIImpl do
|
|||||||
],
|
],
|
||||||
user_agent: config.user_agent
|
user_agent: config.user_agent
|
||||||
)
|
)
|
||||||
|
|> Req.Request.merge_options(config.req_options)
|
||||||
|> Req.Request.put_private(:api_key, config.api_key)
|
|> Req.Request.put_private(:api_key, config.api_key)
|
||||||
|> Req.Request.append_request_steps(log_attempt: &log_attempt/1)
|
|> Req.Request.append_request_steps(log_attempt: &log_attempt/1)
|
||||||
|> Req.Request.append_response_steps(parse_error: &parse_error/1)
|
|> Req.Request.append_response_steps(parse_error: &parse_error/1)
|
||||||
@@ -114,6 +115,9 @@ defmodule LastFm.APIImpl do
|
|||||||
|
|
||||||
{:ok, response} ->
|
{:ok, response} ->
|
||||||
{:ok, response.body}
|
{:ok, response.body}
|
||||||
|
|
||||||
|
error ->
|
||||||
|
error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ defmodule LastFm.Config do
|
|||||||
user: String.t(),
|
user: String.t(),
|
||||||
auto_refresh: boolean(),
|
auto_refresh: boolean(),
|
||||||
refresh_interval: pos_integer(),
|
refresh_interval: pos_integer(),
|
||||||
user_agent: String.t()
|
user_agent: String.t(),
|
||||||
|
req_options: Keyword.t()
|
||||||
}
|
}
|
||||||
|
|
||||||
@enforce_keys [:api, :api_key, :user]
|
@enforce_keys [:api, :api_key, :user]
|
||||||
@@ -14,7 +15,8 @@ defmodule LastFm.Config do
|
|||||||
user: "",
|
user: "",
|
||||||
auto_refresh: true,
|
auto_refresh: true,
|
||||||
refresh_interval: 60_000,
|
refresh_interval: 60_000,
|
||||||
user_agent: "change me"
|
user_agent: "change me",
|
||||||
|
req_options: []
|
||||||
|
|
||||||
@schema NimbleOptions.new!(
|
@schema NimbleOptions.new!(
|
||||||
api: [
|
api: [
|
||||||
@@ -43,6 +45,11 @@ defmodule LastFm.Config do
|
|||||||
type: :string,
|
type: :string,
|
||||||
required: false,
|
required: false,
|
||||||
default: "change me"
|
default: "change me"
|
||||||
|
],
|
||||||
|
req_options: [
|
||||||
|
type: :keyword_list,
|
||||||
|
required: false,
|
||||||
|
default: []
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ defmodule LastFm.ConfigTest do
|
|||||||
describe "resolve/1" do
|
describe "resolve/1" do
|
||||||
test "reads data from application configuration" do
|
test "reads data from application configuration" do
|
||||||
assert %LastFm.Config{
|
assert %LastFm.Config{
|
||||||
api: LastFm.APIMock,
|
api: LastFm.APIImpl,
|
||||||
api_key: api_key,
|
api_key: api_key,
|
||||||
user: user,
|
user: user,
|
||||||
auto_refresh: false,
|
auto_refresh: false,
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
defmodule LastFmTest do
|
defmodule LastFmTest do
|
||||||
use ExUnit.Case, async: true
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
alias LastFm.APIMock
|
alias LastFm.{Artist, Fixtures}
|
||||||
alias LastFm.Fixtures.Artist
|
|
||||||
import Mox
|
|
||||||
|
|
||||||
setup :verify_on_exit!
|
|
||||||
|
|
||||||
describe "get_artist_info/1" do
|
describe "get_artist_info/1" do
|
||||||
test "it returns the artist info" do
|
test "it returns the artist info" do
|
||||||
name = "Steven Wilson"
|
name = "Steven Wilson"
|
||||||
musicbrainz_id = Ecto.UUID.generate()
|
musicbrainz_id = Ecto.UUID.generate()
|
||||||
expected_info = Artist.get_info()
|
|
||||||
|
|
||||||
expect(APIMock, :get_artist_info, fn {:musicbrainz_id, ^musicbrainz_id}, _config ->
|
expected_info =
|
||||||
{:ok, expected_info}
|
Fixtures.Artist.get_info()
|
||||||
|
|> Map.get("artist")
|
||||||
|
|> Artist.from_api_response()
|
||||||
|
|
||||||
|
Req.Test.stub(LastFm.API, fn conn ->
|
||||||
|
Req.Test.json(conn, Fixtures.Artist.get_info())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert {:ok, expected_info} == LastFm.get_artist_info(musicbrainz_id, name)
|
assert {:ok, expected_info} == LastFm.get_artist_info(musicbrainz_id, name)
|
||||||
|
|||||||
@@ -2,12 +2,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
|
|||||||
use MusicLibraryWeb.ConnCase
|
use MusicLibraryWeb.ConnCase
|
||||||
|
|
||||||
import MusicLibrary.Fixtures.Records
|
import MusicLibrary.Fixtures.Records
|
||||||
import LastFm.Fixtures.Artist
|
alias LastFm.Fixtures
|
||||||
import Mox
|
|
||||||
|
|
||||||
alias LastFm.APIMock
|
|
||||||
|
|
||||||
setup :verify_on_exit!
|
|
||||||
|
|
||||||
defp fill_collection(_config) do
|
defp fill_collection(_config) do
|
||||||
collection_record =
|
collection_record =
|
||||||
@@ -28,13 +23,14 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
|
|||||||
conn: conn,
|
conn: conn,
|
||||||
artist_musicbrainz_id: artist_musicbrainz_id
|
artist_musicbrainz_id: artist_musicbrainz_id
|
||||||
} do
|
} do
|
||||||
expect(APIMock, :get_artist_info, fn {:musicbrainz_id, ^artist_musicbrainz_id}, _config ->
|
Req.Test.stub(LastFm.API, fn conn ->
|
||||||
{:ok, get_info()}
|
case Map.get(conn.params, "method") do
|
||||||
end)
|
"artist.getInfo" ->
|
||||||
|
Req.Test.json(conn, Fixtures.Artist.get_info())
|
||||||
|
|
||||||
expect(APIMock, :get_similar_artists, fn {:musicbrainz_id, ^artist_musicbrainz_id},
|
"artist.getSimilar" ->
|
||||||
_config ->
|
Req.Test.json(conn, Fixtures.Artist.get_similar_artists())
|
||||||
{:ok, []}
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
@@ -49,13 +45,14 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
|
|||||||
conn: conn,
|
conn: conn,
|
||||||
artist_musicbrainz_id: artist_musicbrainz_id
|
artist_musicbrainz_id: artist_musicbrainz_id
|
||||||
} do
|
} do
|
||||||
expect(APIMock, :get_artist_info, fn {:musicbrainz_id, ^artist_musicbrainz_id}, _config ->
|
Req.Test.stub(LastFm.API, fn conn ->
|
||||||
{:error, :timeout}
|
case Map.get(conn.params, "method") do
|
||||||
end)
|
"artist.getInfo" ->
|
||||||
|
Req.Test.transport_error(conn, :timeout)
|
||||||
|
|
||||||
expect(APIMock, :get_similar_artists, fn {:musicbrainz_id, ^artist_musicbrainz_id},
|
"artist.getSimilar" ->
|
||||||
_config ->
|
Req.Test.json(conn, Fixtures.Artist.get_similar_artists())
|
||||||
{:ok, []}
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
@@ -81,14 +78,14 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
|
|||||||
other_collection_record =
|
other_collection_record =
|
||||||
record_with_artist("Porcupine Tree", %{purchased_at: DateTime.utc_now()})
|
record_with_artist("Porcupine Tree", %{purchased_at: DateTime.utc_now()})
|
||||||
|
|
||||||
# for this test, we don't care about the artist info, but we mock it to avoid false test failures
|
Req.Test.stub(LastFm.API, fn conn ->
|
||||||
expect(APIMock, :get_artist_info, fn {:musicbrainz_id, ^artist_musicbrainz_id}, _config ->
|
case Map.get(conn.params, "method") do
|
||||||
{:error, :timeout}
|
"artist.getInfo" ->
|
||||||
end)
|
Req.Test.json(conn, Fixtures.Artist.get_info())
|
||||||
|
|
||||||
expect(APIMock, :get_similar_artists, fn {:musicbrainz_id, ^artist_musicbrainz_id},
|
"artist.getSimilar" ->
|
||||||
_config ->
|
Req.Test.json(conn, Fixtures.Artist.get_similar_artists())
|
||||||
{:ok, []}
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
defmodule LastFm.Fixtures.Artist do
|
defmodule LastFm.Fixtures.Artist do
|
||||||
@fixtures_folder Path.join([File.cwd!(), "test/support/fixtures/last_fm"])
|
@fixtures_folder Path.join([File.cwd!(), "test/support/fixtures/last_fm"])
|
||||||
|
|
||||||
alias LastFm.Artist
|
|
||||||
|
|
||||||
def get_info do
|
def get_info do
|
||||||
Path.join([@fixtures_folder, "artist.getinfo.json"])
|
Path.join([@fixtures_folder, "artist.getinfo.json"])
|
||||||
|> File.read!()
|
|> File.read!()
|
||||||
|> JSON.decode!()
|
|> JSON.decode!()
|
||||||
|> Map.get("artist")
|
end
|
||||||
|> Artist.from_api_response()
|
|
||||||
|
def get_similar_artists do
|
||||||
|
%{"similarartists" => %{"artist" => []}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,13 +6,5 @@ music_brainz_config =
|
|||||||
|
|
||||||
Application.put_env(:music_library, MusicBrainz, music_brainz_config)
|
Application.put_env(:music_library, MusicBrainz, music_brainz_config)
|
||||||
|
|
||||||
Mox.defmock(LastFm.APIMock, for: LastFm.APIBehaviour)
|
|
||||||
|
|
||||||
last_fm_config =
|
|
||||||
Application.get_env(:music_library, LastFm)
|
|
||||||
|> Keyword.put(:api, LastFm.APIMock)
|
|
||||||
|
|
||||||
Application.put_env(:music_library, LastFm, last_fm_config)
|
|
||||||
|
|
||||||
ExUnit.start()
|
ExUnit.start()
|
||||||
Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.Repo, :manual)
|
Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.Repo, :manual)
|
||||||
|
|||||||
Reference in New Issue
Block a user