From 8b5806116af760603d43c53ae95e4eb254578526 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 25 Jul 2022 20:18:39 -0400 Subject: [PATCH] add steps --- lib/memex/steps.ex | 104 ++++++++++++++++++ lib/memex/steps/step.ex | 22 ++++ .../20220726001809_create_steps.exs | 17 +++ test/memex/steps_test.exs | 63 +++++++++++ test/support/fixtures/steps_fixtures.ex | 22 ++++ 5 files changed, 228 insertions(+) create mode 100644 lib/memex/steps.ex create mode 100644 lib/memex/steps/step.ex create mode 100644 priv/repo/migrations/20220726001809_create_steps.exs create mode 100644 test/memex/steps_test.exs create mode 100644 test/support/fixtures/steps_fixtures.ex diff --git a/lib/memex/steps.ex b/lib/memex/steps.ex new file mode 100644 index 0000000..92dc697 --- /dev/null +++ b/lib/memex/steps.ex @@ -0,0 +1,104 @@ +defmodule Memex.Steps do + @moduledoc """ + The Steps context. + """ + + import Ecto.Query, warn: false + alias Memex.Repo + + alias Memex.Steps.Step + + @doc """ + Returns the list of steps. + + ## Examples + + iex> list_steps() + [%Step{}, ...] + + """ + def list_steps do + Repo.all(Step) + end + + @doc """ + Gets a single step. + + Raises `Ecto.NoResultsError` if the Step does not exist. + + ## Examples + + iex> get_step!(123) + %Step{} + + iex> get_step!(456) + ** (Ecto.NoResultsError) + + """ + def get_step!(id), do: Repo.get!(Step, id) + + @doc """ + Creates a step. + + ## Examples + + iex> create_step(%{field: value}) + {:ok, %Step{}} + + iex> create_step(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_step(attrs \\ %{}) do + %Step{} + |> Step.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a step. + + ## Examples + + iex> update_step(step, %{field: new_value}) + {:ok, %Step{}} + + iex> update_step(step, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_step(%Step{} = step, attrs) do + step + |> Step.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a step. + + ## Examples + + iex> delete_step(step) + {:ok, %Step{}} + + iex> delete_step(step) + {:error, %Ecto.Changeset{}} + + """ + def delete_step(%Step{} = step) do + Repo.delete(step) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking step changes. + + ## Examples + + iex> change_step(step) + %Ecto.Changeset{data: %Step{}} + + """ + def change_step(%Step{} = step, attrs \\ %{}) do + Step.changeset(step, attrs) + end +end diff --git a/lib/memex/steps/step.ex b/lib/memex/steps/step.ex new file mode 100644 index 0000000..4edae5a --- /dev/null +++ b/lib/memex/steps/step.ex @@ -0,0 +1,22 @@ +defmodule Memex.Steps.Step do + use Ecto.Schema + import Ecto.Changeset + + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id + schema "steps" do + field :description, :string + field :position, :integer + field :title, :string + field :pipeline_id, :binary_id + + timestamps() + end + + @doc false + def changeset(step, attrs) do + step + |> cast(attrs, [:title, :description, :position]) + |> validate_required([:title, :description, :position]) + end +end diff --git a/priv/repo/migrations/20220726001809_create_steps.exs b/priv/repo/migrations/20220726001809_create_steps.exs new file mode 100644 index 0000000..97c486e --- /dev/null +++ b/priv/repo/migrations/20220726001809_create_steps.exs @@ -0,0 +1,17 @@ +defmodule Memex.Repo.Migrations.CreateSteps do + use Ecto.Migration + + def change do + create table(:steps, primary_key: false) do + add :id, :binary_id, primary_key: true + add :title, :string + add :description, :text + add :position, :integer + add :pipeline_id, references(:pipelines, on_delete: :nothing, type: :binary_id) + + timestamps() + end + + create index(:steps, [:pipeline_id]) + end +end diff --git a/test/memex/steps_test.exs b/test/memex/steps_test.exs new file mode 100644 index 0000000..cc7318b --- /dev/null +++ b/test/memex/steps_test.exs @@ -0,0 +1,63 @@ +defmodule Memex.StepsTest do + use Memex.DataCase + + alias Memex.Steps + + describe "steps" do + alias Memex.Steps.Step + + import Memex.StepsFixtures + + @invalid_attrs %{description: nil, position: nil, title: nil} + + test "list_steps/0 returns all steps" do + step = step_fixture() + assert Steps.list_steps() == [step] + end + + test "get_step!/1 returns the step with given id" do + step = step_fixture() + assert Steps.get_step!(step.id) == step + end + + test "create_step/1 with valid data creates a step" do + valid_attrs = %{description: "some description", position: 42, title: "some title"} + + assert {:ok, %Step{} = step} = Steps.create_step(valid_attrs) + assert step.description == "some description" + assert step.position == 42 + assert step.title == "some title" + end + + test "create_step/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Steps.create_step(@invalid_attrs) + end + + test "update_step/2 with valid data updates the step" do + step = step_fixture() + update_attrs = %{description: "some updated description", position: 43, title: "some updated title"} + + assert {:ok, %Step{} = step} = Steps.update_step(step, update_attrs) + assert step.description == "some updated description" + assert step.position == 43 + assert step.title == "some updated title" + end + + test "update_step/2 with invalid data returns error changeset" do + step = step_fixture() + assert {:error, %Ecto.Changeset{}} = Steps.update_step(step, @invalid_attrs) + assert step == Steps.get_step!(step.id) + end + + test "delete_step/1 deletes the step" do + step = step_fixture() + assert {:ok, %Step{}} = Steps.delete_step(step) + assert_raise Ecto.NoResultsError, fn -> Steps.get_step!(step.id) end + end + + test "change_step/1 returns a step changeset" do + step = step_fixture() + assert %Ecto.Changeset{} = Steps.change_step(step) + end + end +end diff --git a/test/support/fixtures/steps_fixtures.ex b/test/support/fixtures/steps_fixtures.ex new file mode 100644 index 0000000..1e69394 --- /dev/null +++ b/test/support/fixtures/steps_fixtures.ex @@ -0,0 +1,22 @@ +defmodule Memex.StepsFixtures do + @moduledoc """ + This module defines test helpers for creating + entities via the `Memex.Steps` context. + """ + + @doc """ + Generate a step. + """ + def step_fixture(attrs \\ %{}) do + {:ok, step} = + attrs + |> Enum.into(%{ + description: "some description", + position: 42, + title: "some title" + }) + |> Memex.Steps.create_step() + + step + end +end