From f8223601229d5b1a3aafea49c2771355050251cb Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Fri, 21 May 2021 18:21:08 -0400 Subject: [PATCH] add Append::NilFields transform class --- lib/kiba/extend/transforms/append.rb | 13 ++++++++ lib/kiba/extend/version.rb | 2 +- spec/kiba/extend/transforms/append_spec.rb | 38 ++++++++++++++-------- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/lib/kiba/extend/transforms/append.rb b/lib/kiba/extend/transforms/append.rb index 7efcd1e60..31d8d1da0 100644 --- a/lib/kiba/extend/transforms/append.rb +++ b/lib/kiba/extend/transforms/append.rb @@ -3,6 +3,19 @@ module Extend module Transforms module Append ::Append = Kiba::Extend::Transforms::Append + class NilFields + def initialize(fields:) + @fields = fields + end + + def process(row) + @fields.each do |field| + row[field] = nil unless row.key?(field) + end + row + end + end + class ToFieldValue def initialize(field:, value:) @field = field diff --git a/lib/kiba/extend/version.rb b/lib/kiba/extend/version.rb index 4993c5b02..b73bf7752 100644 --- a/lib/kiba/extend/version.rb +++ b/lib/kiba/extend/version.rb @@ -1,5 +1,5 @@ module Kiba module Extend - VERSION = "1.10.0" + VERSION = "1.11.0" end end diff --git a/spec/kiba/extend/transforms/append_spec.rb b/spec/kiba/extend/transforms/append_spec.rb index a9a08ad18..bfc78bb20 100644 --- a/spec/kiba/extend/transforms/append_spec.rb +++ b/spec/kiba/extend/transforms/append_spec.rb @@ -1,32 +1,44 @@ require 'spec_helper' RSpec.describe Kiba::Extend::Transforms::Append do + let(:test_csv) { 'tmp/test.csv' } + before{ generate_csv(test_csv, rows) } + + describe 'NilFields' do + let(:rows) { [ + ['id', 'z'], + [1, 'zz'] + ] } + let(:result) { execute_job(filename: test_csv, + xform: Append::NilFields, + xformopt: {fields: %i[a b c z]}) } + it 'adds non-existing fields, populating with nil, while leaving existing fields alone' do + expected = {id: '1', z: 'zz', a: nil, b: nil, c: nil} + expect(result[0]).to eq(expected) + end + end + describe 'ToFieldValue' do - test_csv = 'tmp/test.csv' - rows = [ + let(:rows) { [ ['id', 'name'], [1, 'Weddy'], [2, nil], [3, ''] - ] - - before do - generate_csv(test_csv, rows) - @result = execute_job(filename: test_csv, - xform: Append::ToFieldValue, - xformopt: {field: :name, value: ' (name)'}) - end + ] } + let(:result) { execute_job(filename: test_csv, + xform: Append::ToFieldValue, + xformopt: {field: :name, value: ' (name)'}) } it 'prepends given value to existing field values' do expected = {id: '1', name: 'Weddy (name)'} - expect(@result[0]).to eq(expected) + expect(result[0]).to eq(expected) end it 'leaves nil values alone' do expected = {id: '2', name: nil} - expect(@result[1]).to eq(expected) + expect(result[1]).to eq(expected) end it 'leaves blank values alone' do expected = {id: '3', name: ''} - expect(@result[2]).to eq(expected) + expect(result[2]).to eq(expected) end end end