Skip to content

Commit

Permalink
Put default timestamps directly in changeset
Browse files Browse the repository at this point in the history
Workers that override `new/2` and don't pass options through would end
up without necessary timestamps, causing a `CaseClauseError` during
execution when timestamps couldn't be compared.
  • Loading branch information
sorentwo committed Nov 14, 2022
1 parent 4447f4f commit 13b5f10
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
21 changes: 15 additions & 6 deletions lib/oban/testing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,18 @@ defmodule Oban.Testing do

{conf_opts, opts} = Keyword.split(opts, [:log, :prefix, :repo])

opts =
opts
|> Keyword.put_new(:attempt, 1)
|> Keyword.put_new(:attempted_at, DateTime.utc_now())
|> Keyword.put_new(:scheduled_at, DateTime.utc_now())
|> Keyword.put_new(:inserted_at, DateTime.utc_now())
utc_now = DateTime.utc_now()

changeset =
args
|> worker.new(opts)
|> Changeset.put_change(:id, System.unique_integer([:positive]))
|> Changeset.update_change(:args, &json_encode_decode/1)
|> Changeset.update_change(:meta, &json_encode_decode/1)
|> put_new_change(:attempt, 1)
|> put_new_change(:attempted_at, utc_now)
|> put_new_change(:scheduled_at, utc_now)
|> put_new_change(:inserted_at, utc_now)

assert_valid_changeset(changeset)

Expand Down Expand Up @@ -414,6 +413,16 @@ defmodule Oban.Testing do

# Perform Helpers

defp put_new_change(changeset, key, value) do
case Changeset.fetch_change(changeset, key) do
{:ok, _} ->
changeset

:error ->
Changeset.put_change(changeset, key, value)
end
end

defp assert_valid_worker(worker) do
assert Code.ensure_loaded?(worker) and implements_worker?(worker), """
Expected worker to be a module that implements the Oban.Worker behaviour, got:
Expand Down
4 changes: 2 additions & 2 deletions test/oban/testing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ defmodule Oban.TestingTest do
use Oban.Worker

@impl Worker
def new({key, val}, opts) do
super(%{key => val}, opts)
def new({key, val}, _) do
super(%{key => val}, [])
end

@impl Worker
Expand Down

0 comments on commit 13b5f10

Please sign in to comment.