Skip to content

Commit

Permalink
Use partial matches for args/meta fields
Browse files Browse the repository at this point in the history
It's now possible to match a subset of fields on args or meta with
`all_enqueued`, `assert_enqueued`, and `refute_enqueued`.
  • Loading branch information
sorentwo committed Sep 12, 2023
1 parent ac8411f commit b2ebd71
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
31 changes: 18 additions & 13 deletions lib/oban/testing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,20 @@ defmodule Oban.Testing do
The behaviour can be exercised in your test code:
defmodule MyApp.BusinessTest do
use ExUnit.Case, async: true
use Oban.Testing, repo: MyApp.Repo
```elixir
defmodule MyApp.BusinessTest do
use ExUnit.Case, async: true
use Oban.Testing, repo: MyApp.Repo
alias MyApp.Business
alias MyApp.Business
test "jobs are enqueued with provided arguments" do
Business.work(%{id: 1, message: "Hello!"})
test "jobs are enqueued with provided arguments" do
Business.work(%{id: 1, message: "Hello!"})
assert_enqueued worker: MyApp.Worker, args: %{id: 1, message: "Hello!"}
end
end
assert_enqueued worker: MyApp.Worker, args: %{id: 1, message: "Hello!"}
end
end
```
## Matching Scheduled Jobs and Timestamps
Expand Down Expand Up @@ -113,6 +115,7 @@ defmodule Oban.Testing do
end
```
"""

@moduledoc since: "0.3.0"

import ExUnit.Assertions, only: [assert: 2, refute: 2, flunk: 1]
Expand All @@ -128,11 +131,11 @@ defmodule Oban.Testing do
| {:prefix, binary()}
| {:repo, module()}

@conf_keys ~w(log prefix repo)a
@json_fields ~w(args meta)a
@timestamp_fields ~w(attempted_at cancelled_at completed_at discarded_at inserted_at scheduled_at)a
@wait_interval 10

@conf_keys ~w(log prefix repo)a

@doc false
defmacro __using__(repo_opts) do
_repo = Keyword.fetch!(repo_opts, :repo)
Expand Down Expand Up @@ -578,12 +581,14 @@ defmodule Oban.Testing do
Enum.reduce(opts, query, &apply_where/2)
end

defp apply_where({:args, args}, query), do: where(query, args: ^json_encode_decode(args))
defp apply_where({:meta, meta}, query), do: where(query, meta: ^json_encode_decode(meta))
defp apply_where({:queue, queue}, query), do: where(query, queue: ^to_string(queue))
defp apply_where({:state, state}, query), do: where(query, state: ^to_string(state))
defp apply_where({:worker, worker}, query), do: where(query, worker: ^Worker.to_string(worker))

defp apply_where({key, val}, query) when key in @json_fields do
where(query, [j], fragment("? @> ?", field(j, ^key), ^val))
end

defp apply_where({key, val}, query) when key in @timestamp_fields do
{time, delta} =
case val do
Expand Down
13 changes: 7 additions & 6 deletions test/oban/testing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ defmodule Oban.TestingTest do

describe "assert_enqueued/1" do
test "checking for jobs with matching properties" do
insert!(%{id: 1}, worker: Ping, queue: :alpha)
insert!(%{id: 2}, worker: Pong, queue: :gamma)
insert!(%{id: 1, xd: 1}, worker: Ping, queue: :alpha)
insert!(%{id: 2, xd: 2}, worker: Pong, queue: :gamma)
insert!(%{message: "hello"}, worker: Pong, queue: :gamma)

assert_enqueued worker: Ping
Expand All @@ -248,6 +248,7 @@ defmodule Oban.TestingTest do
assert_enqueued worker: "Pong", queue: "gamma"
assert_enqueued worker: "Pong", queue: "gamma", args: %{message: "hello"}
assert_enqueued args: %{id: 1}
assert_enqueued args: %{id: 1, xd: 1}
assert_enqueued args: %{message: "hello"}
assert_enqueued worker: Ping, prefix: "public"
end
Expand Down Expand Up @@ -298,10 +299,10 @@ defmodule Oban.TestingTest do

describe "refute_enqueued/1" do
test "refuting jobs with specific properties have been enqueued" do
insert!(%{id: 1}, worker: Ping, queue: :alpha)
insert!(%{id: 2}, worker: Pong, queue: :gamma)
insert!(%{id: 3}, worker: Pong, queue: :gamma, state: "completed")
insert!(%{id: 4}, worker: Pong, queue: :gamma, state: "discarded")
insert!(%{id: 1, xd: 1}, worker: Ping, queue: :alpha)
insert!(%{id: 2, xd: 2}, worker: Pong, queue: :gamma)
insert!(%{id: 3, xd: 3}, worker: Pong, queue: :gamma, state: "completed")
insert!(%{id: 4, xd: 4}, worker: Pong, queue: :gamma, state: "discarded")
insert!(%{message: "hello"}, worker: Pong, queue: :gamma)

refute_enqueued worker: Pongo
Expand Down

0 comments on commit b2ebd71

Please sign in to comment.