Skip to content

Commit fc4b6ea

Browse files
committed
Failing test for module calculation inside expression one
1 parent 5f26d69 commit fc4b6ea

File tree

7 files changed

+328
-1
lines changed

7 files changed

+328
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "fragment(\"gen_random_uuid()\")",
6+
"generated?": false,
7+
"primary_key?": true,
8+
"references": null,
9+
"size": null,
10+
"source": "id",
11+
"type": "uuid"
12+
},
13+
{
14+
"allow_nil?": true,
15+
"default": "nil",
16+
"generated?": false,
17+
"primary_key?": false,
18+
"references": null,
19+
"size": null,
20+
"source": "name",
21+
"type": "text"
22+
},
23+
{
24+
"allow_nil?": false,
25+
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
26+
"generated?": false,
27+
"primary_key?": false,
28+
"references": null,
29+
"size": null,
30+
"source": "inserted_at",
31+
"type": "utc_datetime_usec"
32+
},
33+
{
34+
"allow_nil?": false,
35+
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
36+
"generated?": false,
37+
"primary_key?": false,
38+
"references": null,
39+
"size": null,
40+
"source": "updated_at",
41+
"type": "utc_datetime_usec"
42+
}
43+
],
44+
"base_filter": null,
45+
"check_constraints": [],
46+
"custom_indexes": [],
47+
"custom_statements": [],
48+
"has_create_action": false,
49+
"hash": "A376A922D4610DEAD0294DD863E8E2FF72FEC6316AC6B809C3DFC17B29094EBA",
50+
"identities": [],
51+
"multitenancy": {
52+
"attribute": null,
53+
"global": null,
54+
"strategy": null
55+
},
56+
"repo": "Elixir.AshPostgres.TestRepo",
57+
"schema": null,
58+
"table": "comedians"
59+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "fragment(\"gen_random_uuid()\")",
6+
"generated?": false,
7+
"primary_key?": true,
8+
"references": null,
9+
"size": null,
10+
"source": "id",
11+
"type": "uuid"
12+
},
13+
{
14+
"allow_nil?": true,
15+
"default": "nil",
16+
"generated?": false,
17+
"primary_key?": false,
18+
"references": null,
19+
"size": null,
20+
"source": "text",
21+
"type": "text"
22+
},
23+
{
24+
"allow_nil?": true,
25+
"default": "false",
26+
"generated?": false,
27+
"primary_key?": false,
28+
"references": null,
29+
"size": null,
30+
"source": "is_good",
31+
"type": "boolean"
32+
},
33+
{
34+
"allow_nil?": false,
35+
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
36+
"generated?": false,
37+
"primary_key?": false,
38+
"references": null,
39+
"size": null,
40+
"source": "inserted_at",
41+
"type": "utc_datetime_usec"
42+
},
43+
{
44+
"allow_nil?": false,
45+
"default": "fragment(\"(now() AT TIME ZONE 'utc')\")",
46+
"generated?": false,
47+
"primary_key?": false,
48+
"references": null,
49+
"size": null,
50+
"source": "updated_at",
51+
"type": "utc_datetime_usec"
52+
},
53+
{
54+
"allow_nil?": true,
55+
"default": "nil",
56+
"generated?": false,
57+
"primary_key?": false,
58+
"references": {
59+
"deferrable": false,
60+
"destination_attribute": "id",
61+
"destination_attribute_default": null,
62+
"destination_attribute_generated": null,
63+
"index?": false,
64+
"match_type": null,
65+
"match_with": null,
66+
"multitenancy": {
67+
"attribute": null,
68+
"global": null,
69+
"strategy": null
70+
},
71+
"name": "jokes_comedian_id_fkey",
72+
"on_delete": null,
73+
"on_update": null,
74+
"primary_key?": true,
75+
"schema": "public",
76+
"table": "comedians"
77+
},
78+
"size": null,
79+
"source": "comedian_id",
80+
"type": "uuid"
81+
}
82+
],
83+
"base_filter": null,
84+
"check_constraints": [],
85+
"custom_indexes": [],
86+
"custom_statements": [],
87+
"has_create_action": false,
88+
"hash": "CFDCBDBAC925B20A1EA8EB4B3C37ACF1FD915FA21F6B07DE4D066AA167B9EEF5",
89+
"identities": [],
90+
"multitenancy": {
91+
"attribute": null,
92+
"global": null,
93+
"strategy": null
94+
},
95+
"repo": "Elixir.AshPostgres.TestRepo",
96+
"schema": null,
97+
"table": "jokes"
98+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
defmodule AshPostgres.TestRepo.Migrations.MigrateResources45 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(:jokes, primary_key: false) do
12+
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
13+
add(:text, :text)
14+
add(:is_good, :boolean, default: false)
15+
16+
add(:inserted_at, :utc_datetime_usec,
17+
null: false,
18+
default: fragment("(now() AT TIME ZONE 'utc')")
19+
)
20+
21+
add(:updated_at, :utc_datetime_usec,
22+
null: false,
23+
default: fragment("(now() AT TIME ZONE 'utc')")
24+
)
25+
26+
add(:comedian_id, :uuid)
27+
end
28+
29+
create table(:comedians, primary_key: false) do
30+
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
31+
end
32+
33+
alter table(:jokes) do
34+
modify(
35+
:comedian_id,
36+
references(:comedians,
37+
column: :id,
38+
name: "jokes_comedian_id_fkey",
39+
type: :uuid,
40+
prefix: "public"
41+
)
42+
)
43+
end
44+
45+
alter table(:comedians) do
46+
add(:name, :text)
47+
48+
add(:inserted_at, :utc_datetime_usec,
49+
null: false,
50+
default: fragment("(now() AT TIME ZONE 'utc')")
51+
)
52+
53+
add(:updated_at, :utc_datetime_usec,
54+
null: false,
55+
default: fragment("(now() AT TIME ZONE 'utc')")
56+
)
57+
end
58+
end
59+
60+
def down do
61+
alter table(:comedians) do
62+
remove(:updated_at)
63+
remove(:inserted_at)
64+
remove(:name)
65+
end
66+
67+
drop(constraint(:jokes, "jokes_comedian_id_fkey"))
68+
69+
alter table(:jokes) do
70+
modify(:comedian_id, :uuid)
71+
end
72+
73+
drop(table(:comedians))
74+
75+
drop(table(:jokes))
76+
end
77+
end

