update converters and im-/exporters to accommodate for changes in proforma gem

This commit is contained in:
Karol
2023-11-10 18:21:51 +01:00
committed by Sebastian Serth
parent 0dd9d4fd2e
commit 3f8f4cee5b
7 changed files with 191 additions and 86 deletions

View File

@ -30,14 +30,35 @@ module ProformaService
language: DEFAULT_LANGUAGE, language: DEFAULT_LANGUAGE,
model_solutions:, model_solutions:,
meta_data: { meta_data: {
CodeOcean: { '@@order' => %w[meta-data],
public: @exercise.public, 'meta-data' => {
hide_file_tree: @exercise.hide_file_tree, '@@order' => %w[CodeOcean:public CodeOcean:hide_file_tree CodeOcean:allow_file_creation CodeOcean:allow_auto_completion CodeOcean:expected_difficulty CodeOcean:execution_environment_id CodeOcean:files],
allow_file_creation: @exercise.allow_file_creation, '@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
allow_auto_completion: @exercise.allow_auto_completion, 'CodeOcean:public' => {
expected_difficulty: @exercise.expected_difficulty, '@@order' => %w[$1],
execution_environment_id: @exercise.execution_environment_id, '$1' => @exercise.public,
files: task_files_meta_data, },
'CodeOcean:hide_file_tree' => {
'@@order' => %w[$1],
'$1' => @exercise.hide_file_tree,
},
'CodeOcean:allow_file_creation' => {
'@@order' => %w[$1],
'$1' => @exercise.allow_file_creation,
},
'CodeOcean:allow_auto_completion' => {
'@@order' => %w[$1],
'$1' => @exercise.allow_auto_completion,
},
'CodeOcean:expected_difficulty' => {
'@@order' => %w[$1],
'$1' => @exercise.expected_difficulty,
},
'CodeOcean:execution_environment_id' => {
'@@order' => %w[$1],
'$1' => @exercise.execution_environment_id,
},
'CodeOcean:files' => task_files_meta_data,
}, },
}, },
}.compact }.compact
@ -86,9 +107,18 @@ module ProformaService
def test_meta_data(file) def test_meta_data(file)
{ {
CodeOcean: { '@@order' => %w[test-meta-data],
'feedback-message': file.feedback_message, 'test-meta-data' => {
weight: file.weight, '@@order' => %w[CodeOcean:feedback-message CodeOcean:weight],
'@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
'CodeOcean:feedback-message' => {
'@@order' => %w[$1],
'$1' => file.feedback_message,
},
'CodeOcean:weight' => {
'@@order' => %w[$1],
'$1' => file.weight,
},
}, },
} }
end end
@ -109,10 +139,20 @@ module ProformaService
end end
def task_files_meta_data def task_files_meta_data
exercise_files.to_h do |file| # TODO: refactor?
# added CO- to id, otherwise the key would have CodeOcean as a prefix after export and import (cause unknown) task_files_hash = {
["CO-#{file.id}", {role: file.role}] '@@order' => exercise_files.map {|file| "CodeOcean:CO-#{file.id}" },
}
exercise_files.each do |file|
task_files_hash["CodeOcean:CO-#{file.id}"] = {
'@@order' => ['CodeOcean:role'],
'CodeOcean:role' => {
'@@order' => ['$1'],
'$1' => file.role,
},
}
end end
task_files_hash
end end
def task_files def task_files

View File

