memEx/test/memex/steps_test.exs

149 lines
5.2 KiB
Elixir
Raw Normal View History

2022-07-25 20:18:39 -04:00
defmodule Memex.StepsTest do
use Memex.DataCase
2023-03-22 22:08:37 -04:00
import Memex.Fixtures
2022-11-26 21:26:21 -05:00
alias Memex.Pipelines.{Steps, Steps.Step}
2022-11-24 15:35:29 -05:00
@moduletag :steps_test
@invalid_attrs %{content: nil, title: nil}
2022-07-25 20:18:39 -04:00
describe "steps" do
2022-11-24 15:35:29 -05:00
setup do
user = user_fixture()
pipeline = pipeline_fixture(user)
2022-07-25 20:18:39 -04:00
2022-11-24 15:35:29 -05:00
[user: user, pipeline: pipeline]
end
2022-07-25 20:18:39 -04:00
2022-11-26 21:26:21 -05:00
test "list_steps/2 returns all steps for a user", %{pipeline: pipeline, user: user} do
2022-11-24 15:35:29 -05:00
step_a = step_fixture(0, pipeline, user)
step_b = step_fixture(1, pipeline, user)
step_c = step_fixture(2, pipeline, user)
assert Steps.list_steps(pipeline, user) == [step_a, step_b, step_c]
end
2022-07-25 20:18:39 -04:00
2022-11-26 21:26:21 -05:00
test "get_step!/2 returns the step with given id", %{pipeline: pipeline, user: user} do
2022-11-24 15:35:29 -05:00
step = step_fixture(0, pipeline, user)
assert Steps.get_step!(step.id, user) == step
2022-07-25 20:18:39 -04:00
end
2022-11-26 21:26:21 -05:00
test "get_step!/2 only returns unlisted or public steps for other users", %{user: user} do
2022-11-24 15:35:29 -05:00
another_user = user_fixture()
another_pipeline = pipeline_fixture(another_user)
step = step_fixture(0, another_pipeline, another_user)
assert_raise Ecto.NoResultsError, fn ->
Steps.get_step!(step.id, user)
end
2022-07-25 20:18:39 -04:00
end
2022-11-26 21:26:21 -05:00
test "create_step/4 with valid data creates a step", %{pipeline: pipeline, user: user} do
2022-11-24 15:35:29 -05:00
valid_attrs = %{
2023-03-22 22:08:37 -04:00
content: "some content",
title: "some title"
2022-11-24 15:35:29 -05:00
}
2022-07-25 20:18:39 -04:00
2022-11-24 15:35:29 -05:00
assert {:ok, %Step{} = step} = Steps.create_step(valid_attrs, 0, pipeline, user)
assert step.content == "some content"
2022-07-25 20:18:39 -04:00
assert step.title == "some title"
end
2022-11-26 21:26:21 -05:00
test "create_step/4 with invalid data returns error changeset",
2022-11-24 15:35:29 -05:00
%{pipeline: pipeline, user: user} do
assert {:error, %Ecto.Changeset{}} = Steps.create_step(@invalid_attrs, 0, pipeline, user)
2022-07-25 20:18:39 -04:00
end
2022-11-26 21:26:21 -05:00
test "update_step/3 with valid data updates the step", %{pipeline: pipeline, user: user} do
2022-11-24 15:35:29 -05:00
step = step_fixture(0, pipeline, user)
2022-07-25 22:05:54 -04:00
update_attrs = %{
2023-03-22 22:08:37 -04:00
content: "some updated content",
title: "some updated title"
2022-07-25 22:05:54 -04:00
}
2022-07-25 20:18:39 -04:00
2022-11-26 21:26:21 -05:00
assert {:ok, %Step{} = step} = Steps.update_step(step, update_attrs, user)
2022-11-24 15:35:29 -05:00
assert step.content == "some updated content"
2022-07-25 20:18:39 -04:00
assert step.title == "some updated title"
end
2022-11-26 21:26:21 -05:00
test "update_step/3 with invalid data returns error changeset", %{
2022-11-24 15:35:29 -05:00
pipeline: pipeline,
user: user
} do
step = step_fixture(0, pipeline, user)
2022-11-26 21:26:21 -05:00
assert {:error, %Ecto.Changeset{}} = Steps.update_step(step, @invalid_attrs, user)
2022-11-24 15:35:29 -05:00
assert step == Steps.get_step!(step.id, user)
end
2022-11-26 21:26:21 -05:00
test "delete_step/2 deletes the step", %{pipeline: pipeline, user: user} do
2022-11-24 15:35:29 -05:00
step = step_fixture(0, pipeline, user)
assert {:ok, %Step{}} = Steps.delete_step(step, user)
assert_raise Ecto.NoResultsError, fn -> Steps.get_step!(step.id, user) end
2022-07-25 20:18:39 -04:00
end
2022-11-26 21:26:21 -05:00
test "delete_step/2 moves past steps up", %{pipeline: pipeline, user: user} do
first_step = step_fixture(0, pipeline, user)
second_step = step_fixture(1, pipeline, user)
assert {:ok, %Step{}} = Steps.delete_step(first_step, user)
assert %{position: 0} = second_step |> Repo.reload!()
end
test "delete_step/2 deletes the step for an admin user", %{pipeline: pipeline, user: user} do
2022-11-24 15:35:29 -05:00
admin_user = admin_fixture()
step = step_fixture(0, pipeline, user)
assert {:ok, %Step{}} = Steps.delete_step(step, admin_user)
assert_raise Ecto.NoResultsError, fn -> Steps.get_step!(step.id, user) end
2022-07-25 20:18:39 -04:00
end
2022-11-26 21:26:21 -05:00
test "change_step/2 returns a step changeset", %{pipeline: pipeline, user: user} do
step = step_fixture(0, pipeline, user)
assert %Ecto.Changeset{} = Steps.change_step(step, user)
end
2022-11-24 15:35:29 -05:00
test "change_step/1 returns a step changeset", %{pipeline: pipeline, user: user} do
step = step_fixture(0, pipeline, user)
2022-11-26 21:26:21 -05:00
assert %Ecto.Changeset{} = Steps.change_step(step, user)
end
test "reorder_step/1 reorders steps properly", %{pipeline: pipeline, user: user} do
[
%{id: first_step_id} = first_step,
%{id: second_step_id} = second_step,
%{id: third_step_id} = third_step
] = Enum.map(0..2, fn index -> step_fixture(index, pipeline, user) end)
Steps.reorder_step(third_step, :up, user)
assert [
%{id: ^first_step_id, position: 0},
%{id: ^third_step_id, position: 1},
%{id: ^second_step_id, position: 2}
] = Steps.list_steps(pipeline, user)
Steps.reorder_step(first_step, :up, user)
assert [
%{id: ^first_step_id, position: 0},
%{id: ^third_step_id, position: 1},
%{id: ^second_step_id, position: 2}
] = Steps.list_steps(pipeline, user)
second_step
|> Repo.reload!()
|> Steps.reorder_step(:down, user)
assert [
%{id: ^first_step_id, position: 0},
%{id: ^third_step_id, position: 1},
%{id: ^second_step_id, position: 2}
] = Steps.list_steps(pipeline, user)
Steps.reorder_step(first_step, :down, user)
assert [
%{id: ^third_step_id, position: 0},
%{id: ^first_step_id, position: 1},
%{id: ^second_step_id, position: 2}
] = Steps.list_steps(pipeline, user)
2022-07-25 20:18:39 -04:00
end
end
end