forked from ash-project/ash_postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_create_test.exs
More file actions
759 lines (672 loc) · 25.3 KB
/
bulk_create_test.exs
File metadata and controls
759 lines (672 loc) · 25.3 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
#
# SPDX-License-Identifier: MIT
defmodule AshPostgres.BulkCreateTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.{IntegerPost, Post, Record}
require Ash.Query
import Ash.Expr
describe "bulk creates" do
test "bulk creates insert each input" do
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
assert [%{title: "fred"}, %{title: "george"}] =
Post
|> Ash.Query.sort(:title)
|> Ash.read!()
end
test "bulk creates with atomic_set applies to all records" do
assert %Ash.BulkResult{status: :success, records: records} =
Ash.bulk_create(
[%{title: "post1"}, %{title: "post2"}, %{title: "post3"}],
Post,
:create_with_atomic_set,
return_records?: true
)
assert length(records) == 3
for record <- records do
assert record.score == 100
end
end
test "bulk creates perform before action hooks" do
Ash.bulk_create!(
[%{title: "before_action"}, %{title: "before_action"}],
Post,
:create_with_before_action,
return_errors?: true,
return_records?: true
)
assert [%{title: "before_action"}, %{title: "before_action"}] =
Post
|> Ash.Query.sort(:title)
|> Ash.read!()
end
test "bulk creates can be streamed" do
assert [{:ok, %{title: "fred"}}, {:ok, %{title: "george"}}] =
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create,
return_stream?: true,
return_records?: true
)
|> Enum.sort_by(fn {:ok, result} -> result.title end)
end
test "bulk creates can upsert" do
assert [
{:ok, %{title: "fred", uniq_one: "one", uniq_two: "two", price: 10}},
{:ok, %{title: "george", uniq_one: "three", uniq_two: "four", price: 20}}
] =
Ash.bulk_create!(
[
%{title: "fred", uniq_one: "one", uniq_two: "two", price: 10},
%{title: "george", uniq_one: "three", uniq_two: "four", price: 20}
],
Post,
:create,
return_stream?: true,
return_records?: true
)
|> Enum.sort_by(fn {:ok, result} -> result.title end)
assert [
{:ok, %{title: "fred", uniq_one: "one", uniq_two: "two", price: 1000}},
{:ok, %{title: "george", uniq_one: "three", uniq_two: "four", price: 20_000}}
] =
Ash.bulk_create!(
[
%{title: "something", uniq_one: "one", uniq_two: "two", price: 1000},
%{title: "else", uniq_one: "three", uniq_two: "four", price: 20_000}
],
Post,
:create,
upsert?: true,
upsert_identity: :uniq_one_and_two,
upsert_fields: [:price],
return_stream?: true,
return_errors?: true,
return_records?: true
)
|> Enum.sort_by(fn
{:ok, result} ->
result.title
_ ->
nil
end)
end
test "bulk creates with upsert updates update_timestamp" do
past = DateTime.add(DateTime.utc_now(), -60, :second)
assert [
{:ok, %{title: "fred", uniq_one: "one", uniq_two: "two"} = initial}
] =
Ash.bulk_create!(
[
%{
title: "fred",
uniq_one: "one",
uniq_two: "two",
price: 10,
updated_at: past
}
],
Post,
:create,
return_stream?: true,
return_records?: true
)
|> Enum.to_list()
assert DateTime.compare(initial.updated_at, past) == :eq
assert [
{:ok, %{title: "fred", uniq_one: "one", uniq_two: "two", price: 1000} = upserted}
] =
Ash.bulk_create!(
[%{title: "something", uniq_one: "one", uniq_two: "two", price: 1000}],
Post,
:create,
upsert?: true,
upsert_identity: :uniq_one_and_two,
upsert_fields: [:price],
return_stream?: true,
return_errors?: true,
return_records?: true
)
|> Enum.to_list()
assert DateTime.after?(upserted.updated_at, initial.updated_at)
end
test "bulk creates with empty upsert does not update update_timestamp" do
past = DateTime.add(DateTime.utc_now(), -60, :second)
assert [
{:ok, %{title: "fred", uniq_one: "one", uniq_two: "two"} = initial}
] =
Ash.bulk_create!(
[
%{
title: "fred",
uniq_one: "one",
uniq_two: "two",
price: 10,
updated_at: past
}
],
Post,
:create,
return_stream?: true,
return_records?: true
)
|> Enum.to_list()
assert [
{:ok, %{title: "fred"} = upserted}
] =
Ash.bulk_create!(
[%{title: "something", uniq_one: "one", uniq_two: "two", price: 1000}],
Post,
:create,
upsert?: true,
upsert_identity: :uniq_one_and_two,
upsert_fields: [],
return_stream?: true,
return_errors?: true,
return_records?: true
)
|> Enum.to_list()
assert DateTime.compare(upserted.updated_at, initial.updated_at) == :eq
end
test "bulk creates with upsert does not update update_timestamp when touch_update_defaults? is false" do
past = DateTime.add(DateTime.utc_now(), -60, :second)
assert [
{:ok, %{title: "fred", uniq_one: "one", uniq_two: "two"} = initial}
] =
Ash.bulk_create!(
[
%{
title: "fred",
uniq_one: "one",
uniq_two: "two",
price: 10,
updated_at: past
}
],
Post,
:create,
return_stream?: true,
return_records?: true
)
|> Enum.to_list()
assert DateTime.compare(initial.updated_at, past) == :eq
assert [
{:ok, %{title: "fred", uniq_one: "one", uniq_two: "two", price: 1000} = upserted}
] =
Ash.bulk_create!(
[%{title: "something", uniq_one: "one", uniq_two: "two", price: 1000}],
Post,
:create,
upsert?: true,
upsert_identity: :uniq_one_and_two,
upsert_fields: [:price],
context: %{data_layer: %{touch_update_defaults?: false}},
return_stream?: true,
return_errors?: true,
return_records?: true
)
|> Enum.to_list()
assert DateTime.compare(upserted.updated_at, initial.updated_at) == :eq
end
test "bulk upsert skips with filter" do
assert [
{:ok, %{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10}},
{:ok, %{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20}},
{:ok, %{title: "herbert", uniq_if_contains_foo: "3", price: 30}}
] =
Ash.bulk_create!(
[
%{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10},
%{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20},
%{title: "herbert", uniq_if_contains_foo: "3", price: 30}
],
Post,
:create,
return_stream?: true,
return_records?: true
)
|> Enum.sort_by(fn {:ok, result} -> result.title end)
assert [
{:ok, %{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20_000}},
{:ok, %{title: "herbert", uniq_if_contains_foo: "3", price: 30}}
] =
Ash.bulk_create!(
[
%{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10},
%{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20_000},
%{title: "herbert", uniq_if_contains_foo: "3", price: 30}
],
Post,
:upsert_with_filter,
return_stream?: true,
return_errors?: true,
return_records?: true
)
|> Enum.sort_by(fn
{:ok, result} ->
result.title
_ ->
nil
end)
end
test "bulk upsert skips with upsert_condition" do
assert [
{:ok, %{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10}},
{:ok, %{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20}},
{:ok, %{title: "herbert", uniq_if_contains_foo: "3", price: 30}}
] =
Ash.bulk_create!(
[
%{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10},
%{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20},
%{title: "herbert", uniq_if_contains_foo: "3", price: 30}
],
Post,
:create,
return_stream?: true,
return_records?: true
)
|> Enum.sort_by(fn {:ok, result} -> result.title end)
assert [
{:ok, %{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20_000}},
{:ok, %{title: "herbert", uniq_if_contains_foo: "3", price: 30}}
] =
Ash.bulk_create!(
[
%{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10},
%{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20_000},
%{title: "herbert", uniq_if_contains_foo: "3", price: 30}
],
Post,
:upsert_with_no_filter,
return_stream?: true,
upsert_condition: expr(price != upsert_conflict(:price)),
return_errors?: true,
return_records?: true
)
|> Enum.sort_by(fn
{:ok, result} ->
result.title
_ ->
nil
end)
end
test "bulk upsert returns skipped records with return_skipped_upsert?" do
assert [
{:ok, %{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10}},
{:ok, %{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20}},
{:ok, %{title: "herbert", uniq_if_contains_foo: "3", price: 30}}
] =
Ash.bulk_create!(
[
%{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10},
%{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20},
%{title: "herbert", uniq_if_contains_foo: "3", price: 30}
],
Post,
:create,
return_stream?: true,
return_records?: true
)
|> Enum.sort_by(fn {:ok, result} -> result.title end)
results =
Ash.bulk_create!(
[
%{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 10},
%{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 20_000},
%{title: "herbert", uniq_if_contains_foo: "3", price: 30}
],
Post,
:upsert_with_no_filter,
return_stream?: true,
upsert_condition: expr(price != upsert_conflict(:price)),
return_errors?: true,
return_records?: true,
return_skipped_upsert?: true
)
|> Enum.sort_by(fn
{:ok, result} ->
result.title
_ ->
nil
end)
assert [
{:ok, skipped},
{:ok, updated},
{:ok, no_conflict}
] = results
assert skipped.title == "fredfoo"
assert skipped.price == 10
assert Ash.Resource.get_metadata(skipped, :upsert_skipped) == true
assert updated.title == "georgefoo"
assert updated.price == 20_000
refute Ash.Resource.get_metadata(updated, :upsert_skipped)
assert no_conflict.title == "herbert"
assert no_conflict.price == 30
refute Ash.Resource.get_metadata(no_conflict, :upsert_skipped)
end
test "bulk upsert with return_skipped_upsert? when all records are not skipped" do
%Ash.BulkResult{records: records, errors: errors} =
Ash.bulk_create!(
[
%{title: "fredfoo", uniq_if_contains_foo: "1foo", price: 100},
%{title: "georgefoo", uniq_if_contains_foo: "2foo", price: 200}
],
Post,
:upsert_with_no_filter,
upsert_condition: expr(price != upsert_conflict(:price)),
return_errors?: true,
return_records?: true,
return_skipped_upsert?: true
)
assert errors == []
assert [row1, row2] = records
assert row1.title == "fredfoo"
assert row1.price == 100
refute Ash.Resource.get_metadata(row1, :upsert_skipped)
assert row2.title == "georgefoo"
assert row2.price == 200
refute Ash.Resource.get_metadata(row2, :upsert_skipped)
end
# confirmed that this doesn't work because it can't. An upsert must map to a potentially successful insert.
# leaving this test here for posterity
# test "bulk creates can upsert with id" do
# org_id = Ash.UUID.generate()
# _new_org =
# Organization
# |> Ash.Changeset.for_create(:create, %{
# id: org_id,
# title: "Avengers"
# })
# |> Ash.create!()
# assert [
# {:ok,
# %{
# name: "Bruce Banner",
# code: "BB01",
# must_be_present: "I am Hulk",
# organization_id: org_id
# }},
# {:ok,
# %{
# name: "Tony Stark",
# code: "TS01",
# must_be_present: "I am Iron Man",
# organization_id: org_id
# }}
# ] =
# Ash.bulk_create!(
# [
# %{
# name: "Tony Stark",
# code: "TS01",
# must_be_present: "I am Iron Man",
# organization_id: org_id
# },
# %{
# name: "Bruce Banner",
# code: "BB01",
# must_be_present: "I am Hulk",
# organization_id: org_id
# }
# ],
# Manager,
# :create,
# return_stream?: true,
# return_records?: true,
# return_errors?: true
# )
# |> Enum.sort_by(fn {:ok, result} -> result.name end)
# assert [
# {:ok,
# %{
# name: "Bruce Banner",
# code: "BB01",
# must_be_present: "I am Hulk",
# organization_id: org_id,
# role: "bone breaker"
# }},
# {:ok,
# %{
# name: "Tony Stark",
# code: "TS01",
# must_be_present: "I am Iron Man",
# organization_id: org_id,
# role: "master in chief"
# }}
# ] =
# Ash.bulk_create!(
# [
# %{
# name: "Tony Stark",
# code: "TS01",
# organization_id: org_id,
# role: "master in chief"
# },
# %{
# name: "Brice Brenner",
# code: "BB01",
# organization_id: org_id,
# role: "bone breaker"
# }
# ],
# Manager,
# :create,
# upsert?: true,
# upsert_identity: :uniq_code,
# upsert_fields: [:role],
# return_stream?: true,
# return_records?: true,
# return_errors?: true
# )
# |> Enum.sort_by(fn
# {:ok, result} ->
# result.name
# _ ->
# nil
# end)
# end
test "bulk creates can create relationships" do
Ash.bulk_create!(
[%{title: "fred", rating: %{score: 5}}, %{title: "george", rating: %{score: 0}}],
Post,
:create
)
assert [
%{title: "fred", ratings: [%{score: 5}]},
%{title: "george", ratings: [%{score: 0}]}
] =
Post
|> Ash.Query.sort(:title)
|> Ash.Query.load(:ratings)
|> Ash.read!()
end
test "bulk creates with integer primary key return records" do
%Ash.BulkResult{records: records} =
Ash.bulk_create!(
[%{title: "first"}, %{title: "second"}, %{title: "third"}],
IntegerPost,
:create,
return_records?: true
)
assert length(records) == 3
end
end
describe "validation errors" do
test "skips invalid with stop_on_error?: false" do
assert %{records: [_], errors: [_]} =
Ash.bulk_create([%{title: "fred"}, %{title: "not allowed"}], Post, :create,
stop_on_error?: false,
return_records?: true,
return_errors?: true
)
end
test "returns errors in the stream" do
assert [{:ok, _}, {:error, _}] =
Ash.bulk_create!([%{title: "fred"}, %{title: "not allowed"}], Post, :create,
return_records?: true,
return_stream?: true,
return_errors?: true
)
|> Enum.to_list()
end
test "handle allow_nil? false correctly" do
assert %{
errors: [
%Ash.Error.Invalid{errors: [%Ash.Error.Changes.Required{field: :full_name}]}
]
} =
Ash.bulk_create([%{full_name: ""}], Record, :create,
return_records?: true,
return_errors?: true
)
end
end
describe "database errors" do
test "database errors affect the entire batch" do
# assert %{records: [_], errors: [_]} =
Ash.bulk_create(
[%{title: "fred"}, %{title: "george", organization_id: Ash.UUID.generate()}],
Post,
:create,
return_records?: true
)
assert [] =
Post
|> Ash.Query.sort(:title)
|> Ash.read!()
end
test "database errors don't affect other batches" do
Ash.bulk_create(
[%{title: "george", organization_id: Ash.UUID.generate()}, %{title: "fred"}],
Post,
:create,
stop_on_error?: false,
return_records?: true,
batch_size: 1
)
assert [%{title: "fred"}] =
Post
|> Ash.Query.sort(:title)
|> Ash.read!()
end
end
describe "nested bulk operations" do
test "supports bulk_create in after_action callbacks" do
result =
Ash.bulk_create!(
[%{title: "trigger_nested"}],
Post,
:create_with_nested_bulk_create,
return_records?: true,
authorize?: false
)
# Assert the bulk result contains the expected data
assert %Ash.BulkResult{records: [original_post]} = result
assert original_post.title == "trigger_nested"
# Verify all posts that should exist after the nested operation
all_posts =
Post
|> Ash.Query.sort(:title)
|> Ash.read!()
# Should have: 1 original + 2 nested = 3 total posts
assert length(all_posts) == 3
# Verify we have the expected posts with correct titles
post_titles = Enum.map(all_posts, & &1.title) |> Enum.sort()
assert post_titles == ["nested_post_1", "nested_post_2", "trigger_nested"]
# Verify the specific nested posts were created by the after_action callback
nested_posts =
Post
|> Ash.Query.filter(expr(title in ["nested_post_1", "nested_post_2"]))
|> Ash.Query.sort(:title)
|> Ash.read!()
assert length(nested_posts) == 2
assert [%{title: "nested_post_1"}, %{title: "nested_post_2"}] = nested_posts
# Verify that each nested post has proper metadata
Enum.each(nested_posts, fn post ->
assert is_binary(post.id)
assert post.title in ["nested_post_1", "nested_post_2"]
end)
end
test "supports bulk_update in after_action callbacks" do
# Create the original post - the after_action callback will create and update additional posts
result =
Ash.bulk_create!(
[%{title: "trigger_nested_update"}],
Post,
:create_with_nested_bulk_update,
return_records?: true,
authorize?: false
)
# Assert the bulk result contains the expected data
assert %Ash.BulkResult{records: [original_post]} = result
assert original_post.title == "trigger_nested_update"
# Verify all posts that should exist after the nested operations
# The after_action callback should have created 2 posts and updated them
all_posts =
Post
|> Ash.Query.sort(:title)
|> Ash.read!()
# Should have: 1 original + 2 created and updated = 3 total posts
assert length(all_posts) == 3
# Verify the original post still exists
original_posts =
Post
|> Ash.Query.filter(expr(title == "trigger_nested_update"))
|> Ash.read!()
assert length(original_posts) == 1
assert hd(original_posts).title == "trigger_nested_update"
# Verify the nested posts were created and then updated by the after_action callback
updated_posts =
Post
|> Ash.Query.filter(expr(title == "updated_via_nested_bulk"))
|> Ash.read!()
assert length(updated_posts) == 2
# Verify that the updated posts have proper metadata and were actually updated
Enum.each(updated_posts, fn post ->
assert is_binary(post.id)
assert post.title == "updated_via_nested_bulk"
end)
# Verify no posts remain with the intermediate titles (they should have been updated)
intermediate_posts =
Post
|> Ash.Query.filter(expr(title in ["post_to_update_1", "post_to_update_2"]))
|> Ash.read!()
assert intermediate_posts == [],
"Posts should have been updated, not left with intermediate titles"
end
test "nested bulk operations handle metadata indexing correctly" do
# Create multiple posts in the parent bulk operation to test indexing
# Each parent post's after_action callback will create nested posts
result =
Ash.bulk_create!(
[
%{title: "trigger_nested"},
%{title: "trigger_nested_2"}
],
Post,
:create_with_nested_bulk_create,
return_records?: true,
authorize?: false
)
# Assert both parent posts were created
assert %Ash.BulkResult{records: parent_posts} = result
assert length(parent_posts) == 2
parent_titles = Enum.map(parent_posts, & &1.title) |> Enum.sort()
assert parent_titles == ["trigger_nested", "trigger_nested_2"]
# Verify total posts: 2 parent + (2 nested per parent from after_action) = 6 total
all_posts = Post |> Ash.Query.sort(:title) |> Ash.read!()
assert length(all_posts) == 6
# Count posts by type
nested_posts =
Post
|> Ash.Query.filter(expr(title in ["nested_post_1", "nested_post_2"]))
|> Ash.read!()
# Should have 4 nested posts (2 for each parent operation via after_action callbacks)
assert length(nested_posts) == 4
# Verify each nested post has proper structure
Enum.each(nested_posts, fn post ->
assert is_binary(post.id)
assert post.title in ["nested_post_1", "nested_post_2"]
end)
end
end
end