Skip to content

Commit 23dd650

Browse files
committed
test: test improvements, regression test
1 parent 2b3b6b0 commit 23dd650

File tree

11 files changed

+338
-2
lines changed

11 files changed

+338
-2
lines changed

lib/aggregate.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule AshPostgres.Aggregate do
22
@moduledoc false
33

44
require Ecto.Query
5+
import Ecto.Query, only: [from: 2]
56

67
@next_aggregate_names Enum.reduce(0..999, %{}, fn i, acc ->
78
Map.put(acc, :"aggregate_#{i}", :"aggregate_#{i + 1}")
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"attributes": [
3+
{
4+
"default": "fragment(\"uuid_generate_v4()\")",
5+
"size": null,
6+
"type": "uuid",
7+
"source": "id",
8+
"references": null,
9+
"allow_nil?": false,
10+
"generated?": false,
11+
"primary_key?": true
12+
},
13+
{
14+
"default": "nil",
15+
"size": null,
16+
"type": "uuid",
17+
"source": "post_id",
18+
"references": {
19+
"name": "post_followers_post_id_fkey",
20+
"table": "posts",
21+
"schema": "public",
22+
"on_delete": null,
23+
"multitenancy": {
24+
"global": null,
25+
"attribute": null,
26+
"strategy": null
27+
},
28+
"primary_key?": true,
29+
"destination_attribute": "id",
30+
"deferrable": false,
31+
"match_type": null,
32+
"match_with": null,
33+
"on_update": null,
34+
"destination_attribute_default": null,
35+
"destination_attribute_generated": null
36+
},
37+
"allow_nil?": false,
38+
"generated?": false,
39+
"primary_key?": false
40+
},
41+
{
42+
"default": "nil",
43+
"size": null,
44+
"type": "uuid",
45+
"source": "user_id",
46+
"references": {
47+
"name": "post_followers_user_id_fkey",
48+
"table": "users",
49+
"schema": "public",
50+
"on_delete": null,
51+
"multitenancy": {
52+
"global": null,
53+
"attribute": null,
54+
"strategy": null
55+
},
56+
"primary_key?": true,
57+
"destination_attribute": "id",
58+
"deferrable": false,
59+
"match_type": null,
60+
"match_with": null,
61+
"on_update": null,
62+
"destination_attribute_default": null,
63+
"destination_attribute_generated": null
64+
},
65+
"allow_nil?": false,
66+
"generated?": false,
67+
"primary_key?": false
68+
}
69+
],
70+
"table": "post_followers",
71+
"hash": "FEC22A5082958637A5B8D470E36E718103BE57900FA75735A30D4D2AE2055923",
72+
"repo": "Elixir.AshPostgres.TestRepo",
73+
"identities": [],
74+
"schema": null,
75+
"check_constraints": [],
76+
"custom_indexes": [],
77+
"base_filter": null,
78+
"multitenancy": {
79+
"global": null,
80+
"attribute": null,
81+
"strategy": null
82+
},
83+
"custom_statements": [],
84+
"has_create_action": false
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"attributes": [
3+
{
4+
"default": "fragment(\"uuid_generate_v4()\")",
5+
"size": null,
6+
"type": "uuid",
7+
"source": "id",
8+
"references": null,
9+
"allow_nil?": false,
10+
"primary_key?": true,
11+
"generated?": false
12+
},
13+
{
14+
"default": "nil",
15+
"size": null,
16+
"type": "uuid",
17+
"source": "post_id",
18+
"references": {
19+
"name": "post_followers_post_id_fkey",
20+
"table": "posts",
21+
"destination_attribute": "id",
22+
"primary_key?": true,
23+
"schema": "public",
24+
"multitenancy": {
25+
"global": null,
26+
"attribute": null,
27+
"strategy": null
28+
},
29+
"on_delete": null,
30+
"on_update": null,
31+
"deferrable": false,
32+
"match_with": null,
33+
"match_type": null,
34+
"destination_attribute_default": null,
35+
"destination_attribute_generated": null
36+
},
37+
"allow_nil?": false,
38+
"primary_key?": false,
39+
"generated?": false
40+
},
41+
{
42+
"default": "nil",
43+
"size": null,
44+
"type": "uuid",
45+
"source": "follower_id",
46+
"references": {
47+
"name": "post_followers_follower_id_fkey",
48+
"table": "users",
49+
"destination_attribute": "id",
50+
"primary_key?": true,
51+
"schema": "public",
52+
"multitenancy": {
53+
"global": null,
54+
"attribute": null,
55+
"strategy": null
56+
},
57+
"on_delete": null,
58+
"on_update": null,
59+
"deferrable": false,
60+
"match_with": null,
61+
"match_type": null,
62+
"destination_attribute_default": null,
63+
"destination_attribute_generated": null
64+
},
65+
"allow_nil?": false,
66+
"primary_key?": false,
67+
"generated?": false
68+
}
69+
],
70+
"table": "post_followers",
71+
"hash": "B143CF0FE750DA20BBC417283D1B23E330E5E180B0861473597B75CEDC8FA0B4",
72+
"repo": "Elixir.AshPostgres.TestRepo",
73+
"check_constraints": [],
74+
"custom_indexes": [],
75+
"base_filter": null,
76+
"identities": [],
77+
"schema": null,
78+
"multitenancy": {
79+
"global": null,
80+
"attribute": null,
81+
"strategy": null
82+
},
83+
"custom_statements": [],
84+
"has_create_action": false
85+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
defmodule AshPostgres.TestRepo.Migrations.MigrateResources17 do
2+
@moduledoc """
3+
Updates resources based on their most recent snapshots.
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
create table(:post_followers, primary_key: false) do
12+
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
13+
14+
add :post_id,
15+
references(:posts,
16+
column: :id,
17+
name: "post_followers_post_id_fkey",
18+
type: :uuid,
19+
prefix: "public"
20+
),
21+
null: false
22+
23+
add :user_id,
24+
references(:users,
25+
column: :id,
26+
name: "post_followers_user_id_fkey",
27+
type: :uuid,
28+
prefix: "public"
29+
),
30+
null: false
31+
end
32+
end
33+
34+
def down do
35+
drop constraint(:post_followers, "post_followers_post_id_fkey")
36+
37+
drop constraint(:post_followers, "post_followers_user_id_fkey")
38+
39+
drop table(:post_followers)
40+
end
41+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule AshPostgres.TestRepo.Migrations.MigrateResources18 do
2+
@moduledoc """
3+
Updates resources based on their most recent snapshots.
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
rename table(:post_followers), :user_id, to: :follower_id
12+
13+
drop constraint(:post_followers, "post_followers_user_id_fkey")
14+
15+
alter table(:post_followers) do
16+
modify :follower_id,
17+
references(:users,
18+
column: :id,
19+
name: "post_followers_follower_id_fkey",
20+
type: :uuid,
21+
prefix: "public"
22+
)
23+
end
24+
25+
execute(
26+
"ALTER TABLE post_followers alter CONSTRAINT post_followers_follower_id_fkey NOT DEFERRABLE"
27+
)
28+
end
29+
30+
def down do
31+
drop constraint(:post_followers, "post_followers_follower_id_fkey")
32+
33+
alter table(:post_followers) do
34+
modify :user_id,
35+
references(:users,
36+
column: :id,
37+
name: "post_followers_user_id_fkey",
38+
type: :uuid,
39+
prefix: "public"
40+
)
41+
end
42+
43+
rename table(:post_followers), :follower_id, to: :user_id
44+
end
45+
end

test/calculation_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,20 @@ defmodule AshPostgres.CalculationTest do
724724
|> Api.read_one!()
725725
end
726726

727+
test "exists with a relationship that has a filtered read action works" do
728+
post =
729+
Post
730+
|> Ash.Changeset.for_create(:create, %{})
731+
|> Api.create!()
732+
733+
post_id = post.id
734+
735+
assert [%{id: ^post_id}] =
736+
Post
737+
|> Ash.Query.filter(has_no_followers)
738+
|> Api.read!()
739+
end
740+
727741
def fred do
728742
"fred"
729743
end

test/subquery_test.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ defmodule AshPostgres.SubqueryTest do
1313

1414
Through.create(%{parent_id: parent.id, child_id: child.id})
1515

16-
assert {:ok, _} =
17-
Child.read(actor: %{email: "foo@bar.com"})
16+
Child.read!(actor: %{email: "foo@bar.com"})
1817
end
1918
end

test/support/registry.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ defmodule AshPostgres.Test.Registry do
1818
entry(AshPostgres.Test.Entity)
1919
entry(AshPostgres.Test.TempEntity)
2020
entry(AshPostgres.Test.Record)
21+
entry(AshPostgres.Test.PostFollower)
2122
end
2223
end

test/support/resources/post.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ defmodule AshPostgres.Test.Post do
208208
destination_attribute_on_join_resource: :destination_post_id
209209
)
210210

