Skip to content

Commit

Permalink
improvement: add test helper for running triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Aug 4, 2023
1 parent b5ef503 commit 6f9e6ff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/ash_oban.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ defmodule AshOban do
AshOban.Transformers.DefineSchedulers
]

def schedule(resource, trigger) do
trigger =
case trigger do
%AshOban.Trigger{} ->
trigger

name when is_atom(name) ->
AshOban.Info.oban_trigger(resource, name)
end

%{}
|> trigger.scheduler.new()
|> Oban.insert!()
end

def run_trigger(%resource{} = record, trigger) do
trigger =
case trigger do
Expand Down
23 changes: 23 additions & 0 deletions lib/test.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule AshOban.Test do
@moduledoc "Helpers for testing ash_oban triggers"

def schedule_and_run_triggers(resource) do
triggers =
AshOban.Info.oban_triggers(resource)

Enum.each(triggers, fn trigger ->
AshOban.schedule(resource, trigger)
end)

triggers
|> Enum.map(& &1.queue)
|> Enum.uniq()
|> Enum.reduce(%{}, fn queue, acc ->
queue
|> Oban.drain_queue()
|> Map.merge(acc, fn _key, left, right ->
left + right
end)
end)
end
end

0 comments on commit 6f9e6ff

Please sign in to comment.