After 'mix phx.gen.live Items Item items name:string description:string info:string amount:integer factor:float type:string'

This commit is contained in:
2026-04-21 13:29:13 +02:00
parent e930c742b5
commit 851665ef60
10 changed files with 698 additions and 0 deletions

View File

@ -0,0 +1,101 @@
defmodule GenericRestServer.ItemsTest do
use GenericRestServer.DataCase
alias GenericRestServer.Items
describe "items" do
alias GenericRestServer.Items.Item
import GenericRestServer.AccountsFixtures, only: [user_scope_fixture: 0]
import GenericRestServer.ItemsFixtures
@invalid_attrs %{info: nil, name: nil, type: nil, description: nil, amount: nil, factor: nil}
test "list_items/1 returns all scoped items" do
scope = user_scope_fixture()
other_scope = user_scope_fixture()
item = item_fixture(scope)
other_item = item_fixture(other_scope)
assert Items.list_items(scope) == [item]
assert Items.list_items(other_scope) == [other_item]
end
test "get_item!/2 returns the item with given id" do
scope = user_scope_fixture()
item = item_fixture(scope)
other_scope = user_scope_fixture()
assert Items.get_item!(scope, item.id) == item
assert_raise Ecto.NoResultsError, fn -> Items.get_item!(other_scope, item.id) end
end
test "create_item/2 with valid data creates a item" do
valid_attrs = %{info: "some info", name: "some name", type: "some type", description: "some description", amount: 42, factor: 120.5}
scope = user_scope_fixture()
assert {:ok, %Item{} = item} = Items.create_item(scope, valid_attrs)
assert item.info == "some info"
assert item.name == "some name"
assert item.type == "some type"
assert item.description == "some description"
assert item.amount == 42
assert item.factor == 120.5
assert item.user_id == scope.user.id
end
test "create_item/2 with invalid data returns error changeset" do
scope = user_scope_fixture()
assert {:error, %Ecto.Changeset{}} = Items.create_item(scope, @invalid_attrs)
end
test "update_item/3 with valid data updates the item" do
scope = user_scope_fixture()
item = item_fixture(scope)
update_attrs = %{info: "some updated info", name: "some updated name", type: "some updated type", description: "some updated description", amount: 43, factor: 456.7}
assert {:ok, %Item{} = item} = Items.update_item(scope, item, update_attrs)
assert item.info == "some updated info"
assert item.name == "some updated name"
assert item.type == "some updated type"
assert item.description == "some updated description"
assert item.amount == 43
assert item.factor == 456.7
end
test "update_item/3 with invalid scope raises" do
scope = user_scope_fixture()
other_scope = user_scope_fixture()
item = item_fixture(scope)
assert_raise MatchError, fn ->
Items.update_item(other_scope, item, %{})
end
end
test "update_item/3 with invalid data returns error changeset" do
scope = user_scope_fixture()
item = item_fixture(scope)
assert {:error, %Ecto.Changeset{}} = Items.update_item(scope, item, @invalid_attrs)
assert item == Items.get_item!(scope, item.id)
end
test "delete_item/2 deletes the item" do
scope = user_scope_fixture()
item = item_fixture(scope)
assert {:ok, %Item{}} = Items.delete_item(scope, item)
assert_raise Ecto.NoResultsError, fn -> Items.get_item!(scope, item.id) end
end
test "delete_item/2 with invalid scope raises" do
scope = user_scope_fixture()
other_scope = user_scope_fixture()
item = item_fixture(scope)
assert_raise MatchError, fn -> Items.delete_item(other_scope, item) end
end
test "change_item/2 returns a item changeset" do
scope = user_scope_fixture()
item = item_fixture(scope)
assert %Ecto.Changeset{} = Items.change_item(scope, item)
end
end
end

View File

@ -0,0 +1,125 @@
defmodule GenericRestServerWeb.ItemLiveTest do
use GenericRestServerWeb.ConnCase
import Phoenix.LiveViewTest
import GenericRestServer.ItemsFixtures
@create_attrs %{info: "some info", name: "some name", type: "some type", description: "some description", amount: 42, factor: 120.5}
@update_attrs %{info: "some updated info", name: "some updated name", type: "some updated type", description: "some updated description", amount: 43, factor: 456.7}
@invalid_attrs %{info: nil, name: nil, type: nil, description: nil, amount: nil, factor: nil}
setup :register_and_log_in_user
defp create_item(%{scope: scope}) do
item = item_fixture(scope)
%{item: item}
end
describe "Index" do
setup [:create_item]
test "lists all items", %{conn: conn, item: item} do
{:ok, _index_live, html} = live(conn, ~p"/items")
assert html =~ "Listing Items"
assert html =~ item.name
end
test "saves new item", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/items")
assert {:ok, form_live, _} =
index_live
|> element("a", "New Item")
|> render_click()
|> follow_redirect(conn, ~p"/items/new")
assert render(form_live) =~ "New Item"
assert form_live
|> form("#item-form", item: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert {:ok, index_live, _html} =
form_live
|> form("#item-form", item: @create_attrs)
|> render_submit()
|> follow_redirect(conn, ~p"/items")
html = render(index_live)
assert html =~ "Item created successfully"
assert html =~ "some name"
end
test "updates item in listing", %{conn: conn, item: item} do
{:ok, index_live, _html} = live(conn, ~p"/items")
assert {:ok, form_live, _html} =
index_live
|> element("#items-#{item.id} a", "Edit")
|> render_click()
|> follow_redirect(conn, ~p"/items/#{item}/edit")
assert render(form_live) =~ "Edit Item"
assert form_live
|> form("#item-form", item: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert {:ok, index_live, _html} =
form_live
|> form("#item-form", item: @update_attrs)
|> render_submit()
|> follow_redirect(conn, ~p"/items")
html = render(index_live)
assert html =~ "Item updated successfully"
assert html =~ "some updated name"
end
test "deletes item in listing", %{conn: conn, item: item} do
{:ok, index_live, _html} = live(conn, ~p"/items")
assert index_live |> element("#items-#{item.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#items-#{item.id}")
end
end
describe "Show" do
setup [:create_item]
test "displays item", %{conn: conn, item: item} do
{:ok, _show_live, html} = live(conn, ~p"/items/#{item}")
assert html =~ "Show Item"
assert html =~ item.name
end
test "updates item and returns to show", %{conn: conn, item: item} do
{:ok, show_live, _html} = live(conn, ~p"/items/#{item}")
assert {:ok, form_live, _} =
show_live
|> element("a", "Edit")
|> render_click()
|> follow_redirect(conn, ~p"/items/#{item}/edit?return_to=show")
assert render(form_live) =~ "Edit Item"
assert form_live
|> form("#item-form", item: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert {:ok, show_live, _html} =
form_live
|> form("#item-form", item: @update_attrs)
|> render_submit()
|> follow_redirect(conn, ~p"/items/#{item}")
html = render(show_live)
assert html =~ "Item updated successfully"
assert html =~ "some updated name"
end
end
end

View File

@ -0,0 +1,24 @@
defmodule GenericRestServer.ItemsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `GenericRestServer.Items` context.
"""
@doc """
Generate a item.
"""
def item_fixture(scope, attrs \\ %{}) do
attrs =
Enum.into(attrs, %{
amount: 42,
description: "some description",
factor: 120.5,
info: "some info",
name: "some name",
type: "some type"
})
{:ok, item} = GenericRestServer.Items.create_item(scope, attrs)
item
end
end