Skip to content
This repository was archived by the owner on Nov 7, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/data_magic/index/document_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ def calculated_fields(row, config)

# row: a hash (keys may be strings or symbols)
# valid_types: an array of allowed types
# null_value: row values that will map to null
# field_types: hash field_name : type (float, integer, string)
# returns a hash where values have been coerced to the new type
# TODO: move type validation to config load time instead
def map_column_types(row, config)
valid_types = config.valid_types
null_value = config.null_value || null_value = 'NULL'
null_value = [*config.null_value] || ['NULL']

mapped = {}
row.each do |key, value|
if value == null_value
if null_value.include? value
mapped[key] = nil
else
type = config.csv_column_type(key)
Expand All @@ -76,6 +77,7 @@ def map_column_types(row, config)
def lowercase_columns(row, field_types = {})
new_columns = {}
row.each do |key, value|
next if value.nil?
type = field_types[key.to_sym] || field_types[key.to_s]
new_columns["_#{key}"] = value.downcase if type == "name" || type == "autocomplete"
end
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/data_magic/index/document_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@
end
end

context "with custom null_value" do

describe "a single null_value mapping" do
before do
allow(config).to receive(:null_value).and_return(["NULL"])
end

subject {{ name: 'Smithville', sometimesNULL: 'NULL' }}
let(:expected_document) {{ 'name' => 'Smithville',
'sometimesNULL' => nil }}
it_correctly "creates a document"
end

describe "multiple null_value mappings" do
before do
allow(config).to receive(:null_value).and_return(["NULL","CustomNull"])
end

subject {{ name: 'Smithville', maybeNull: 'CustomNull', sometimesNULL: 'NULL' }}
let(:expected_document) {{ 'name' => 'Smithville',
'maybeNull' => nil,
'sometimesNULL' => nil }}
it_correctly "creates a document"
end

end

context "with type mapping" do
describe "integer" do
before do
Expand Down