Don't run LastFm.Feed in tests
This commit is contained in:
@@ -27,3 +27,5 @@ config :phoenix, :plug_init_mode, :runtime
|
||||
# Enable helpful, but potentially expensive runtime checks
|
||||
config :phoenix_live_view,
|
||||
enable_expensive_runtime_checks: true
|
||||
|
||||
config :music_library, :last_fm, nil
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
defmodule LastFm.APIBehaviour do
|
||||
alias LastFm.Track
|
||||
|
||||
@type user :: String.t()
|
||||
@type api_key :: String.t()
|
||||
@callback get_recent_tracks(user, api_key) :: {:ok, [%Track{}]} | {:error, String.t()}
|
||||
end
|
||||
@@ -1,10 +1,13 @@
|
||||
defmodule LastFm.API do
|
||||
defmodule LastFm.APIImpl do
|
||||
@behaviour LastFm.APIBehaviour
|
||||
|
||||
require Logger
|
||||
|
||||
alias LastFm.Track
|
||||
|
||||
@base_url "http://ws.audioscrobbler.com/2.0/"
|
||||
|
||||
@impl true
|
||||
def get_recent_tracks(user, api_key) do
|
||||
options = [
|
||||
method: "user.getrecenttracks",
|
||||
@@ -3,7 +3,7 @@ defmodule LastFm.Refresh do
|
||||
|
||||
require Logger
|
||||
|
||||
alias LastFm.{API, Feed}
|
||||
alias LastFm.Feed
|
||||
|
||||
@refresh_interval System.convert_time_unit(30, :second, :millisecond)
|
||||
|
||||
@@ -12,7 +12,7 @@ defmodule LastFm.Refresh do
|
||||
end
|
||||
|
||||
def init(config) do
|
||||
if config.api_key do
|
||||
if enabled?(config) do
|
||||
{:ok, config, {:continue, :refresh}}
|
||||
else
|
||||
:ignore
|
||||
@@ -20,7 +20,7 @@ defmodule LastFm.Refresh do
|
||||
end
|
||||
|
||||
def handle_continue(:refresh, config) do
|
||||
case API.get_recent_tracks(config.user, config.api_key) do
|
||||
case config.api.get_recent_tracks(config.user, config.api_key) do
|
||||
{:ok, tracks} ->
|
||||
Feed.update(tracks)
|
||||
Process.send_after(self(), :refresh, @refresh_interval)
|
||||
@@ -32,4 +32,8 @@ defmodule LastFm.Refresh do
|
||||
{:noreply, config}
|
||||
end
|
||||
end
|
||||
|
||||
defp enabled?(config) do
|
||||
config.api && config.api_key
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,11 +6,15 @@ defmodule LastFm.Supervisor do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_init_arg) do
|
||||
def init(opts) do
|
||||
:ok = LastFm.Feed.create_table!()
|
||||
|
||||
api =
|
||||
Keyword.fetch!(opts, :api)
|
||||
|> IO.inspect()
|
||||
|
||||
children = [
|
||||
{LastFm.Refresh, %{user: "cloud8421", api_key: api_key()}}
|
||||
{LastFm.Refresh, %{api: api, user: "cloud8421", api_key: api_key()}}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_one)
|
||||
|
||||
@@ -15,7 +15,7 @@ defmodule MusicLibrary.Application do
|
||||
repos: Application.fetch_env!(:music_library, :ecto_repos), skip: skip_migrations?()},
|
||||
{DNSCluster, query: Application.get_env(:music_library, :dns_cluster_query) || :ignore},
|
||||
{Phoenix.PubSub, name: MusicLibrary.PubSub},
|
||||
LastFm.Supervisor,
|
||||
{LastFm.Supervisor, api: last_fm()},
|
||||
# Start a worker by calling: MusicLibrary.Worker.start_link(arg)
|
||||
# {MusicLibrary.Worker, arg},
|
||||
# Start to serve requests, typically the last entry
|
||||
@@ -40,4 +40,8 @@ defmodule MusicLibrary.Application do
|
||||
# By default, sqlite migrations are run when using a release
|
||||
System.get_env("RELEASE_NAME") != nil
|
||||
end
|
||||
|
||||
defp last_fm do
|
||||
Application.get_env(:music_library, :last_fm, LastFm.APIImpl)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
Mox.defmock(MusicBrainz.APIBehaviourMock, for: MusicBrainz.APIBehaviour)
|
||||
Application.put_env(:music_library, :musicbrainz, MusicBrainz.APIBehaviourMock)
|
||||
|
||||
Mox.defmock(LastFm.APIBehaviourMock, for: LastFm.APIBehaviour)
|
||||
Application.put_env(:music_library, :last_fm, LastFm.APIBehaviourMock)
|
||||
|
||||
ExUnit.start()
|
||||
Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.Repo, :manual)
|
||||
|
||||
Reference in New Issue
Block a user