Skip to content

Commit 449ae3f

Browse files
authored
test: typed struct arrays with storage_type :jsonb & {:array, map} (#640)
1 parent b2216c4 commit 449ae3f

File tree

7 files changed

+275
-2
lines changed

7 files changed

+275
-2
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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": null,
22+
"scale": null,
23+
"size": null,
24+
"source": "first_name",
25+
"type": "text"
26+
},
27+
{
28+
"allow_nil?": true,
29+
"default": "nil",
30+
"generated?": false,
31+
"precision": null,
32+
"primary_key?": false,
33+
"references": null,
34+
"scale": null,
35+
"size": null,
36+
"source": "last_name",
37+
"type": "text"
38+
},
39+
{
40+
"allow_nil?": true,
41+
"default": "nil",
42+
"generated?": false,
43+
"precision": null,
44+
"primary_key?": false,
45+
"references": null,
46+
"scale": null,
47+
"size": null,
48+
"source": "bio",
49+
"type": "map"
50+
},
51+
{
52+
"allow_nil?": true,
53+
"default": "nil",
54+
"generated?": false,
55+
"precision": null,
56+
"primary_key?": false,
57+
"references": null,
58+
"scale": null,
59+
"size": null,
60+
"source": "bios",
61+
"type": "jsonb"
62+
},
63+
{
64+
"allow_nil?": true,
65+
"default": "nil",
66+
"generated?": false,
67+
"precision": null,
68+
"primary_key?": false,
69+
"references": null,
70+
"scale": null,
71+
"size": null,
72+
"source": "badges",
73+
"type": [
74+
"array",
75+
"text"
76+
]
77+
},
78+
{
79+
"allow_nil?": true,
80+
"default": "nil",
81+
"generated?": false,
82+
"precision": null,
83+
"primary_key?": false,
84+
"references": null,
85+
"scale": null,
86+
"size": null,
87+
"source": "settings",
88+
"type": "jsonb"
89+
},
90+
{
91+
"allow_nil?": true,
92+
"default": "nil",
93+
"generated?": false,
94+
"precision": null,
95+
"primary_key?": false,
96+
"references": null,
97+
"scale": null,
98+
"size": null,
99+
"source": "identities",
100+
"type": "jsonb"
101+
},
102+
{
103+
"allow_nil?": true,
104+
"default": "nil",
105+
"generated?": false,
106+
"precision": null,
107+
"primary_key?": false,
108+
"references": null,
109+
"scale": null,
110+
"size": null,
111+
"source": "preferences",
112+
"type": [
113+
"array",
114+
"map"
115+
]
116+
}
117+
],
118+
"base_filter": null,
119+
"check_constraints": [],
120+
"custom_indexes": [],
121+
"custom_statements": [],
122+
"has_create_action": true,
123+
"hash": "55CFEDA4CB07DD89D2807B080A6F0E14A0370D7EA0C48E67C36ACAC568137D95",
124+
"identities": [],
125+
"multitenancy": {
126+
"attribute": null,
127+
"global": null,
128+
"strategy": null
129+
},
130+
"repo": "Elixir.AshPostgres.TestRepo",
131+
"schema": null,
132+
"table": "authors"
133+
}
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.MigrateResources64 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(:authors) do
16+
add(:identities, :jsonb)
17+
add(:preferences, {:array, :map})
18+
end
19+
end
20+
21+
def down do
22+
alter table(:authors) do
23+
remove(:preferences)
24+
remove(:identities)
25+
end
26+
end
27+
end

