Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Sep 3, 2023
1 parent e9f4046 commit bef92dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
17 changes: 5 additions & 12 deletions lib/parzival/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule Parzival.Store do
|> Product.changeset(attrs)
|> Repo.update()
|> after_save(after_save)
|> broadcast(:updated)
|> broadcast(:product_updated)
end

def update_product_image(%Product{} = product, attrs) do
Expand Down Expand Up @@ -121,7 +121,7 @@ defmodule Parzival.Store do
message: "This product cant be deleted, because users have bought it!"
)
|> Repo.delete()
|> broadcast(:deleted)
|> broadcast(:product_deleted)
end

@doc """
Expand Down Expand Up @@ -496,22 +496,15 @@ defmodule Parzival.Store do
[%Item{}, ...]
"""
def list_items(params \\ %{})

def list_items(opts) when is_list(opts) do
def list_items do
Item
|> apply_filters(opts)
|> Repo.all()
end

def list_items(flop) do
Flop.validate_and_run(Item, flop, for: Item)
end

def list_items(%{} = flop, opts) when is_list(opts) do
def list_items(opts) when is_list(opts) do
Item
|> apply_filters(opts)
|> Flop.validate_and_run(flop, for: Item)
|> Repo.all()
end

@doc """
Expand Down
22 changes: 12 additions & 10 deletions test/parzival/store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ defmodule Parzival.StoreTest do

import Parzival.StoreFixtures

@invalid_attrs %{finish: nil}
@invalid_attrs %{type: nil}

test "list_boosts/0 returns all boosts" do
boost = boost_fixture()
assert Store.list_boosts() == [boost]

case Store.list_boosts() do
{:ok, {boosts, _meta}} -> assert hd(boosts) == boost
{:error, _} -> assert false
end
end

test "get_boost!/1 returns the boost with given id" do
Expand All @@ -158,19 +162,16 @@ defmodule Parzival.StoreTest do

test "create_boost/1 with valid data creates a boost" do
valid_attrs = %{
finish: ~N[2022-06-18 01:07:00],
multiplier: 2,
name: "some name",
price: 42,
start: ~N[2022-06-17 01:07:00],
stock: 42,
type: "exp",
description: "some description",
item: StoreFixtures.product_fixture()
}

assert {:ok, %Boost{} = boost} = Store.create_boost(valid_attrs)
assert boost.finish == ~N[2022-06-18 01:07:00]
assert boost.multiplier == 2
end

test "create_boost/1 with invalid data returns error changeset" do
Expand All @@ -179,10 +180,10 @@ defmodule Parzival.StoreTest do

test "update_boost/2 with valid data updates the boost" do
boost = boost_fixture()
update_attrs = %{finish: ~N[2022-06-19 01:07:00]}
update_attrs = %{name: "some updated name"}

assert {:ok, %Boost{} = boost} = Store.update_boost(boost, update_attrs)
assert boost.finish == ~N[2022-06-19 01:07:00]
assert boost.name == "some updated name"
end

test "update_boost/2 with invalid data returns error changeset" do
Expand All @@ -208,7 +209,7 @@ defmodule Parzival.StoreTest do

import Parzival.StoreFixtures

@invalid_attrs %{}
@invalid_attrs %{user_id: nil}

test "list_items/0 returns all items" do
item = item_fixture()
Expand All @@ -228,7 +229,8 @@ defmodule Parzival.StoreTest do
item = item_fixture()
update_attrs = %{expires_at: ~N[2022-06-19 01:07:00]}

assert {:ok, %Item{}} = Store.update_item(item, update_attrs)
assert {:ok, %Item{} = item} = Store.update_item(item, update_attrs)
assert item.expires_at == ~N[2022-06-19 01:07:00]
end

test "update_item/2 with invalid data returns error changeset" do
Expand Down
10 changes: 4 additions & 6 deletions test/support/fixtures/store_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Parzival.StoreFixtures do
"""

alias Parzival.AccountsFixtures
alias Parzival.StoreFixtures

@doc """
Generate a product.
Expand All @@ -31,8 +32,8 @@ defmodule Parzival.StoreFixtures do
{:ok, order} =
attrs
|> Enum.into(%{
product_id: Parzival.StoreFixtures.product_fixture().id,
user_id: Parzival.AccountsFixtures.user_fixture().id,
product_id: StoreFixtures.product_fixture().id,
user_id: AccountsFixtures.user_fixture().id,
redeemed: false
})
|> Parzival.Store.create_order()
Expand All @@ -49,12 +50,9 @@ defmodule Parzival.StoreFixtures do
|> Enum.into(%{
name: "some name",
description: "some description",
start: ~N[2022-06-17 01:07:00],
finish: ~N[2022-06-18 01:07:00],
price: 42,
type: "exp",
multiplier: 1.5,
item: Parzival.StoreFixtures.item_fixture()
multiplier: 1.5
})
|> Parzival.Store.create_boost()

Expand Down

0 comments on commit bef92dd

Please sign in to comment.