@ -21,19 +21,25 @@ module ProformaService
user: @user, user: @user,
title: @task.title, title: @task.title,
description: @task.description, description: @task.description,
public: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:public)) || false, public: string_to_bool(extract_meta_data(@task.meta_data&.dig('meta-data'), 'public')) || false,
hide_file_tree: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:hide_file_tree)) || false, hide_file_tree: string_to_bool(extract_meta_data(@task.meta_data&.dig('meta-data'), 'hide_file_tree')) || false,
allow_file_creation: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:allow_file_creation)) || false, allow_file_creation: string_to_bool(extract_meta_data(@task.meta_data&.dig('meta-data'), 'allow_file_creation')) || false,
allow_auto_completion: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:allow_auto_completion)) || false, allow_auto_completion: string_to_bool(extract_meta_data(@task.meta_data&.dig('meta-data'), 'allow_auto_completion')) || false,
expected_difficulty: @task.meta_data[:CodeOcean]&.dig(:expected_difficulty) || 1, expected_difficulty: extract_meta_data(@task.meta_data&.dig('meta-data'), 'expected_difficulty') || 1,
execution_environment_id:, execution_environment_id:,
files: files:
) )
end end
def extract_meta_data(meta_data, *path)
current_level = meta_data
path.each {|attribute| current_level = current_level&.dig("CodeOcean:#{attribute}") }
current_level&.dig('$1')
end
def execution_environment_id def execution_environment_id
from_meta_data = @task.meta_data[:CodeOcean]&.dig(:execution_environment_id) from_meta_data = extract_meta_data(@task.meta_data&.dig('meta-data'), 'execution_environment_id')
return from_meta_data if from_meta_data return from_meta_data if from_meta_data
return nil unless @task.proglang return nil unless @task.proglang
@ -60,8 +66,8 @@ module ProformaService
def test_files def test_files
@task.tests.map do |test_object| @task.tests.map do |test_object|
task_files.delete(test_object.files.first.id).tap do |file| task_files.delete(test_object.files.first.id).tap do |file|
file.weight = test_object.meta_data[:CodeOcean]&.dig(:weight) || 1.0 file.weight = extract_meta_data(test_object.meta_data&.dig('test-meta-data'), 'weight').presence || 1.0
file.feedback_message = test_object.meta_data[:CodeOcean]&.dig(:'feedback-message').presence || 'Feedback' file.feedback_message = extract_meta_data(test_object.meta_data&.dig('test-meta-data'), 'feedback-message').presence || 'Feedback'
file.role ||= 'teacher_defined_test' file.role ||= 'teacher_defined_test'
end end
end end
@ -89,7 +95,7 @@ module ProformaService
hidden: file.visible != 'yes', # hides 'delayed' and 'no' hidden: file.visible != 'yes', # hides 'delayed' and 'no'
name: File.basename(file.filename, '.*'), name: File.basename(file.filename, '.*'),
read_only: file.usage_by_lms != 'edit', read_only: file.usage_by_lms != 'edit',
role: @task.meta_data[:CodeOcean]&.dig(:files)&.dig("CO-#{file.id}".to_sym)&.dig(:role), role: extract_meta_data(@task.meta_data&.dig('meta-data'), 'files', "CO-#{file.id}", 'role'),
path: File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename) path: File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename)
) )
if file.binary if file.binary

View File

@ -9,8 +9,7 @@ module ProformaService
def execute def execute
@task = ConvertExerciseToTask.call(exercise: @exercise) @task = ConvertExerciseToTask.call(exercise: @exercise)
namespaces = [{prefix: 'CodeOcean', uri: 'codeocean.openhpi.de'}] exporter = ProformaXML::Exporter.new(task: @task)
exporter = ProformaXML::Exporter.new(task: @task, custom_namespaces: namespaces)
exporter.perform exporter.perform
end end
end end

View File

@ -12,7 +12,7 @@ module ProformaService
if single_task? if single_task?
importer = ProformaXML::Importer.new(zip: @zip) importer = ProformaXML::Importer.new(zip: @zip)
import_result = importer.perform import_result = importer.perform
@task = import_result[:task] @task = import_result
exercise = base_exercise exercise = base_exercise
exercise_files = exercise&.files&.to_a exercise_files = exercise&.files&.to_a

View File

@ -34,17 +34,37 @@ RSpec.describe ProformaService::ConvertExerciseToTask do
description: exercise.description, description: exercise.description,
uuid: exercise.uuid, uuid: exercise.uuid,
language: described_class::DEFAULT_LANGUAGE, language: described_class::DEFAULT_LANGUAGE,
meta_data: { meta_data:
CodeOcean: { {
allow_auto_completion: exercise.allow_auto_completion, '@@order' => ['meta-data'], 'meta-data' =>
allow_file_creation: exercise.allow_file_creation, {'@@order' => %w[CodeOcean:public CodeOcean:hide_file_tree CodeOcean:allow_file_creation CodeOcean:allow_auto_completion CodeOcean:expected_difficulty CodeOcean:execution_environment_id CodeOcean:files],
execution_environment_id: exercise.execution_environment_id, '@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
expected_difficulty: exercise.expected_difficulty, 'CodeOcean:allow_auto_completion' => {
hide_file_tree: exercise.hide_file_tree, '@@order' => ['$1'],
public: exercise.public, '$1' => exercise.allow_auto_completion,
files: {}, },
'CodeOcean:allow_file_creation' => {
'@@order' => ['$1'],
'$1' => exercise.allow_file_creation,
},
'CodeOcean:execution_environment_id' => {
'@@order' => ['$1'],
'$1' => exercise.execution_environment_id,
},
'CodeOcean:expected_difficulty' => {
'@@order' => ['$1'],
'$1' => exercise.expected_difficulty,
},
'CodeOcean:files' => {'@@order' => []},
'CodeOcean:hide_file_tree' => {
'@@order' => ['$1'],
'$1' => exercise.hide_file_tree,
},
'CodeOcean:public' => {
'@@order' => ['$1'],
'$1' => exercise.public,
}}
}, },
},
files: [], files: [],
tests: [], tests: [],
model_solutions: [] model_solutions: []
@ -76,9 +96,17 @@ RSpec.describe ProformaService::ConvertExerciseToTask do
it 'adds the file\'s role to the file hash in task-meta_data' do it 'adds the file\'s role to the file hash in task-meta_data' do
expect(task).to have_attributes( expect(task).to have_attributes(
meta_data: { meta_data: a_hash_including(
CodeOcean: a_hash_including(files: {"CO-#{file.id}" => {role: 'main_file'}}), 'meta-data' => a_hash_including(
} 'CodeOcean:files' => {
'@@order' => ["CodeOcean:CO-#{file.id}"],
"CodeOcean:CO-#{file.id}" => {
'@@order' => ['CodeOcean:role'],
'CodeOcean:role' => {'$1' => 'main_file', '@@order' => ['$1']},
},
}
)
)
) )
end end
end end
@ -104,9 +132,17 @@ RSpec.describe ProformaService::ConvertExerciseToTask do
it 'adds the file\'s role to the file hash in task-meta_data' do it 'adds the file\'s role to the file hash in task-meta_data' do
expect(task).to have_attributes( expect(task).to have_attributes(
meta_data: { meta_data: a_hash_including(
CodeOcean: a_hash_including(files: {"CO-#{file.id}" => {role: 'regular_file'}}), 'meta-data' => a_hash_including(
} 'CodeOcean:files' => {
'@@order' => ["CodeOcean:CO-#{file.id}"],
"CodeOcean:CO-#{file.id}" => {
'@@order' => ['CodeOcean:role'],
'CodeOcean:role' => {'$1' => 'regular_file', '@@order' => ['$1']},
},
}
)
)
) )
end end
@ -190,12 +226,12 @@ RSpec.describe ProformaService::ConvertExerciseToTask do
id: test_file.id, id: test_file.id,
title: test_file.name, title: test_file.name,
files: have(1).item, files: have(1).item,
meta_data: { meta_data: a_hash_including(
CodeOcean: { 'test-meta-data' => a_hash_including(
'feedback-message': 'feedback_message', 'CodeOcean:feedback-message' => {'$1' => 'feedback_message', '@@order' => ['$1']},
weight: test_file.weight, 'CodeOcean:weight' => {'$1' => test_file.weight, '@@order' => ['$1']}
}, )
} )
) )
end end

View File