test/storage_types_test.exs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,86 @@ defmodule AshPostgres.StorageTypesTest do
9393
|> Ash.Query.filter(not is_nil(settings["dues_reminders"]))
9494
|> Ash.read!()
9595
end
96+
97+
test "can bulk update {:array, CustomTypedStruct} stored as jsonb" do
98+
%{id: id} =
99+
Author
100+
|> Ash.Changeset.for_create(
101+
:create,
102+
%{
103+
first_name: "Test",
104+
last_name: "User",
105+
identities: [%{provider: "github", uid: "123"}, %{provider: "google", uid: "456"}]
106+
}
107+
)
108+
|> Ash.create!()
109+
110+
%BulkResult{records: [author]} =
111+
Author
112+
|> Ash.Query.filter(id == ^id)
113+
|> Ash.bulk_update(:update, %{identities: [%{provider: "gitlab", uid: "789"}]},
114+
return_errors?: true,
115+
notify?: true,
116+
strategy: [:atomic, :stream, :atomic_batches],
117+
allow_stream_with: :full_read,
118+
return_records?: true
119+
)
120+
121+
assert length(author.identities) == 1
122+
assert %{provider: "gitlab", uid: "789"} = hd(author.identities)
123+
124+
%BulkResult{records: [author]} =
125+
Author
126+
|> Ash.Query.filter(id == ^id)
127+
|> Ash.bulk_update(:update, %{identities: []},
128+
return_errors?: true,
129+
notify?: true,
130+
strategy: [:atomic, :stream, :atomic_batches],
131+
allow_stream_with: :full_read,
132+
return_records?: true
133+
)
134+
135+
assert author.identities == []
136+
end
137+
138+
test "can bulk update {:array, CustomTypedStruct} stored as {:array, :map}" do
139+
%{id: id} =
140+
Author
141+
|> Ash.Changeset.for_create(
142+
:create,
143+
%{
144+
first_name: "Test",
145+
last_name: "User",
146+
preferences: [%{key: "theme", value: "dark"}, %{key: "lang", value: "en"}]
147+
}
148+
)
149+
|> Ash.create!()
150+
151+
%BulkResult{records: [author]} =
152+
Author
153+
|> Ash.Query.filter(id == ^id)
154+
|> Ash.bulk_update(:update, %{preferences: [%{key: "theme", value: "light"}]},
155+
return_errors?: true,
156+
notify?: true,
157+
strategy: [:atomic, :stream, :atomic_batches],
158+
allow_stream_with: :full_read,
159+
return_records?: true
160+
)
161+
162+
assert length(author.preferences) == 1
163+
assert %{key: "theme", value: "light"} = hd(author.preferences)
164+
165+
%BulkResult{records: [author]} =
166+
Author
167+
|> Ash.Query.filter(id == ^id)
168+
|> Ash.bulk_update(:update, %{preferences: []},
169+
return_errors?: true,
170+
notify?: true,
171+
strategy: [:atomic, :stream, :atomic_batches],
172+
allow_stream_with: :full_read,
173+
return_records?: true
174+
)
175+
176+
assert author.preferences == []
177+
end
96178
end

test/support/resources/author.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ defmodule AshPostgres.Test.Author do
2323
table("authors")
2424
repo(AshPostgres.TestRepo)
2525

26-
migration_types bios: :jsonb, settings: :jsonb
27-
storage_types(bios: :jsonb, settings: :jsonb)
26+
migration_types bios: :jsonb, settings: :jsonb, identities: :jsonb
27+
storage_types(bios: :jsonb, settings: :jsonb, identities: :jsonb)
2828
end
2929

3030
attributes do
@@ -35,6 +35,8 @@ defmodule AshPostgres.Test.Author do
3535
attribute(:bios, {:array, :map}, public?: true)
3636
attribute(:badges, {:array, :atom}, public?: true)
3737
attribute(:settings, AshPostgres.Test.Settings, public?: true)
38+
attribute(:identities, {:array, AshPostgres.Test.Identity}, public?: true)
39+
attribute(:preferences, {:array, AshPostgres.Test.Preference}, public?: true)
3840
end
3941

4042
actions do

test/support/resources/identity.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.Test.Identity do
6+
@moduledoc false
7+
use Ash.TypedStruct
8+
9+
typed_struct do
10+
field(:provider, :string, allow_nil?: false)
11+
field(:uid, :string, allow_nil?: false)
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.Test.Preference do
6+
@moduledoc false
7+
use Ash.TypedStruct
8+
9+
typed_struct do
10+
field(:key, :string, allow_nil?: false)
11+
field(:value, :string, allow_nil?: false)
12+
end
13+
end

0 commit comments

Comments
 (0)