Skip to content

Commit

Permalink
rubocop remarks (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld authored Jan 6, 2025
1 parent 0e48ee7 commit 19a8a93
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 31 deletions.
3 changes: 3 additions & 0 deletions config/locales/errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ en:
missing: needs to be present
id_format: needs to be a String
nested.id2_format: needs to be a String
id_format: needs to be a String
test_format: needs to be 5
details.id_format: needs to be a String

config:
missing: needs to be present
Expand Down
22 changes: 11 additions & 11 deletions spec/lib/karafka/core/configurable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
it { expect(config.nested1.nested2.leaf).to eq(6) }
it { expect(config.nested1.nested1).to eq(1) }
it { expect(config.nested1.nested2.with_constructor).to eq(5) }
it { expect(config.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config.nested1.nested2.ov_constructor).to be(true) }
it { expect(config.nested1.nested2.with_zero_constructor).to eq(7) }
end

Expand All @@ -52,7 +52,7 @@
it { expect(config.nested1.nested2.leaf).to eq(8) }
it { expect(config.nested1.nested1).to eq(1) }
it { expect(config.nested1.nested2.with_constructor).to eq(5) }
it { expect(config.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config.nested1.nested2.ov_constructor).to be(true) }
end

context 'when we inherit and alter settings' do
Expand All @@ -75,12 +75,12 @@
it { expect(config.nested1.nested2.leaf).to eq(6) }
it { expect(config.nested1.nested1).to eq(1) }
it { expect(config.nested1.nested2.with_constructor).to eq(5) }
it { expect(config.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config.nested1.nested2.ov_constructor).to be(true) }
it { expect(config_sub.with_default).to eq(123) }
it { expect(config_sub.nested1.nested2.leaf).to eq(6) }
it { expect(config_sub.nested1.nested1).to eq(1) }
it { expect(config_sub.nested1.nested2.with_constructor).to eq(5) }
it { expect(config_sub.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config_sub.nested1.nested2.ov_constructor).to be(true) }
end

context 'when we inherit and change values' do
Expand Down Expand Up @@ -200,7 +200,7 @@
let(:constructor) { ->(default) { default || attempts.pop } }

it 'expect to retry until non-false is present and then cache it' do
3.times { expect(config.lazy_setting).to eq(false) }
3.times { expect(config.lazy_setting).to be(false) }
expect(config.lazy_setting).to eq(10)
expect(config.lazy_setting).to eq(10)
end
Expand All @@ -225,7 +225,7 @@
let(:constructor) { -> { attempts.pop } }

it 'expect to retry until non-false is present and then cache it' do
3.times { expect(config.lazy_setting).to eq(false) }
3.times { expect(config.lazy_setting).to be(false) }
expect(config.lazy_setting).to eq(10)
expect(config.lazy_setting).to eq(10)
end
Expand Down Expand Up @@ -275,7 +275,7 @@
it { expect(config.nested1.nested2.leaf).to eq(6) }
it { expect(config.nested1.nested1).to eq(1) }
it { expect(config.nested1.nested2.with_constructor).to eq(5) }
it { expect(config.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config.nested1.nested2.ov_constructor).to be(true) }
end

context 'when we have two instances' do
Expand Down Expand Up @@ -306,7 +306,7 @@
it { expect(config.nested1.nested2.leaf).to eq(8) }
it { expect(config.nested1.nested1).to eq(1) }
it { expect(config.nested1.nested2.with_constructor).to eq(5) }
it { expect(config.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config.nested1.nested2.ov_constructor).to be(true) }
end

context 'when we inherit and alter settings' do
Expand All @@ -329,12 +329,12 @@
it { expect(config.nested1.nested2.leaf).to eq(6) }
it { expect(config.nested1.nested1).to eq(1) }
it { expect(config.nested1.nested2.with_constructor).to eq(5) }
it { expect(config.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config.nested1.nested2.ov_constructor).to be(true) }
it { expect(config_sub.with_default).to eq(123) }
it { expect(config_sub.nested1.nested2.leaf).to eq(6) }
it { expect(config_sub.nested1.nested1).to eq(1) }
it { expect(config_sub.nested1.nested2.with_constructor).to eq(5) }
it { expect(config_sub.nested1.nested2.ov_constructor).to eq(true) }
it { expect(config_sub.nested1.nested2.ov_constructor).to be(true) }
end

context 'when we inherit and change values' do
Expand Down Expand Up @@ -426,7 +426,7 @@ def testable
let(:constructor) { ->(default) { default || attempts.pop } }

it 'expect to retry until non-false is present and then cache it' do
3.times { expect(config.lazy_setting).to eq(false) }
3.times { expect(config.lazy_setting).to be(false) }
expect(config.lazy_setting).to eq(10)
expect(config.lazy_setting).to eq(10)
end
Expand Down
99 changes: 95 additions & 4 deletions spec/lib/karafka/core/contractable/result_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,110 @@
# frozen_string_literal: true

RSpec.describe_current do
subject(:contract_class) do
Class.new(Karafka::Core::Contractable::Contract) do
configure do |config|
config.error_messages = YAML.safe_load(
File.read(
File.join(Karafka::Core.gem_root, 'config', 'locales', 'errors.yml')
)
).fetch('en').fetch('validations').fetch('test')
end

required(:id) { |id| id.is_a?(String) }

optional(:test) { |test| test == 5 }
end
end

let(:contract) { contract_class.new }

context 'when there are no errors' do
pending
let(:data) { { id: '123', test: 5 } }
let(:result) { contract.call(data) }

it 'returns success' do
expect(result.success?).to be true
end

it 'has no errors' do
expect(result.errors).to be_empty
end
end

context 'when there are errors and all keys can be mapped to messages' do
pending
let(:data) { { id: 123, test: 4 } }
let(:result) { contract.call(data) }

it 'does not return success' do
expect(result.success?).to be false
end

it 'maps errors to messages' do
expect(result.errors).to eq(
id: 'needs to be a String',
test: 'needs to be 5'
)
end
end

context 'when there are errors with nested keys' do
pending
subject(:contract_class) do
Class.new(Karafka::Core::Contractable::Contract) do
configure do |config|
config.error_messages = YAML.safe_load(
File.read(
File.join(Karafka::Core.gem_root, 'config', 'locales', 'errors.yml')
)
).fetch('en').fetch('validations').fetch('test')
end

nested(:details) do
required(:id) { |id| id.is_a?(String) }
end
end
end

let(:data) { { details: { id: 123 } } }
let(:result) { contract.call(data) }

it 'does not return success' do
expect(result.success?).to be false
end

it 'maps nested errors to messages with dot notation' do
expect(result.errors).to eq(
'details.id': 'needs to be a String'
)
end
end

context 'when there are errors and key cannot be mapped to a message' do
pending
subject(:contract_class) do
Class.new(Karafka::Core::Contractable::Contract) do
configure do |config|
config.error_messages = YAML.safe_load(
File.read(
File.join(Karafka::Core.gem_root, 'config', 'locales', 'errors.yml')
)
).fetch('en').fetch('validations').fetch('test')
end

required(:id) { |id| id.is_a?(String) }
end
end

let(:data) { { id: 123 } }
let(:result) { contract.call(data) }

it 'does not return success' do
expect(result.success?).to be false
end

it 'falls back to a generic error message' do
expect(result.errors).to eq(
id: 'needs to be a String'
)
end
end
end
32 changes: 16 additions & 16 deletions spec/lib/karafka/core/monitoring/statistics_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
subject(:decorated) { decorator.call(emited_stats1) }

it { expect(decorated['string']).to eq('value1') }
it { expect(decorated.key?('string_d')).to eq(false) }
it { expect(decorated.key?('string_d')).to be(false) }
it { expect(decorated['float_d']).to eq(0) }
it { expect(decorated['int_d']).to eq(0) }
it { expect(decorated.dig(*broker_scope)['txbytes_d']).to eq(0) }
Expand All @@ -68,7 +68,7 @@
end

it { expect(decorated['string']).to eq('value2') }
it { expect(decorated.key?('string_d')).to eq(false) }
it { expect(decorated.key?('string_d')).to be(false) }
it { expect(decorated['float_d'].round(10)).to eq(0.4) }
it { expect(decorated['int_d']).to eq(18) }
it { expect(decorated.dig(*broker_scope)['txbytes_d']).to eq(30) }
Expand All @@ -83,16 +83,16 @@
end

it { expect(decorated['string']).to eq('value3') }
it { expect(decorated.key?('string_d')).to eq(false) }
it { expect(decorated.key?('string_fd')).to eq(false) }
it { expect(decorated.key?('string_d')).to be(false) }
it { expect(decorated.key?('string_fd')).to be(false) }
it { expect(decorated['float_d'].round(10)).to eq(1.0) }
it { expect(decorated['float_fd']).to be_within(5).of(0) }
it { expect(decorated['int_d']).to eq(-120) }
it { expect(decorated['int_fd']).to be_within(5).of(0) }
it { expect(decorated.dig(*broker_scope)['txbytes_d']).to eq(-151) }
it { expect(decorated.dig(*broker_scope)['txbytes_fd']).to be_within(5).of(0) }
it { expect(decorated).to be_frozen }
it { expect(decorated.key?('float_d_d')).to eq(false) }
it { expect(decorated.key?('float_d_d')).to be(false) }
end

context 'when a broker is no longer present' do
Expand All @@ -104,15 +104,15 @@
before { emited_stats2['nested'] = {} }

it { expect(decorated['string']).to eq('value2') }
it { expect(decorated.key?('string_d')).to eq(false) }
it { expect(decorated.key?('string_fd')).to eq(false) }
it { expect(decorated.key?('string_d')).to be(false) }
it { expect(decorated.key?('string_fd')).to be(false) }
it { expect(decorated['float_d'].round(10)).to eq(0.4) }
it { expect(decorated['float_fd']).to be_within(5).of(0) }
it { expect(decorated['int_d']).to eq(18) }
it { expect(decorated['int_fd']).to be_within(5).of(0) }
it { expect(decorated['nested']).to eq({}) }
it { expect(decorated).to be_frozen }
it { expect(decorated.key?('float_d_d')).to eq(false) }
it { expect(decorated.key?('float_d_d')).to be(false) }
end

context 'when broker was introduced later on' do
Expand All @@ -124,15 +124,15 @@
before { emited_stats1['nested'] = {} }

it { expect(decorated['string']).to eq('value2') }
it { expect(decorated.key?('string_d')).to eq(false) }
it { expect(decorated.key?('string_d')).to be(false) }
it { expect(decorated['float_d'].round(10)).to eq(0.4) }
it { expect(decorated['float_fd']).to be_within(5).of(0) }
it { expect(decorated['int_d']).to eq(18) }
it { expect(decorated['int_fd']).to be_within(5).of(0) }
it { expect(decorated.dig(*broker_scope)['txbytes_d']).to eq(0) }
it { expect(decorated.dig(*broker_scope)['txbytes_fd']).to be_within(5).of(0) }
it { expect(decorated).to be_frozen }
it { expect(decorated.key?('float_d_d')).to eq(false) }
it { expect(decorated.key?('float_d_d')).to be(false) }
end

context 'when value remains unchanged over time' do
Expand All @@ -148,14 +148,14 @@

let(:deep_copy) { -> { Marshal.load(Marshal.dump(emited_stats1)) } }

it { expect(decorated.key?('string_d')).to eq(false) }
it { expect(decorated.key?('string_fd')).to eq(false) }
it { expect(decorated.key?('string_d')).to be(false) }
it { expect(decorated.key?('string_fd')).to be(false) }
it { expect(decorated['float_d']).to eq(0) }
it { expect(decorated['float_fd']).to be_within(5).of(10) }
it { expect(decorated['int_d']).to eq(0) }
it { expect(decorated['int_fd']).to be_within(5).of(10) }
it { expect(decorated).to be_frozen }
it { expect(decorated.key?('float_d_d')).to eq(false) }
it { expect(decorated.key?('float_d_d')).to be(false) }
end

context 'when value remains unchanged over multiple occurrences and time' do
Expand All @@ -173,13 +173,13 @@

let(:deep_copy) { -> { Marshal.load(Marshal.dump(emited_stats1)) } }

it { expect(decorated.key?('string_d')).to eq(false) }
it { expect(decorated.key?('string_fd')).to eq(false) }
it { expect(decorated.key?('string_d')).to be(false) }
it { expect(decorated.key?('string_fd')).to be(false) }
it { expect(decorated['float_d']).to eq(0) }
it { expect(decorated['float_fd']).to be_within(5).of(20) }
it { expect(decorated['int_d']).to eq(0) }
it { expect(decorated['int_fd']).to be_within(5).of(20) }
it { expect(decorated).to be_frozen }
it { expect(decorated.key?('float_d_d')).to eq(false) }
it { expect(decorated.key?('float_d_d')).to be(false) }
end
end

0 comments on commit 19a8a93

Please sign in to comment.