From f44530730b22674ce3e82707881bc46b298f6423 Mon Sep 17 00:00:00 2001 From: Frank Midigo Date: Tue, 7 Nov 2023 10:41:22 +0300 Subject: [PATCH] fix scheduler tests --- lib/lightning/invocation/dataclip.ex | 5 +++ lib/lightning/workflows/scheduler.ex | 4 +- .../live/run_live/rerun_job_component.ex | 2 + test/lightning/jobs_test.exs | 45 +++++++++---------- .../lightning_web/plugs/webhook_auth_test.exs | 15 ++++++- 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/lib/lightning/invocation/dataclip.ex b/lib/lightning/invocation/dataclip.ex index 2c602bd0a7..f78e63b676 100644 --- a/lib/lightning/invocation/dataclip.ex +++ b/lib/lightning/invocation/dataclip.ex @@ -21,6 +21,7 @@ defmodule Lightning.Invocation.Dataclip do """ use Ecto.Schema import Ecto.Changeset + import Ecto.Query alias Lightning.Invocation.Run alias Lightning.Projects.Project @@ -94,4 +95,8 @@ defmodule Lightning.Invocation.Dataclip do def get_types do @source_types end + + def body_included_query do + from d in __MODULE__, select: %{d | body: d.body} + end end diff --git a/lib/lightning/workflows/scheduler.ex b/lib/lightning/workflows/scheduler.ex index fe74931215..d3798e2b6d 100644 --- a/lib/lightning/workflows/scheduler.ex +++ b/lib/lightning/workflows/scheduler.ex @@ -3,7 +3,6 @@ defmodule Lightning.Workflows.Scheduler do The Scheduler is responsible for finding jobs that are ready to run based on their cron schedule, and then running them. """ - alias Lightning.WorkOrders use Oban.Worker, queue: :scheduler, @@ -16,9 +15,8 @@ defmodule Lightning.Workflows.Scheduler do alias Lightning.{ Invocation, - Pipeline, Repo, - WorkOrderService, + WorkOrders, Workflows } diff --git a/lib/lightning_web/live/run_live/rerun_job_component.ex b/lib/lightning_web/live/run_live/rerun_job_component.ex index 198bd15b30..cfb13520fa 100644 --- a/lib/lightning_web/live/run_live/rerun_job_component.ex +++ b/lib/lightning_web/live/run_live/rerun_job_component.ex @@ -7,6 +7,7 @@ defmodule LightningWeb.RunLive.RerunJobComponent do alias Lightning.Jobs alias Lightning.Workflows + @impl true def update( %{ total_entries: _count, @@ -41,6 +42,7 @@ defmodule LightningWeb.RunLive.RerunJobComponent do {:noreply, assign(socket, selected_job: selected_job)} end + @impl true def render(assigns) do ~H"""
true}) + output_dataclip: + build(:dataclip, type: :run_result, body: %{"changed" => true}) ) ] ) - old = - %Workflows.Job{id: job.id} - |> Lightning.Invocation.Query.last_successful_run_for_job() - |> Repo.one() - |> Repo.preload(:input_dataclip) - |> Repo.preload(:output_dataclip) + [old_run] = attempt.runs _result = Scheduler.enqueue_cronjobs() - new = - %Workflows.Job{id: job.id} - |> Lightning.Invocation.Query.last_successful_run_for_job() + new_attempt = + Attempt + |> last(:inserted_at) + |> preload(dataclip: ^Dataclip.body_included_query()) |> Repo.one() - |> Repo.preload(:input_dataclip) - |> Repo.preload(:output_dataclip) - assert old.input_dataclip.type == :http_request - assert old.input_dataclip.body == %{} + assert attempt.dataclip.type == :http_request + assert old_run.input_dataclip.type == :http_request + assert old_run.input_dataclip.body == %{} - assert new.input_dataclip.type == :run_result - assert new.input_dataclip.body == old.output_dataclip.body - assert new.output_dataclip.body == %{"changed" => true} + refute new_attempt.id == attempt.id + assert new_attempt.dataclip.type == :run_result + assert new_attempt.dataclip.body == old_run.output_dataclip.body end end end diff --git a/test/lightning_web/plugs/webhook_auth_test.exs b/test/lightning_web/plugs/webhook_auth_test.exs index 3f44998642..ca0fb7f7a6 100644 --- a/test/lightning_web/plugs/webhook_auth_test.exs +++ b/test/lightning_web/plugs/webhook_auth_test.exs @@ -22,7 +22,13 @@ defmodule LightningWeb.Plugs.WebhookAuthTest do trigger: trigger } do conn = conn(:post, "/i/#{trigger.id}") |> WebhookAuth.call([]) - assert conn.assigns[:trigger] == trigger |> unload_relation(:workflow) + + expected_trigger = + trigger + |> unload_relation(:workflow) + |> Repo.preload([:workflow, :edges]) + + assert conn.assigns[:trigger] == expected_trigger end test "responds with 401 for an unauthenticated request to a protected trigger", @@ -61,7 +67,12 @@ defmodule LightningWeb.Plugs.WebhookAuthTest do |> put_req_header("authorization", correct_credentials) |> WebhookAuth.call([]) - assert conn.assigns[:trigger] == trigger |> unload_relation(:workflow) + expected_trigger = + trigger + |> unload_relation(:workflow) + |> Repo.preload([:workflow, :edges]) + + assert conn.assigns[:trigger] == expected_trigger end defp associate_auth_method(trigger, auth_method) do