Skip to content

Commit

Permalink
Merge pull request #24 from annatel/extract_jobs_created
Browse files Browse the repository at this point in the history
Extract jobs_created function as a test helper
  • Loading branch information
lohayon authored Nov 10, 2022
2 parents 950acf0 + 765e2d1 commit 629e905
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
41 changes: 28 additions & 13 deletions lib/queuetopia/test/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ defmodule Queuetopia.Test.Assertions do
alias Queuetopia.Queue
alias Queuetopia.Queue.Job

@doc """
Find jobs that have just been created
It can be used as below:
# Examples
MyQueuetopia.create_job("mailer_queue", "deliver_mail", %{body: "hello", from: "from", to: "to"})
assert [job] = jobs_created(MyQueuetopia)
"""
def jobs_created(queuetopia, %{} = job_attrs \\ %{}) do
job_attrs = full_job_attrs(queuetopia, job_attrs)

job_params = Map.get(job_attrs, :params, %{})

Queue.list_jobs(queuetopia.repo(),
filters: job_attrs |> Map.take(filterable_fields()) |> Enum.to_list()
)
|> Enum.filter(fn job -> subset?(job_params, job.params) end)
|> mapify()
end

defp full_job_attrs(queuetopia, job_attrs),
do: job_attrs |> Map.put(:scope, to_string(queuetopia)) |> mapify()

@doc """
Asserts the job has juste been created
Expand All @@ -30,20 +55,10 @@ defmodule Queuetopia.Test.Assertions do

def assert_job_created(queuetopia, expected_count, %{} = job_attrs)
when is_integer(expected_count) do
job_attrs = job_attrs |> Map.put(:scope, to_string(queuetopia)) |> mapify()

job_params = Map.get(job_attrs, :params, %{})

jobs =
Queue.list_jobs(queuetopia.repo(),
filters: job_attrs |> Map.take(filterable_fields()) |> Enum.to_list()
)
|> Enum.filter(fn job -> subset?(job_params, job.params) end)
|> mapify()

count = jobs |> length()
count = jobs_created(queuetopia, job_attrs) |> length()

assert count == expected_count, message("job", job_attrs, expected_count, count)
assert count == expected_count,
message("job", full_job_attrs(queuetopia, job_attrs), expected_count, count)
end

defp filterable_fields(), do: [:id, :scope, :queue, :action]
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Queuetopia.MixProject do
use Mix.Project

@source_url "https://github.com/annatel/queuetopia"
@version "2.4.1"
@version "2.4.2"

def project do
[
Expand Down
17 changes: 17 additions & 0 deletions test/queuetopia/test/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ defmodule Queuetopia.Test.AssertionsTest do

import Queuetopia.Test.Assertions

describe "jobs_created/1" do
test "when the job is found" do
Factory.insert!(:job, scope: Queuetopia.TestQueuetopia.scope())
assert [_] = jobs_created(Queuetopia.TestQueuetopia)
end

test "multiple jobs" do
Factory.insert!(:job, scope: Queuetopia.TestQueuetopia.scope())
Factory.insert!(:job, scope: Queuetopia.TestQueuetopia.scope())
assert [_, _] = jobs_created(Queuetopia.TestQueuetopia)
end

test "when no job is found" do
assert [] = jobs_created(Queuetopia.TestQueuetopia)
end
end

describe "assert_job_created/1" do
test "when the job is found" do
Factory.insert!(:job, scope: Queuetopia.TestQueuetopia.scope())
Expand Down

0 comments on commit 629e905

Please sign in to comment.