@@ -46,4 +46,55 @@ defmodule CF.SpeakersTest do
4646 assert DB.Repo . get ( DB.Schema.Speaker , speaker . id ) . slug == @ slug
4747 end
4848 end
49+
50+ describe "merge_speakers" do
51+ test "merges profiles and related data" do
52+ speaker1 = insert ( :speaker , % { title: "speaker_1" } )
53+ speaker2 = insert ( :speaker , % { title: nil } )
54+ speaker1_statements = insert_list ( 3 , :statement , speaker: speaker1 )
55+ speaker2_statements = insert_list ( 4 , :statement , speaker: speaker2 )
56+ speaker1_videos = insert_list ( 3 , :video_speaker , speaker: speaker1 )
57+ speaker2_videos = insert_list ( 4 , :video_speaker , speaker: speaker2 )
58+ speaker1_users = insert_list ( 3 , :user , speaker: speaker1 )
59+ speaker2_users = insert_list ( 4 , :user , speaker: speaker2 )
60+
61+ { :ok , result } = Speakers . merge_speakers ( speaker1 , speaker2 )
62+
63+ assert result . speaker_from . id === speaker1 . id
64+ assert result . speaker_into . id === speaker2 . id
65+
66+ # Profiles should be merged
67+ assert result . speaker_into . title == speaker1 . title
68+
69+ # Statements should be upddated
70+ assert elem ( result . statements , 0 ) == 3
71+
72+ assert DB.Repo . aggregate (
73+ from ( s in DB.Schema.Statement , where: s . speaker_id == ^ speaker2 . id ) ,
74+ :count ,
75+ :id
76+ ) == 7
77+
78+ # Videos should be updated
79+ assert elem ( result . videos_speakers , 0 ) == 3
80+
81+ assert DB.Repo . aggregate (
82+ from ( s in DB.Schema.VideoSpeaker , where: s . speaker_id == ^ speaker2 . id ) ,
83+ :count ,
84+ :video_id
85+ ) == 7
86+
87+ # Users should be upddated
88+ assert elem ( result . users , 0 ) == 3
89+
90+ assert DB.Repo . aggregate (
91+ from ( s in DB.Schema.User , where: s . speaker_id == ^ speaker2 . id ) ,
92+ :count ,
93+ :id
94+ ) == 7
95+
96+ # First speaker should be deleted
97+ assert DB.Repo . get ( DB.Schema.Speaker , speaker1 . id ) == nil
98+ end
99+ end
49100end
0 commit comments