-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
81 additions
and
30 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
lib/dump_cleaner/cleanup/cleaning_steps/fill_up_with_string.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module DumpCleaner | ||
module Cleanup | ||
module CleaningSteps | ||
class FillUpWithString < Base | ||
include BytesizeHelpers | ||
|
||
def run(string: "anonymized #{type}", padding: " ", strict_bytesize_check: false) | ||
if strict_bytesize_check && string.bytesize != orig_value.bytesize | ||
raise "The bytesize of the string must be equal to the bytesize of the original value." | ||
end | ||
|
||
string = set_to_bytesize(string, bytesize: orig_value.bytesize, padding:) | ||
AddRepetitionSuffix.new(StepContext.new_from(step_context, current_value: string)).run | ||
end | ||
end | ||
end | ||
end | ||
end |
14 changes: 0 additions & 14 deletions
14
lib/dump_cleaner/cleanup/cleaning_steps/same_length_anonymized_string.rb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
spec/lib/dump_cleaner/cleanup/cleaning_steps/fill_up_with_string_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe DumpCleaner::Cleanup::CleaningSteps::FillUpWithString do | ||
def step_context(orig_value:, record: { "id_value" => "123" }, type: "some_type", cleanup_data: [], repetition: 0) | ||
DumpCleaner::Cleanup::StepContext.new(orig_value:, record:, type:, cleanup_data:, repetition:) | ||
end | ||
|
||
def cleaner(step_context) | ||
described_class.new(step_context) | ||
end | ||
|
||
describe "#run" do | ||
it "returns a step_context" do | ||
step_context = step_context(orig_value: "abc") | ||
expect(cleaner(step_context).run).to be_a(DumpCleaner::Cleanup::StepContext) | ||
end | ||
|
||
context "with custom string provided" do | ||
it "returns the string if bytesize equal" do | ||
step_context = step_context(orig_value: "abcdef") | ||
expect(cleaner(step_context).run(string: "uvwxyz").current_value).to eq("uvwxyz") | ||
end | ||
|
||
it "returns the string truncated if too long" do | ||
step_context = step_context(orig_value: "abc") | ||
expect(cleaner(step_context).run(string: "efghij").current_value).to eq("efg") | ||
end | ||
|
||
it "returns the string repeated if too short" do | ||
step_context = step_context(orig_value: "abcdefg") | ||
expect(cleaner(step_context).run(string: "xyz").current_value).to eq("xyz xyz") | ||
end | ||
end | ||
|
||
context "with default string" do | ||
it "returns the 'anonymized type' string if bytesize equal" do | ||
step_context = step_context(orig_value: "some equally long st") | ||
expect(cleaner(step_context).run.current_value).to eq("anonymized some_type") | ||
end | ||
|
||
it "returns the 'anonymized type' string truncated if too long" do | ||
step_context = step_context(orig_value: "abc") | ||
expect(cleaner(step_context).run.current_value).to eq("ano") | ||
end | ||
|
||
it "returns the 'anonymized type' string repeated if too short" do | ||
step_context = step_context(orig_value: "some very long string and still even longer") | ||
expect(cleaner(step_context).run.current_value).to eq("anonymized some_type anonymized some_type a") | ||
end | ||
end | ||
|
||
it "raises error if byte sizes don't match and strict check requested" do | ||
step_context = step_context(orig_value: "abcdefg") | ||
expect do | ||
cleaner(step_context).run(string: "xyz", strict_bytesize_check: true).current_value | ||
end.to raise_error(/must be equal/) | ||
end | ||
end | ||
end |