Skip to content

Commit

Permalink
feat(testing): adds Phoenix 1.7 support, adds testing, bugfixes (#6)
Browse files Browse the repository at this point in the history
* feat(testing): adds Phoenix 1.7 support, adds testing, fixes a few bugs caught by tests
  • Loading branch information
hamptokr authored Jun 22, 2023
1 parent 915816a commit 334e227
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 34 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ After running `mix phx.new`, `cd` into your application's directory (ex. `my_app
```elixir
def deps do
[
{:phx_gen_solid, "~> 0.1", only: [:dev], runtime: false}
{:phx_gen_solid, "~> 0.3", only: [:dev], runtime: false}
...
]
end
Expand Down Expand Up @@ -64,11 +64,21 @@ To override the name of the module where the helpers exist:
This will override the name used in the generated value to alias the module
given.

#### mix phx.gen.solid.handler
#### mix phx.gen.solid.service

TODO
Generates C~~R~~UD Services for a resource.

#### mix phx.gen.solid.service
$ mix phx.gen.solid.service Accounts User users id slug name

The first argument is the context module followed by the schema module and its
plural name.

This creates the following services:
- `MyApp.Accounts.Service.CreateUser`
- `MyApp.Accounts.Service.UpdateUser`
- `MyApp.Accounts.Service.DeleteUser`

#### mix phx.gen.solid.handler

TODO

Expand Down
6 changes: 4 additions & 2 deletions lib/mix/tasks/phx.gen.solid.service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ defmodule Mix.Tasks.Phx.Gen.Solid.Service do
@moduledoc """
Generates C~~R~~UD Services for a resource.
mix phx.gen.solid.value Accounts User users
mix phx.gen.solid.service Accounts User users
The first argument is the context module followed by the schema module and its
plural name.
This creates the following services:
This creates the following services:
- `MyApp.Accounts.Service.CreateUser`
- `MyApp.Accounts.Service.UpdateUser`
- `MyApp.Accounts.Service.DeleteUser`
Expand Down Expand Up @@ -51,6 +51,8 @@ defmodule Mix.Tasks.Phx.Gen.Solid.Service do
Generator.copy_new_files(context, binding, paths, &files_to_be_generated/1)
end

def raise_with_help(msg), do: Generator.raise_with_help(msg)

defp files_to_be_generated(%Context{schema: schema} = context) do
[
{:eex, "service_create.ex",
Expand Down
2 changes: 2 additions & 0 deletions lib/mix/tasks/phx.gen.solid.value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ defmodule Mix.Tasks.Phx.Gen.Solid.Value do
Generator.copy_new_files(context, binding, paths, &files_to_be_generated/2, opts)
end

def raise_with_help(msg), do: Generator.raise_with_help(msg)

defp build_value_context(context, opts) do
default_context = Module.concat([context.base_module, "Value"])

Expand Down
18 changes: 18 additions & 0 deletions lib/phx_gen_solid/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ defmodule PhxGenSolid.Generator do
|> files_fn.(opts)
|> Mix.Phoenix.prompt_for_conflicts()
end

def raise_with_help(msg) do
Mix.raise("""
#{msg}
mix phx.gen.solid.service and phx.gen.solid.value expect a context module
name, followed by a singular and plural name of the generated resource,
ending with any number of attributes.
For example:
mix phx.gen.solid.service Accounts User users name:string
mix phx.gen.solid.value Accounts User users name:string
The context serves as the API boundary for the given resource. Multiple
resources may belong to a context and a resource may be split over distinct
contexts (such as Accounts.User and Payments.User).
""")
end
end
10 changes: 5 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule PhxGenSolid.MixProject do
use Mix.Project

@version "0.2.0"
@version "0.3.0"

def project do
[
app: :phx_gen_solid,
version: @version,
description: "A SOLID generator for Phoenix 1.6 applications",
description: "A SOLID generator for Phoenix 1.7 applications",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand All @@ -26,10 +26,10 @@ defmodule PhxGenSolid.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:phoenix, "~> 1.6.2"},
{:phx_new, "~> 1.6.6", only: [:dev, :test]},
{:phoenix, "~> 1.7.6"},
{:phx_new, "~> 1.7.6", only: [:dev, :test]},
# Docs
{:ex_doc, "~> 0.28", only: :dev, runtime: false}
{:ex_doc, "~> 0.29.4", only: :dev, runtime: false}
]
end

Expand Down
32 changes: 18 additions & 14 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
%{
"earmark_parser": {:hex, :earmark_parser, "1.4.19", "de0d033d5ff9fc396a24eadc2fcf2afa3d120841eb3f1004d138cbf9273210e8", [:mix], [], "hexpm", "527ab6630b5c75c3a3960b75844c314ec305c76d9899bb30f71cb85952a9dc45"},
"ex_doc": {:hex, :ex_doc, "0.28.0", "7eaf526dd8c80ae8c04d52ac8801594426ae322b52a6156cd038f30bafa8226f", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e55cdadf69a5d1f4cfd8477122ebac5e1fadd433a8c1022dafc5025e48db0131"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.1", "264fc6864936b59fedb3ceb89998c64e9bb91945faf1eb115d349b96913cc2ef", [:mix], [], "hexpm", "23c31d0ec38c97bf9adde35bc91bc8e1181ea5202881f48a192f4aa2d2cf4d59"},
"phoenix": {:hex, :phoenix, "1.6.6", "281c8ce8dccc9f60607346b72cdfc597c3dde134dd9df28dff08282f0b751754", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "807bd646e64cd9dc83db016199715faba72758e6db1de0707eef0a2da4924364"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"},
"phoenix_view": {:hex, :phoenix_view, "1.1.1", "11a945509d1270ef42bb19ca8f609aab378fc02543c2d4bdb8d243f1ca88f9b5", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "1f8148a9163ca4b40065ef177c9c56c0df174cf3596a27995a1a1657f0c949e7"},
"phx_new": {:hex, :phx_new, "1.6.6", "e759dc5e204f7e4a2e7ed1239910a7cfc7f24defad6d9a5c73f1fd002a84e9d9", [:mix], [], "hexpm", "fa86a24bd6e7b28412829e9c5fbf390c306b88f8268086b06b833a9c816ee994"},
"plug": {:hex, :plug, "1.12.1", "645678c800601d8d9f27ad1aebba1fdb9ce5b2623ddb961a074da0b96c35187d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d57e799a777bc20494b784966dc5fbda91eb4a09f571f76545b72a634ce0d30b"},
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
"castore": {:hex, :castore, "1.0.3", "7130ba6d24c8424014194676d608cb989f62ef8039efd50ff4b3f33286d06db8", [:mix], [], "hexpm", "680ab01ef5d15b161ed6a95449fac5c6b8f60055677a8e79acf01b27baa4390b"},
"earmark_parser": {:hex, :earmark_parser, "1.4.32", "fa739a0ecfa34493de19426681b23f6814573faee95dfd4b4aafe15a7b5b32c6", [:mix], [], "hexpm", "b8b0dd77d60373e77a3d7e8afa598f325e49e8663a51bcc2b88ef41838cca755"},
"ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"phoenix": {:hex, :phoenix, "1.7.6", "61f0625af7c1d1923d582470446de29b008c0e07ae33d7a3859ede247ddaf59a", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "f6b4be7780402bb060cbc6e83f1b6d3f5673b674ba73cc4a7dd47db0322dfb88"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
"phoenix_template": {:hex, :phoenix_template, "1.0.1", "85f79e3ad1b0180abb43f9725973e3b8c2c3354a87245f91431eec60553ed3ef", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "157dc078f6226334c91cb32c1865bf3911686f8bcd6bcff86736f6253e6993ee"},
"phoenix_view": {:hex, :phoenix_view, "2.0.2", "6bd4d2fd595ef80d33b439ede6a19326b78f0f1d8d62b9a318e3d9c1af351098", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}], "hexpm", "a929e7230ea5c7ee0e149ffcf44ce7cf7f4b6d2bfe1752dd7c084cdff152d36f"},
"phx_new": {:hex, :phx_new, "1.7.6", "b91ebcfa77976a18c0d0f3b9091ceab89d462f7958bea1f8a6764b516adba586", [:mix], [], "hexpm", "07aa3648f65201aa27cc8db14dee71c9e57addd94f3585af4f57d9d15d6cd2df"},
"plug": {:hex, :plug, "1.14.2", "cff7d4ec45b4ae176a227acd94a7ab536d9b37b942c8e8fa6dfc0fff98ff4d80", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "842fc50187e13cf4ac3b253d47d9474ed6c296a8732752835ce4a86acdf68d13"},
"plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"websock": {:hex, :websock, "0.5.2", "b3c08511d8d79ed2c2f589ff430bd1fe799bb389686dafce86d28801783d8351", [:mix], [], "hexpm", "925f5de22fca6813dfa980fb62fd542ec43a2d1a1f83d2caec907483fe66ff05"},
"websock_adapter": {:hex, :websock_adapter, "0.5.3", "4908718e42e4a548fc20e00e70848620a92f11f7a6add8cf0886c4232267498d", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "cbe5b814c1f86b6ea002b52dd99f345aeecf1a1a6964e209d208fb404d930d3d"},
}
6 changes: 3 additions & 3 deletions priv/templates/phx.gen.solid/service_create.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ defmodule <%= inspect service_create_module %> do

alias <%= inspect context.module %>

def call(params) do
def call(params) do
params
|> <%= inspect context.alias %>.create_<%= schema.singular %>(params)
|> handle_result()
end
end

defp handle_result(result), do: result
end
6 changes: 3 additions & 3 deletions priv/templates/phx.gen.solid/service_delete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ defmodule <%= inspect service_delete_module %> do

alias <%= inspect context.module %>

def call(params) do
def call(params) do
params
|> <%= inspect context.alias %>.delete_<%= schema.singular %>(params)
|> handle_result()
end
end

defp handle_result(result), do: result
end
6 changes: 3 additions & 3 deletions priv/templates/phx.gen.solid/service_update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ defmodule <%= inspect service_update_module %> do

alias <%= inspect context.module %>

def call(params) do
def call(params) do
params
|> <%= inspect context.alias %>.update_<%= schema.singular %>(params)
|> handle_result()
end
end

defp handle_result(result), do: result
end
89 changes: 89 additions & 0 deletions test/mix/tasks/phx.gen.solid.service_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
Code.require_file("../../mix_helper.exs", __DIR__)

defmodule Mix.Tasks.Phx.Gen.Solid.ServiceTest do
use ExUnit.Case
import MixHelper
alias Mix.Tasks.Phx.Gen.Solid

setup do
Mix.Task.clear()
:ok
end

test "invalid mix args", config do
in_tmp_project(config.test, fn ->
assert_raise Mix.Error,
~r/Expected the context, "accounts", to be a valid module name/,
fn ->
Solid.Service.run(~w(accounts User users name:string))
end

assert_raise Mix.Error, ~r/Expected the schema, "users", to be a valid module name/, fn ->
Solid.Service.run(~w(User users name:string))
end

assert_raise Mix.Error, ~r/The context and schema should have different names/, fn ->
Solid.Service.run(~w(Accounts Accounts users))
end

assert_raise Mix.Error, ~r/Invalid arguments/, fn ->
Solid.Service.run(~w(Accounts.User users))
end

assert_raise Mix.Error, ~r/Invalid arguments/, fn ->
Solid.Service.run(~w(Accounts User))
end
end)
end

test "generates create, update, delete services", config do
in_tmp_project(config.test, fn ->
Solid.Service.run(~w(Accounts User users name:string))

assert_file("lib/phx_gen_solid/accounts/services/create_user.ex", fn file ->
assert file =~ "defmodule PhxGenSolid.Accounts.Service.CreateUser"
assert file =~ "alias PhxGenSolid.Accounts"

assert file =~ """
def call(params) do
params
|> Accounts.create_user(params)
|> handle_result()
end
"""

assert file =~ "defp handle_result(result), do: result"
end)

assert_file("lib/phx_gen_solid/accounts/services/update_user.ex", fn file ->
assert file =~ "defmodule PhxGenSolid.Accounts.Service.UpdateUser"
assert file =~ "alias PhxGenSolid.Accounts"

assert file =~ """
def call(params) do
params
|> Accounts.update_user(params)
|> handle_result()
end
"""

assert file =~ "defp handle_result(result), do: result"
end)

assert_file("lib/phx_gen_solid/accounts/services/delete_user.ex", fn file ->
assert file =~ "defmodule PhxGenSolid.Accounts.Service.DeleteUser"
assert file =~ "alias PhxGenSolid.Accounts"

assert file =~ """
def call(params) do
params
|> Accounts.delete_user(params)
|> handle_result()
end
"""

assert file =~ "defp handle_result(result), do: result"
end)
end)
end
end
74 changes: 74 additions & 0 deletions test/mix/tasks/phx.gen.solid.value_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
Code.require_file("../../mix_helper.exs", __DIR__)

defmodule Mix.Tasks.Phx.Gen.Solid.ValueTest do
use ExUnit.Case
import MixHelper
alias Mix.Tasks.Phx.Gen.Solid

setup do
Mix.Task.clear()
:ok
end

test "invalid mix args", config do
in_tmp_project(config.test, fn ->
assert_raise Mix.Error,
~r/Expected the context, "accounts", to be a valid module name/,
fn ->
Solid.Value.run(~w(accounts User users name:string))
end

assert_raise Mix.Error, ~r/Expected the schema, "users", to be a valid module name/, fn ->
Solid.Value.run(~w(User users name:string))
end

assert_raise Mix.Error, ~r/The context and schema should have different names/, fn ->
Solid.Value.run(~w(Accounts Accounts users))
end

assert_raise Mix.Error, ~r/Invalid arguments/, fn ->
Solid.Value.run(~w(Accounts.User users))
end

assert_raise Mix.Error, ~r/Invalid arguments/, fn ->
Solid.Value.run(~w(Accounts User))
end
end)
end

test "generates value", config do
in_tmp_project(config.test, fn ->
Solid.Value.run(~w(Accounts User users name:string))

assert_file("lib/phx_gen_solid/accounts/values/user.ex", fn file ->
assert file =~ "defmodule PhxGenSolid.Accounts.Value.User"
assert file =~ "alias PhxGenSolid.Value"
assert file =~ "@user_fields [:name]"

assert file =~ "def build(user, user_fields \\\\ @user_fields)"
assert file =~ "def build(nil, _), do: nil"

assert file =~ """
def build(user, user_fields) do
user
|> Value.init()
|> Value.only(user_fields)
end
"""
end)

# Because we didn't pass --helpers make sure we didn't generate it
refute_file("lib/phx_gen_solid/value.ex")
end)
end

test "creates helpers when option is passed", config do
in_tmp_project(config.test, fn ->
Solid.Value.run(~w(Accounts User users name:string --helpers))

assert_file("lib/phx_gen_solid/value.ex", fn file ->
assert file =~ "defmodule PhxGenSolid.Value"
end)
end)
end
end
Loading

0 comments on commit 334e227

Please sign in to comment.