@@ -20,7 +20,8 @@ class UnitModelTest < ActiveSupport::TestCase
2020 def test_sync_unit
2121 import_settings = {
2222 replace_existing_campus : false ,
23- replace_existing_tutorial : false
23+ replace_existing_tutorial : false ,
24+ merge_duplicate_students : false
2425 }
2526
2627 student = FactoryBot . create :user , :student
@@ -84,6 +85,75 @@ def test_sync_unit
8485 campus2 . destroy!
8586 end
8687
88+ def test_sync_unit_merge_duplicate_students
89+ import_settings = {
90+ replace_existing_campus : false ,
91+ replace_existing_tutorial : true ,
92+ merge_duplicate_students : true
93+ }
94+
95+ student = FactoryBot . create :user , :student
96+
97+ tutorial_stream1 = @unit . tutorial_streams . first
98+ tutorial_stream2 = FactoryBot . create ( :tutorial_stream , unit : @unit )
99+
100+ tutorial1 = FactoryBot . create :tutorial , unit : @unit , campus : Campus . first , tutorial_stream : tutorial_stream1
101+ tutorial2 = FactoryBot . create :tutorial , unit : @unit , campus : Campus . first , tutorial_stream : tutorial_stream2
102+
103+ student_list = [
104+ {
105+ unit_code : 'COS10001' ,
106+ username : student . username ,
107+ student_id : student . student_id ,
108+ first_name : student . first_name ,
109+ last_name : student . last_name ,
110+ nickname : student . nickname ,
111+ email : student . email ,
112+ tutorials : [ tutorial1 . abbreviation ] ,
113+ enrolled : true ,
114+ campus : Campus . first . abbreviation
115+ } ,
116+ {
117+ unit_code : 'COS10001' ,
118+ username : student . username ,
119+ student_id : student . student_id ,
120+ first_name : student . first_name ,
121+ last_name : student . last_name ,
122+ nickname : student . nickname ,
123+ email : student . email ,
124+ tutorials : [ tutorial2 . abbreviation ] ,
125+ enrolled : true ,
126+ campus : Campus . first . abbreviation
127+ }
128+ ]
129+
130+ result = {
131+ success : [ ] ,
132+ ignored : [ ] ,
133+ errors : [ ]
134+ }
135+
136+ @unit . sync_enrolment_with ( student_list , import_settings , result )
137+
138+ assert_equal 1 , result [ :ignored ] . count , result . inspect
139+ assert_equal 0 , result [ :errors ] . count , result . inspect
140+ assert_equal 1 , result [ :success ] . count , result . inspect
141+
142+ assert_equal 2 , @unit . tutorials . count
143+
144+ project = @unit . projects . first
145+ assert project . valid?
146+
147+ # Ensure that tutorials from both rows were merged and student was enrolled into each
148+ assert project . enrolled_in? ( tutorial1 )
149+ assert project . enrolled_in? ( tutorial2 )
150+
151+ @unit . projects . first . destroy
152+ tutorial1 . destroy!
153+ tutorial2 . destroy!
154+ tutorial_stream2 . destroy!
155+ end
156+
87157 def test_import_tasks_worked
88158 @unit . import_tasks_from_csv File . open ( Rails . root . join ( 'test_files' , "#{ @unit . code } -Tasks.csv" ) )
89159 assert_equal 37 , @unit . task_definitions . count , 'imported all task definitions'
0 commit comments