Skip to content

Commit 5b8f46f

Browse files
authored
test: failing test for calculations in different schema (#563)
* failing test for calculations in different schema Signed-off-by: kernel-io <kernel-io@users.noreply.github.com> * fix test to not require grouping Signed-off-by: kernel-io <kernel-io@users.noreply.github.com> --------- Signed-off-by: kernel-io <kernel-io@users.noreply.github.com>
1 parent 5e21f89 commit 5b8f46f

File tree

6 files changed

+179
-3
lines changed

6 files changed

+179
-3
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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": "nil",
18+
"generated?": false,
19+
"precision": null,
20+
"primary_key?": false,
21+
"references": {
22+
"deferrable": false,
23+
"destination_attribute": "id",
24+
"destination_attribute_default": null,
25+
"destination_attribute_generated": null,
26+
"index?": false,
27+
"match_type": null,
28+
"match_with": null,
29+
"multitenancy": {
30+
"attribute": null,
31+
"global": null,
32+
"strategy": null
33+
},
34+
"name": "records_temp_entities_record_id_fkey",
35+
"on_delete": null,
36+
"on_update": null,
37+
"primary_key?": true,
38+
"schema": "public",
39+
"table": "records"
40+
},
41+
"scale": null,
42+
"size": null,
43+
"source": "record_id",
44+
"type": "uuid"
45+
},
46+
{
47+
"allow_nil?": true,
48+
"default": "nil",
49+
"generated?": false,
50+
"precision": null,
51+
"primary_key?": false,
52+
"references": {
53+
"deferrable": false,
54+
"destination_attribute": "id",
55+
"destination_attribute_default": null,
56+
"destination_attribute_generated": null,
57+
"index?": false,
58+
"match_type": null,
59+
"match_with": null,
60+
"multitenancy": {
61+
"attribute": null,
62+
"global": null,
63+
"strategy": null
64+
},
65+
"name": "records_temp_entities_temp_entity_id_fkey",
66+
"on_delete": null,
67+
"on_update": null,
68+
"primary_key?": true,
69+
"schema": "temp",
70+
"table": "temp_entities"
71+
},
72+
"scale": null,
73+
"size": null,
74+
"source": "temp_entity_id",
75+
"type": "uuid"
76+
}
77+
],
78+
"base_filter": null,
79+
"check_constraints": [],
80+
"custom_indexes": [],
81+
"custom_statements": [],
82+
"has_create_action": true,
83+
"hash": "1F00231E86F4E276096E683FD2836B40F36C6AD617A777D947E908FD52D09FDB",
84+
"identities": [],
85+
"multitenancy": {
86+
"attribute": null,
87+
"global": null,
88+
"strategy": null
89+
},
90+
"repo": "Elixir.AshPostgres.TestRepo",
91+
"schema": null,
92+
"table": "records_temp_entities"
93+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule AshPostgres.TestRepo.Migrations.CreateRecordTempEntitiesTable 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(:records_temp_entities, primary_key: false) do
12+
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
13+
14+
add(
15+
:record_id,
16+
references(:records,
17+
column: :id,
18+
name: "records_temp_entities_record_id_fkey",
19+
type: :uuid,
20+
prefix: "public"
21+
)
22+
)
23+
24+
add(
25+
:temp_entity_id,
26+
references(:temp_entities,
27+
column: :id,
28+
name: "records_temp_entities_temp_entity_id_fkey",
29+
type: :uuid,
30+
prefix: "temp"
31+
)
32+
)
33+
end
34+
end
35+
36+
def down do
37+
drop(constraint(:records_temp_entities, "records_temp_entities_record_id_fkey"))
38+
39+
drop(constraint(:records_temp_entities, "records_temp_entities_temp_entity_id_fkey"))
40+
41+
drop(table(:records_temp_entities))
42+
end
43+
end

test/calculation_test.exs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule AshPostgres.CalculationTest do
2+
alias AshPostgres.Test.RecordTempEntity
23
use AshPostgres.RepoCase, async: false
34
alias AshPostgres.Test.{Account, Author, Comedian, Comment, Post, Record, TempEntity, User}
45

@@ -1018,9 +1019,12 @@ defmodule AshPostgres.CalculationTest do
10181019
end
10191020

10201021
test "calculation references use the appropriate schema" do
1021-
Record |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!()
1022+
record = Record |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!()
10221023

1023-
TempEntity |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!()
1024+
temp_entity =
1025+
TempEntity |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!()
1026+
1027+
Ash.Seed.seed!(RecordTempEntity, %{record_id: record.id, temp_entity_id: temp_entity.id})
10241028

10251029
full_name =
10261030
Record

test/support/domain.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ defmodule AshPostgres.Test.Domain do
3131
resource(AshPostgres.Test.Entity)
3232
resource(AshPostgres.Test.ContentVisibilityGroup)
3333
resource(AshPostgres.Test.TempEntity)
34+
resource(AshPostgres.Test.RecordTempEntity)
3435
resource(AshPostgres.Test.Permalink)
3536
resource(AshPostgres.Test.Record)
3637
resource(AshPostgres.Test.PostFollower)

test/support/resources/record.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ defmodule AshPostgres.Test.Record do
2828
source_attribute(:full_name)
2929
destination_attribute(:full_name)
3030
end
31+
32+
many_to_many :temp_entities, AshPostgres.Test.TempEntity do
33+
public?(true)
34+
35+
through(AshPostgres.Test.RecordTempEntity)
36+
end
3137
end
3238

3339
postgres do
@@ -36,7 +42,11 @@ defmodule AshPostgres.Test.Record do
3642
end
3743

3844
calculations do
39-
calculate(:temp_entity_full_name, :string, expr(temp_entity.full_name))
45+
calculate(
46+
:temp_entity_full_name,
47+
:string,
48+
expr(fragment("coalesce(?, '')", temp_entities.full_name))
49+
)
4050
end
4151

4252
actions do
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule AshPostgres.Test.RecordTempEntity do
2+
@moduledoc false
3+
4+
use Ash.Resource,
5+
domain: AshPostgres.Test.Domain,
6+
data_layer: AshPostgres.DataLayer
7+
8+
postgres do
9+
table "records_temp_entities"
10+
repo AshPostgres.TestRepo
11+
end
12+
13+
attributes do
14+
uuid_primary_key(:id)
15+
end
16+
17+
relationships do
18+
belongs_to(:record, AshPostgres.Test.Record, public?: true)
19+
belongs_to(:temp_entity, AshPostgres.Test.TempEntity, public?: true)
20+
end
21+
22+
actions do
23+
defaults([:read, :destroy, create: :*, update: :*])
24+
end
25+
end

0 commit comments

Comments
 (0)