Skip to content

Commit

Permalink
Add foreign key constraint migration to menus table, fix tests affect…
Browse files Browse the repository at this point in the history
…ed by this change
  • Loading branch information
tbarmann committed Nov 30, 2022
1 parent 3c462b4 commit 36d2823
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Lunchbot.Repo.Migrations.AddReferencesToMenu do
use Ecto.Migration

def change do
alter table(:menus) do
modify :office_id, references(:offices, on_delete: :delete_all)
end
end
end
26 changes: 20 additions & 6 deletions test/lunchbot/lunchbot_data_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,16 @@ defmodule Lunchbot.LunchbotDataTest do

describe "#create_menu/1" do
test "with valid data creates a menu" do
assert {:ok, %Menu{} = menu} = LunchbotData.create_menu(@valid_attrs)
assert menu.name == "some name"
assert menu.office_id == 42
office = LunchbotDataFixtures.office_fixture()

params = %{
name: "Pizza Master",
office_id: office.id
}

assert {:ok, %Menu{} = menu} = LunchbotData.create_menu(params)
assert menu.name == params.name
assert menu.office_id == office.id
end

test "with invalid data returns error changeset" do
Expand All @@ -245,10 +252,17 @@ defmodule Lunchbot.LunchbotDataTest do
describe "#update_menu/2" do
test "with valid data updates the menu" do
menu = LunchbotDataFixtures.menu_fixture()
assert {:ok, menu} = LunchbotData.update_menu(menu, @update_attrs)
office = LunchbotDataFixtures.office_fixture()

params = %{
name: "Chowder Hound",
office_id: office.id
}

assert {:ok, menu} = LunchbotData.update_menu(menu, params)
assert %Menu{} = menu
assert menu.name == "some updated name"
assert menu.office_id == 43
assert menu.name == params.name
assert menu.office_id == office.id
end

test "with invalid data returns error changeset" do
Expand Down
26 changes: 19 additions & 7 deletions test/lunchbot_web/controllers/admin/menu_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
defmodule LunchbotWeb.Admin.MenuControllerTest do
use LunchbotWeb.ConnCase

alias Lunchbot.LunchbotData
alias Lunchbot.LunchbotDataFixtures

@create_attrs %{name: "some name", office_id: 42}
@update_attrs %{name: "some updated name", office_id: 43}
@invalid_attrs %{name: nil, office_id: nil}

def fixture(:menu) do
{:ok, menu} = LunchbotData.create_menu(@create_attrs)
{:ok, menu} = LunchbotDataFixtures.menu_fixture()
menu
end

Expand All @@ -32,7 +30,14 @@ defmodule LunchbotWeb.Admin.MenuControllerTest do

describe "create menu" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post conn, Routes.admin_menu_path(conn, :create), menu: @create_attrs
office = LunchbotDataFixtures.office_fixture()

params = %{
name: "Menu name",
office_id: office.id
}

conn = post conn, Routes.admin_menu_path(conn, :create), menu: params

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.admin_menu_path(conn, :show, id)
Expand Down Expand Up @@ -60,7 +65,14 @@ defmodule LunchbotWeb.Admin.MenuControllerTest do
setup [:create_menu]

test "redirects when data is valid", %{conn: conn, menu: menu} do
conn = put conn, Routes.admin_menu_path(conn, :update, menu), menu: @update_attrs
office = LunchbotDataFixtures.office_fixture()

params = %{
name: "some updated name",
office_id: office.id
}

conn = put conn, Routes.admin_menu_path(conn, :update, menu), menu: params
assert redirected_to(conn) == Routes.admin_menu_path(conn, :show, menu)

conn = get(conn, Routes.admin_menu_path(conn, :show, menu))
Expand All @@ -87,7 +99,7 @@ defmodule LunchbotWeb.Admin.MenuControllerTest do
end

defp create_menu(_) do
menu = fixture(:menu)
menu = LunchbotDataFixtures.menu_fixture()
{:ok, menu: menu}
end
end
34 changes: 27 additions & 7 deletions test/lunchbot_web/controllers/admin/order_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule LunchbotWeb.Admin.OrderControllerTest do
use LunchbotWeb.ConnCase

alias Lunchbot.LunchbotData
alias Lunchbot.LunchbotDataFixtures
alias Inspect.Lunchbot
alias LunchbotDataFixtures

@create_attrs %{office_lunch_order_id: 42, menu_id: 42, user_id: 42}
@update_attrs %{office_lunch_order_id: 43, menu_id: 43, user_id: 43}
@invalid_attrs %{office_lunch_order_id: nil, menu_id: nil, user_id: nil}

def fixture(:order) do
{:ok, order} = LunchbotData.create_order(@create_attrs)
{:ok, order} = LunchbotDataFixtures.order_fixture()
order
end

Expand All @@ -32,7 +32,17 @@ defmodule LunchbotWeb.Admin.OrderControllerTest do

describe "create order" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post conn, Routes.admin_order_path(conn, :create), order: @create_attrs
user = LunchbotDataFixtures.user_fixture()
menu = LunchbotDataFixtures.menu_fixture()
office_lunch_order = LunchbotDataFixtures.office_lunch_order_fixture(%{menu_id: menu.id})

params = %{
office_lunch_order_id: office_lunch_order.id,
menu_id: menu.id,
user_id: user.id
}

conn = post conn, Routes.admin_order_path(conn, :create), order: params

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.admin_order_path(conn, :show, id)
Expand Down Expand Up @@ -60,7 +70,17 @@ defmodule LunchbotWeb.Admin.OrderControllerTest do
setup [:create_order]

