@@ -62,7 +62,7 @@ defmodule Ecto.Integration.PreloadTest do
6262 p3 = TestRepo . insert! ( % Post { title: "3" } )
6363
6464 % Permalink { id: pid1 } = TestRepo . insert! ( % Permalink { url: "1" , post_id: p1 . id } )
65- % Permalink { } = TestRepo . insert! ( % Permalink { url: "2" , post_id: nil } )
65+ % Permalink { } = TestRepo . insert! ( % Permalink { url: "2" , post_id: nil } )
6666 % Permalink { id: pid3 } = TestRepo . insert! ( % Permalink { url: "3" , post_id: p3 . id } )
6767
6868 assert % Ecto.Association.NotLoaded { } = p1 . permalink
@@ -126,12 +126,14 @@ defmodule Ecto.Integration.PreloadTest do
126126 % User { id: uid2 } = TestRepo . insert! ( % User { name: "2" } )
127127 % User { id: uid4 } = TestRepo . insert! ( % User { name: "3" } )
128128
129- TestRepo . insert_all "posts_users" , [ [ post_id: p1 . id , user_id: uid1 ] ,
130- [ post_id: p1 . id , user_id: uid2 ] ,
131- [ post_id: p2 . id , user_id: uid3 ] ,
132- [ post_id: p2 . id , user_id: uid4 ] ,
133- [ post_id: p3 . id , user_id: uid1 ] ,
134- [ post_id: p3 . id , user_id: uid4 ] ]
129+ TestRepo . insert_all ( "posts_users" , [
130+ [ post_id: p1 . id , user_id: uid1 ] ,
131+ [ post_id: p1 . id , user_id: uid2 ] ,
132+ [ post_id: p2 . id , user_id: uid3 ] ,
133+ [ post_id: p2 . id , user_id: uid4 ] ,
134+ [ post_id: p3 . id , user_id: uid1 ] ,
135+ [ post_id: p3 . id , user_id: uid4 ]
136+ ] )
135137
136138 assert % Ecto.Association.NotLoaded { } = p1 . users
137139
@@ -258,9 +260,11 @@ defmodule Ecto.Integration.PreloadTest do
258260 % User { id: uid1 } = TestRepo . insert! ( % User { name: "foo" } )
259261 % User { id: uid2 } = TestRepo . insert! ( % User { name: "bar" } )
260262
261- TestRepo . insert_all "posts_users" , [ [ post_id: p1 . id , user_id: uid1 ] ,
262- [ post_id: p1 . id , user_id: uid2 ] ,
263- [ post_id: p2 . id , user_id: uid2 ] ]
263+ TestRepo . insert_all ( "posts_users" , [
264+ [ post_id: p1 . id , user_id: uid1 ] ,
265+ [ post_id: p1 . id , user_id: uid2 ] ,
266+ [ post_id: p2 . id , user_id: uid2 ]
267+ ] )
264268
265269 % Comment { id: cid1 } = TestRepo . insert! ( % Comment { author_id: uid1 } )
266270 % Comment { id: cid2 } = TestRepo . insert! ( % Comment { author_id: uid1 } )
@@ -340,8 +344,11 @@ defmodule Ecto.Integration.PreloadTest do
340344 % Comment { id: cid2 } = TestRepo . insert! ( % Comment { text: "2" , post_id: p1 . id } )
341345 % Comment { id: cid4 } = TestRepo . insert! ( % Comment { text: "3" , post_id: p2 . id } )
342346
343- assert [ pe3 , pe1 , pe2 ] = TestRepo . preload ( [ p3 , p1 , p2 ] ,
344- comments: fn _ -> TestRepo . all ( Comment ) end )
347+ assert [ pe3 , pe1 , pe2 ] =
348+ TestRepo . preload ( [ p3 , p1 , p2 ] ,
349+ comments: fn _ -> TestRepo . all ( Comment ) end
350+ )
351+
345352 assert [ % Comment { id: ^ cid1 } , % Comment { id: ^ cid2 } ] = pe1 . comments
346353 assert [ % Comment { id: ^ cid3 } , % Comment { id: ^ cid4 } ] = pe2 . comments
347354 assert [ ] = pe3 . comments
@@ -375,36 +382,40 @@ defmodule Ecto.Integration.PreloadTest do
375382 % User { id: uid2 } = TestRepo . insert! ( % User { name: "2" } )
376383 % User { id: uid4 } = TestRepo . insert! ( % User { name: "3" } )
377384
378- TestRepo . insert_all "posts_users" , [ [ post_id: p1 . id , user_id: uid1 ] ,
379- [ post_id: p1 . id , user_id: uid2 ] ,
380- [ post_id: p2 . id , user_id: uid3 ] ,
381- [ post_id: p2 . id , user_id: uid4 ] ,
382- [ post_id: p3 . id , user_id: uid1 ] ,
383- [ post_id: p3 . id , user_id: uid4 ] ]
385+ TestRepo . insert_all ( "posts_users" , [
386+ [ post_id: p1 . id , user_id: uid1 ] ,
387+ [ post_id: p1 . id , user_id: uid2 ] ,
388+ [ post_id: p2 . id , user_id: uid3 ] ,
389+ [ post_id: p2 . id , user_id: uid4 ] ,
390+ [ post_id: p3 . id , user_id: uid1 ] ,
391+ [ post_id: p3 . id , user_id: uid4 ]
392+ ] )
384393
385394 wrong_preloader = fn post_ids ->
386395 TestRepo . all (
387396 from u in User ,
388- join: pu in "posts_users" ,
389- on: true ,
390- where: pu . post_id in ^ post_ids and pu . user_id == u . id ,
391- order_by: u . id ,
392- select: map ( u , [ :id ] )
397+ join: pu in "posts_users" ,
398+ on: true ,
399+ where: pu . post_id in ^ post_ids and pu . user_id == u . id ,
400+ order_by: u . id ,
401+ select: map ( u , [ :id ] )
393402 )
394403 end
395404
396- assert_raise RuntimeError , ~r/ invalid custom preload for `users` on `Ecto.Integration.Post`/ , fn ->
397- TestRepo . preload ( [ p1 , p2 , p3 ] , users: wrong_preloader )
398- end
405+ assert_raise RuntimeError ,
406+ ~r/ invalid custom preload for `users` on `Ecto.Integration.Post`/ ,
407+ fn ->
408+ TestRepo . preload ( [ p1 , p2 , p3 ] , users: wrong_preloader )
409+ end
399410
400411 right_preloader = fn post_ids ->
401412 TestRepo . all (
402413 from u in User ,
403- join: pu in "posts_users" ,
404- on: true ,
405- where: pu . post_id in ^ post_ids and pu . user_id == u . id ,
406- order_by: u . id ,
407- select: { pu . post_id , map ( u , [ :id ] ) }
414+ join: pu in "posts_users" ,
415+ on: true ,
416+ where: pu . post_id in ^ post_ids and pu . user_id == u . id ,
417+ order_by: u . id ,
418+ select: { pu . post_id , map ( u , [ :id ] ) }
408419 )
409420 end
410421
@@ -428,29 +439,41 @@ defmodule Ecto.Integration.PreloadTest do
428439 assert % Ecto.Association.NotLoaded { } = p1 . comments
429440
430441 # With empty query
431- assert [ pe3 , pe1 , pe2 ] = TestRepo . preload ( [ p3 , p1 , p2 ] ,
432- comments: from ( c in Comment , where: false ) )
442+ assert [ pe3 , pe1 , pe2 ] =
443+ TestRepo . preload ( [ p3 , p1 , p2 ] ,
444+ comments: from ( c in Comment , where: false )
445+ )
446+
433447 assert [ ] = pe1 . comments
434448 assert [ ] = pe2 . comments
435449 assert [ ] = pe3 . comments
436450
437451 # With custom select
438- assert [ pe3 , pe1 , pe2 ] = TestRepo . preload ( [ p3 , p1 , p2 ] ,
439- comments: from ( c in Comment , select: c . id , order_by: c . id ) )
452+ assert [ pe3 , pe1 , pe2 ] =
453+ TestRepo . preload ( [ p3 , p1 , p2 ] ,
454+ comments: from ( c in Comment , select: c . id , order_by: c . id )
455+ )
456+
440457 assert [ ^ cid1 , ^ cid2 ] = pe1 . comments
441458 assert [ ^ cid3 , ^ cid4 ] = pe2 . comments
442459 assert [ ] = pe3 . comments
443460
444461 # With custom ordered query
445- assert [ pe3 , pe1 , pe2 ] = TestRepo . preload ( [ p3 , p1 , p2 ] ,
446- comments: from ( c in Comment , order_by: [ desc: c . text ] ) )
462+ assert [ pe3 , pe1 , pe2 ] =
463+ TestRepo . preload ( [ p3 , p1 , p2 ] ,
464+ comments: from ( c in Comment , order_by: [ desc: c . text ] )
465+ )
466+
447467 assert [ % Comment { id: ^ cid2 } , % Comment { id: ^ cid1 } ] = pe1 . comments
448468 assert [ % Comment { id: ^ cid4 } , % Comment { id: ^ cid3 } ] = pe2 . comments
449469 assert [ ] = pe3 . comments
450470
451471 # With custom ordered query with preload
452- assert [ pe3 , pe1 , pe2 ] = TestRepo . preload ( [ p3 , p1 , p2 ] ,
453- comments: { from ( c in Comment , order_by: [ desc: c . text ] ) , :post } )
472+ assert [ pe3 , pe1 , pe2 ] =
473+ TestRepo . preload ( [ p3 , p1 , p2 ] ,
474+ comments: { from ( c in Comment , order_by: [ desc: c . text ] ) , :post }
475+ )
476+
454477 assert [ % Comment { id: ^ cid2 } = c2 , % Comment { id: ^ cid1 } = c1 ] = pe1 . comments
455478 assert [ % Comment { id: ^ cid4 } = c4 , % Comment { id: ^ cid3 } = c3 ] = pe2 . comments
456479 assert [ ] = pe3 . comments
@@ -478,22 +501,30 @@ defmodule Ecto.Integration.PreloadTest do
478501 np1 = TestRepo . preload ( p1 , comments_authors: from ( u in User , where: u . name == "foo" ) )
479502 assert np1 . comments_authors == [ u1 ]
480503
481- assert_raise ArgumentError , ~r/ Ecto expected a map\/ struct with the key `id` but got: \d +/ , fn ->
482- TestRepo . preload ( p1 , comments_authors: from ( u in User , order_by: u . name , select: u . id ) )
483- end
504+ assert_raise ArgumentError ,
505+ ~r/ Ecto expected a map\/ struct with the key `id` but got: \d +/ ,
506+ fn ->
507+ TestRepo . preload ( p1 ,
508+ comments_authors: from ( u in User , order_by: u . name , select: u . id )
509+ )
510+ end
484511
485512 # The subpreload order does not matter because the result is dictated by comments
486- np1 = TestRepo . preload ( p1 , comments_authors: from ( u in User , order_by: u . name , select: % { id: u . id } ) )
513+ np1 =
514+ TestRepo . preload ( p1 ,
515+ comments_authors: from ( u in User , order_by: u . name , select: % { id: u . id } )
516+ )
517+
487518 assert np1 . comments_authors ==
488- [ % { id: u1 . id } , % { id: u2 . id } , % { id: u3 . id } , % { id: u4 . id } ]
519+ [ % { id: u1 . id } , % { id: u2 . id } , % { id: u3 . id } , % { id: u4 . id } ]
489520 end
490521
491522 test "preload into a subquery source" do
492523 % { id: p_id } = TestRepo . insert! ( % Post { } )
493524 % { id: c_id } = TestRepo . insert! ( % Comment { post_id: p_id } )
494525
495526 q =
496- from c in subquery ( from c in Comment ) ,
527+ from c in subquery ( from ( c in Comment ) ) ,
497528 join: p in Post ,
498529 on: c . post_id == p . id ,
499530 preload: [ post: p ]
@@ -515,7 +546,9 @@ defmodule Ecto.Integration.PreloadTest do
515546
516547 assert % Ecto.Association.NotLoaded { } = p1 . comments
517548
518- posts = TestRepo . all ( from Post , preload: [ :comments ] , select: [ :id , comments: [ :id , :post_id ] ] )
549+ posts =
550+ TestRepo . all ( from Post , preload: [ :comments ] , select: [ :id , comments: [ :id , :post_id ] ] )
551+
519552 [ p1 , p2 , p3 ] = sort_by_id ( posts )
520553 assert p1 . title == nil
521554 assert p2 . title == nil
@@ -581,7 +614,9 @@ defmodule Ecto.Integration.PreloadTest do
581614 % Comment { } = TestRepo . insert! ( % Comment { post_id: pid1 , author_id: uid1 } )
582615 % Comment { } = TestRepo . insert! ( % Comment { post_id: pid1 , author_id: uid2 } )
583616
584- [ p1 ] = TestRepo . all from Post , preload: [ :comments_authors ] , select: [ :id , comments_authors: :id ]
617+ [ p1 ] =
618+ TestRepo . all ( from Post , preload: [ :comments_authors ] , select: [ :id , comments_authors: :id ] )
619+
585620 [ % { id: ^ uid1 , name: nil } , % { id: ^ uid2 , name: nil } ] = p1 . comments_authors |> sort_by_id
586621 end
587622
@@ -607,7 +642,7 @@ defmodule Ecto.Integration.PreloadTest do
607642 TestRepo . insert! ( % Comment { text: "3" , post_id: p2 . id } )
608643 TestRepo . insert! ( % Comment { text: "4" , post_id: p2 . id } )
609644
610- assert [ p2 , p1 ] = TestRepo . preload ( [ p2 , p1 ] , [ comments: :post ] )
645+ assert [ p2 , p1 ] = TestRepo . preload ( [ p2 , p1 ] , comments: :post )
611646 assert [ c1 , c2 ] = p1 . comments
612647 assert [ c3 , c4 ] = p2 . comments
613648 assert p1 . id == c1 . post . id
@@ -691,8 +726,8 @@ defmodule Ecto.Integration.PreloadTest do
691726 updated = % { c1 | author: u1 , author_id: nil }
692727
693728 assert ExUnit.CaptureLog . capture_log ( fn ->
694- assert TestRepo . preload ( updated , [ :author ] ) . author == u1
695- end ) =~ ~r/ its association key `author_id` is nil/
729+ assert TestRepo . preload ( updated , [ :author ] ) . author == u1
730+ end ) =~ ~r/ its association key `author_id` is nil/
696731
697732 assert TestRepo . preload ( updated , [ :author ] , force: true ) . author == nil
698733 end
@@ -710,12 +745,12 @@ defmodule Ecto.Integration.PreloadTest do
710745 [ c1 , c2 ] = TestRepo . preload ( [ c1 , c2 ] , post: :comments )
711746 assert [ % Comment { id: ^ cid1 } ] = c1 . post . comments
712747
713- TestRepo . update_all Post , set: [ title: "0" ]
714- TestRepo . update_all Comment , set: [ post_id: pid ]
748+ TestRepo . update_all ( Post , set: [ title: "0" ] )
749+ TestRepo . update_all ( Comment , set: [ post_id: pid ] )
715750
716751 # Preloading once again shouldn't change the result
717752 [ c1 , c2 ] = TestRepo . preload ( [ c1 , c2 ] , :post )
718- assert % Post { id: ^ pid , title: "1" , comments: [ _ | _ ] } = c1 . post
753+ assert % Post { id: ^ pid , title: "1" , comments: [ _ | _ ] } = c1 . post
719754 assert c2 . post == nil
720755
721756 [ c1 , c2 ] = TestRepo . preload ( [ c1 , % { c2 | post_id: pid } ] , :post , force: true )
@@ -738,7 +773,7 @@ defmodule Ecto.Integration.PreloadTest do
738773 assert hd ( p1 . comments ) . post . id == p1 . id
739774 assert hd ( p2 . comments ) . post . id == p2 . id
740775
741- TestRepo . update_all Comment , set: [ text: "0" ]
776+ TestRepo . update_all ( Comment , set: [ text: "0" ] )
742777
743778 # Preloading once again shouldn't change the result
744779 [ p1 , p2 ] = TestRepo . preload ( [ p1 , p2 ] , :comments )
@@ -788,7 +823,6 @@ defmodule Ecto.Integration.PreloadTest do
788823 assert [ ] = p3 . comments
789824 end
790825
791-
792826 test "preload belongs_to in embedded_schema" do
793827 % User { id: uid1 } = TestRepo . insert! ( % User { name: "1" } )
794828 item = % Item { user_id: uid1 }
@@ -847,20 +881,20 @@ defmodule Ecto.Integration.PreloadTest do
847881 expected_items_user =
848882 Enum . map ( context . orders , fn
849883 % { items: nil } -> { nil , nil }
850- % { items: items } -> Enum . map ( items , & { & 1 . id , & 1 . user_id } )
884+ % { items: items } -> Enum . map ( items , & { & 1 . id , & 1 . user_id } )
851885 end )
852886
853887 actual_items_user =
854888 Enum . map ( preloaded_orders , fn
855889 % { items: nil } -> { nil , nil }
856- % { items: items } -> Enum . map ( items , & { & 1 . id , & 1 . user . id } )
890+ % { items: items } -> Enum . map ( items , & { & 1 . id , & 1 . user . id } )
857891 end )
858892
859893 assert expected_items_user == actual_items_user
860894 end
861895 end
862896
863897 defp sort_by_id ( values ) do
864- Enum . sort_by ( values , & ( & 1 . id ) )
898+ Enum . sort_by ( values , & & 1 . id )
865899 end
866900end
0 commit comments