-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
embedded_test.exs
1177 lines (940 loc) · 43.6 KB
/
embedded_test.exs
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
863
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Ecto.Changeset.EmbeddedTest do
use ExUnit.Case, async: true
alias Ecto.Changeset
alias Ecto.Changeset.Relation
alias Ecto.Embedded
alias Ecto.TestRepo
alias __MODULE__.Author
alias __MODULE__.Profile
alias __MODULE__.Post
alias __MODULE__.Nested
defmodule Author do
use Ecto.Schema
schema "authors" do
field :name, :string
embeds_one :profile, Profile, on_replace: :delete
embeds_one :raise_profile, Profile, on_replace: :raise
embeds_one :invalid_profile, Profile, on_replace: :mark_as_invalid
embeds_one :update_profile, Profile, on_replace: :update
embeds_one :inline_profile, Profile do
field :name, :string
end
embeds_one :nested, Nested
embeds_many :posts, Post, on_replace: :delete
embeds_many :raise_posts, Post, on_replace: :raise
embeds_many :invalid_posts, Post, on_replace: :mark_as_invalid
embeds_many :inline_posts, Post do
field :title, :string
end
end
end
defmodule Post do
use Ecto.Schema
import Ecto.Changeset
schema "posts" do
field :title, :string
field :position, :integer
end
def changeset(schema, params) do
cast(schema, params, ~w(title)a)
|> validate_required(:title)
|> validate_length(:title, min: 3)
end
def changeset_with_position(schema, params, position) do
cast(schema, params, ~w(title)a)
|> validate_required(:title)
|> validate_length(:title, min: 3)
|> put_change(:position, position)
end
def optional_changeset(schema, params) do
cast(schema, params, ~w(title)a)
end
def set_action(schema, params) do
changeset(schema, params)
|> Map.put(:action, Map.get(params, :action, :update))
end
end
defmodule Profile do
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
field :name
end
def changeset(schema, params) do
cast(schema, params, ~w(name id)a)
|> validate_required(:name)
|> validate_length(:name, min: 3)
end
def optional_changeset(schema, params) do
cast(schema, params, ~w(name)a)
end
def set_action(schema, params) do
changeset(schema, params)
|> Map.put(:action, Map.get(params, :action, :update))
end
def changeset_with_position(schema, params, _position) do
cast(schema, params, ~w(name id)a)
|> validate_required(:name)
|> validate_length(:name, min: 3)
end
end
defmodule Nested do
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
embeds_one :profile, Profile
end
def changeset(schema, params) do
cast(schema, params, [])
|> cast_embed(:profile)
end
end
defmodule NestedInline do
use Ecto.Schema
alias Alias.One, warn: false
alias Alias.Many, warn: false
alias Alias.OneNoPK, warn: false
alias Alias.ManyNoPK, warn: false
schema "nested_inline" do
embeds_one :root, Elixir.Root do
field :name, :string
end
embeds_one :expanded, :"Elixir.Expanded" do
field :name, :string
end
embeds_one :one, One do
field :name, :string
end
embeds_one :one_no_pk, OneNoPK, primary_key: false do
field :name, :string
end
embeds_many :many, Many do
field :title, :string
end
embeds_many :many_no_pk, ManyNoPK, primary_key: false do
field :title, :string
end
end
end
test "nested inline embed can be a root module" do
assert Root.__schema__(:fields) == [:id, :name]
assert �to.Embedded{related: Root} = NestedInline.__schema__(:embed, :root)
end
test "nested inline embed alias can already be expanded" do
assert Expanded.__schema__(:fields) == [:id, :name]
assert �to.Embedded{related: Expanded} = NestedInline.__schema__(:embed, :expanded)
end
test "nested inilne embed overwrites conflicting alias" do
assert NestedInline.One.__schema__(:fields) == [:id, :name]
assert NestedInline.OneNoPK.__schema__(:fields) == [:name]
assert NestedInline.Many.__schema__(:fields) == [:id, :title]
assert NestedInline.ManyNoPK.__schema__(:fields) == [:title]
assert �to.Embedded{related: NestedInline.One} = NestedInline.__schema__(:embed, :one)
assert �to.Embedded{related: NestedInline.OneNoPK} = NestedInline.__schema__(:embed, :one_no_pk)
assert �to.Embedded{related: NestedInline.Many} = NestedInline.__schema__(:embed, :many)
assert �to.Embedded{related: NestedInline.ManyNoPK} = NestedInline.__schema__(:embed, :many_no_pk)
end
defp cast(schema, params, embed, opts \\ []) do
schema
|> Changeset.cast(params, ~w())
|> Changeset.cast_embed(embed, opts)
end
## Cast embeds one
test "cast embeds_one with valid params" do
changeset = cast(%Author{}, %{"profile" => %{"name" => "michal"}}, :profile)
profile = changeset.changes.profile
assert profile.changes == %{name: "michal"}
assert profile.errors == []
assert profile.action == :insert
assert profile.valid?
assert changeset.valid?
end
test "cast embeds_one with invalid params" do
changeset = cast(%Author{}, %{"profile" => %{}}, :profile)
assert changeset.changes.profile.changes == %{}
assert changeset.changes.profile.errors == [name: {"can't be blank", [validation: :required]}]
assert changeset.changes.profile.action == :insert
refute changeset.changes.profile.valid?
refute changeset.valid?
# string
changeset = cast(%Author{}, %{"profile" => "value"}, :profile, required: true)
assert changeset.errors == [profile: {"is invalid", [validation: :embed, type: :map]}]
refute changeset.valid?
# list
changeset = cast(%Author{}, %{"profile" => [%{"name" => "michal"}]}, :profile)
assert changeset.changes == %{}
assert changeset.errors == [profile: {"is invalid", [validation: :embed, type: :map]}]
end
test "cast embeds_one with existing struct updating" do
changeset = cast(%Author{profile: %Profile{name: "michal", id: "michal"}},
%{"profile" => %{"name" => "new", "id" => "michal"}}, :profile)
profile = changeset.changes.profile
assert profile.changes == %{name: "new"}
assert profile.errors == []
assert profile.action == :update
assert profile.valid?
assert changeset.valid?
end
test "cast embeds_one with existing struct updating from atom params" do
# Emulate atom params from nested associations
changeset = Changeset.cast(%Author{profile: %Profile{name: "michal", id: "michal"}}, %{}, ~w())
changeset = put_in changeset.params, %{"profile" => %{name: "new", id: "michal"}}
changeset = Changeset.cast_embed(changeset, :profile, [])
profile = changeset.changes.profile
assert profile.changes == %{name: "new"}
assert profile.errors == []
assert profile.action == :update
assert profile.valid?
assert changeset.valid?
end
test "cast embeds_one with existing struct replacing" do
changeset = cast(%Author{profile: %Profile{name: "michal", id: "michal"}},
%{"profile" => %{"name" => "new"}}, :profile)
profile = changeset.changes.profile
assert profile.changes == %{name: "new"}
assert profile.errors == []
assert profile.action == :insert
assert profile.valid?
assert changeset.valid?
changeset = cast(%Author{profile: %Profile{name: "michal", id: "michal"}},
%{"profile" => %{"name" => "new", "id" => "new"}}, :profile)
profile = changeset.changes.profile
assert profile.changes == %{name: "new", id: "new"}
assert profile.errors == []
assert profile.action == :insert
assert profile.valid?
assert changeset.valid?
assert_raise RuntimeError, ~r"cannot update related", fn ->
cast(%Author{profile: %Profile{name: "michal", id: "michal"}},
%{"profile" => %{"name" => "new", "id" => "new"}},
:profile, with: &Profile.set_action/2)
end
end
test "cast embeds_one without changes skips" do
changeset = cast(%Author{profile: %Profile{name: "michal", id: "michal"}},
%{"profile" => %{"id" => "michal"}}, :profile)
assert changeset.changes == %{}
assert changeset.errors == []
end
test "cast embeds_on discards changesets marked as ignore" do
changeset = cast(%Author{},
%{"profile" => %{name: "michal", id: "id", action: :ignore}},
:profile, with: &Profile.set_action/2)
assert changeset.changes == %{}
end
test "cast embeds_one when required" do
changeset = cast(%Author{profile: nil}, %{}, :profile, required: true)
assert changeset.required == [:profile]
assert changeset.changes == %{}
assert changeset.errors == [profile: {"can't be blank", [validation: :required]}]
changeset = cast(%Author{profile: nil}, %{}, :profile, required: true, required_message: "a custom message")
assert changeset.required == [:profile]
assert changeset.changes == %{}
assert changeset.errors == [profile: {"a custom message", [validation: :required]}]
changeset = cast(%Author{profile: %Profile{}}, %{}, :profile, required: true)
assert changeset.required == [:profile]
assert changeset.changes == %{}
assert changeset.errors == []
changeset = cast(%Author{profile: nil}, %{"profile" => nil}, :profile, required: true)
assert changeset.required == [:profile]
assert changeset.changes == %{}
assert changeset.errors == [profile: {"can't be blank", [validation: :required]}]
changeset = cast(%Author{profile: %Profile{}}, %{"profile" => nil}, :profile, required: true)
assert changeset.required == [:profile]
assert changeset.changes == %{profile: nil}
assert changeset.errors == [profile: {"can't be blank", [validation: :required]}]
end
test "cast embeds_one with optional" do
changeset = cast(%Author{profile: %Profile{id: "id"}}, %{"profile" => nil}, :profile)
assert changeset.changes.profile == nil
assert changeset.valid?
end
test "cast embeds_one with `:force_update_on_change` option" do
changeset = cast(%Author{profile: %Profile{id: "id"}}, %{profile: nil}, :profile,
force_update_on_change: true)
assert changeset.repo_opts[:force]
changeset = cast(%Author{profile: %Profile{id: "id"}}, %{profile: nil}, :profile,
force_update_on_change: false)
assert changeset.repo_opts == []
changeset = cast(%Author{profile: nil}, %{profile: nil}, :profile, force_update_on_change: true)
assert changeset.repo_opts == []
end
test "cast embeds_one with custom changeset" do
changeset = cast(%Author{}, %{"profile" => %{"name" => "michal"}}, :profile,
with: &Profile.optional_changeset/2)
assert (changeset.types.profile |> elem(1)).on_cast == &Profile.optional_changeset/2
profile = changeset.changes.profile
assert profile.changes == %{name: "michal"}
assert profile.errors == []
assert profile.action == :insert
assert profile.valid?
assert changeset.valid?
end
test "cast embeds_one keeps appropriate action from changeset" do
changeset = cast(%Author{profile: %Profile{id: "id"}},
%{"profile" => %{"name" => "michal", "id" => "id"}},
:profile, with: &Profile.set_action/2)
assert changeset.changes.profile.action == :update
assert_raise RuntimeError, ~r"cannot update related", fn ->
cast(%Author{profile: %Profile{id: "old"}},
%{"profile" => %{"name" => "michal", "id" => "new"}},
:profile, with: &Profile.set_action/2)
end
end
test "cast embeds_one with empty parameters" do
changeset = cast(%Author{profile: nil}, %{}, :profile)
assert changeset.changes == %{}
changeset = cast(%Author{profile: %Profile{}}, %{}, :profile)
assert changeset.changes == %{}
end
test "cast embeds_one with on_replace: :raise" do
schema = %Author{raise_profile: %Profile{id: 1}}
params = %{"raise_profile" => %{"name" => "jose", "id" => 1}}
changeset = cast(schema, params, :raise_profile)
assert changeset.changes.raise_profile.action == :update
params = %{"raise_profile" => nil}
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
cast(schema, params, :raise_profile)
end
params = %{"raise_profile" => %{"name" => "new", "id" => 2}}
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
cast(schema, params, :raise_profile)
end
end
test "cast embeds_one with on_replace: :mark_as_invalid" do
schema = %Author{invalid_profile: %Profile{id: 1}}
changeset = cast(schema, %{"invalid_profile" => nil}, :invalid_profile)
assert changeset.changes == %{}
assert changeset.errors == [invalid_profile: {"is invalid", [validation: :embed, type: :map]}]
refute changeset.valid?
changeset = cast(schema, %{"invalid_profile" => %{"id" => 2}}, :invalid_profile)
assert changeset.changes == %{}
assert changeset.errors == [invalid_profile: {"is invalid", [validation: :embed, type: :map]}]
refute changeset.valid?
changeset = cast(schema, %{"invalid_profile" => nil}, :invalid_profile, invalid_message: "a custom message")
assert changeset.changes == %{}
assert changeset.errors == [invalid_profile: {"a custom message", [validation: :embed, type: :map]}]
refute changeset.valid?
end
test "cast embeds_one with on_replace: :update" do
{:ok, schema} = TestRepo.insert(%Author{name: "Enio",
update_profile: %Profile{name: "Enio"}})
changeset = cast(schema, %{"update_profile" => %{name: "Jose"}}, :update_profile)
assert changeset.changes.update_profile.changes == %{name: "Jose"}
assert changeset.changes.update_profile.action == :update
assert changeset.errors == []
assert changeset.valid?
end
test "raises when :update is used on embeds_many" do
error_message = "invalid `:on_replace` option for :tags. The only valid " <>
"options are: `:raise`, `:mark_as_invalid`, `:delete`"
assert_raise ArgumentError, error_message, fn ->
defmodule Topic do
use Ecto.Schema
schema "topics" do
embeds_many :tags, Tag, on_replace: :update
end
end
end
end
test "cast inline embeds_one with valid params" do
changeset = cast(%Author{}, %{"inline_profile" => %{"name" => "michal"}},
:inline_profile, with: &Profile.changeset/2)
profile = changeset.changes.inline_profile
assert profile.changes == %{name: "michal"}
assert profile.errors == []
assert profile.action == :insert
assert profile.valid?
assert changeset.valid?
end
## cast embeds many
test "cast embeds_many with only new schemas" do
changeset = cast(%Author{}, %{"posts" => [%{"title" => "hello"}]}, :posts)
[post_change] = changeset.changes.posts
assert post_change.changes == %{title: "hello"}
assert post_change.errors == []
assert post_change.action == :insert
assert post_change.valid?
assert changeset.valid?
end
test "cast embeds_many with map" do
changeset = cast(%Author{}, %{"posts" => %{0 => %{"title" => "hello"}}}, :posts)
[post_change] = changeset.changes.posts
assert post_change.changes == %{title: "hello"}
assert post_change.errors == []
assert post_change.action == :insert
assert post_change.valid?
assert changeset.valid?
end
test "cast embeds_many with map and sort_param and delete_param" do
posts = %{1 => %{"title" => "one"}, 2 => %{"title" => "two"}, 3 => %{"title" => "three"}}
opts = [sort_param: :sort, drop_param: :drop]
changeset = cast(%Author{}, %{"posts" => posts}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ~w(one two three)
changeset = cast(%Author{}, %{"posts" => posts, "drop" => [2]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ["one", "three"]
changeset = cast(%Author{}, %{posts: posts, drop: [2]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ["one", "three"]
changeset = cast(%Author{}, %{"posts" => posts, "sort" => [2, 3, 1]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ~w(two three one)
changeset = cast(%Author{}, %{sort: ["new"]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == [nil]
changeset = cast(%Author{}, %{posts: posts, sort: [2, 3, 1]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ~w(two three one)
changeset = cast(%Author{}, %{posts: posts, sort: [2]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ~w(two one three)
changeset = cast(%Author{}, %{posts: posts, sort: [2, "new"]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ["two", nil, "one", "three"]
changeset = cast(%Author{}, %{posts: posts, sort: [3, 2, 1], drop: [2]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ["three", "one"]
changeset = cast(%Author{}, %{posts: posts, sort: [1, "new"], drop: [2, "new"]}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ["one", "three"]
changeset = cast(%Author{}, %{posts: posts, sort: :x, drop: :y}, :posts, opts)
assert Enum.map(changeset.changes.posts, & &1.changes[:title]) == ["one", "two", "three"]
end
test "cast embeds_many with custom changeset" do
changeset = cast(%Author{}, %{"posts" => [%{"title" => "hello"}]},
:posts, with: &Post.optional_changeset/2)
[post_change] = changeset.changes.posts
assert post_change.changes == %{title: "hello"}
assert post_change.errors == []
assert post_change.action == :insert
assert post_change.valid?
assert changeset.valid?
end
# Please note the order is important in this test.
test "cast embeds_many changing schemas" do
posts = [%Post{title: "first", id: 1},
%Post{title: "second", id: 2},
%Post{title: "third", id: 3}]
params = [%{"title" => "new"},
%{"id" => "2", "title" => nil},
%{"id" => "3", "title" => "new name"}]
changeset = cast(%Author{posts: posts}, %{"posts" => params}, :posts)
[first, new, second, third] = changeset.changes.posts
assert first.data.id == 1
assert first.required == [] # Check for not running changeset function
assert first.action == :replace
assert first.valid?
assert new.changes == %{title: "new"}
assert new.action == :insert
assert new.valid?
assert second.data.id == 2
assert second.errors == [title: {"can't be blank", [validation: :required]}]
assert second.action == :update
refute second.valid?
assert third.data.id == 3
assert third.action == :update
assert third.valid?
refute changeset.valid?
end
test "cast embeds_many with invalid operation" do
params = %{"posts" => [%{"id" => 1, "title" => "new"}]}
assert_raise RuntimeError, ~r"cannot update related", fn ->
cast(%Author{posts: []}, params, :posts, with: &Post.set_action/2)
end
end
test "cast embeds_many with invalid params" do
changeset = cast(%Author{}, %{"posts" => "value"}, :posts)
assert changeset.errors == [posts: {"is invalid", [validation: :embed, type: {:array, :map}]}]
refute changeset.valid?
changeset = cast(%Author{}, %{"posts" => ["value"]}, :posts)
assert changeset.errors == [posts: {"is invalid", [validation: :embed, type: {:array, :map}]}]
refute changeset.valid?
changeset = cast(%Author{}, %{"posts" => nil}, :posts)
assert changeset.errors == [posts: {"is invalid", [validation: :embed, type: {:array, :map}]}]
refute changeset.valid?
changeset = cast(%Author{}, %{"posts" => %{"id" => "invalid"}}, :posts)
assert changeset.errors == [posts: {"is invalid", [validation: :embed, type: {:array, :map}]}]
refute changeset.valid?
end
test "cast embeds_many without changes skips" do
changeset = cast(%Author{posts: [%Post{title: "hello", id: 1}]},
%{"posts" => [%{"id" => 1}]}, :posts)
refute Map.has_key?(changeset.changes, :posts)
end
test "cast embeds_many discards changesets marked as ignore" do
changeset = cast(%Author{},
%{"posts" => [%{title: "oops", action: :ignore}]},
:posts, with: &Post.set_action/2)
assert changeset.changes == %{}
posts = [
%{title: "hello", action: :insert},
%{title: "oops", action: :ignore},
%{title: "world", action: :insert}
]
changeset = cast(%Author{}, %{"posts" => posts},
:posts, with: &Post.set_action/2)
assert Enum.map(changeset.changes.posts, &Ecto.Changeset.get_change(&1, :title)) ==
["hello", "world"]
end
test "cast embeds_many when required" do
changeset = cast(%Author{posts: []}, %{}, :posts, required: true)
assert changeset.required == [:posts]
assert changeset.changes == %{}
assert changeset.errors == [posts: {"can't be blank", [validation: :required]}]
changeset = cast(%Author{posts: []}, %{"posts" => nil}, :posts, required: true)
assert changeset.changes == %{}
assert changeset.errors == [posts: {"is invalid", [validation: :embed, type: {:array, :map}]}]
end
test "cast embeds_many with `:force_update_on_change` option" do
params = [%{title: "hello"}]
changeset = cast(%Author{}, %{posts: params}, :posts, force_update_on_change: true)
assert changeset.repo_opts[:force]
changeset = cast(%Author{}, %{posts: params}, :posts, force_update_on_change: false)
assert changeset.repo_opts == []
changeset = cast(%Author{posts: [%Post{title: "hello"}]}, %{posts: params}, :posts,
force_update_on_change: true)
assert changeset.repo_opts == []
end
test "cast embeds_many with empty parameters" do
changeset = cast(%Author{posts: []}, %{}, :posts)
assert changeset.changes == %{}
changeset = cast(%Author{posts: [%Post{}]}, %{}, :posts)
assert changeset.changes == %{}
end
test "cast embeds_many with on_replace: :raise" do
schema = %Author{raise_posts: [%Post{id: 1}]}
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
cast(schema, %{"raise_posts" => []}, :raise_posts)
end
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
cast(schema, %{"raise_posts" => [%{"id" => 2}]}, :raise_posts)
end
end
test "cast embeds_many with on_replace: :mark_as_invalid" do
schema = %Author{invalid_posts: [%Post{id: 1}]}
changeset = cast(schema, %{"invalid_posts" => []}, :invalid_posts)
assert changeset.changes == %{}
assert changeset.errors == [invalid_posts: {"is invalid", [validation: :embed, type: {:array, :map}]}]
refute changeset.valid?
changeset = cast(schema, %{"invalid_posts" => [%{"id" => 2}]}, :invalid_posts)
assert changeset.changes == %{}
assert changeset.errors == [invalid_posts: {"is invalid", [validation: :embed, type: {:array, :map}]}]
refute changeset.valid?
end
test "argument error when casting embeds_one with arity 3 function" do
assert_raise(ArgumentError, ~r/invalid \:with function for relation \:profile/, fn ->
params = %{"profile" => %{"name" => "John"}}
cast(%Author{}, params, :profile, with: &Profile.changeset_with_position/3)
end)
end
test "sends index when casting embeds_many with arity 3 function" do
changeset = cast(%Author{}, %{"posts" => [%{"title" => "hello"}, %{"title" => "bye"}]},
:posts, with: &Post.changeset_with_position/3)
[post, post2] = changeset.changes.posts
assert post.changes == %{title: "hello", position: 0}
assert post2.changes == %{title: "bye", position: 1}
end
## Others
test "validate_required/3 with has_many raises" do
base_changeset = Changeset.change(%Author{})
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
changeset = Changeset.validate_required(base_changeset, :posts)
assert changeset.valid?
end) =~ ~r/attempting to determine the presence of embed_many field/
end
test "change embeds_one" do
embed = Author.__schema__(:embed, :profile)
assert {:ok, nil, true} = Relation.change(embed, nil, nil)
assert {:ok, nil, true} = Relation.change(embed, nil, %Profile{})
embed_schema = %Profile{}
embed_schema_changeset = Changeset.change(embed_schema, name: "michal")
assert {:ok, changeset, true} =
Relation.change(embed, embed_schema_changeset, nil)
assert changeset.action == :insert
assert changeset.changes == %{name: "michal"}
assert {:ok, changeset, true} =
Relation.change(embed, embed_schema_changeset, embed_schema)
assert changeset.action == :update
assert changeset.changes == %{name: "michal"}
assert :ignore = Relation.change(embed, %{embed_schema_changeset | action: :ignore}, nil)
empty_changeset = Changeset.change(embed_schema)
assert :ignore = Relation.change(embed, empty_changeset, embed_schema)
embed_with_id = %Profile{id: 2}
assert {:ok, _, true} =
Relation.change(embed, %Profile{id: 1}, embed_with_id)
end
test "change embeds_one with attributes" do
embed = Author.__schema__(:embed, :profile)
assert {:ok, changeset, true} =
Relation.change(embed, %{name: "michal"}, nil)
assert changeset.action == :insert
assert changeset.changes == %{name: "michal"}
profile = %Profile{name: "other"}
assert {:ok, changeset, true} =
Relation.change(embed, %{name: "michal"}, profile)
assert changeset.action == :update
assert changeset.changes == %{name: "michal"}
assert {:ok, changeset, true} =
Relation.change(embed, [name: "michal"], profile)
assert changeset.action == :update
assert changeset.changes == %{name: "michal"}
end
test "change embeds_one with structs" do
embed = Author.__schema__(:embed, :profile)
profile = %Profile{name: "michal"}
assert {:ok, changeset, true} =
Relation.change(embed, profile, nil)
assert changeset.action == :insert
end
test "change embeds_one keeps appropriate action from changeset" do
embed = Author.__schema__(:embed, :profile)
embed_schema = %Profile{id: 1}
# Adding
changeset = %{Changeset.change(embed_schema, name: "michal") | action: :insert}
{:ok, changeset, _} = Relation.change(embed, changeset, nil)
assert changeset.action == :insert
changeset = %{changeset | action: :update}
{:ok, changeset, _} = Relation.change(embed, changeset, nil)
assert changeset.action == :update
changeset = %{changeset | action: :delete}
{:ok, changeset, _} = Relation.change(embed, changeset, nil)
assert changeset.action == :delete
# Replacing
changeset = %{changeset | action: :insert}
assert_raise RuntimeError, ~r/cannot insert related/, fn ->
Relation.change(embed, changeset, embed_schema)
end
changeset = %{changeset | action: :update}
{:ok, changeset, _} = Relation.change(embed, changeset, embed_schema)
assert changeset.action == :update
changeset = %{changeset | action: :delete}
{:ok, changeset, _} = Relation.change(embed, changeset, embed_schema)
assert changeset.action == :delete
end
test "change embeds_one with on_replace: :raise" do
embed_schema = %Profile{id: 1}
base_changeset = Changeset.change(%Author{raise_profile: embed_schema})
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
Changeset.put_embed(base_changeset, :raise_profile, nil)
end
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
Changeset.put_embed(base_changeset, :raise_profile, %Profile{id: 2})
end
end
test "change embeds_one with on_replace: :mark_as_invalid" do
embed_schema = %Profile{id: 1}
base_changeset = Changeset.change(%Author{invalid_profile: embed_schema})
changeset = Changeset.put_embed(base_changeset, :invalid_profile, nil)
assert changeset.changes == %{}
assert changeset.errors == [invalid_profile: {"is invalid", [type: :map]}]
refute changeset.valid?
changeset = Changeset.put_embed(base_changeset, :invalid_profile, %Profile{id: 2})
assert changeset.changes == %{}
assert changeset.errors == [invalid_profile: {"is invalid", [type: :map]}]
refute changeset.valid?
end
test "change embeds_many" do
embed = Author.__schema__(:embed, :posts)
assert {:ok, [], true} = Relation.change(embed, [], [])
assert {:ok, [old_changeset, new_changeset], true} =
Relation.change(embed, [%Post{id: 1}], [%Post{id: 2}])
assert old_changeset.action == :replace
assert new_changeset.action == :insert
embed_schema_changeset = Changeset.change(%Post{}, title: "hello")
assert {:ok, [changeset], true} =
Relation.change(embed, [embed_schema_changeset], [])
assert changeset.action == :insert
assert changeset.changes == %{title: "hello"}
embed_schema = %Post{id: 1}
embed_schema_changeset = Changeset.change(embed_schema, title: "hello")
assert {:ok, [changeset], true} =
Relation.change(embed, [embed_schema_changeset], [embed_schema])
assert changeset.action == :update
assert changeset.changes == %{title: "hello"}
assert {:ok, [changeset], true} =
Relation.change(embed, [], [embed_schema_changeset])
assert changeset.action == :replace
embed_schemas = [%Post{id: 1}, %Post{id: 2}]
assert {:ok, [changeset1, changeset2], true} =
Relation.change(embed, Enum.reverse(embed_schemas), embed_schemas)
assert changeset1.action == :update
assert changeset2.action == :update
assert :ignore =
Relation.change(embed, [%{embed_schema_changeset | action: :ignore}], [embed_schema])
assert :ignore =
Relation.change(embed, [%{embed_schema_changeset | action: :ignore}], [])
empty_changeset = Changeset.change(embed_schema)
assert :ignore = Relation.change(embed, [empty_changeset], [embed_schema])
end
test "change embeds_many with attributes" do
assoc = Author.__schema__(:embed, :posts)
assert {:ok, [changeset], true} =
Relation.change(assoc, [%{title: "hello"}], [])
assert changeset.action == :insert
assert changeset.changes == %{title: "hello"}
post = %Post{title: "other"} |> Ecto.put_meta(state: :loaded)
assert {:ok, [changeset], true} =
Relation.change(assoc, [%{title: "hello"}], [post])
assert changeset.action == :update
assert changeset.changes == %{title: "hello"}
assert {:ok, [changeset], true} =
Relation.change(assoc, [[title: "hello"]], [post])
assert changeset.action == :update
assert changeset.changes == %{title: "hello"}
post = %Post{title: "other"}
assert {:ok, [changeset], true} =
Relation.change(assoc, [%{title: "hello"}], [post])
assert changeset.action == :insert
assert changeset.changes == %{title: "hello"}
assert {:ok, [changeset], true} =
Relation.change(assoc, [[title: "hello"]], [post])
assert changeset.action == :insert
assert changeset.changes == %{title: "hello"}
end
test "change embeds_many with structs" do
embed = Author.__schema__(:embed, :posts)
post = %Post{title: "hello"}
assert {:ok, [changeset], true} =
Relation.change(embed, [post], [])
assert changeset.action == :insert
assert {:ok, [changeset], true} =
Relation.change(embed, [Ecto.put_meta(post, state: :loaded)], [])
assert changeset.action == :update
assert {:ok, [changeset], true} =
Relation.change(embed, [Ecto.put_meta(post, state: :deleted)], [])
assert changeset.action == :delete
end
test "change embeds_many with on_replace: :raise" do
embed_schema = %Post{id: 1}
base_changeset = Changeset.change(%Author{raise_posts: [embed_schema]})
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
Changeset.put_embed(base_changeset, :raise_posts, [])
end
assert_raise RuntimeError, ~r"you are attempting to change relation", fn ->
Changeset.put_embed(base_changeset, :raise_posts, [%Post{id: 2}])
end
end
test "change embeds_many with on_replace: :mark_as_invalid" do
embed_schema = %Post{id: 1}
base_changeset = Changeset.change(%Author{invalid_posts: [embed_schema]})
changeset = Changeset.put_embed(base_changeset, :invalid_posts, [])
assert changeset.changes == %{}
assert changeset.errors == [invalid_posts: {"is invalid", [type: {:array, :map}]}]
refute changeset.valid?
changeset = Changeset.put_embed(base_changeset, :invalid_posts, [%Post{id: 2}])
assert changeset.changes == %{}
assert changeset.errors == [invalid_posts: {"is invalid", [type: {:array, :map}]}]
refute changeset.valid?
end
test "put_embed/4 insert with embeds_one" do
base_changeset = Changeset.change(%Author{})
changeset = Changeset.put_embed(base_changeset, :profile, %Profile{name: "michal"})
assert �to.Changeset{} = changeset.changes.profile
assert changeset.changes.profile.action == :insert
changeset = Changeset.put_embed(base_changeset, :profile, Changeset.change(%Profile{name: "michal"}))
assert �to.Changeset{} = changeset.changes.profile
assert changeset.changes.profile.action == :insert
changeset = Changeset.put_embed(base_changeset, :profile, %{name: "michal"})
assert �to.Changeset{} = changeset.changes.profile
assert changeset.changes.profile.action == :insert
changeset = Changeset.put_embed(base_changeset, :profile, name: "michal")
assert �to.Changeset{} = changeset.changes.profile
assert changeset.changes.profile.action == :insert
end
test "put_embed/4 update with embeds_one" do
base_changeset = Changeset.change(%Author{profile: %Profile{id: 1, name: "michal"}})
update_changeset = Changeset.change(%Profile{id: 1, name: "michal"}, %{name: "tom"})
changeset = Changeset.put_embed(base_changeset, :profile, update_changeset)
assert �to.Changeset{} = changeset.changes.profile
assert changeset.changes.profile.action == :update
changeset = Changeset.put_embed(base_changeset, :profile, %{id: 1, name: "tom"})
assert �to.Changeset{} = changeset.changes.profile
assert changeset.changes.profile.action == :update
changeset = Changeset.put_embed(base_changeset, :profile, id: 1, name: "tom")
assert �to.Changeset{} = changeset.changes.profile
assert changeset.changes.profile.action == :update
changeset = Changeset.put_embed(base_changeset, :profile, %Profile{id: 1, name: "tom"})
refute Map.has_key?(changeset.changes, :profile)
changeset = Changeset.put_embed(base_changeset, :profile, %Profile{id: 1, name: "michal"})
refute Map.has_key?(changeset.changes, :profile)
end
test "put_embed/4 with embeds_one and empty" do
changeset =
%Author{}
|> Changeset.change()
|> Changeset.put_embed(:profile, nil)
refute Map.has_key?(changeset.changes, :profile)
changeset =
%Author{profile: nil}
|> Changeset.change()
|> Changeset.put_embed(:profile, nil)
refute Map.has_key?(changeset.changes, :profile)
changeset =
%Author{profile: %Profile{}}
|> Changeset.change()
|> Changeset.put_embed(:profile, nil)
assert Map.has_key?(changeset.changes, :profile)
assert changeset.changes[:profile] == nil
changeset =
%Author{}
|> Changeset.change(profile: %Profile{})
|> Changeset.put_embed(:profile, nil)
refute Map.has_key?(changeset.changes, :profile)
end
test "put_change/4 with embeds one" do
changeset = Changeset.change(%Author{}, profile: %Profile{name: "michal"})
assert �to.Changeset{} = changeset.changes.profile
base_changeset = Changeset.change(%Author{profile: %Profile{name: "michal"}})
empty_update_changeset = Changeset.change(%Profile{name: "michal"})
changeset = Changeset.put_change(base_changeset, :profile, empty_update_changeset)
refute Map.has_key?(changeset.changes, :profile)
end
test "put_change/4 with nested embed" do
changeset =
Changeset.change(%Author{}, %{})
|> Changeset.put_change(:nested, %Nested{profile: %Profile{name: "tom"}})
assert {:ok, author} = TestRepo.insert(changeset)
assert author.nested.id
assert author.nested.profile.id
end
test "put_embed/4 with embeds_many" do
base_changeset = Changeset.change(%Author{})
changeset = Changeset.put_embed(base_changeset, :posts, [%{title: "hello"}])
assert [�to.Changeset{}] = changeset.changes.posts
assert hd(changeset.changes.posts).action == :insert
changeset = Changeset.put_embed(base_changeset, :posts, [[title: "hello"]])