Skip to content

Commit

Permalink
Merge pull request #6 from stereowrench/product-tests
Browse files Browse the repository at this point in the history
Product Tests
  • Loading branch information
hunterboerner authored Feb 19, 2025
2 parents 38fb727 + 9089909 commit b271f2d
Show file tree
Hide file tree
Showing 25 changed files with 657 additions and 104 deletions.
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ config :logger, level: :warning
# Initialize plugs at runtime for faster test compilation
config :phoenix, :plug_init_mode, :runtime

config :phoenix_test, :endpoint, TheronsErpWeb.Endpoint

# Enable helpful, but potentially expensive runtime checks
config :phoenix_live_view,
enable_expensive_runtime_checks: true
2 changes: 1 addition & 1 deletion lib/therons_erp/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ defmodule TheronsErp.Application do
@impl true
def start(_type, _args) do
children = [
TheronsErp.Repo,
{Oban,
AshOban.config(
Application.fetch_env!(:therons_erp, :ash_domains),
Application.fetch_env!(:therons_erp, Oban)
)},
TheronsErpWeb.Telemetry,
TheronsErp.Repo,
{DNSCluster, query: Application.get_env(:therons_erp, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: TheronsErp.PubSub},
# Start the Finch HTTP client for sending emails
Expand Down
1 change: 1 addition & 0 deletions lib/therons_erp/inventory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule TheronsErp.Inventory do
define :create_category, args: [:name], action: :create
define :change_parent, args: [:product_category_id], action: :update_parent
define :get_categories, action: :list
define :create_category_stub, action: :create_stub
end

resource TheronsErp.Inventory.Product do
Expand Down
1 change: 1 addition & 0 deletions lib/therons_erp/inventory/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule TheronsErp.Inventory.Product do
end

destroy :destroy do
primary? true
end
end

Expand Down
42 changes: 25 additions & 17 deletions lib/therons_erp/inventory/product_category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule TheronsErp.Inventory.ProductCategory do
otp_app: :therons_erp,
domain: TheronsErp.Inventory,
data_layer: AshPostgres.DataLayer,
extensions: [AshArchival.Resource]
extensions: [AshArchival.Resource],
primary_read_warning?: false

require Ash.Query

Expand All @@ -19,9 +20,12 @@ defmodule TheronsErp.Inventory.ProductCategory do
defaults [:read]

read :list do
primary? true
prepare build(sort: [id: :desc])
end

destroy :delete do
primary? true
end

create :create do
Expand All @@ -30,6 +34,16 @@ defmodule TheronsErp.Inventory.ProductCategory do
change &perform_full_name_update/2
end

create :create_stub do
accept []

change fn changeset, _context ->
changeset
|> Ash.Changeset.change_attribute(:name, "New Category")
|> Ash.Changeset.change_attribute(:full_name, "New Category")
end
end

update :update_parent do
accept [:product_category_id, :name]
require_atomic? false
Expand Down Expand Up @@ -127,22 +141,16 @@ defmodule TheronsErp.Inventory.ProductCategory do
end
end)
|> Ash.Changeset.after_action(fn changeset, result ->
alt =
Ash.Changeset.get_attribute(changeset, :product_category_id) == nil and
Ash.Changeset.fetch_argument(changeset, :product_category_id) != nil

if Ash.Changeset.get_attribute(changeset, :product_category_id) != nil or alt do
id = Ash.Changeset.get_attribute(changeset, :id)

unless id == nil do
subcats =
ProductCategory
|> Ash.Query.filter(product_category_id == ^id)
|> Ash.read!()

for cat <- subcats do
Inventory.change_parent!(cat.id, changeset.data.id)
end
id = Ash.Changeset.get_attribute(changeset, :id)

unless id == nil do
subcats =
ProductCategory
|> Ash.Query.filter(product_category_id == ^id)
|> Ash.read!()

for cat <- subcats do
Inventory.change_parent!(cat.id, changeset.data.id)
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/therons_erp/sales/sales_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ defmodule TheronsErp.Sales.SalesOrder do

change fn changeset, result ->
Ash.Changeset.after_action(changeset, fn changeset, result ->
IO.inspect(result)

Invoice
|> Ash.Changeset.for_create(:create, %{
sales_order_id: result.id,
Expand Down Expand Up @@ -93,6 +91,9 @@ defmodule TheronsErp.Sales.SalesOrder do
require_atomic? false
argument :sales_lines, {:array, :map}

validate present(:customer_id)
validate present(:address_id)

change manage_relationship(:sales_lines, type: :direct_control),
where: [attribute_equals(:state, :draft)]

Expand All @@ -117,6 +118,7 @@ defmodule TheronsErp.Sales.SalesOrder do
end

belongs_to :customer, TheronsErp.People.Entity

belongs_to :address, TheronsErp.People.Address

has_one :invoice, TheronsErp.Invoices.Invoice do
Expand Down
58 changes: 47 additions & 11 deletions lib/therons_erp_web/breadcrumbs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ defmodule TheronsErpWeb.Breadcrumbs do
true
end

defp to_json_map({route}) do
%{route: route}
end

defp to_json_map({route, action}) do
%{route: route, action: action}
end
Expand All @@ -55,6 +59,10 @@ defmodule TheronsErpWeb.Breadcrumbs do
{route, action}
end

defp from_json_map(%{"route" => route}) do
{route}
end

def encode_breadcrumbs(breadcrumbs) do
breadcrumbs
|> Enum.map(&to_json_map/1)
Expand All @@ -70,8 +78,10 @@ defmodule TheronsErpWeb.Breadcrumbs do
end

def navigate_back(socket, from, args \\ nil) do
breadcrumbs = socket.assigns.breadcrumbs
{path, _breadcrumbs} = _navigate_back(breadcrumbs, from, args)
breadcrumbs =
socket.assigns.breadcrumbs

{path, breadcrumbs} = _navigate_back(breadcrumbs, from, args)

socket
|> assign(:breadcrumbs, encode_breadcrumbs(breadcrumbs))
Expand All @@ -86,12 +96,15 @@ defmodule TheronsErpWeb.Breadcrumbs do
{"product_category", "new", _product_category_id} ->
~p"/product_categories"

{"product_category", product_category_id, _name} ->
{"product_category", product_category_id, _params, _name} ->
~p"/product_categories/#{product_category_id}"

{"products", "edit", product_id} ->
~p"/products/#{product_id}"

{"product_categories", "edit", product_category_id} ->
~p"/product_categories/#{product_category_id}"

{"people", entity_id} ->
~p"/people/#{entity_id}"
end
Expand All @@ -102,6 +115,9 @@ defmodule TheronsErpWeb.Breadcrumbs do
defp _navigate_back([breadcrumb | breadcrumbs], _from, from_args) do
which =
case breadcrumb do
{"product_categories"} ->
~p"/product_categories?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs)]}"

{"people", args, id} ->
if from_args do
~p"/people/#{id}?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs), args: args, from_args: from_args]}"
Expand Down Expand Up @@ -130,11 +146,11 @@ defmodule TheronsErpWeb.Breadcrumbs do
~p"/products/#{pid}?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs)]}"
end

{"product_category", id, _name} ->
{"product_category", id, params, _name} ->
if from_args do
~p"/product_categories/#{id}?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs), from_args: from_args]}"
~p"/product_categories/#{id}?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs), args: params, from_args: from_args]}"
else
~p"/product_categories/#{id}?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs)]}"
~p"/product_categories/#{id}?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs), args: params]}"
end

{"sales_orders", id, params, _identifier} ->
Expand All @@ -157,16 +173,28 @@ defmodule TheronsErpWeb.Breadcrumbs do
~p"/product_categories?#{[breadcrumbs: append_and_encode(breadcrumbs, from)]}"
end

def navigate_to_url(breadcrumbs, {"product_categories", "edit", id}, from) do
~p"/product_categories/#{id}?#{[breadcrumbs: append_and_encode(breadcrumbs, from)]}"
end

def navigate_to_url(breadcrumbs, {"product_category", "new_stub", id, params, new}, _from) do
~p"/product_categories/#{id}?#{[breadcrumbs: encode_breadcrumbs(breadcrumbs), new: new, args: params]}"
end

def navigate_to_url(breadcrumbs, {"product_category", "new"}, from) do
~p"/product_categories/new?#{[breadcrumbs: append_and_encode(breadcrumbs, from)]}"
end

def navigate_to_url(breadcrumbs, {"product_category", "new", product_id}, from) do
~p"/product_categories/new?#{[breadcrumbs: append_and_encode(breadcrumbs, from), product_id: product_id]}"
~p"/product_categories/new?#{[breadcrumbs: append_and_encode(breadcrumbs, from), product_id: product_id, new: true]}"
end

def navigate_to_url(breadcrumbs, {"product_category", "new_cat", category_id}, from) do
~p"/product_categories/new?#{[breadcrumbs: append_and_encode(breadcrumbs, from), category_id: category_id]}"
~p"/product_categories/new?#{[breadcrumbs: append_and_encode(breadcrumbs, from), category_id: category_id, new: true]}"
end

def navigate_to_url(breadcrumbs, {"product_category", id, _full_name}, from) do
~p"/product_categories/#{id}?#{[breadcrumbs: append_and_encode(breadcrumbs, from)]}"
def navigate_to_url(breadcrumbs, {"product_category", id, params, _full_name}, from) do
~p"/product_categories/#{id}?#{[breadcrumbs: append_and_encode(breadcrumbs, from), args: params]}"
end

def navigate_to_url(breadcrumbs, {"products", "new", line_id}, from) do
Expand Down Expand Up @@ -206,14 +234,22 @@ defmodule TheronsErpWeb.Breadcrumbs do
"#{identifier}"
end

defp name_for_crumb({"product_category", _cid, name}) do
defp name_for_crumb({"product_category", "new cat", _cid}) do
"New category"
end

defp name_for_crumb({"product_category", _cid, _params, name}) do
"#{name}"
end

defp name_for_crumb({"sales_orders", _sale_id, _params, serial_no}) do
"S#{serial_no}"
end

defp name_for_crumb({"product_categories"}) do
"Product Categories"
end

def stream_crumbs(list) when is_list(list) do
_stream_crumbs(list)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ defmodule TheronsErpWeb.EntityLive.NewAddressFormComponent do
socket
|> put_flash(:info, "Address #{socket.assigns.form.source.type}d successfully")
|> Breadcrumbs.navigate_back({"people", address.entity_id}, %{
address_id: address.id
address_id: address.id,
customer_id: address.entity_id
})

{:noreply, socket}
Expand Down
Empty file.
48 changes: 34 additions & 14 deletions lib/therons_erp_web/live/product_category_live/index.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
defmodule TheronsErpWeb.ProductCategoryLive.Index do
use TheronsErpWeb, :live_view
alias TheronsErpWeb.Breadcrumbs

@impl true
def render(assigns) do
~H"""
<.header>
Listing Product categories
<:actions>
<.link patch={~p"/product_categories/new"}>
<.link href={
Breadcrumbs.navigate_to_url(
@breadcrumbs,
{"product_category", "new"},
{"product_categories"}
)
}>
<.button>New Product category</.button>
</.link>
</:actions>
Expand All @@ -17,17 +24,33 @@ defmodule TheronsErpWeb.ProductCategoryLive.Index do
id="product_categories"
rows={@streams.product_categories}
row_click={
fn {_id, product_category} -> JS.navigate(~p"/product_categories/#{product_category}") end
fn {_id, product_category} ->
JS.navigate(
Breadcrumbs.navigate_to_url(
@breadcrumbs,
{"product_categories", "edit", product_category.id},
{"product_categories"}
)
)
end
}
>
<:col :let={{_id, product_category}} label="Name">{product_category.full_name}</:col>
<:action :let={{_id, product_category}}>
<div class="sr-only">
<.link navigate={~p"/product_categories/#{product_category}"}>Show</.link>
<.link navigate={
JS.navigate(
Breadcrumbs.navigate_to_url(
@breadcrumbs,
{"product_categories", "edit", product_category.id},
{"product_categories"}
)
)
}>
Show
</.link>
</div>
<.link patch={~p"/product_categories/#{product_category}/edit"}>Edit</.link>
</:action>
<:action :let={{id, product_category}}>
Expand Down Expand Up @@ -83,19 +106,16 @@ defmodule TheronsErpWeb.ProductCategoryLive.Index do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Product category")
|> assign(
:product_category,
Ash.get!(TheronsErp.Inventory.ProductCategory, id, actor: socket.assigns.current_user)
)
end
defp apply_action(socket, :new, params) do
product_category = TheronsErp.Inventory.create_category_stub!()

defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Product category")
|> assign(:product_category, nil)
|> Breadcrumbs.navigate_to(
{"product_category", "new_stub", product_category.id, params, params["new"]},
{"product_categories"}
)
end

defp apply_action(socket, :index, _params) do
Expand Down
Loading

0 comments on commit b271f2d

Please sign in to comment.