channel tasks test

This commit is contained in:
Kieran Eglin 2024-02-01 21:06:55 -08:00
parent d54f9f8a79
commit bbae39b523
No known key found for this signature in database
GPG key ID: 193984967FCF432D
2 changed files with 15 additions and 15 deletions

View file

@ -8,21 +8,21 @@ defmodule Pinchflat.Tasks.ChannelTasks do
alias Pinchflat.Workers.MediaIndexingWorker alias Pinchflat.Workers.MediaIndexingWorker
@doc """ @doc """
Starts tasks for indexing a channel's media. Starts tasks for indexing a source's media.
Returns {:ok, :should_not_index} | {:ok, %Task{}}. Returns {:ok, :should_not_index} | {:ok, %Task{}}.
""" """
def kickoff_indexing_task(%Channel{} = channel) do def kickoff_indexing_task(%Channel{} = source) do
Tasks.delete_pending_tasks_for(channel) Tasks.delete_pending_tasks_for(source)
if channel.index_frequency_minutes <= 0 do if source.index_frequency_minutes <= 0 do
{:ok, :should_not_index} {:ok, :should_not_index}
else else
channel source
|> Map.take([:id]) |> Map.take([:id])
# Schedule this one immediately, but future ones will be on an interval # Schedule this one immediately, but future ones will be on an interval
|> MediaIndexingWorker.new() |> MediaIndexingWorker.new()
|> Tasks.create_job_with_task(channel) |> Tasks.create_job_with_task(source)
|> case do |> case do
# This should never return {:error, :duplicate_job} since we just deleted # This should never return {:error, :duplicate_job} since we just deleted
# any pending tasks. I'm being assertive about it so it's obvious if I'm wrong # any pending tasks. I'm being assertive about it so it's obvious if I'm wrong

View file

@ -12,32 +12,32 @@ defmodule Pinchflat.Tasks.ChannelTasksTest do
test "it does not schedule a job if the interval is <= 0" do test "it does not schedule a job if the interval is <= 0" do
source = source_fixture(index_frequency_minutes: -1) source = source_fixture(index_frequency_minutes: -1)
assert {:ok, :should_not_index} = ChannelTasks.kickoff_indexing_task(channel) assert {:ok, :should_not_index} = ChannelTasks.kickoff_indexing_task(source)
refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => channel.id}) refute_enqueued(worker: MediaIndexingWorker, args: %{"id" => source.id})
end end
test "it schedules a job if the interval is > 0" do test "it schedules a job if the interval is > 0" do
source = source_fixture(index_frequency_minutes: 1) source = source_fixture(index_frequency_minutes: 1)
assert {:ok, _} = ChannelTasks.kickoff_indexing_task(channel) assert {:ok, _} = ChannelTasks.kickoff_indexing_task(source)
assert_enqueued(worker: MediaIndexingWorker, args: %{"id" => channel.id}) assert_enqueued(worker: MediaIndexingWorker, args: %{"id" => source.id})
end end
test "it creates and attaches a task if the interval is > 0" do test "it creates and attaches a task if the interval is > 0" do
source = source_fixture(index_frequency_minutes: 1) source = source_fixture(index_frequency_minutes: 1)
assert {:ok, %Task{} = task} = ChannelTasks.kickoff_indexing_task(channel) assert {:ok, %Task{} = task} = ChannelTasks.kickoff_indexing_task(source)
assert task.channel_id == channel.id assert task.source_id == source.id
end end
test "it deletes any pending tasks for the channel" do test "it deletes any pending tasks for the source" do
source = source_fixture() source = source_fixture()
task = task_fixture(channel_id: channel.id) task = task_fixture(source_id: source.id)
assert {:ok, _} = ChannelTasks.kickoff_indexing_task(channel) assert {:ok, _} = ChannelTasks.kickoff_indexing_task(source)
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end
end end