Skip to content

Commit

Permalink
Merge pull request #8 from annatel/refacto
Browse files Browse the repository at this point in the history
Refacto
  • Loading branch information
Laetitia Ohayon authored Dec 12, 2020
2 parents 972ebd6 + 5be48ad commit 995306f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The package can be installed by adding `queuetopia` to your list of dependencies
```elixir
def deps do
[
{:queuetopia, "~> 1.0.0"}
{:queuetopia, "~> 1.1.0"}
]
end
```
Expand Down
28 changes: 25 additions & 3 deletions lib/queuetopia/test/assertions.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
defmodule Queuetopia.Test.Assertions do
import ExUnit.Assertions
require Ecto.Query
alias Queuetopia.Jobs.Job

@doc """
Asserts the job has juste been created
"""

def assert_job_created(queue, %{} = params) do
repo = queue.repo()
def assert_job_created(queuetopia) when is_atom(queuetopia) do
repo = queuetopia.repo()

job = Job |> Ecto.Query.last() |> repo.one()
refute is_nil(job)
job
end

def assert_job_created(queuetopia, queue) when is_atom(queuetopia) and is_binary(queue) do
repo = queuetopia.repo()

job =
Job
|> Ecto.Query.where([job], job.queue == ^queue)
|> Ecto.Query.last()
|> repo.one()

refute is_nil(job) && assert(^params = job)
refute is_nil(job)
job
end

def assert_job_created(queuetopia, %{} = params) when is_atom(queuetopia) do
job = assert_job_created(queuetopia)
assert ^params = job
end

def assert_job_created(queuetopia, queue, %{} = params)
when is_atom(queuetopia) and is_binary(queue) do
job = assert_job_created(queuetopia, queue)
assert ^params = job
end
end
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 "1.0.0"
@version "1.1.0"

def project do
[
Expand Down
74 changes: 66 additions & 8 deletions test/queuetopia/test/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,82 @@ defmodule Queuetopia.Test.AssertionsTest do
use Queuetopia.DataCase
import Queuetopia.Test.Assertions

describe "assert_job_created/0" do
describe "assert_job_created/1" do
test "when the job has just been created" do
scope = Queuetopia.TestQueuetopia.scope()
queue = [scope] |> Module.safe_concat()
queuetopia = Module.safe_concat([scope])

job = Factory.insert(:job, scope: scope)
Factory.insert(:job, scope: scope)

assert_job_created(queue, Map.from_struct(job))
assert_job_created(queuetopia)
end

test "when the job has not been created" do
assert_raise ExUnit.AssertionError, fn ->
scope = Queuetopia.TestQueuetopia.scope()
queue = [scope] |> Module.safe_concat()
job = Factory.params_for(:job, scope: Queuetopia.TestQueuetopia.scope())
assert_job_created(Queuetopia.TestQueuetopia, job)
end
end
end

describe "assert_job_created/2 for a specific queue" do
test "when the job has just been created" do
job = Factory.insert(:job, scope: Queuetopia.TestQueuetopia.scope())

assert_job_created(Queuetopia.TestQueuetopia, job.queue)
end

test "when the job has not been created for the queue" do
assert_raise ExUnit.AssertionError, fn ->
Factory.insert(:job, scope: Queuetopia.TestQueuetopia.scope())

assert_job_created(Queuetopia.TestQueuetopia, "sample queue")
end
end

test "when the job has not been created at all" do
assert_raise ExUnit.AssertionError, fn ->
assert_job_created(Queuetopia.TestQueuetopia, "sample_queue")
end
end
end

describe "assert_job_created/2 for a specific job" do
test "when the job has just been created" do
job = Factory.insert(:job, scope: Queuetopia.TestQueuetopia.scope())

job_params = Factory.params_for(:job)
assert_job_created(Queuetopia.TestQueuetopia, job)
end

test "when the job has not been created" do
assert_raise ExUnit.AssertionError, fn ->
job = Factory.params_for(:job, scope: Queuetopia.TestQueuetopia.scope())

assert_job_created(Queuetopia.TestQueuetopia, job)
end
end
end

describe "assert_job_created/2 for a specific job and a specific queue" do
test "when the job has just been created" do
job = Factory.insert(:job, scope: Queuetopia.TestQueuetopia.scope())

assert_job_created(Queuetopia.TestQueuetopia, job.queue, job)
end

test "when the job has not been created for the queue" do
assert_raise ExUnit.AssertionError, fn ->
job = Factory.insert(:job, scope: Queuetopia.TestQueuetopia.scope())

assert_job_created(Queuetopia.TestQueuetopia, "sample queue", job)
end
end

test "when the job has not been created at all" do
assert_raise ExUnit.AssertionError, fn ->
job = Factory.params_for(:job, scope: Queuetopia.TestQueuetopia.scope())

assert_job_created(queue, job_params)
assert_job_created(Queuetopia.TestQueuetopia, "sample_queue", job)
end
end
end
Expand Down

0 comments on commit 995306f

Please sign in to comment.