Skip to content

Commit

Permalink
add code snapshot at end of dwyl/phoenix-ecto-append-only-log-example…
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 26, 2019
1 parent 5f6d125 commit 874538a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 66 deletions.
4 changes: 4 additions & 0 deletions lib/append/accounts/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ defmodule Append.Accounts.Address do
use Ecto.Schema
import Ecto.Changeset

use Append.AppendOnlyLog #include the functions from this module's '__using__' macro.


@timestamps_opts [type: :naive_datetime_usec]
schema "addresses" do
field :address_line_1, :string
field :address_line_2, :string
Expand Down
7 changes: 7 additions & 0 deletions priv/repo/migrations/20190425093946_create_addresses.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
defmodule Append.Repo.Migrations.CreateAddresses do
use Ecto.Migration

# Get name of our Ecto Repo module from our config
@repo :append |> Application.get_env(:ecto_repos) |> List.first()
# Get username of Ecto Repo from our config
@db_user Application.get_env(:append, @repo)[:username]


def change do
create table(:addresses) do
add :name, :string
Expand All @@ -13,5 +19,6 @@ defmodule Append.Repo.Migrations.CreateAddresses do
timestamps()
end

execute("REVOKE UPDATE, DELETE ON TABLE addresses FROM #{@db_user}")
end
end
132 changes: 66 additions & 66 deletions test/append/address_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Append.AddressTest do
use Append.DataCase
alias Append.Address
alias Append.Accounts.Address

test "add item to database" do
assert {:ok, item} = Address.insert(%{
Expand All @@ -15,90 +15,90 @@ defmodule Append.AddressTest do
assert item.name == "Thor"
end

describe "get items from database" do
test "get/1" do
{:ok, item} = insert_address()
# describe "get items from database" do
# test "get/1" do
# {:ok, item} = insert_address()

assert Address.get(item.entry_id) == item
end
# assert Address.get(item.entry_id) == item
# end

test "all/0" do
{:ok, _item} = insert_address()
{:ok, _item_2} = insert_address("Loki")
# test "all/0" do
# {:ok, _item} = insert_address()
# {:ok, _item_2} = insert_address("Loki")

assert length(Address.all()) == 2
end
end
# assert length(Address.all()) == 2
# end
# end

def insert_address(name \\ "Thor") do
Address.insert(%{
name: name,
address_line_1: "The Hall",
address_line_2: "Valhalla",
city: "Asgard",
postcode: "AS1 3DG",
tel: "0800123123"
})
end
# def insert_address(name \\ "Thor") do
# Address.insert(%{
# name: name,
# address_line_1: "The Hall",
# address_line_2: "Valhalla",
# city: "Asgard",
# postcode: "AS1 3DG",
# tel: "0800123123"
# })
# end

test "update item in database" do
{:ok, item} = insert_address()
# test "update item in database" do
# {:ok, item} = insert_address()

{:ok, updated_item} = Address.update(item, %{tel: "0123444444"})
# {:ok, updated_item} = Address.update(item, %{tel: "0123444444"})

assert updated_item.name == item.name
assert updated_item.tel != item.tel
end
# assert updated_item.name == item.name
# assert updated_item.tel != item.tel
# end


test "get updated item" do
{:ok, item} = insert_address()
# test "get updated item" do
# {:ok, item} = insert_address()

{:ok, updated_item} = Address.update(item, %{tel: "0123444444"})
# {:ok, updated_item} = Address.update(item, %{tel: "0123444444"})

assert Address.get(item.entry_id) == updated_item
end
# assert Address.get(item.entry_id) == updated_item
# end

test "all/0 does not include old items" do
{:ok, item} = insert_address()
{:ok, _} = insert_address("Loki")
{:ok, _} = Address.update(item, %{postcode: "W2 3EC"})
# test "all/0 does not include old items" do
# {:ok, item} = insert_address()
# {:ok, _} = insert_address("Loki")
# {:ok, _} = Address.update(item, %{postcode: "W2 3EC"})

assert length(Address.all()) == 2
end
# assert length(Address.all()) == 2
# end

test "get history of item" do
{:ok, item} = insert_address()
# IO.inspect item
# test "get history of item" do
# {:ok, item} = insert_address()
# # IO.inspect item

{:ok, updated_item} = Address.update(item, %{
address_line_1: "12",
address_line_2: "Kvadraturen",
city: "Oslo",
postcode: "NW1 SCA",
})
# {:ok, updated_item} = Address.update(item, %{
# address_line_1: "12",
# address_line_2: "Kvadraturen",
# city: "Oslo",
# postcode: "NW1 SCA",
# })

history = Address.get_history(updated_item)
# history = Address.get_history(updated_item)

assert length(history) == 2
assert [h1, h2] = history
assert h1.city == "Asgard"
assert h2.city == "Oslo"
end
# assert length(history) == 2
# assert [h1, h2] = history
# assert h1.city == "Asgard"
# assert h2.city == "Oslo"
# end

describe "delete:" do
test "deleted items are not retrieved with 'get'" do
{:ok, item} = insert_address()
{:ok, _} = Address.delete(item)
# describe "delete:" do
# test "deleted items are not retrieved with 'get'" do
# {:ok, item} = insert_address()
# {:ok, _} = Address.delete(item)

assert Address.get(item.entry_id) == nil
end
# assert Address.get(item.entry_id) == nil
# end

test "deleted items are not retrieved with 'all'" do
{:ok, item} = insert_address()
{:ok, _} = Address.delete(item)
# test "deleted items are not retrieved with 'all'" do
# {:ok, item} = insert_address()
# {:ok, _} = Address.delete(item)

assert length(Address.all()) == 0
end
end
# assert length(Address.all()) == 0
# end
# end
end

0 comments on commit 874538a

Please sign in to comment.