forked from ash-project/ash_postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_test.exs
More file actions
128 lines (111 loc) · 3.63 KB
/
create_test.exs
File metadata and controls
128 lines (111 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
#
# SPDX-License-Identifier: MIT
defmodule AshPostgres.CreateTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.Post
test "seeding data works" do
Ash.Seed.seed!(%Post{title: "fred"})
end
test "creates insert" do
assert {:ok, %Post{}} =
Post
|> Ash.Changeset.for_create(:create, %{title: "fred"})
|> Ash.create()
assert [%{title: "fred"}] =
Post
|> Ash.Query.sort(:title)
|> Ash.read!()
end
test "upserts entry" do
assert {:ok, %Post{id: id}} =
Post
|> Ash.Changeset.for_create(:create, %{
title: "fredfoo",
uniq_if_contains_foo: "foo",
price: 10
})
|> Ash.create()
assert {:ok, %Post{id: ^id, price: 20}} =
Post
|> Ash.Changeset.for_create(:upsert_with_filter, %{
title: "fredfoo",
uniq_if_contains_foo: "foo",
price: 20
})
|> Ash.create()
end
test "skips upsert with filter" do
assert {:ok, %Post{id: id}} =
Post
|> Ash.Changeset.for_create(:create, %{
title: "fredfoo",
uniq_if_contains_foo: "foo",
price: 10
})
|> Ash.create()
assert {:ok, %Post{id: ^id} = post} =
Post
|> Ash.Changeset.for_create(:upsert_with_filter, %{
title: "fredfoo",
uniq_if_contains_foo: "foo",
price: 10
})
|> Ash.create(return_skipped_upsert?: true)
assert Ash.Resource.get_metadata(post, :upsert_skipped)
end
describe "manage_relationship from hooks on create" do
test "before_transaction hook" do
comment =
AshPostgres.Test.Comment
|> Ash.Changeset.for_create(:create_with_post_from_before_transaction, %{
title: "test comment"
})
|> Ash.create!()
assert comment.post_id != nil
loaded = Ash.load!(comment, :post)
assert loaded.post.title == "created-in-before-transaction"
end
test "before_action hook" do
comment =
AshPostgres.Test.Comment
|> Ash.Changeset.for_create(:create_with_post_from_before_action, %{
title: "test comment"
})
|> Ash.create!()
assert comment.post_id != nil
loaded = Ash.load!(comment, :post)
assert loaded.post.title == "created-in-before-action"
end
test "before_transaction hook via bulk_create" do
result =
Ash.bulk_create!(
[%{title: "bulk comment"}],
AshPostgres.Test.Comment,
:create_with_post_from_before_transaction,
return_records?: true,
return_errors?: true
)
assert result.status == :success
[comment] = result.records
assert comment.post_id != nil
loaded = Ash.load!(comment, :post)
assert loaded.post.title == "created-in-before-transaction"
end
test "before_action hook via bulk_create" do
result =
Ash.bulk_create!(
[%{title: "bulk comment"}],
AshPostgres.Test.Comment,
:create_with_post_from_before_action,
return_records?: true,
return_errors?: true
)
assert result.status == :success
[comment] = result.records
assert comment.post_id != nil
loaded = Ash.load!(comment, :post)
assert loaded.post.title == "created-in-before-action"
end
end
end