From 6dcf4a038256520342777303fee9432fc7d8528a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Bor=C3=A1k?= Date: Fri, 7 Jun 2024 15:58:25 +0200 Subject: [PATCH] Add Inspection specs --- lib/dump_cleaner/cleanup/inspection.rb | 4 +- .../dump_cleaner/cleanup/inspection_spec.rb | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 spec/lib/dump_cleaner/cleanup/inspection_spec.rb diff --git a/lib/dump_cleaner/cleanup/inspection.rb b/lib/dump_cleaner/cleanup/inspection.rb index c3c8aaa..3f745d3 100644 --- a/lib/dump_cleaner/cleanup/inspection.rb +++ b/lib/dump_cleaner/cleanup/inspection.rb @@ -3,8 +3,6 @@ module DumpCleaner module Cleanup module Inspection - private - def inspect_step_context(step_context, message: "Inspecting step context") Log.debug { message } Log.debug { "\n#{step_context.pretty_inspect}" } @@ -18,7 +16,7 @@ def subset(data, values: 10) subset_data.each_with_index { |element, index| subset_data[index] = subset(element, values:) } when Hash subset_data = data.take(values).to_h - subset_data["+ #{data.size - values} more..."] = [] if data.size > values + subset_data["+ #{data.size - values} more..."] = nil if data.size > values subset_data.each_key { |key| subset_data[key] = subset(subset_data[key], values:) } else subset_data = data diff --git a/spec/lib/dump_cleaner/cleanup/inspection_spec.rb b/spec/lib/dump_cleaner/cleanup/inspection_spec.rb new file mode 100644 index 0000000..282baa4 --- /dev/null +++ b/spec/lib/dump_cleaner/cleanup/inspection_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe DumpCleaner::Cleanup::Inspection do + let(:dummy) { Class.new { include DumpCleaner::Cleanup::Inspection }.new } + + def step_context(orig_value: "abc", record: { "id_column" => "123" }, type: "some_type", + cleanup_data: %w[a b c d e f g], repetition: 0) + DumpCleaner::Cleanup::StepContext.new(orig_value:, record:, type:, cleanup_data:, repetition:) + end + + describe "#inspect_step_context" do + it "calls pretty_print on the step_context" do + DumpCleaner::Log.instance.level = :debug + + step_context = step_context() + expect(step_context).to receive(:pretty_inspect) + + dummy.inspect_step_context(step_context) + end + end + + describe "subset" do + it "returns a subset of an array" do + expect(dummy.subset(%w[a b c d e f g], values: 3)).to eq(["a", "b", "c", "+ 4 more..."]) + end + + it "returns a subset of a recursive array" do + expect(dummy.subset([%w[a b c d e f g], %w[a b c d e f g]], values: 3)) + .to eq([["a", "b", "c", "+ 4 more..."], ["a", "b", "c", "+ 4 more..."]]) + expect(dummy.subset([%w[a b c d e f g], %w[a b c d e f g]], values: 1)) + .to eq([["a", "+ 6 more..."], "+ 1 more..."]) + end + + it "returns a subset of a hash" do + expect(dummy.subset({ a: 1, b: 2, c: 3, d: 4, e: 5 }, values: 3)) + .to eq({ a: 1, b: 2, c: 3, "+ 2 more..." => nil }) + end + + it "returns a subset of a recursive hash" do + expect(dummy.subset({ aa: { a: 1, b: 2, c: 3, d: 4, e: 5 } }, values: 3)) + .to eq({ aa: { a: 1, b: 2, c: 3, "+ 2 more..." => nil} }) + end + end + + describe "#truncate" do + it "truncates a string to the specified length including omission" do + expect(dummy.truncate("abcdefg", to: 5)).to eq("abcd…") + expect(dummy.truncate("abcdefg", to: 5, omission: "...")).to eq("ab...") + end + end +end