defmodule Exercises.Exercise2Test do
  use ExUnit.Case, async: false

  alias MyApp.{Item, ShopInventory}

  @tag :exercise2
  test "Should list init items" do
    init_items = [%Item{name: "bread", price: 6}]
    {:ok, pid} = ShopInventory.start_link(init_items)
    items = ShopInventory.list_items(pid)
    assert init_items == items
  end

  @tag :exercise2
  test "Should get item by name" do
    init_items = [init_item] = [%Item{name: "bread", price: 6}]
    {:ok, pid} = ShopInventory.start_link(init_items)
    item = ShopInventory.get_item_by_name(pid, "bread")
    assert init_item == item
  end

  @tag :exercise2
  test "Should create an item" do
    {:ok, pid} = ShopInventory.start_link([])
    new_item = %Item{name: "bread", price: 6}
    ShopInventory.create_item(pid, new_item)
    items = ShopInventory.list_items(pid)
    assert [new_item] == items
  end

  @tag :exercise2
  test "Should delete an item" do
    init_items = [init_item] = [%Item{name: "bread", price: 6}]
    {:ok, pid} = ShopInventory.start_link(init_items)
    ShopInventory.delete_item(pid, init_item)
    items = ShopInventory.list_items(pid)
    assert [] == items
  end
end
