Skip to content

Commit

Permalink
Renamed CommandRunnerMock to have a more descriptive name
Browse files Browse the repository at this point in the history
  • Loading branch information
kieraneglin committed Jan 23, 2024
1 parent 94ca110 commit d664e82
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions test/pinchflat/downloader/backends/yt_dlp/channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.ChannelTest do

describe "get_channel_info/1" do
test "it returns a %ChannelDetails{} with data on success" do
expect(CommandRunnerMock, :run, fn _url, _opts ->
expect(YtDlpRunnerMock, :run, fn _url, _opts ->
{:ok, "{\"channel\": \"TheUselessTrials\", \"channel_id\": \"UCQH2\"}"}
end)

Expand All @@ -20,7 +20,7 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.ChannelTest do
end

test "it passes the expected args to the backend runner" do
expect(CommandRunnerMock, :run, fn @channel_url, opts ->
expect(YtDlpRunnerMock, :run, fn @channel_url, opts ->
assert opts == [{:print, "%(.{channel,channel_id})j"}, {:playlist_end, 1}]

{:ok, "{}"}
Expand All @@ -30,13 +30,13 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.ChannelTest do
end

test "it returns an error if the runner returns an error" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:error, "Big issue", 1} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:error, "Big issue", 1} end)

assert {:error, "Big issue", 1} = Channel.get_channel_info(@channel_url)
end

test "it returns an error if the output is not JSON" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:ok, "Not JSON"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:ok, "Not JSON"} end)

assert {:error, %Jason.DecodeError{}} = Channel.get_channel_info(@channel_url)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.VideoCollectionTest do

describe "get_video_ids/2" do
test "returns a list of video ids with no blank elements" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:ok, "id1\nid2\n\nid3\n"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:ok, "id1\nid2\n\nid3\n"} end)

assert {:ok, ["id1", "id2", "id3"]} = VideoCollectionUser.get_video_ids(@channel_url)
end

test "it passes the expected default args" do
expect(CommandRunnerMock, :run, fn _url, opts ->
expect(YtDlpRunnerMock, :run, fn _url, opts ->
assert opts == [:simulate, :skip_download, {:print, :id}]

{:ok, ""}
Expand All @@ -30,7 +30,7 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.VideoCollectionTest do
end

test "it passes the expected custom args" do
expect(CommandRunnerMock, :run, fn _url, opts ->
expect(YtDlpRunnerMock, :run, fn _url, opts ->
assert opts == [:custom_arg, :simulate, :skip_download, {:print, :id}]

{:ok, ""}
Expand All @@ -40,7 +40,7 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.VideoCollectionTest do
end

test "returns the error straight through when the command fails" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:error, "Big issue", 1} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:error, "Big issue", 1} end)

assert {:error, "Big issue", 1} = VideoCollectionUser.get_video_ids(@channel_url)
end
Expand Down
10 changes: 5 additions & 5 deletions test/pinchflat/downloader/backends/yt_dlp/video_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.VideoTest do

describe "download/2" do
test "it calls the backend runner with the expected arguments" do
expect(CommandRunnerMock, :run, fn @video_url, opts ->
expect(YtDlpRunnerMock, :run, fn @video_url, opts ->
assert opts == [:no_simulate, {:print, "%()j"}]

{:ok, "{}"}
Expand All @@ -20,7 +20,7 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.VideoTest do
end

test "it passes along additional options" do
expect(CommandRunnerMock, :run, fn _url, opts ->
expect(YtDlpRunnerMock, :run, fn _url, opts ->
assert opts == [:no_simulate, {:print, "%()j"}, :custom_arg]

{:ok, "{}"}
Expand All @@ -30,19 +30,19 @@ defmodule Pinchflat.Downloader.Backends.YtDlp.VideoTest do
end

test "it parses the output as JSON" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:ok, "{\"title\": \"Test\"}"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:ok, "{\"title\": \"Test\"}"} end)

assert {:ok, %{"title" => "Test"}} = Video.download(@video_url)
end

test "it returns an error if the output is not JSON" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:ok, "Not JSON"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:ok, "Not JSON"} end)

assert {:error, %Jason.DecodeError{}} = Video.download(@video_url)
end

test "it directly passes along any errors" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:error, "Big issue", 1} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:error, "Big issue", 1} end)

assert {:error, "Big issue", 1} = Video.download(@video_url)
end
Expand Down
4 changes: 2 additions & 2 deletions test/pinchflat/downloader/channel_details_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Pinchflat.Downloader.ChannelDetailsTest do

describe "get_channel_details/2" do
test "it passes the expected arguments to the backend" do
expect(CommandRunnerMock, :run, fn @channel_url, opts ->
expect(YtDlpRunnerMock, :run, fn @channel_url, opts ->
assert opts == [{:print, "%(.{channel,channel_id})j"}, {:playlist_end, 1}]

{:ok, "{\"channel\": \"TheUselessTrials\", \"channel_id\": \"UCQH2\"}"}
Expand All @@ -27,7 +27,7 @@ defmodule Pinchflat.Downloader.ChannelDetailsTest do
end

test "it returns a struct composed of the returned data" do
expect(CommandRunnerMock, :run, fn _url, _opts ->
expect(YtDlpRunnerMock, :run, fn _url, _opts ->
{:ok, "{\"channel\": \"TheUselessTrials\", \"channel_id\": \"UCQH2\"}"}
end)

Expand Down
4 changes: 2 additions & 2 deletions test/pinchflat/downloader/video_downloader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Pinchflat.Downloader.VideoDownloaderTest do

describe "download_for_media_profile/3" do
test "it calls the backend runner with the arguments built from the media profile" do
expect(CommandRunnerMock, :run, fn @video_url, opts ->
expect(YtDlpRunnerMock, :run, fn @video_url, opts ->
assert :no_simulate in opts
assert {:print, "%()j"} in opts
assert {:output, "/tmp/yt-dlp/videos/%(title)S.%(ext)s"} in opts
Expand All @@ -26,7 +26,7 @@ defmodule Pinchflat.Downloader.VideoDownloaderTest do
end

test "it returns the parsed JSON output" do
expect(CommandRunnerMock, :run, fn _url, _opts -> {:ok, "{\"title\": \"Test\"}"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts -> {:ok, "{\"title\": \"Test\"}"} end)

assert {:ok, %{"title" => "Test"}} =
VideoDownloader.download_for_media_profile(@video_url, @media_profile)
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Mox.defmock(CommandRunnerMock, for: Pinchflat.Downloader.Backends.BackendCommandRunner)
Application.put_env(:pinchflat, :yt_dlp_runner, CommandRunnerMock)
Mox.defmock(YtDlpRunnerMock, for: Pinchflat.Downloader.Backends.BackendCommandRunner)
Application.put_env(:pinchflat, :yt_dlp_runner, YtDlpRunnerMock)

ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Pinchflat.Repo, :manual)

0 comments on commit d664e82

Please sign in to comment.