forked from ash-project/ash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo_test.exs
More file actions
229 lines (187 loc) · 5.73 KB
/
info_test.exs
File metadata and controls
229 lines (187 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
defmodule Ash.Test.Resource.InfoTest do
@moduledoc false
use ExUnit.Case, async: true
require Spark.Dsl
require Spark.Dsl.Extension
alias Ash.Resource
alias Ash.Resource.Info
alias Ash.Test.Domain, as: Domain
defmodule Post do
@moduledoc false
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :title, :string do
public?(true)
end
attribute :contents, :string do
public?(true)
end
attribute :metadata, :map do
public?(true)
end
attribute :authors, {:array, :string} do
public?(true)
end
attribute :points, :integer
end
aggregates do
count :count_of_comments, :comments do
public? true
end
end
relationships do
many_to_many :tags, Ash.Test.Resource.InfoTest.Tag do
through Ash.Test.Resource.InfoTest.TagOnPost
public?(true)
source_attribute_on_join_resource(:tag_id)
destination_attribute_on_join_resource(:post_id)
end
has_many :comments, Ash.Test.Resource.InfoTest.Comment,
destination_attribute: :post_id,
public?: true
has_one :private, Ash.Test.Resource.InfoTest.PostPrivate,
destination_attribute: :post_id,
public?: true
end
end
defmodule Tag do
@moduledoc false
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :label, :string do
public?(true)
end
end
end
defmodule TagOnPost do
@moduledoc false
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
end
relationships do
belongs_to :post, Post do
public?(true)
end
belongs_to :tag, Tag do
public?(true)
end
end
end
defmodule PostPrivate do
@moduledoc false
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :notes, :string do
public?(true)
end
end
relationships do
belongs_to :post, Post do
public?(true)
end
end
end
defmodule Comment do
@moduledoc false
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :subjects, {:array, :string} do
public?(true)
end
attribute :post_id, :uuid do
public?(true)
end
end
aggregates do
first :post_title, :post, :title do
public? true
end
first :post_authors, :post, :authors do
public? true
end
end
calculations do
calculate :formatted_post_title, :string, expr("Post title: " <> post_title),
public?: true,
load: [:post_title]
end
relationships do
belongs_to :post, Post do
public?(true)
end
end
end
describe "resource info" do
test "get only public fields" do
assert %Resource.Attribute{name: :title} = Info.public_field(Post, :title)
assert nil == Info.public_field(Post, :points)
assert [
:authors,
:comments,
:contents,
:count_of_comments,
:id,
:metadata,
:private,
:tags,
:title
] = Info.public_fields(Post) |> Enum.map(& &1.name) |> Enum.sort()
end
test "get any fields" do
assert %Resource.Attribute{name: :title} = Info.public_field(Post, :title)
assert %Resource.Attribute{name: :points} = Info.field(Post, :points)
assert [
:authors,
:comments,
:contents,
:count_of_comments,
:id,
:metadata,
:points,
:private,
:tags,
:tags_join_assoc,
:title
] = Info.fields(Post) |> Enum.map(& &1.name) |> Enum.sort()
end
test "determine if field is sortable" do
assert true == Info.sortable?(Post, :title)
assert true == Info.sortable?(Post, :points)
assert false == Info.sortable?(Post, :points, include_private?: false)
assert false == Info.sortable?(Post, :authors)
assert false == Info.sortable?(Post, :tags)
assert false == Info.sortable?(Post, :metadata)
assert false == Info.sortable?(Post, :comments)
assert false == Info.sortable?(Post, :private)
assert false == Info.sortable?(Comment, :post)
assert true == Info.sortable?(Post, :count_of_comments)
assert false == Info.sortable?(Post, :count_of_comments, pagination_type: :keyset)
assert true == Info.sortable?(Comment, :post_title)
assert true == Info.sortable?(Comment, :formatted_post_title)
assert false == Info.sortable?(Comment, :post_authors)
assert false == Info.sortable?(Comment, :formatted_post_title, pagination_type: :keyset)
end
test "get datalayer from resource" do
assert Ash.DataLayer.Ets == Info.data_layer(Post)
end
test "get relationship fields" do
assert Spark.Dsl.Extension.get_entities(Comment, [:post])
comment = Info.public_relationship(Post, [:comments]).destination
assert Spark.Dsl.Extension.get_entities(comment, [:post])
assert %Resource.Relationships.ManyToMany{name: :tags} =
Info.public_relationship(Post, :tags)
assert %Resource.Relationships.BelongsTo{name: :post} =
Info.public_relationship(Post, [:comments, :post])
end
end
describe "extensions/1" do
test "returns extensions in use by the resource" do
assert Ash.DataLayer.Ets in Info.extensions(Post)
end
end
end