211+
many_to_many(:followers, AshPostgres.Test.User,
212+
through: AshPostgres.Test.PostFollower,
213+
source_attribute_on_join_resource: :post_id,
214+
destination_attribute_on_join_resource: :follower_id,
215+
read_action: :active
216+
)
217+
211218
has_many(:views, AshPostgres.Test.PostView)
212219
end
213220

@@ -216,12 +223,35 @@ defmodule AshPostgres.Test.Post do
216223
end
217224

218225
calculations do
226+
calculate(
227+
:author_has_post_with_follower_named_fred,
228+
:boolean,
229+
expr(
230+
exists(
231+
author.posts,
232+
has_follower_named_fred
233+
)
234+
)
235+
)
236+
237+
calculate(
238+
:has_no_followers,
239+
:boolean,
240+
expr(is_nil(author.posts.followers))
241+
)
242+
219243
calculate(:score_after_winning, :integer, expr((score || 0) + 1))
220244
calculate(:negative_score, :integer, expr(-score))
221245
calculate(:category_label, :ci_string, expr("(" <> category <> ")"))
222246
calculate(:score_with_score, :string, expr(score <> score))
223247
calculate(:foo_bar_from_stuff, :string, expr(stuff[:foo][:bar]))
224248

249+
calculate(
250+
:has_follower_named_fred,
251+
:boolean,
252+
expr(exists(followers, name == "fred"))
253+
)
254+
225255
calculate(
226256
:composite_origin,
227257
AshPostgres.Test.CompositePoint,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule AshPostgres.Test.PostFollower do
2+
use Ash.Resource,
3+
data_layer: AshPostgres.DataLayer
4+
5+
postgres do
6+
table "post_followers"
7+
repo AshPostgres.TestRepo
8+
end
9+
10+
actions do
11+
defaults([:create, :read, :update, :destroy])
12+
end
13+
14+
attributes do
15+
uuid_primary_key(:id)
16+
end
17+
18+
relationships do
19+
belongs_to :post, AshPostgres.Test.Post do
20+
allow_nil?(false)
21+
end
22+
23+
belongs_to :follower, AshPostgres.Test.User do
24+
allow_nil?(false)
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)