-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |