Skip to content

Commit c9a6e3e

Browse files
authored
test:add failing test (#627)
1 parent 6a2675c commit c9a6e3e

File tree

6 files changed

+285
-1
lines changed

6 files changed

+285
-1
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "fragment(\"gen_random_uuid()\")",
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": "\"active\"",
18+
"generated?": false,
19+
"precision": null,
20+
"primary_key?": false,
21+
"references": null,
22+
"scale": null,
23+
"size": null,
24+
"source": "state",
25+
"type": "text"
26+
},
27+
{
28+
"allow_nil?": false,
29+
"default": "nil",
30+
"generated?": false,
31+
"precision": null,
32+
"primary_key?": false,
33+
"references": {
34+
"deferrable": false,
35+
"destination_attribute": "id",
36+
"destination_attribute_default": null,
37+
"destination_attribute_generated": null,
38+
"index?": true,
39+
"match_type": null,
40+
"match_with": null,
41+
"multitenancy": {
42+
"attribute": null,
43+
"global": false,
44+
"strategy": "context"
45+
},
46+
"name": "non_multitenant_post_multitenant_links_source_id_fkey",
47+
"on_delete": "delete",
48+
"on_update": null,
49+
"primary_key?": true,
50+
"schema": "public",
51+
"table": "multitenant_posts"
52+
},
53+
"scale": null,
54+
"size": null,
55+
"source": "source_id",
56+
"type": "uuid"
57+
},
58+
{
59+
"allow_nil?": false,
60+
"default": "nil",
61+
"generated?": false,
62+
"precision": null,
63+
"primary_key?": false,
64+
"references": {
65+
"deferrable": false,
66+
"destination_attribute": "id",
67+
"destination_attribute_default": null,
68+
"destination_attribute_generated": null,
69+
"index?": true,
70+
"match_type": null,
71+
"match_with": null,
72+
"multitenancy": {
73+
"attribute": null,
74+
"global": null,
75+
"strategy": null
76+
},
77+
"name": "non_multitenant_post_multitenant_links_dest_id_fkey",
78+
"on_delete": "delete",
79+
"on_update": null,
80+
"primary_key?": true,
81+
"schema": "public",
82+
"table": "posts"
83+
},
84+
"scale": null,
85+
"size": null,
86+
"source": "dest_id",
87+
"type": "uuid"
88+
}
89+
],
90+
"base_filter": null,
91+
"check_constraints": [],
92+
"custom_indexes": [],
93+
"custom_statements": [],
94+
"has_create_action": true,
95+
"hash": "F19D22A316D43A64BF2F1E052F545346BD0BCBC660E8EF559D1D4D73D8969A4D",
96+
"identities": [
97+
{
98+
"all_tenants?": false,
99+
"base_filter": null,
100+
"index_name": "non_multitenant_post_multitenant_links_unique_link_index",
101+
"keys": [
102+
{
103+
"type": "atom",
104+
"value": "source_id"
105+
},
106+
{
107+
"type": "atom",
108+
"value": "dest_id"
109+
}
110+
],
111+
"name": "unique_link",
112+
"nils_distinct?": true,
113+
"where": null
114+
}
115+
],
116+
"multitenancy": {
117+
"attribute": null,
118+
"global": false,
119+
"strategy": "context"
120+
},
121+
"repo": "Elixir.AshPostgres.TestRepo",
122+
"schema": null,
123+
"table": "non_multitenant_post_multitenant_links"
124+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
defmodule AshPostgres.TestRepo.TenantMigrations.MigrateResources7 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(:non_multitenant_post_multitenant_links, primary_key: false, prefix: prefix()) do
12+
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
13+
add(:state, :text, default: "active")
14+
15+
add(
16+
:source_id,
17+
references(:multitenant_posts,
18+
column: :id,
19+
name: "non_multitenant_post_multitenant_links_source_id_fkey",
20+
type: :uuid,
21+
prefix: prefix(),
22+
on_delete: :delete_all
23+
),
24+
null: false
25+
)
26+
27+
add(
28+
:dest_id,
29+
references(:posts,
30+
column: :id,
31+
name: "non_multitenant_post_multitenant_links_dest_id_fkey",
32+
type: :uuid,
33+
prefix: "public",
34+
on_delete: :delete_all
35+
),
36+
null: false
37+
)
38+
end
39+
40+
create(index(:non_multitenant_post_multitenant_links, [:source_id]))
41+
42+
create(index(:non_multitenant_post_multitenant_links, [:dest_id]))
43+
44+
create(
45+
unique_index(:non_multitenant_post_multitenant_links, [:source_id, :dest_id],
46+
name: "non_multitenant_post_multitenant_links_unique_link_index"
47+
)
48+
)
49+
end
50+
51+
def down do
52+
drop_if_exists(
53+
unique_index(:non_multitenant_post_multitenant_links, [:source_id, :dest_id],
54+
name: "non_multitenant_post_multitenant_links_unique_link_index"
55+
)
56+
)
57+
58+
drop_if_exists(index(:non_multitenant_post_multitenant_links, [:dest_id]))
59+
60+
drop_if_exists(index(:non_multitenant_post_multitenant_links, [:source_id]))
61+
62+
drop(
63+
constraint(
64+
:non_multitenant_post_multitenant_links,
65+
"non_multitenant_post_multitenant_links_source_id_fkey"
66+
)
67+
)
68+
69+
drop(
70+
constraint(
71+
:non_multitenant_post_multitenant_links,
72+
"non_multitenant_post_multitenant_links_dest_id_fkey"
73+
)
74+
)
75+
76+
drop(table(:non_multitenant_post_multitenant_links, prefix: prefix()))
77+
end
78+
end

test/multitenancy_test.exs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule AshPostgres.Test.MultitenancyTest do
22
use AshPostgres.RepoCase, async: false
33

44
require Ash.Query
5-
alias AshPostgres.MultitenancyTest.{CompositeKeyPost, NamedOrg, Org, Post, User}
5+
alias AshPostgres.MultitenancyTest.{CompositeKeyPost, NamedOrg, Org, Post, User, NonMultitenantPostMultitenantLink}
66
alias AshPostgres.Test.Post, as: GlobalPost
77

88
setup do
@@ -226,6 +226,24 @@ defmodule AshPostgres.Test.MultitenancyTest do
226226
)
227227
end
228228

229+
test "loading non multitenant resource across a many_to_many works", %{org1: org1} do
230+
post = Post
231+
|> Ash.Changeset.for_create(:create, %{name: "foo"})
232+
|> Ash.Changeset.set_tenant(org1)
233+
|> Ash.create!()
234+
235+
GlobalPost
236+
|> Ash.Changeset.for_create(:create, %{title: "fred"})
237+
|> Ash.create!()
238+
239+
NonMultitenantPostMultitenantLink
240+
|> Ash.Changeset.for_create(:create, %{source_id: post.id, dest_id: global_post.id}, tenant: org1)
241+
|> Ash.create!()
242+
243+
post |> Ash.load!([:linked_non_multitenant_posts_through_multitenant_link], tenant: org1) |> IO.inspect()
244+
end
245+
246+
229247
test "manage_relationship from context multitenant resource to attribute multitenant resource doesn't raise an error" do
230248
org = Org |> Ash.Changeset.new() |> Ash.create!()
231249
user = User |> Ash.Changeset.new() |> Ash.create!()

test/support/multitenancy/domain.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule AshPostgres.MultitenancyTest.Domain do
1212
resource(AshPostgres.MultitenancyTest.NonMultitenantPostLink)
1313
resource(AshPostgres.MultitenancyTest.CrossTenantPostLink)
1414
resource(AshPostgres.MultitenancyTest.CompositeKeyPost)
15+
resource(AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink)
1516
end
1617

1718
authorization do
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
defmodule AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink do
2+
@moduledoc false
3+
use Ash.Resource,
4+
domain: AshPostgres.MultitenancyTest.Domain,
5+
data_layer: AshPostgres.DataLayer
6+
7+
postgres do
8+
table "non_multitenant_post_multitenant_links"
9+
repo AshPostgres.TestRepo
10+
11+
references do
12+
reference :source, on_delete: :delete, index?: true
13+
reference :dest, on_delete: :delete, index?: true
14+
end
15+
end
16+
17+
multitenancy do
18+
strategy(:context)
19+
end
20+
21+
actions do
22+
default_accept(:*)
23+
24+
defaults([:create, :read, :update, :destroy])
25+
end
26+
27+
identities do
28+
identity(:unique_link, [:source_id, :dest_id])
29+
end
30+
31+
attributes do
32+
uuid_primary_key :id
33+
34+
attribute :state, :atom do
35+
public?(true)
36+
constraints(one_of: [:active, :archived])
37+
default(:active)
38+
end
39+
end
40+
41+
relationships do
42+
belongs_to :source, AshPostgres.MultitenancyTest.Post do
43+
public? true
44+
allow_nil? false
45+
end
46+
47+
belongs_to :dest, AshPostgres.Test.Post do
48+
public? true
49+
allow_nil? false
50+
end
51+
end
52+
end

test/support/multitenancy/resources/post.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ defmodule AshPostgres.MultitenancyTest.Post do
5959

6060
# has_many(:non_multitenant_post_links, AshPostgres.MultitenancyTest.NonMultitenantPostLink)
6161

62+
has_many :non_multitenant_post_multitenant_links, AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink do
63+
destination_attribute :source_id
64+
end
65+
6266
many_to_many :linked_non_multitenant_posts, AshPostgres.Test.Post do
6367
through(AshPostgres.MultitenancyTest.NonMultitenantPostLink)
6468
join_relationship(:non_multitenant_post_links)
6569
source_attribute_on_join_resource(:source_id)
6670
destination_attribute_on_join_resource(:dest_id)
6771
end
72+
73+
many_to_many :linked_non_multitenant_posts_through_multitenant_link, AshPostgres.Test.Post do
74+
through(AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink)
75+
join_relationship(:non_multitenant_post_links_through_multitenant_link)
76+
source_attribute_on_join_resource(:source_id)
77+
destination_attribute_on_join_resource(:dest_id)
78+
end
6879
end
6980

7081
calculations do

0 commit comments

Comments
 (0)