Skip to content

Commit bb2cc07

Browse files
committed
Add test for ash.gen.resources task
1 parent fcbcbd7 commit bb2cc07

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
defmodule Mix.Tasks.Ash.Gen.ResourcesTest do
2+
use ExUnit.Case
3+
import Igniter.Test
4+
5+
@moduletag :igniter
6+
7+
test "generates complete blog system with different resource configurations" do
8+
test_project()
9+
|> Igniter.compose_task("ash.gen.resources", [
10+
"MyApp.Blog.Post --uuid-primary-key id --attribute title:string:required:public,body:text:public,status:atom --relationship belongs_to:author:MyApp.Accounts.User:required --default-actions read,create,update --timestamps;MyApp.Blog.Comment --attribute content:text:required:public --relationship belongs_to:post:MyApp.Blog.Post:required,belongs_to:author:MyApp.Accounts.User:required --default-actions read,create;MyApp.Accounts.User --uuid-primary-key id --attribute name:string:required:public,email:string:required:public --default-actions read,create,update"
11+
])
12+
|> assert_creates("lib/my_app/blog/post.ex", """
13+
defmodule MyApp.Blog.Post do
14+
use Ash.Resource,
15+
otp_app: :test,
16+
domain: MyApp.Blog
17+
18+
attributes do
19+
uuid_primary_key(:id)
20+
21+
attribute :title, :string do
22+
allow_nil?(false)
23+
public?(true)
24+
end
25+
26+
attribute :body, :text do
27+
public?(true)
28+
end
29+
30+
attribute(:status, :atom)
31+
timestamps()
32+
end
33+
34+
relationships do
35+
belongs_to :author, MyApp.Accounts.User do
36+
allow_nil?(false)
37+
end
38+
end
39+
40+
actions do
41+
defaults([:read, create: [:title, :body], update: [:title, :body]])
42+
end
43+
end
44+
""")
45+
|> assert_creates("lib/my_app/blog/comment.ex", """
46+
defmodule MyApp.Blog.Comment do
47+
use Ash.Resource,
48+
otp_app: :test,
49+
domain: MyApp.Blog
50+
51+
attributes do
52+
attribute :content, :text do
53+
allow_nil?(false)
54+
public?(true)
55+
end
56+
end
57+
58+
relationships do
59+
belongs_to :post, MyApp.Blog.Post do
60+
allow_nil?(false)
61+
end
62+
63+
belongs_to :author, MyApp.Accounts.User do
64+
allow_nil?(false)
65+
end
66+
end
67+
68+
actions do
69+
defaults([:read, create: [:content]])
70+
end
71+
end
72+
""")
73+
|> assert_creates("lib/my_app/accounts/user.ex", """
74+
defmodule MyApp.Accounts.User do
75+
use Ash.Resource,
76+
otp_app: :test,
77+
domain: MyApp.Accounts
78+
79+
attributes do
80+
uuid_primary_key(:id)
81+
82+
attribute :name, :string do
83+
allow_nil?(false)
84+
public?(true)
85+
end
86+
87+
attribute :email, :string do
88+
allow_nil?(false)
89+
public?(true)
90+
end
91+
end
92+
93+
actions do
94+
defaults([:read, create: [:name, :email], update: [:name, :email]])
95+
end
96+
end
97+
""")
98+
|> assert_creates("lib/my_app/blog.ex", """
99+
defmodule MyApp.Blog do
100+
use Ash.Domain,
101+
otp_app: :test
102+
103+
resources do
104+
resource(MyApp.Blog.Post)
105+
resource(MyApp.Blog.Comment)
106+
end
107+
end
108+
""")
109+
|> assert_creates("lib/my_app/accounts.ex", """
110+
defmodule MyApp.Accounts do
111+
use Ash.Domain,
112+
otp_app: :test
113+
114+
resources do
115+
resource(MyApp.Accounts.User)
116+
end
117+
end
118+
""")
119+
end
120+
end

0 commit comments

Comments
 (0)