View Source Pacer.Workflow.Testing (Pacer v0.1.3)
This module provides testing helpers for dependencies and fields for Pacer.Workflow.
How to use in tests
use
this module in tests:
use Pacer.Workflow.Testing
This will allow you to use the helper assertions in your test module.
Examples:
defmodule SomeWorkflowTest do
use Pacer.Workflow.Testing
defmodule TestGraph do
use Pacer.Workflow
graph do
field(:field_a)
field(:field_with_default,
virtual?: true,
default: "this is a default value"
)
batch :http_requests do
field(:request_1,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a],
default: 2
)
field(:request_2,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a],
default: "this is a default value for request2"
)
end
end
def do_work(_), do: :ok
end
describe "testing graph dependencies and fields" do
test "assert dependency of :field_a has no dependencies" do
assert_dependencies(TestGraph, :field_a, [])
end
test "assert batch dependency of :request_2" do
assert_batch_dependencies(TestGraph, :request_2, [:field_a])
end
test "provided fields match fields from workflow" do
assert_fields(TestGraph, [
:field_a,
:field_with_default,
:request_1,
:request_2
])
end
end
end
Summary
Functions
Test helper to assert on batch field dependencies. First argument is your workflow, second is the batch field as an atom that you want to test, third are the dependencies you are passing in to be dependencies for the field passed in as the second argument. Note, the third argument should be a list of atoms or an empty list in the case of no dependencies.
Test helper to assert on non-batch field dependencies. First argument is your workflow, second is the field as an atom that you want to test, third are the dependencies you are passing in to be dependencies for the field passed in as the second argument. Note, the third argument should be a list of atoms or an empty list in the case of no dependencies.
Test helper to assert on the field defined in the workflow. First argument is your workflow, second is a list of atoms that are fields in the workflow. Ordering does not matter in the list of atoms since we use MapSets to do the comparison.
Functions
Test helper to assert on batch field dependencies. First argument is your workflow, second is the batch field as an atom that you want to test, third are the dependencies you are passing in to be dependencies for the field passed in as the second argument. Note, the third argument should be a list of atoms or an empty list in the case of no dependencies.
Example
If we have a workflow defined as:
defmodule TestGraph do
use Pacer.Workflow
graph do
field(:field_a)
batch :http_requests do
field(:request_1,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a],
default: 2
)
field(:request_2,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a],
default: "this is a default value for request2"
)
end
end
def do_work(_), do: :ok
end
We can test the dependencies of both fields:
test "assert batch fields have dependencies on :field_a" do
assert_batch_dependencies(TestGraph, :request_1, [:field_a])
assert_batch_dependencies(TestGraph, :request_2, [:field_a])
end
Test helper to assert on non-batch field dependencies. First argument is your workflow, second is the field as an atom that you want to test, third are the dependencies you are passing in to be dependencies for the field passed in as the second argument. Note, the third argument should be a list of atoms or an empty list in the case of no dependencies.
Example
If we have a workflow defined as:
defmodule TestGraph do
use Pacer.Workflow
graph do
field(:field_a)
field(:field_b,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a]
)
end
end
We can test the dependencies of both fields:
test "assert :field_a has no dependencies" do
assert_dependencies(TestGraph, :field_a, [])
end
test "assert dependencies of :field_b" do
assert_dependencies(TestGraph, :field_b, [:field_a])
end
Test helper to assert on the field defined in the workflow. First argument is your workflow, second is a list of atoms that are fields in the workflow. Ordering does not matter in the list of atoms since we use MapSets to do the comparison.
Example
If we have a workflow defined as:
defmodule TestGraph do
use Pacer.Workflow
graph do
field(:field_a)
field(:field_b,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a]
)
field(:field_with_default,
virtual?: true,
default: "this is a default value"
)
batch :http_requests do
field(:request_1,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a],
default: 2
)
field(:request_2,
resolver: &__MODULE__.do_work/1,
dependencies: [:field_a, :field_b],
default: "this is a default value for request2"
)
end
end
def do_work(_), do: :ok
end
We can test the fields:
test "fields in the workflow" do
assert_fields(TestGraph, [:field_a, :field_b, :request_2, :request_1])
end