CSV ファイルを複数のテーブルにインポートするインポート機能を作成しています。CsvParser
CSV ファイルを解析してレコードを作成するというモジュールを作成しました。作成アクションを受け取る私のモデルは、CsvParser
. これらは を呼び出してCsvParser.create
、正しい属性順序とオプションのラムダを渡しますvalue_parser
。このラムダは、ハッシュ内の値を優先形式に変換します。
class Mutation < ActiveRecord::Base
extend CsvParser
def self.import_csv(csv_file)
attribute_order = %w[reg_nr receipt_date reference_number book_date is_credit sum balance description]
value_parser = lambda do |h|
h["is_credit"] = ((h["is_credit"] == 'B') if h["is_credit"].present?)
h["sum"] = -1 * h["sum"].to_f unless h["is_credit"]
return [h]
end
CsvParser.create(csv_file, self, attribute_order, value_parser)
end
end
メソッド内のチェックの代わりにラムダを使用している理由CsvParser.create
は、ラムダがこのモデルに属するビジネス ルールのようなものだからです。
私の質問は、このラムダをテストする方法です。モデルまたは CsvParser でテストする必要がありますか? self.import
ラムダ自体またはメソッドの配列の結果をテストする必要がありますか? 多分私は別のコード構造を作るべきですか?
私の CsvParser は次のようになります。
require "csv"
module CsvParser
def self.create(csv_file, klass, attribute_order, value_parser = nil)
parsed_csv = CSV.parse(csv_file, col_sep: "|")
records = []
ActiveRecord::Base.transaction do
parsed_csv.each do |row|
record = Hash.new {|h, k| h[k] = []}
row.each_with_index do |value, index|
record[attribute_order[index]] = value
end
if value_parser.blank?
records << klass.create(record)
else
value_parser.call(record).each do |parsed_record|
records << klass.create(parsed_record)
end
end
end
end
return records
end
end
モジュール自体をテストしています: require 'spec_helper'
describe CsvParser do
it "should create relations" do
file = File.new(Rails.root.join('spec/fixtures/files/importrelaties.txt'))
Relation.should_receive(:create).at_least(:once)
Relation.import_csv(file).should be_kind_of Array
end
it "should create mutations" do
file = File.new(Rails.root.join('spec/fixtures/files/importmutaties.txt'))
Mutation.should_receive(:create).at_least(:once)
Mutation.import_csv(file).should be_kind_of Array
end
it "should create strategies" do
file = File.new(Rails.root.join('spec/fixtures/files/importplan.txt'))
Strategy.should_receive(:create).at_least(:once)
Strategy.import_csv(file).should be_kind_of Array
end
it "should create reservations" do
file = File.new(Rails.root.join('spec/fixtures/files/importreservering.txt'))
Reservation.should_receive(:create).at_least(:once)
Reservation.import_csv(file).should be_kind_of Array
end
end