Commit e9a29a01 by Ivan Lan

导入测试增加上下文

parent b14c28b8
...@@ -80,7 +80,6 @@ module TalltyImportExport ...@@ -80,7 +80,6 @@ module TalltyImportExport
else else
associations.create!(line_info) associations.create!(line_info)
end end
end end
end end
end end
...@@ -8,9 +8,10 @@ RSpec.describe TalltyImportExport::Importable do ...@@ -8,9 +8,10 @@ RSpec.describe TalltyImportExport::Importable do
[ [
{ key: 'name', name: '名称' }, { key: 'name', name: '名称' },
{ key: 'code', name: '学号', convert: :handle_code }, { key: 'code', name: '学号', convert: :handle_code },
{ key: 'sub_code', name: '副学号', convert: :skip}, { key: 'sub_code', name: '副学号', convert: :skip },
{ key: 'meta1', name: 'meta字段1', json: :meta }, { key: 'meta1', name: 'meta字段1', json: :meta },
{ key: 'meta2', name: 'meta字段2', json: :meta }, { key: 'meta2', name: 'meta字段2', json: :meta },
{ key: 'sum', name: '累加值', convert: :handle_sum },
] ]
end end
end end
...@@ -23,6 +24,10 @@ RSpec.describe TalltyImportExport::Importable do ...@@ -23,6 +24,10 @@ RSpec.describe TalltyImportExport::Importable do
def handle_code code, processing_line_info, raw_line_info def handle_code code, processing_line_info, raw_line_info
"#{code}_#{raw_line_info['sub_code']}" "#{code}_#{raw_line_info['sub_code']}"
end end
def handle_sum num, processing_line_info, raw_line_info
context.last_line_info ? context.last_line_info['sum'] + num : num
end
end end
end end
end end
...@@ -38,11 +43,11 @@ RSpec.describe TalltyImportExport::Importable do ...@@ -38,11 +43,11 @@ RSpec.describe TalltyImportExport::Importable do
Test.import_xlsx('./spec/test1.xlsx', @result) Test.import_xlsx('./spec/test1.xlsx', @result)
expect(@result).to eq([ expect(@result).to eq([
{"name"=>1, "code"=>"20070101_1", "sub_code"=>nil, "meta"=>{"meta1"=>"meta1", "meta2"=>"metaA"}}, {"name"=>1, "code"=>"20070101_1", "sub_code"=>nil, "meta"=>{"meta1"=>"meta1", "meta2"=>"metaA" }, "sum" => 1 },
{"name"=>2, "code"=>"20070102_2", "sub_code"=>nil, "meta"=>{"meta1"=>"meta2", "meta2"=>"metaB"}}, {"name"=>2, "code"=>"20070102_2", "sub_code"=>nil, "meta"=>{"meta1"=>"meta2", "meta2"=>"metaB" }, "sum" => 3 },
{"name"=>3, "code"=>"20070103_3", "sub_code"=>nil, "meta"=>{"meta1"=>"meta3", "meta2"=>"metaC"}}, {"name"=>3, "code"=>"20070103_3", "sub_code"=>nil, "meta"=>{"meta1"=>"meta3", "meta2"=>"metaC" }, "sum" => 6 },
{"name"=>4, "code"=>"20070104_4", "sub_code"=>nil, "meta"=>{"meta1"=>"meta4", "meta2"=>"metaD"}}, {"name"=>4, "code"=>"20070104_4", "sub_code"=>nil, "meta"=>{"meta1"=>"meta4", "meta2"=>"metaD" }, "sum" => 10 },
{"name"=>5, "code"=>"20070105_5", "sub_code"=>nil, "meta"=>{"meta1"=>"meta5", "meta2"=>"metaE"}}, {"name"=>5, "code"=>"20070105_5", "sub_code"=>nil, "meta"=>{"meta1"=>"meta5", "meta2"=>"metaE" }, "sum" => 15 },
]) ])
end end
end end
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment