Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
borama committed Jun 1, 2024
1 parent be405de commit 38e829a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--format documentation
--color
--require spec_helper
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
require "spec_helper"

RSpec.describe DumpCleaner::Cleanup::CleaningSteps::RandomizedNumber do
let(:cleaner) { described_class.new(data: [], type: "some_type") }
let(:record) { { "id_value" => "123" } }
def step_context(orig_value:, record: { "id_value" => "123" }, type: "some_type", cleanup_data: [])
DumpCleaner::Cleanup::StepContext.new(orig_value:, record:, type:, cleanup_data:)
end

def cleaner(step_context)
described_class.new(step_context)
end

describe "#run" do
it "returns a number as a string" do
expect(cleaner.run(orig_value: "1", record:)).to be_a(String)
it "returns the step_context" do
step_context = step_context(orig_value: "1")
expect(cleaner(step_context).run).to eq(step_context)
end

it "returns the current_value number as a string" do
expect(cleaner(step_context(orig_value: "1")).run.current_value).to be_a(String)
end

it "returns a number with the same length" do
expect(cleaner.run(orig_value: "123.456", record:).length).to eq(7)
expect(cleaner.run(orig_value: "-123.456", record:).length).to eq(8)
expect(cleaner(step_context(orig_value: "123.456")).run.current_value.length).to eq(7)
expect(cleaner(step_context(orig_value: "-123.456")).run.current_value.length).to eq(8)
end

it "returns a number with the same number of decimal places" do
expect(cleaner.run(orig_value: "123.456", record:).split(".")[1].to_s.length).to eq(3)
expect(cleaner.run(orig_value: "-123.4", record:).split(".")[1].to_s.length).to eq(1)
expect(cleaner.run(orig_value: "-123", record:).split(".")[1].to_s.length).to eq(0)
expect(cleaner(step_context(orig_value: "123.456")).run.current_value.split(".")[1].to_s.length).to eq(3)
expect(cleaner(step_context(orig_value: "-123.4")).run.current_value.split(".")[1].to_s.length).to eq(1)
expect(cleaner(step_context(orig_value: "-123")).run.current_value.split(".")[1].to_s.length).to eq(0)
end

it "returns a random number that is within the default range" do
expect(cleaner.run(orig_value: "10.0", record:).to_f).to be_within(1).of(10)
expect(cleaner(step_context(orig_value: "10.0")).run.current_value.to_f).to be_within(1).of(10)
end

it "returns a random number that is within the specified range" do
expect(cleaner.run(orig_value: "1.0", record:, difference_within: 0.2).to_f).to be_within(0.2).of(1)
expect(cleaner(step_context(orig_value: "1.0")).run(difference_within: 0.2).current_value.to_f)
.to be_within(0.2).of(1)
end

it "clamps the number when difference too small" do
expect(cleaner.run(orig_value: "1000", record:, difference_within: 1.0).to_f).to eq(1000)
expect(cleaner.run(orig_value: "10.0", record:, difference_within: 0.1).to_f).to eq(10)
expect(cleaner(step_context(orig_value: "1000")).run(difference_within: 1.0).current_value.to_f).to eq(1000)
expect(cleaner(step_context(orig_value: "10.0")).run(difference_within: 0.1).current_value.to_f).to eq(10)
end

it "returns a deterministic random number" do
result1 = cleaner.run(orig_value: "1", record:, difference_within: 1_000_000)
result2 = cleaner.run(orig_value: "1", record:, difference_within: 1_000_000)
result1 = cleaner(step_context(orig_value: "1")).run(difference_within: 1_000_000).current_value
result2 = cleaner(step_context(orig_value: "1")).run(difference_within: 1_000_000).current_value
expect(result1).to eq(result2)

result3 = cleaner.run(orig_value: "2", record:, difference_within: 1_000_000)
result4 = cleaner.run(orig_value: "2", record:, difference_within: 1_000_000)
result3 = cleaner(step_context(orig_value: "2")).run(difference_within: 1_000_000).current_value
result4 = cleaner(step_context(orig_value: "2")).run(difference_within: 1_000_000).current_value
expect(result3).to eq(result4)
expect(result3).not_to eq(result1)
end

it "keeps the sign of the original number by default" do
(-10..-1).each do |i|
expect(cleaner.run(orig_value: i.to_s, record:, difference_within: 1_000_000).to_f).to be < 0
expect(cleaner(step_context(orig_value: i.to_s)).run(difference_within: 1_000_000).current_value.to_f).to be < 0
end

(1..10).each do |i|
expect(cleaner.run(orig_value: i.to_s, record:, difference_within: 1_000_000).to_f).to be > 0
expect(cleaner(step_context(orig_value: i.to_s)).run(difference_within: 1_000_000).current_value.to_f).to be > 0
end
end
end
Expand Down

0 comments on commit 38e829a

Please sign in to comment.