Skip to content

Commit 5e3e1a0

Browse files
committed
test: add a test that shows problems with two multiple relationships
1 parent 5cd3023 commit 5e3e1a0

File tree

9 files changed

+441
-0
lines changed

9 files changed

+441
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "fragment(\"uuid_generate_v7()\")",
6+
"generated?": false,
7+
"precision": null,
8+
"primary_key?": true,
9+
"references": null,
10+
"scale": null,
11+
"size": null,
12+
"source": "id",
13+
"type": "uuid"
14+
}
15+
],
16+
"base_filter": null,
17+
"check_constraints": [],
18+
"custom_indexes": [],
19+
"custom_statements": [],
20+
"has_create_action": true,
21+
"hash": "85454A705DFC565FD634E25E9CE8FB510367A800470B9B85E0FA65494AC296D8",
22+
"identities": [],
23+
"multitenancy": {
24+
"attribute": null,
25+
"global": null,
26+
"strategy": null
27+
},
28+
"repo": "Elixir.AshPostgres.TestRepo",
29+
"schema": null,
30+
"table": "containers"
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
2+
3+
SPDX-License-Identifier: MIT
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "fragment(\"uuid_generate_v7()\")",
6+
"generated?": false,
7+
"precision": null,
8+
"primary_key?": true,
9+
"references": null,
10+
"scale": null,
11+
"size": null,
12+
"source": "id",
13+
"type": "uuid"
14+
},
15+
{
16+
"allow_nil?": true,
17+
"default": "nil",
18+
"generated?": false,
19+
"precision": null,
20+
"primary_key?": false,
21+
"references": null,
22+
"scale": null,
23+
"size": null,
24+
"source": "name",
25+
"type": "text"
26+
},
27+
{
28+
"allow_nil?": true,
29+
"default": "false",
30+
"generated?": false,
31+
"precision": null,
32+
"primary_key?": false,
33+
"references": null,
34+
"scale": null,
35+
"size": null,
36+
"source": "active",
37+
"type": "boolean"
38+
},
39+
{
40+
"allow_nil?": true,
41+
"default": "nil",
42+
"generated?": false,
43+
"precision": null,
44+
"primary_key?": false,
45+
"references": {
46+
"deferrable": false,
47+
"destination_attribute": "id",
48+
"destination_attribute_default": null,
49+
"destination_attribute_generated": null,
50+
"index?": false,
51+
"match_type": null,
52+
"match_with": null,
53+
"multitenancy": {
54+
"attribute": null,
55+
"global": null,
56+
"strategy": null
57+
},
58+
"name": "items_container_id_fkey",
59+
"on_delete": null,
60+
"on_update": null,
61+
"primary_key?": true,
62+
"schema": "public",
63+
"table": "containers"
64+
},
65+
"scale": null,
66+
"size": null,
67+
"source": "container_id",
68+
"type": "uuid"
69+
},
70+
{
71+
"allow_nil?": true,
72+
"default": "nil",
73+
"generated?": false,
74+
"precision": null,
75+
"primary_key?": false,
76+
"references": null,
77+
"scale": null,
78+
"size": null,
79+
"source": "key",
80+
"type": "text"
81+
},
82+
{
83+
"allow_nil?": true,
84+
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
85+
"generated?": false,
86+
"precision": null,
87+
"primary_key?": false,
88+
"references": null,
89+
"scale": null,
90+
"size": null,
91+
"source": "inserted_at",
92+
"type": "utc_datetime_usec"
93+
},
94+
{
95+
"allow_nil?": true,
96+
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
97+
"generated?": false,
98+
"precision": null,
99+
"primary_key?": false,
100+
"references": null,
101+
"scale": null,
102+
"size": null,
103+
"source": "updated_at",
104+
"type": "utc_datetime_usec"
105+
}
106+
],
107+
"base_filter": null,
108+
"check_constraints": [],
109+
"custom_indexes": [],
110+
"custom_statements": [],
111+
"has_create_action": true,
112+
"hash": "8BF7AAEFEDD546F7EF1E399B6267932F80A964026BD5299E54C56AC52346833C",
113+
"identities": [],
114+
"multitenancy": {
115+
"attribute": null,
116+
"global": null,
117+
"strategy": null
118+
},
119+
"repo": "Elixir.AshPostgres.TestRepo",
120+
"schema": null,
121+
"table": "items"
122+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
2+
3+
SPDX-License-Identifier: MIT
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule AshPostgres.TestRepo.Migrations.MigrateResources67 do
6+
@moduledoc """
7+
Updates resources based on their most recent snapshots.
8+
9+
This file was autogenerated with `mix ash_postgres.generate_migrations`
10+
"""
11+
12+
use Ecto.Migration
13+
14+
def up do
15+
alter table(:items) do
16+
modify(:updated_at, :utc_datetime_usec, null: true)
17+
modify(:inserted_at, :utc_datetime_usec, null: true)
18+
add(:name, :text)
19+
add(:active, :boolean, default: false)
20+
add(:container_id, :uuid)
21+
end
22+
23+
create table(:containers, primary_key: false) do
24+
add(:id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true)
25+
end
26+
27+
alter table(:items) do
28+
modify(
29+
:container_id,
30+
references(:containers,
31+
column: :id,
32+
name: "items_container_id_fkey",
33+
type: :uuid,
34+
prefix: "public"
35+
)
36+
)
37+
end
38+
end
39+
40+
def down do
41+
drop(constraint(:items, "items_container_id_fkey"))
42+
43+
alter table(:items) do
44+
modify(:container_id, :uuid)
45+
end
46+
47+
drop(table(:containers))
48+
49+
alter table(:items) do
50+
remove(:container_id)
51+
remove(:active)
52+
remove(:name)
53+
modify(:inserted_at, :utc_datetime_usec, null: false)
54+
modify(:updated_at, :utc_datetime_usec, null: false)
55+
end
56+
end
57+
end
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule AshPostgres.CalculationWithMultipleRelationshipsTest do
6+
@moduledoc """
7+
Tests for calculations using multiple relationships to the same resource
8+
with different read actions on the Postgres data layer.
9+
10+
Bug verification: When two relationships are defined to the same resource with different
11+
read actions, calculations using each relationship should respect the correct read_action.
12+
13+
This tests the same behavior as Ash.Test.CalculationWithMultipleRelationshipsTest
14+
but with the Postgres data layer to ensure SQL generation is correct.
15+
"""
16+
use AshPostgres.RepoCase, async: false
17+
18+
alias AshPostgres.Test.{Container, Item}
19+
20+
test "calculations use correct read actions from their respective relationships" do
21+
container = Ash.Seed.seed!(Container, %{})
22+
23+
item =
24+
Ash.Seed.seed!(Item, %{
25+
container_id: container.id,
26+
name: "Inactive Item",
27+
active: false
28+
})
29+
30+
loaded_container = Ash.load!(container, [:item_all])
31+
assert item.id == loaded_container.item_all.id
32+
33+
loaded_container = Ash.load!(container, [:item_active])
34+
assert nil == loaded_container.item_active
35+
36+
loaded_container = Ash.load!(container, [:active_item_name, :all_item_name])
37+
38+
assert loaded_container.active_item_name == nil
39+
40+
assert loaded_container.all_item_name == "Inactive Item"
41+
end
42+
43+
test "loading calculations one at a time works" do
44+
container = Ash.Seed.seed!(Container, %{})
45+
46+
item =
47+
Ash.Seed.seed!(Item, %{
48+
container_id: container.id,
49+
name: "Inactive Item",
50+
active: false
51+
})
52+
53+
loaded_container = Ash.load!(container, [:item_all])
54+
assert item.id == loaded_container.item_all.id
55+
56+
loaded_container = Ash.load!(container, [:item_active])
57+
assert nil == loaded_container.item_active
58+
59+
loaded_container = Ash.load!(container, [:active_item_name])
60+
assert loaded_container.active_item_name == nil
61+
62+
loaded_container = Ash.load!(container, [:all_item_name])
63+
assert loaded_container.all_item_name == "Inactive Item"
64+
end
65+
66+
test "with active item, both calculations return values" do
67+
container = Ash.Seed.seed!(Container, %{})
68+
69+
item =
70+
Ash.Seed.seed!(Item, %{
71+
container_id: container.id,
72+
name: "Active Item",
73+
active: true
74+
})
75+
76+
loaded_container = Ash.load!(container, [:item_all])
77+
assert item.id == loaded_container.item_all.id
78+
79+
loaded_container = Ash.load!(container, [:item_active])
80+
assert item.id == loaded_container.item_active.id
81+
82+
loaded_container = Ash.load!(container, [:active_item_name, :all_item_name])
83+
84+
assert loaded_container.active_item_name == "Active Item"
85+
assert loaded_container.all_item_name == "Active Item"
86+
end
87+
88+
test "multiple containers with mixed active/inactive items" do
89+
container1 = Ash.Seed.seed!(Container, %{})
90+
91+
Ash.Seed.seed!(Item, %{
92+
container_id: container1.id,
93+
name: "Inactive Item 1",
94+
active: false
95+
})
96+
97+
container2 = Ash.Seed.seed!(Container, %{})
98+
99+
Ash.Seed.seed!(Item, %{
100+
container_id: container2.id,
101+
name: "Active Item 2",
102+
active: true
103+
})
104+
105+
_container3 = Ash.Seed.seed!(Container, %{})
106+
107+
containers =
108+
Container
109+
|> Ash.Query.sort(:id)
110+
|> Ash.Query.load([:active_item_name, :all_item_name])
111+
|> Ash.read!()
112+
113+
[loaded1, loaded2, loaded3] = containers
114+
115+
assert loaded1.active_item_name == nil
116+
assert loaded1.all_item_name == "Inactive Item 1"
117+
118+
assert loaded2.active_item_name == "Active Item 2"
119+
assert loaded2.all_item_name == "Active Item 2"
120+
121+
assert loaded3.active_item_name == nil
122+
assert loaded3.all_item_name == nil
123+
end
124+
end

test/support/domain.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ defmodule AshPostgres.Test.Domain do
6464
resource(AshPostgres.Test.FoodItem)
6565
resource(AshPostgres.Test.Meal)
6666
resource(AshPostgres.Test.MealItem)
67+
resource(AshPostgres.Test.Container)
68+
resource(AshPostgres.Test.Item)
6769
end
6870

6971
authorization do

0 commit comments

Comments
 (0)