Skip to content

Commit

Permalink
fix scheduler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
midigofrank committed Nov 7, 2023
1 parent f42ba29 commit f445307
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
5 changes: 5 additions & 0 deletions lib/lightning/invocation/dataclip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 1 addition & 3 deletions lib/lightning/workflows/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,9 +15,8 @@ defmodule Lightning.Workflows.Scheduler do

alias Lightning.{
Invocation,
Pipeline,
Repo,
WorkOrderService,
WorkOrders,
Workflows
}

Expand Down
2 changes: 2 additions & 0 deletions lib/lightning_web/live/run_live/rerun_job_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule LightningWeb.RunLive.RerunJobComponent do
alias Lightning.Jobs
alias Lightning.Workflows

@impl true
def update(
%{
total_entries: _count,
Expand Down Expand Up @@ -41,6 +42,7 @@ defmodule LightningWeb.RunLive.RerunJobComponent do
{:noreply, assign(socket, selected_job: selected_job)}
end

@impl true
def render(assigns) do
~H"""
<div
Expand Down
45 changes: 21 additions & 24 deletions test/lightning/jobs_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Lightning.JobsTest do
alias Lightning.Attempt
alias Lightning.Invocation.Dataclip
use Lightning.DataCase, async: true

alias Lightning.Jobs
Expand Down Expand Up @@ -333,18 +335,17 @@ defmodule Lightning.JobsTest do
workflow: job.workflow
})

edge =
insert(:edge, %{
workflow: job.workflow,
source_trigger: trigger,
target_job: job
})
insert(:edge, %{
workflow: job.workflow,
source_trigger: trigger,
target_job: job
})

dataclip = insert(:dataclip)

attempt =
insert(:attempt,
workorder:
work_order:
build(:workorder,
workflow: job.workflow,
dataclip: dataclip,
Expand All @@ -359,33 +360,29 @@ defmodule Lightning.JobsTest do
exit_code: 0,
job: job,
input_dataclip: dataclip,
output_dataclip: build(:dataclip, body: %{"changed" => 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
15 changes: 13 additions & 2 deletions test/lightning_web/plugs/webhook_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f445307

Please sign in to comment.