Skip to content

Commit

Permalink
Add Inspection specs
Browse files Browse the repository at this point in the history
  • Loading branch information
borama committed Jun 7, 2024
1 parent bbf9a50 commit 6dcf4a0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/dump_cleaner/cleanup/inspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}" }
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions spec/lib/dump_cleaner/cleanup/inspection_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6dcf4a0

Please sign in to comment.