diff --git a/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex b/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex index 4036509..c31388d 100644 --- a/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex +++ b/lib/pinchflat/fast_indexing/fast_indexing_helpers.ex @@ -21,7 +21,12 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do alias Pinchflat.YtDlp.Media, as: YtDlpMedia - # TODO: test + @doc """ + Kicks off a new fast indexing task for a source. This will delete any existing fast indexing + tasks for the source before starting a new one. + + Returns {:ok, %Task{}} + """ def kickoff_indexing_task(%Source{} = source) do Tasks.delete_pending_tasks_for(source, "FastIndexingWorker", include_executing: true) FastIndexingWorker.kickoff_with_task(source) diff --git a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex index 2e483bf..643390f 100644 --- a/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex +++ b/lib/pinchflat/slow_indexing/slow_indexing_helpers.ex @@ -25,7 +25,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do Starts tasks for indexing a source's media regardless of the source's indexing frequency. It's assumed the caller will check for indexing frequency. - Returns {:ok, %Task{}}. + Returns {:ok, %Task{}} """ def kickoff_indexing_task(%Source{} = source, job_args \\ %{}, job_opts \\ []) do Tasks.delete_pending_tasks_for(source, "FastIndexingWorker") @@ -34,7 +34,12 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do MediaCollectionIndexingWorker.kickoff_with_task(source, job_args, job_opts) end - # TODO: test + @doc """ + A helper method to delete all indexing-related tasks for a source. + Optionally, you can include executing tasks in the deletion process. + + Returns :ok + """ def delete_indexing_tasks(%Source{} = source, opts \\ []) do include_executing = Keyword.get(opts, :include_executing, false) diff --git a/test/pinchflat/fast_indexing/fast_indexing_helpers_test.exs b/test/pinchflat/fast_indexing/fast_indexing_helpers_test.exs index 5422018..34f5952 100644 --- a/test/pinchflat/fast_indexing/fast_indexing_helpers_test.exs +++ b/test/pinchflat/fast_indexing/fast_indexing_helpers_test.exs @@ -1,6 +1,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do use Pinchflat.DataCase + import Pinchflat.TasksFixtures import Pinchflat.MediaFixtures import Pinchflat.SourcesFixtures import Pinchflat.ProfilesFixtures @@ -8,6 +9,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do alias Pinchflat.Tasks alias Pinchflat.Settings alias Pinchflat.Media.MediaItem + alias Pinchflat.FastIndexing.FastIndexingWorker alias Pinchflat.Downloading.MediaDownloadWorker alias Pinchflat.FastIndexing.FastIndexingHelpers @@ -19,6 +21,23 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do {:ok, [source: source_fixture()]} end + describe "kickoff_indexing_task/1" do + test "deletes any existing fast indexing tasks", %{source: source} do + {:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id})) + task = task_fixture(source_id: source.id, job_id: job.id) + + assert Repo.reload!(task) + assert {:ok, _} = FastIndexingHelpers.kickoff_indexing_task(source) + assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end + end + + test "kicks off a new fast indexing task", %{source: source} do + assert {:ok, _} = FastIndexingHelpers.kickoff_indexing_task(source) + assert [worker] = all_enqueued(worker: FastIndexingWorker) + assert worker.args["id"] == source.id + end + end + describe "kickoff_download_tasks_from_youtube_rss_feed/1" do test "enqueues a new worker for each new media_id in the source's RSS feed", %{source: source} do expect(HTTPClientMock, :get, fn _url -> {:ok, "test_1"} end) diff --git a/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs b/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs index 36abcd7..c7d9a1c 100644 --- a/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs +++ b/test/pinchflat/slow_indexing/slow_indexing_helpers_test.exs @@ -92,6 +92,56 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do end end + describe "delete_indexing_tasks/2" do + setup do + source = source_fixture() + + {:ok, %{source: source}} + end + + test "deletes slow indexing tasks for the source", %{source: source} do + {:ok, job} = Oban.insert(MediaCollectionIndexingWorker.new(%{"id" => source.id})) + _task = task_fixture(source_id: source.id, job_id: job.id) + + assert_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id}) + assert :ok = SlowIndexingHelpers.delete_indexing_tasks(source) + refute_enqueued(worker: MediaCollectionIndexingWorker) + end + + test "deletes fast indexing tasks for the source", %{source: source} do + {:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id})) + _task = task_fixture(source_id: source.id, job_id: job.id) + + assert_enqueued(worker: FastIndexingWorker, args: %{"id" => source.id}) + assert :ok = SlowIndexingHelpers.delete_indexing_tasks(source) + refute_enqueued(worker: FastIndexingWorker) + end + + test "doesn't normally delete currently executing tasks", %{source: source} do + {:ok, job} = Oban.insert(MediaCollectionIndexingWorker.new(%{"id" => source.id})) + task = task_fixture(source_id: source.id, job_id: job.id) + + from(Oban.Job, where: [id: ^job.id], update: [set: [state: "executing"]]) + |> Repo.update_all([]) + + assert Repo.reload!(task) + assert :ok = SlowIndexingHelpers.delete_indexing_tasks(source) + assert Repo.reload!(task) + end + + test "can optionally delete currently executing tasks", %{source: source} do + {:ok, job} = Oban.insert(MediaCollectionIndexingWorker.new(%{"id" => source.id})) + task = task_fixture(source_id: source.id, job_id: job.id) + + from(Oban.Job, where: [id: ^job.id], update: [set: [state: "executing"]]) + |> Repo.update_all([]) + + assert Repo.reload!(task) + assert :ok = SlowIndexingHelpers.delete_indexing_tasks(source, include_executing: true) + assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end + end + end + describe "index_and_enqueue_download_for_media_items/1" do setup do stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> diff --git a/test/pinchflat/tasks_test.exs b/test/pinchflat/tasks_test.exs index 4b99f12..53d1c32 100644 --- a/test/pinchflat/tasks_test.exs +++ b/test/pinchflat/tasks_test.exs @@ -247,7 +247,7 @@ defmodule Pinchflat.TasksTest do assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end end - test "deletion can optionall include executing tasks" do + test "deletion can optionally include executing tasks" do source = source_fixture() task = task_fixture(source_id: source.id)