test "redirects when data is valid", %{conn: conn, order: order} do
conn = put conn, Routes.admin_order_path(conn, :update, order), order: @update_attrs
user = LunchbotDataFixtures.user_fixture()
menu = LunchbotDataFixtures.menu_fixture()
office_lunch_order = LunchbotDataFixtures.office_lunch_order_fixture(%{menu_id: menu.id})

params = %{
office_lunch_order_id: office_lunch_order.id,
menu_id: menu.id,
user_id: user.id
}

conn = put conn, Routes.admin_order_path(conn, :update, order), order: params
assert redirected_to(conn) == Routes.admin_order_path(conn, :show, order)

conn = get(conn, Routes.admin_order_path(conn, :show, order))
Expand All @@ -87,7 +107,7 @@ defmodule LunchbotWeb.Admin.OrderControllerTest do
end

defp create_order(_) do
order = fixture(:order)
order = LunchbotDataFixtures.order_fixture()
{:ok, order: order}
end
end
18 changes: 7 additions & 11 deletions test/lunchbot_web/controllers/admin/order_item_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
defmodule LunchbotWeb.Admin.OrderItemControllerTest do
use LunchbotWeb.ConnCase

alias Lunchbot.LunchbotData
alias Lunchbot.LunchbotDataFixtures

@create_attrs %{item_id: 42, order_id: 42}
@update_attrs %{item_id: 43, order_id: 43}
@invalid_attrs %{item_id: nil, order_id: nil}

def fixture(:order_item) do
{:ok, order_item} = LunchbotData.create_order_item(@create_attrs)
order_item
end

setup do
initialize_admin_user()
end
Expand All @@ -32,7 +25,11 @@ defmodule LunchbotWeb.Admin.OrderItemControllerTest do

describe "create order_item" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post conn, Routes.admin_order_item_path(conn, :create), order_item: @create_attrs
item = LunchbotDataFixtures.item_fixture()
order = LunchbotDataFixtures.order_fixture()
params = %{item_id: item.id, order_id: order.id}

conn = post conn, Routes.admin_order_item_path(conn, :create), order_item: params

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.admin_order_item_path(conn, :show, id)
Expand Down Expand Up @@ -70,7 +67,6 @@ defmodule LunchbotWeb.Admin.OrderItemControllerTest do
end

defp create_order_item(_) do
order_item = fixture(:order_item)
{:ok, order_item: order_item}
{:ok, order_item: LunchbotDataFixtures.order_item_fixture()}
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
defmodule LunchbotWeb.Admin.OrderItemOptionsControllerTest do
use LunchbotWeb.ConnCase

alias Lunchbot.LunchbotData
alias Lunchbot.LunchbotDataFixtures

@create_attrs %{option_id: 42, order_item_id: 42}
@update_attrs %{option_id: 43, order_item_id: 43}
@invalid_attrs %{option_id: nil, order_item_id: nil}

def fixture(:order_item_options) do
{:ok, order_item_options} = LunchbotData.create_order_item_options(@create_attrs)
order_item_options
end

setup do
initialize_admin_user()
end
Expand All @@ -32,9 +25,17 @@ defmodule LunchbotWeb.Admin.OrderItemOptionsControllerTest do

describe "create order_item_options" do
test "redirects to show when data is valid", %{conn: conn} do
# @create_attrs %{option_id: 42, order_item_id: 42}
option = LunchbotDataFixtures.options_fixture()
order_item = LunchbotDataFixtures.order_item_fixture()

params = %{
option_id: option.id,
order_item_id: order_item.id
}

conn =
post conn, Routes.admin_order_item_options_path(conn, :create),
order_item_options: @create_attrs
post conn, Routes.admin_order_item_options_path(conn, :create), order_item_options: params

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.admin_order_item_options_path(conn, :show, id)
Expand Down Expand Up @@ -68,9 +69,17 @@ defmodule LunchbotWeb.Admin.OrderItemOptionsControllerTest do
setup [:create_order_item_options]

test "redirects when data is valid", %{conn: conn, order_item_options: order_item_options} do
option = LunchbotDataFixtures.options_fixture()
order_item = LunchbotDataFixtures.order_item_fixture()

params = %{
option_id: option.id,
order_item_id: order_item.id
}

conn =
put conn, Routes.admin_order_item_options_path(conn, :update, order_item_options),
order_item_options: @update_attrs
order_item_options: params

assert redirected_to(conn) ==
Routes.admin_order_item_options_path(conn, :show, order_item_options)
Expand Down Expand Up @@ -108,7 +117,7 @@ defmodule LunchbotWeb.Admin.OrderItemOptionsControllerTest do
end

defp create_order_item_options(_) do
order_item_options = fixture(:order_item_options)
order_item_options = LunchbotDataFixtures.order_item_options_fixture()
{:ok, order_item_options: order_item_options}
end
end
11 changes: 8 additions & 3 deletions test/support/fixtures/lunchbot_data_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ defmodule Lunchbot.LunchbotDataFixtures do
Generate a menu.
"""
def menu_fixture(attrs \\ %{}) do
office = office_fixture()

{:ok, menu} =
attrs
|> Enum.into(%{
name: "some name",
office_id: 42
office_id: office.id
})
|> Lunchbot.LunchbotData.create_menu()

Expand All @@ -56,12 +58,15 @@ defmodule Lunchbot.LunchbotDataFixtures do
Generate a office_lunch_order.
"""
def office_lunch_order_fixture(attrs \\ %{}) do
office = office_fixture()
menu = menu_fixture()

{:ok, office_lunch_order} =
attrs
|> Enum.into(%{
day: ~D[2022-07-07],
office_id: 42,
menu_id: 42
office_id: office.id,
menu_id: menu.id
})
|> Lunchbot.LunchbotData.create_office_lunch_order()

Expand Down

0 comments on commit 36d2823

Please sign in to comment.