test/calculation_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule AshPostgres.CalculationTest do
22
use AshPostgres.RepoCase, async: false
3-
alias AshPostgres.Test.{Account, Author, Comment, Post, User}
3+
alias AshPostgres.Test.{Account, Author, Comedy, Comment, Post, User}
44

55
require Ash.Query
66
import Ash.Expr
@@ -937,6 +937,12 @@ defmodule AshPostgres.CalculationTest do
937937
|> Ash.read!()
938938
end
939939

940+
test "module calculation inside expr calculation works" do
941+
commedian = Comedian.create!(%{name: "John"})
942+
commedian = Ash.get!(Comedian, commedian.id, load: [:has_jokes_expr], authorize?: false)
943+
assert %{has_jokes_expr: false} = commedian
944+
end
945+
940946
def fred do
941947
"fred"
942948
end

test/support/domain.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule AshPostgres.Test.Domain do
55
resources do
66
resource(AshPostgres.Test.CoAuthorPost)
77
resource(AshPostgres.Test.Post)
8+
resource(AshPostgres.Test.Comedian)
89
resource(AshPostgres.Test.Comment)
910
resource(AshPostgres.Test.IntegerPost)
1011
resource(AshPostgres.Test.Rating)
@@ -14,6 +15,7 @@ defmodule AshPostgres.Test.Domain do
1415
resource(AshPostgres.Test.Profile)
1516
resource(AshPostgres.Test.User)
1617
resource(AshPostgres.Test.Invite)
18+
resource(AshPostgres.Test.Joke)
1719
resource(AshPostgres.Test.Account)
1820
resource(AshPostgres.Test.Organization)
1921
resource(AshPostgres.Test.Manager)

test/support/resources/comedian.ex

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
defmodule AshPostgres.Test.Comedian do
2+
@moduledoc false
3+
use Ash.Resource,
4+
domain: AshPostgres.Test.Domain,
5+
data_layer: AshPostgres.DataLayer,
6+
authorizers: [Ash.Policy.Authorizer]
7+
8+
attributes do
9+
uuid_primary_key(:id)
10+
attribute(:name, :string, public?: true)
11+
create_timestamp(:inserted_at, public?: true)
12+
update_timestamp(:updated_at, public?: true)
13+
end
14+
15+
relationships do
16+
has_many(:jokes, AshPostgres.Test.Joke, public?: true)
17+
end
18+
19+
calculations do
20+
calculate(:has_jokes_mod, :boolean, AshPostgres.Test.Comedian.HasJokes)
21+
calculate(:has_jokes_expr, :boolean, expr(has_jokes_mod == true))
22+
end
23+
24+
actions do
25+
defaults([:read])
26+
27+
create :create do
28+
primary?(true)
29+
accept([:name])
30+
end
31+
end
32+
33+
code_interface do
34+
define(:create)
35+
end
36+
37+
postgres do
38+
table("comedians")
39+
repo(AshPostgres.TestRepo)
40+
end
41+
end
42+
43+
defmodule AshPostgres.Test.Comedian.HasJokes do
44+
use Ash.Resource.Calculation
45+
46+
@impl true
47+
def load(_, _, _) do
48+
[:jokes]
49+
end
50+
51+
@impl true
52+
def calculate(comedians, _, _) do
53+
Enum.map(comedians, fn %{jokes: jokes} ->
54+
Enum.any?(jokes)
55+
end)
56+
end
57+
end

test/support/resources/joke.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule AshPostgres.Test.Joke do
2+
@moduledoc false
3+
use Ash.Resource,
4+
domain: AshPostgres.Test.Domain,
5+
data_layer: AshPostgres.DataLayer,
6+
authorizers: [Ash.Policy.Authorizer]
7+
8+
attributes do
9+
uuid_primary_key(:id)
10+
attribute(:text, :string, public?: true)
11+
attribute(:is_good, :boolean, default: false, public?: true)
12+
create_timestamp(:inserted_at, public?: true)
13+
update_timestamp(:updated_at, public?: true)
14+
end
15+
16+
relationships do
17+
belongs_to(:comedian, AshPostgres.Test.Comedian, public?: true)
18+
end
19+
20+
actions do
21+
defaults([:read])
22+
end
23+
24+
postgres do
25+
table("jokes")
26+
repo(AshPostgres.TestRepo)
27+
end
28+
end

0 commit comments

Comments
 (0)