From 38e829aecb8f2fc9ab13a24c2b9213c54acc81df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Bor=C3=A1k?= Date: Sat, 1 Jun 2024 22:44:27 +0200 Subject: [PATCH] Fix tests --- .rspec | 1 - .../cleaning_steps/randomized_number_spec.rb | 49 ++++++++++++------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/.rspec b/.rspec index 34c5164..83e16f8 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,2 @@ ---format documentation --color --require spec_helper diff --git a/spec/lib/dump_cleaner/cleanup/cleaning_steps/randomized_number_spec.rb b/spec/lib/dump_cleaner/cleanup/cleaning_steps/randomized_number_spec.rb index e184a45..69aafff 100644 --- a/spec/lib/dump_cleaner/cleanup/cleaning_steps/randomized_number_spec.rb +++ b/spec/lib/dump_cleaner/cleanup/cleaning_steps/randomized_number_spec.rb @@ -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