From d060bcb06446b6631e8e0aade19a4bd8847079aa Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 12 Sep 2018 17:53:40 +0100 Subject: [PATCH] adds insert function to append only, #1 --- lib/append/address.ex | 17 ++++++++++------- lib/append/append_only_log.ex | 6 ++++++ test/append/address_test.exs | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 test/append/address_test.exs diff --git a/lib/append/address.ex b/lib/append/address.ex index f6eae99..2d994e4 100644 --- a/lib/append/address.ex +++ b/lib/append/address.ex @@ -2,18 +2,21 @@ defmodule Append.Address do use Ecto.Schema import Ecto.Changeset - schema "addresses" do - field :address_line_1, :string - field :address_line_2, :string - field :city, :string - field :name, :string - field :postcode, :string - field :tel, :string + field(:address_line_1, :string) + field(:address_line_2, :string) + field(:city, :string) + field(:name, :string) + field(:postcode, :string) + field(:tel, :string) timestamps() end + # Because we are referencing the Address struct in our Append Only behaviour/macro + # it needs to be used after we have defined the struct using 'schema' + use Append.AppendOnlyLog + @doc false def changeset(address, attrs) do address diff --git a/lib/append/append_only_log.ex b/lib/append/append_only_log.ex index 2a22bda..16617b0 100644 --- a/lib/append/append_only_log.ex +++ b/lib/append/append_only_log.ex @@ -1,4 +1,6 @@ defmodule Append.AppendOnlyLog do + alias Append.Repo + @moduledoc """ Behaviour that defines functions for accessing and inserting data in an Append-Only database @@ -14,6 +16,9 @@ defmodule Append.AppendOnlyLog do @behaviour Append.AppendOnlyLog def insert(attrs) do + %__MODULE__{} + |> __MODULE__.changeset(attrs) + |> Repo.insert() end def get(id) do @@ -22,6 +27,7 @@ defmodule Append.AppendOnlyLog do def update(%__MODULE__{} = item, attrs) do end + defoverridable Append.AppendOnlyLog end end end diff --git a/test/append/address_test.exs b/test/append/address_test.exs new file mode 100644 index 0000000..f27d240 --- /dev/null +++ b/test/append/address_test.exs @@ -0,0 +1,17 @@ +defmodule Append.AddressTest do + use Append.DataCase + alias Append.Address + + test "add item to database" do + assert {:ok, item} = Address.insert(%{ + name: "Thor", + address_line_1: "The Hall", + address_line_2: "Valhalla", + city: "Asgard", + postcode: "AS1 3DG", + tel: "0800123123" + }) + + assert item.name == "Thor" + end +end