From fd866e69a10ff3e94e06cbba8a8bc8767764e225 Mon Sep 17 00:00:00 2001 From: Nathan Casados Date: Thu, 13 Feb 2025 18:37:43 -0700 Subject: [PATCH 1/2] Updating Tests Adding product and product category for live tests Updating factory to build using build Updating to use exmachina build Starting Repo before Oban--If Oban tries to start before repo, migrations fail. Adding missing comma --- lib/therons_erp/application.ex | 2 +- mix.exs | 4 +- mix.lock | 4 +- test/support/factory.ex | 26 ++++ test/test_helper.exs | 2 + test/therons_erp/inventory/product_test.exs | 8 ++ .../live/product_category_live_test.exs | 119 ++++++++++++++++++ .../live/product_live_test.exs | 111 ++++++++++++++++ 8 files changed, 273 insertions(+), 3 deletions(-) create mode 100644 test/support/factory.ex create mode 100644 test/therons_erp/inventory/product_test.exs diff --git a/lib/therons_erp/application.ex b/lib/therons_erp/application.ex index 625d571..f5e2c22 100644 --- a/lib/therons_erp/application.ex +++ b/lib/therons_erp/application.ex @@ -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 diff --git a/mix.exs b/mix.exs index a784c5f..b069541 100644 --- a/mix.exs +++ b/mix.exs @@ -77,7 +77,9 @@ defmodule TheronsErp.MixProject do {:seqfuzz, "~> 0.2.0"}, {:igniter, "~> 0.5", only: [:dev, :test]}, {:petal_components, "~> 2.8"}, - {:credo, "~> 1.7"} + {:credo, "~> 1.7"}, + {:ex_machina, "~> 2.8.0", only: :test}, + {:faker, "~> 0.18.0", only: :test} ] end diff --git a/mix.lock b/mix.lock index 3f52e1b..e28299f 100644 --- a/mix.lock +++ b/mix.lock @@ -33,15 +33,17 @@ "ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.16.3", "1ec6444b5d0c0aabba5a3bc321d73f1c9c751c6add92e7fb7775ccc071d96bd8", [:mix], [{:ex_cldr, "~> 2.38", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "4d1b5f8449fdf0ece6a2e5c7401ad8fcfde77ee6ea480bddc16e266dfa2b570c"}, "ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.33.5", "6cd39c549019562f83349d5aba1dcaa40d8ea36ed0e92b0d03dff65231a2eb40", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:digital_token, "~> 0.3 or ~> 1.0", [hex: :digital_token, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.38", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.16", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "1f0baf18ebdec2016018544fc92c8280722e7a6c90aad08b047b00d55594c9b4"}, "ex_doc": {:hex, :ex_doc, "0.22.1", "9bb6d51508778193a4ea90fa16eac47f8b67934f33f8271d5e1edec2dc0eee4c", [:mix], [{:earmark, "~> 1.4.0", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "d957de1b75cb9f78d3ee17820733dc4460114d8b1e11f7ee4fd6546e69b1db60"}, + "ex_machina": {:hex, :ex_machina, "2.8.0", "a0e847b5712065055ec3255840e2c78ef9366634d62390839d4880483be38abe", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "79fe1a9c64c0c1c1fab6c4fa5d871682cb90de5885320c187d117004627a7729"}, "ex_money": {:hex, :ex_money, "5.19.1", "1a404ce5d6dd99a4330a8da7c8416dcebd83863130498b5c7594e73300e31074", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ex_cldr_numbers, "~> 2.33", [hex: :ex_cldr_numbers, repo: "hexpm", optional: false]}, {:gringotts, "~> 1.1", [hex: :gringotts, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.0 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:poison, "~> 3.0 or ~> 4.0 or ~> 5.0 or ~> 6.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "3195892e004cae23b9af1f123bc2c4219ff6f45e19a59cbbfa42d6500742bd1d"}, "ex_money_sql": {:hex, :ex_money_sql, "1.11.0", "1b9b2f920d5d9220fa6dd4d8aae258daf562deaed2fb037b38b1f7ba4d0a344c", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ex_money, "~> 5.7", [hex: :ex_money, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.15", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "629e0541ae9f87122d34650f8c8febbc7349bbc6f881cf7a51b4d0779886107d"}, "expo": {:hex, :expo, "1.1.0", "f7b9ed7fb5745ebe1eeedf3d6f29226c5dd52897ac67c0f8af62a07e661e5c75", [:mix], [], "hexpm", "fbadf93f4700fb44c331362177bdca9eeb8097e8b0ef525c9cc501cb9917c960"}, + "faker": {:hex, :faker, "0.18.0", "943e479319a22ea4e8e39e8e076b81c02827d9302f3d32726c5bf82f430e6e14", [:mix], [], "hexpm", "bfbdd83958d78e2788e99ec9317c4816e651ad05e24cfd1196ce5db5b3e81797"}, "file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"}, "finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"}, "floki": {:hex, :floki, "0.37.0", "b83e0280bbc6372f2a403b2848013650b16640cd2470aea6701f0632223d719e", [:mix], [], "hexpm", "516a0c15a69f78c47dc8e0b9b3724b29608aa6619379f91b1ffa47109b5d0dd3"}, "gettext": {:hex, :gettext, "0.26.2", "5978aa7b21fada6deabf1f6341ddba50bc69c999e812211903b169799208f2a8", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "aa978504bcf76511efdc22d580ba08e2279caab1066b76bb9aa81c4a1e0a32a5"}, "glob_ex": {:hex, :glob_ex, "0.1.11", "cb50d3f1ef53f6ca04d6252c7fde09fd7a1cf63387714fe96f340a1349e62c93", [:mix], [], "hexpm", "342729363056e3145e61766b416769984c329e4378f1d558b63e341020525de4"}, - "heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized"]}, + "heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized", depth: 1]}, "hpax": {:hex, :hpax, "1.0.2", "762df951b0c399ff67cc57c3995ec3cf46d696e41f0bba17da0518d94acd4aac", [:mix], [], "hexpm", "2f09b4c1074e0abd846747329eaa26d535be0eb3d189fa69d812bfb8bfefd32f"}, "igniter": {:hex, :igniter, "0.5.20", "f95227c1cc1e9ee21151d670a3f1bce2151fb2cd7a81fec85a3b832f6ba0d866", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:inflex, "~> 2.0", [hex: :inflex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:owl, "~> 0.11", [hex: :owl, repo: "hexpm", optional: false]}, {:phx_new, "~> 1.7", [hex: :phx_new, repo: "hexpm", optional: true]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}, {:rewrite, ">= 1.1.1 and < 2.0.0-0", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "fbc48dfb15ce7cf35e2e688d969ebbaad7ca3291a21a297879169370cc8efef4"}, "inflex": {:hex, :inflex, "2.1.0", "a365cf0821a9dacb65067abd95008ca1b0bb7dcdd85ae59965deef2aa062924c", [:mix], [], "hexpm", "14c17d05db4ee9b6d319b0bff1bdf22aa389a25398d1952c7a0b5f3d93162dd8"}, diff --git a/test/support/factory.ex b/test/support/factory.ex new file mode 100644 index 0000000..ebe7c4a --- /dev/null +++ b/test/support/factory.ex @@ -0,0 +1,26 @@ +defmodule TheronsErp.Factory do + use ExMachina + + def product_factory() do + %TheronsErp.Inventory.Product{ + id: Faker.UUID.v4(), + name: Faker.Commerce.En.product_name(), + identifier: Faker.random_between(1, 10_000_000_000), + sales_price: Money.new(500, :USD), + type: TheronsErp.Inventory.Product.Types.values() |> Enum.random(), + inserted_at: Faker.DateTime.backward(7), + updated_at: Faker.DateTime.backward(7) + } + end + + def product_category_factory() do + %TheronsErp.Inventory.ProductCategory{ + id: Faker.UUID.v4(), + name: Faker.Commerce.En.product_name(), + product_category_id: Faker.UUID.v4(), + full_name: Faker.Commerce.En.product_name(), + inserted_at: Faker.DateTime.backward(7), + updated_at: Faker.DateTime.backward(7) + } + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 3646b47..054d160 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,2 +1,4 @@ +{:ok, _} = Application.ensure_all_started(:ex_machina) + ExUnit.start() Ecto.Adapters.SQL.Sandbox.mode(TheronsErp.Repo, :manual) diff --git a/test/therons_erp/inventory/product_test.exs b/test/therons_erp/inventory/product_test.exs new file mode 100644 index 0000000..ccbca70 --- /dev/null +++ b/test/therons_erp/inventory/product_test.exs @@ -0,0 +1,8 @@ +defmodule TheronsErp.Inventory.ProductTest do + use ExUnit.Case + + test "can make a product" do + TheronsErp.Inventory.Product + |> Ash.Changeset.for_create(:create) + end +end diff --git a/test/therons_erp_web/live/product_category_live_test.exs b/test/therons_erp_web/live/product_category_live_test.exs index af930bd..20cf684 100644 --- a/test/therons_erp_web/live/product_category_live_test.exs +++ b/test/therons_erp_web/live/product_category_live_test.exs @@ -1,3 +1,122 @@ defmodule TheronsErpWeb.ProductCategoryLiveTest do use TheronsErpWeb.ConnCase + + import Phoenix.LiveViewTest + import TheronsErp.Factory + import TheronsErp.InventoryFixtures + + @create_attrs %{name: "some name"} + @update_attrs %{name: "some updated name"} + @invalid_attrs %{name: nil} + + defp create_product_category(_) do + product_category = build(:product_category) + %{product_category: product_category} + end + + describe "Index" do + setup [:create_product_category] + + test "lists all product_categories", %{conn: conn, product_category: product_category} do + {:ok, _index_live, html} = live(conn, ~p"/product_categories") + + assert html =~ "Listing Product categories" + assert html =~ product_category.name + end + + test "saves new product_category", %{conn: conn} do + {:ok, index_live, _html} = live(conn, ~p"/product_categories") + + assert index_live |> element("a", "New Product category") |> render_click() =~ + "New Product category" + + assert_patch(index_live, ~p"/product_categories/new") + + assert index_live + |> form("#product_category-form", product_category: @invalid_attrs) + |> render_change() =~ "can't be blank" + + assert index_live + |> form("#product_category-form", product_category: @create_attrs) + |> render_submit() + + assert_patch(index_live, ~p"/product_categories") + + html = render(index_live) + assert html =~ "Product category created successfully" + assert html =~ "some name" + end + + test "updates product_category in listing", %{conn: conn, product_category: product_category} do + {:ok, index_live, _html} = live(conn, ~p"/product_categories") + + assert index_live + |> element("#product_categories-#{product_category.id} a", "Edit") + |> render_click() =~ + "Edit Product category" + + assert_patch(index_live, ~p"/product_categories/#{product_category}/edit") + + assert index_live + |> form("#product_category-form", product_category: @invalid_attrs) + |> render_change() =~ "can't be blank" + + assert index_live + |> form("#product_category-form", product_category: @update_attrs) + |> render_submit() + + assert_patch(index_live, ~p"/product_categories") + + html = render(index_live) + assert html =~ "Product category updated successfully" + assert html =~ "some updated name" + end + + test "deletes product_category in listing", %{conn: conn, product_category: product_category} do + {:ok, index_live, _html} = live(conn, ~p"/product_categories") + + assert index_live + |> element("#product_categories-#{product_category.id} a", "Delete") + |> render_click() + + refute has_element?(index_live, "#product_categories-#{product_category.id}") + end + end + + describe "Show" do + setup [:create_product_category] + + test "displays product_category", %{conn: conn, product_category: product_category} do + {:ok, _show_live, html} = live(conn, ~p"/product_categories/#{product_category}") + + assert html =~ "Show Product category" + assert html =~ product_category.name + end + + test "updates product_category within modal", %{ + conn: conn, + product_category: product_category + } do + {:ok, show_live, _html} = live(conn, ~p"/product_categories/#{product_category}") + + assert show_live |> element("a", "Edit") |> render_click() =~ + "Edit Product category" + + assert_patch(show_live, ~p"/product_categories/#{product_category}/show/edit") + + assert show_live + |> form("#product_category-form", product_category: @invalid_attrs) + |> render_change() =~ "can't be blank" + + assert show_live + |> form("#product_category-form", product_category: @update_attrs) + |> render_submit() + + assert_patch(show_live, ~p"/product_categories/#{product_category}") + + html = render(show_live) + assert html =~ "Product category updated successfully" + assert html =~ "some updated name" + end + end end diff --git a/test/therons_erp_web/live/product_live_test.exs b/test/therons_erp_web/live/product_live_test.exs index 42fce11..4275bc9 100644 --- a/test/therons_erp_web/live/product_live_test.exs +++ b/test/therons_erp_web/live/product_live_test.exs @@ -1,3 +1,114 @@ defmodule TheronsErpWeb.ProductLiveTest do use TheronsErpWeb.ConnCase + + import Phoenix.LiveViewTest + import TheronsErp.Factory + import TheronsErp.InventoryFixtures + + @create_attrs %{name: "some name", tags: ["option1", "option2"]} + @update_attrs %{name: "some updated name", tags: ["option1"]} + @invalid_attrs %{name: nil, tags: []} + + defp create_product(_) do + product = build(:product) + %{product: product} + end + + describe "Index" do + setup [:create_product] + + test "lists all products", %{conn: conn, product: product} do + {:ok, _index_live, html} = live(conn, ~p"/products") + + assert html =~ "Listing Products" + assert html =~ product.name + end + + test "saves new product", %{conn: conn} do + {:ok, index_live, _html} = live(conn, ~p"/products") + + assert index_live |> element("a", "New Product") |> render_click() =~ + "New Product" + + assert_patch(index_live, ~p"/products/new") + + assert index_live + |> form("#product-form", product: @invalid_attrs) + |> render_change() =~ "can't be blank" + + assert index_live + |> form("#product-form", product: @create_attrs) + |> render_submit() + + assert_patch(index_live, ~p"/products") + + html = render(index_live) + assert html =~ "Product created successfully" + assert html =~ "some name" + end + + test "updates product in listing", %{conn: conn, product: product} do + {:ok, index_live, _html} = live(conn, ~p"/products") + + assert index_live |> element("#products-#{product.id} a", "Edit") |> render_click() =~ + "Edit Product" + + assert_patch(index_live, ~p"/products/#{product}/edit") + + assert index_live + |> form("#product-form", product: @invalid_attrs) + |> render_change() =~ "can't be blank" + + assert index_live + |> form("#product-form", product: @update_attrs) + |> render_submit() + + assert_patch(index_live, ~p"/products") + + html = render(index_live) + assert html =~ "Product updated successfully" + assert html =~ "some updated name" + end + + test "deletes product in listing", %{conn: conn, product: product} do + {:ok, index_live, _html} = live(conn, ~p"/products") + + assert index_live |> element("#products-#{product.id} a", "Delete") |> render_click() + refute has_element?(index_live, "#products-#{product.id}") + end + end + + describe "Show" do + setup [:create_product] + + test "displays product", %{conn: conn, product: product} do + {:ok, _show_live, html} = live(conn, ~p"/products/#{product}") + + assert html =~ "Show Product" + assert html =~ product.name + end + + test "updates product within modal", %{conn: conn, product: product} do + {:ok, show_live, _html} = live(conn, ~p"/products/#{product}") + + assert show_live |> element("a", "Edit") |> render_click() =~ + "Edit Product" + + assert_patch(show_live, ~p"/products/#{product}/show/edit") + + assert show_live + |> form("#product-form", product: @invalid_attrs) + |> render_change() =~ "can't be blank" + + assert show_live + |> form("#product-form", product: @update_attrs) + |> render_submit() + + assert_patch(show_live, ~p"/products/#{product}") + + html = render(show_live) + assert html =~ "Product updated successfully" + assert html =~ "some updated name" + end + end end From 5ca4af5f8d992ee4557a4f079910a992fc614e8a Mon Sep 17 00:00:00 2001 From: Nathan Casados Date: Mon, 17 Feb 2025 10:25:56 -0700 Subject: [PATCH 2/2] WIP on nc/updating-tests --- test/support/factory.ex | 3 ++- test/therons_erp_web/live/product_live_test.exs | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/test/support/factory.ex b/test/support/factory.ex index ebe7c4a..d08ee38 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -1,5 +1,5 @@ defmodule TheronsErp.Factory do - use ExMachina + use ExMachina.Ecto, repo: TheronsErp.Repo def product_factory() do %TheronsErp.Inventory.Product{ @@ -11,6 +11,7 @@ defmodule TheronsErp.Factory do inserted_at: Faker.DateTime.backward(7), updated_at: Faker.DateTime.backward(7) } + |> AshPostgres.DataLayer.to_ecto() end def product_category_factory() do diff --git a/test/therons_erp_web/live/product_live_test.exs b/test/therons_erp_web/live/product_live_test.exs index 4275bc9..cb67880 100644 --- a/test/therons_erp_web/live/product_live_test.exs +++ b/test/therons_erp_web/live/product_live_test.exs @@ -10,14 +10,14 @@ defmodule TheronsErpWeb.ProductLiveTest do @invalid_attrs %{name: nil, tags: []} defp create_product(_) do - product = build(:product) + product = insert(:product) %{product: product} end describe "Index" do setup [:create_product] - test "lists all products", %{conn: conn, product: product} do + test "shows product page", %{conn: conn, product: product} do {:ok, _index_live, html} = live(conn, ~p"/products") assert html =~ "Listing Products" @@ -25,7 +25,9 @@ defmodule TheronsErpWeb.ProductLiveTest do end test "saves new product", %{conn: conn} do - {:ok, index_live, _html} = live(conn, ~p"/products") + {:ok, index_live, html} = live(conn, ~p"/products") + + dbg(html) assert index_live |> element("a", "New Product") |> render_click() =~ "New Product"