Skip to content

Commit

Permalink
Code for step 3
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Jun 1, 2020
1 parent 56e57e5 commit 4ff11db
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
37 changes: 37 additions & 0 deletions lib/store_inventory/test_repo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule StoreInventory.TestRepo do
alias Ecto.Changeset
alias StoreInventory.Item

def all(Item, _opts \\ []) do
[
%Item{
name: "Thing 1",
description: "A really cool thing",
price: 100,
quantity: 2
},
%Item{
name: "Thing 2",
description: "Another really cool thing",
price: 50,
quantity: 0
},
%Item{
name: "Thing 3",
description: "A really cool thing",
price: 75,
quantity: 5
}
]
end

def insert(changeset, opts \\ [])

def insert(%Changeset{errors: [], changes: values}, _opts) do
{:ok, struct(Item, values)}
end

def insert(changeset, _opts) do
{:error, changeset}
end
end
33 changes: 30 additions & 3 deletions test/store_inventory_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
defmodule StoreInventoryTest do
use ExUnit.Case
doctest StoreInventory

test "greets the world" do
assert StoreInventory.hello() == :world
alias Ecto.Changeset
alias StoreInventory.Item

test "insert_item/1 should return an error when invalid params are provided" do
params = %{
name: "Item",
description: "Something people want",
price: 1,
quantity: "INVALID"
}

{:error, changeset} = StoreInventory.insert_item(params)

expected_error = {"is invalid", [type: :integer, validation: :cast]}
assert %Changeset{errors: [quantity: ^expected_error]} = changeset
end

test "insert_item/1 should return an :ok tuple when valid params are provided" do
params = %{
name: "Item",
description: "Something people want",
price: 1,
quantity: 5
}

assert {:ok, %Item{}} = StoreInventory.insert_item(params)
end

test "average_item_price/0 should return the average price of all the items in the DB" do
assert 75.0 = StoreInventory.average_item_price()
end
end

0 comments on commit 4ff11db

Please sign in to comment.