From be2c1c54141d554299244d2d10fae9052b5ac6d3 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Wed, 21 Jul 2021 21:49:46 -0400 Subject: [PATCH] sort with usenull --- lib/kiba/extend/transforms/clean.rb | 2 ++ lib/kiba/extend/transforms/helpers.rb | 2 +- spec/kiba/extend/transforms/clean_spec.rb | 32 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/kiba/extend/transforms/clean.rb b/lib/kiba/extend/transforms/clean.rb index 5f5d6308d..481fa6d36 100644 --- a/lib/kiba/extend/transforms/clean.rb +++ b/lib/kiba/extend/transforms/clean.rb @@ -36,6 +36,8 @@ def process(row) private def process_for_sort(val) + return val.gsub('%NULLVALUE%', 'zzzzzzzzzzzzzz').downcase.gsub(/[^[:alnum:][:space:]]/, '') if @usenull + val.downcase.gsub(/[^[:alnum:][:space:]]/, '') end diff --git a/lib/kiba/extend/transforms/helpers.rb b/lib/kiba/extend/transforms/helpers.rb index b248bd860..f7afa13fd 100644 --- a/lib/kiba/extend/transforms/helpers.rb +++ b/lib/kiba/extend/transforms/helpers.rb @@ -45,7 +45,7 @@ def field_values(row:, fields:, discard: %i[nil empty delim], delim: DELIM, usen # @return [Array(Symbol)] of names of fields that should be kept, based on given discard # and usenull param values and the field values private_class_method def keep_fields(field_vals, discard, delim, usenull) - field_vals.transform_values!{ |val| val.gsub('%NULLVALUE%', '') } if usenull + field_vals = field_vals.transform_values{ |val| val.gsub('%NULLVALUE%', '') } if usenull field_vals = field_vals.reject{ |field, val| val.empty? } if discard.any?(:empty) field_vals = field_vals.reject{ |field, val| delim_only?(val, delim) } if discard.any?(:delim) field_vals.keys diff --git a/spec/kiba/extend/transforms/clean_spec.rb b/spec/kiba/extend/transforms/clean_spec.rb index 97c22f159..0f5abe56c 100644 --- a/spec/kiba/extend/transforms/clean_spec.rb +++ b/spec/kiba/extend/transforms/clean_spec.rb @@ -54,6 +54,38 @@ end end end + + context 'when usenull = true' do + let(:usenull) { true } + it 'sorts as expected' do + expected = [ + 'Organization;Person;unmapped', + ';', + nil, + '', + 'notmapped;Person', + 'apple;%NULLVALUE%', + 'oatmeal;%NULLVALUE%' + ] + expect(result.map{ |res| res[:type] }).to eq(expected) + end + + context 'when direction = :desc' do + let(:direction) { :desc } + it 'sorts as expected' do + expected = [ + 'unmapped;Person;Organization', + ';', + nil, + '', + 'Person;notmapped', + '%NULLVALUE%;apple', + '%NULLVALUE%;oatmeal' + ] + expect(result.map{ |res| res[:type] }).to eq(expected) + end + end + end end describe 'ClearFields' do