@ -79,15 +79,34 @@ RSpec.describe ProformaService::ConvertTaskToExercise do
context 'when meta_data is set' do context 'when meta_data is set' do
let(:meta_data) do let(:meta_data) do
{ {
CodeOcean: { '@@order' => ['meta-data'], 'meta-data' =>
public:, {'@@order' => %w[CodeOcean:public CodeOcean:hide_file_tree CodeOcean:allow_file_creation CodeOcean:allow_auto_completion CodeOcean:expected_difficulty CodeOcean:execution_environment_id CodeOcean:files],
hide_file_tree:, '@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
allow_file_creation:, 'CodeOcean:allow_auto_completion' => {
allow_auto_completion:, '@@order' => ['$1'],
expected_difficulty:, '$1' => allow_auto_completion,
execution_environment_id: execution_environment&.id, },
files: files_meta_data, 'CodeOcean:allow_file_creation' => {
}, '@@order' => ['$1'],
'$1' => allow_file_creation,
},
'CodeOcean:execution_environment_id' => {
'@@order' => ['$1'],
'$1' => execution_environment.id,
},
'CodeOcean:expected_difficulty' => {
'@@order' => ['$1'],
'$1' => expected_difficulty,
},
'CodeOcean:files' => {'@@order' => []},
'CodeOcean:hide_file_tree' => {
'@@order' => ['$1'],
'$1' => hide_file_tree,
},
'CodeOcean:public' => {
'@@order' => ['$1'],
'$1' => public,
}}
} }
end end
let(:files_meta_data) { {} } let(:files_meta_data) { {} }
@ -178,12 +197,22 @@ RSpec.describe ProformaService::ConvertTaskToExercise do
context 'when file is a main_file' do context 'when file is a main_file' do
let(:meta_data) do let(:meta_data) do
{ {
CodeOcean: { '@@order' => ['meta-data'], 'meta-data' => {
files: files_meta_data, '@@order' => %w[CodeOcean:files],
'@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
'CodeOcean:files' => files_meta_data,
}
}
end
let(:files_meta_data) do
{
'@@order' => ["CodeOcean:CO-#{file.id}"],
"CodeOcean:CO-#{file.id}" => {
'@@order' => ['CodeOcean:role'],
'CodeOcean:role' => {'$1' => 'main_file', '@@order' => ['$1']},
}, },
} }
end end
let(:files_meta_data) { {"CO-#{file.id}".to_sym => {role: 'main_file'}} }
it 'creates an exercise with a file that has the correct attributes' do it 'creates an exercise with a file that has the correct attributes' do
expect(convert_to_exercise_service.files.first).to have_attributes(role: 'main_file') expect(convert_to_exercise_service.files.first).to have_attributes(role: 'main_file')
@ -331,10 +360,10 @@ RSpec.describe ProformaService::ConvertTaskToExercise do
test_type: 'test_type', test_type: 'test_type',
files: test_files, files: test_files,
meta_data: { meta_data: {
CodeOcean: { 'test-meta-data' => {
'feedback-message': 'feedback-message', '@@order' => %w[CodeOcean:feedback-message CodeOcean:weight],
'testing-framework': 'testing-framework', 'CodeOcean:feedback-message' => {'$1' => 'feedback-message', '@@order' => ['$1']},
'testing-framework-version': 'testing-framework-version', 'CodeOcean:weight' => {'$1' => '0.7', '@@order' => ['$1']},
}, },
} }
) )
@ -361,6 +390,7 @@ RSpec.describe ProformaService::ConvertTaskToExercise do
it 'creates an exercise with a test with correct attributes' do it 'creates an exercise with a test with correct attributes' do
expect(convert_to_exercise_service.files.find {|file| file.role == 'teacher_defined_test' }).to have_attributes( expect(convert_to_exercise_service.files.find {|file| file.role == 'teacher_defined_test' }).to have_attributes(
feedback_message: 'feedback-message', feedback_message: 'feedback-message',
weight: 0.7,
content: 'testfile-content', content: 'testfile-content',
name: 'testfile', name: 'testfile',
role: 'teacher_defined_test', role: 'teacher_defined_test',
@ -373,12 +403,22 @@ RSpec.describe ProformaService::ConvertTaskToExercise do
context 'when test file is a teacher_defined_linter' do context 'when test file is a teacher_defined_linter' do
let(:meta_data) do let(:meta_data) do
{ {
CodeOcean: { '@@order' => ['meta-data'], 'meta-data' => {
files: files_meta_data, '@@order' => %w[CodeOcean:files],
'@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
'CodeOcean:files' => files_meta_data,
}
}
end
let(:files_meta_data) do
{
'@@order' => ["CodeOcean:CO-#{test_file.id}"],
"CodeOcean:CO-#{test_file.id}" => {
'@@order' => ['CodeOcean:role'],
'CodeOcean:role' => {'$1' => 'teacher_defined_linter', '@@order' => ['$1']},
}, },
} }
end end
let(:files_meta_data) { {"CO-#{test_file.id}".to_sym => {role: 'teacher_defined_linter'}} }
it 'creates an exercise with a test' do it 'creates an exercise with a test' do
expect(convert_to_exercise_service.files.select {|file| file.role == 'teacher_defined_linter' }).to have(1).item expect(convert_to_exercise_service.files.select {|file| file.role == 'teacher_defined_linter' }).to have(1).item
@ -388,16 +428,7 @@ RSpec.describe ProformaService::ConvertTaskToExercise do
context 'when task has multiple tests' do context 'when task has multiple tests' do
let(:tests) { [test, test2] } let(:tests) { [test, test2] }
let(:test2) do let(:test2) do
ProformaXML::Test.new( ProformaXML::Test.new(files: test_files2)
files: test_files2,
meta_data: {
CodeOcean: {
'feedback-message': 'feedback-message',
'testing-framework': 'testing-framework',
'testing-framework-version': 'testing-framework-version',
},
}
)
end end
let(:test_files2) { [test_file2] } let(:test_files2) { [test_file2] }
let(:test_file2) do let(:test_file2) do
@ -467,14 +498,7 @@ RSpec.describe ProformaService::ConvertTaskToExercise do
title: 'title', title: 'title',
description: 'description', description: 'description',
test_type: 'test_type', test_type: 'test_type',
files: test_files, files: test_files
meta_data: {
CodeOcean: {
'feedback-message': 'feedback-message',
'testing-framework': 'testing-framework',
'testing-framework-version': 'testing-framework-version',
},
}
) )
end end
let(:test_files) { [test_file] } let(:test_files) { [test_file] }

View File

@ -30,7 +30,7 @@ RSpec.describe ProformaService::ExportTask do
before do before do
allow(ProformaService::ConvertExerciseToTask).to receive(:call).with(exercise:).and_return(task) allow(ProformaService::ConvertExerciseToTask).to receive(:call).with(exercise:).and_return(task)
allow(ProformaXML::Exporter).to receive(:new).with(task:, custom_namespaces: [{prefix: 'CodeOcean', uri: 'codeocean.openhpi.de'}]).and_return(exporter) allow(ProformaXML::Exporter).to receive(:new).with(task:).and_return(exporter)
end end
it do it do