Skip to content
Permalink
Browse files
Code for step 3
  • Loading branch information
akoutmos committed Jun 1, 2020
1 parent 56e57e5 commit 4ff11db5bde545f39748e48cac8b2f042734ee71
Showing 2 changed files with 67 additions and 3 deletions.
@@ -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
@@ -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.