Skip to content

Commit ca97dd2

Browse files
committed
ProFormA-import: add deduplication for file with the same context, name, path, role and file_type
1 parent dbcf10b commit ca97dd2

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

app/services/proforma_service/convert_task_to_exercise.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def string_to_bool(str)
6868
end
6969

7070
def files
71-
model_solution_files + test_files + task_files
71+
(model_solution_files + test_files + task_files).uniq {|f| [f.name, f.file_type_id, f.role, f.path] }
7272
end
7373

7474
def test_files
@@ -106,16 +106,20 @@ def task_files
106106

107107
def codeocean_file_from_task_file(file, parent_object = nil)
108108
extension = File.extname(file.filename)
109+
path = File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename)
110+
name = File.basename(file.filename, '.*')
111+
role = extract_meta_data(@task.meta_data&.dig('meta-data'), 'files', "CO-#{file.id}", 'role')
112+
109113
# checking the last element of xml_id_path array for file.id
110114
codeocean_file = @exercise.files.detect {|f| f.xml_id_path.last == file.id } || @exercise.files.new
111115
codeocean_file.assign_attributes(
112116
context: @exercise,
113117
file_type: file_type(extension),
114118
hidden: file.visible != 'yes', # hides 'delayed' and 'no'
115-
name: File.basename(file.filename, '.*'),
119+
name:,
116120
read_only: file.usage_by_lms != 'edit',
117-
role: extract_meta_data(@task.meta_data&.dig('meta-data'), 'files', "CO-#{file.id}", 'role'),
118-
path: File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename),
121+
role:,
122+
path:,
119123
xml_id_path: (parent_object.nil? ? [file.id] : [parent_object.id, file.id]).map(&:to_s)
120124
)
121125
if file.binary

spec/services/proforma_service/convert_task_to_exercise_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,27 @@
187187
expect { convert_to_exercise_service.save! }.to change(Exercise, :count).by(1)
188188
end
189189

190+
context 'with two files with similar contents' do
191+
let(:file_dup) do
192+
ProformaXML::TaskFile.new(
193+
id: 'id2',
194+
content:,
195+
filename:,
196+
used_by_grader: 'used_by_grader',
197+
visible: 'yes',
198+
usage_by_lms:,
199+
binary:,
200+
mimetype:
201+
)
202+
end
203+
204+
let(:files) { [file, file_dup] }
205+
206+
it 'creates an exercises with only one file' do
207+
expect(convert_to_exercise_service.files.length).to be 1
208+
end
209+
end
210+
190211
context 'when file is a Makefile' do
191212
let(:filename) { "#{path}Makefile" }
192213

0 commit comments

Comments
 (0)