-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
7 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
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
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module WaterDrop | ||
module Contracts | ||
# Contract to ensure that arguments provided to the transactional offset commit are valid | ||
# and match our expectations | ||
class TransactionalOffset < ::Karafka::Core::Contractable::Contract | ||
configure do |config| | ||
config.error_messages = YAML.safe_load( | ||
File.read( | ||
File.join(WaterDrop.gem_root, 'config', 'locales', 'errors.yml') | ||
) | ||
).fetch('en').fetch('validations').fetch('transactional_offset') | ||
end | ||
|
||
required(:consumer) { |val| val.respond_to?(:consumer_group_metadata_pointer) } | ||
required(:topic) { |val| val.is_a?(String) && !val.empty? } | ||
required(:partition) { |val| val.is_a?(Integer) && val >= 0 } | ||
required(:offset) { |val| val.is_a?(Integer) && val >= 0 } | ||
required(:offset_metadata) { |val| val.is_a?(String) || val.nil? } | ||
end | ||
end | ||
end |
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
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
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
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,80 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe_current do | ||
subject(:contract_result) { described_class.new.call(input) } | ||
|
||
let(:consumer) { instance_double('Consumer', consumer_group_metadata_pointer: true) } | ||
let(:topic) { 'test_topic' } | ||
let(:partition) { 0 } | ||
let(:offset) { 10 } | ||
let(:offset_metadata) { 'metadata' } | ||
let(:input) do | ||
{ | ||
consumer: consumer, | ||
topic: topic, | ||
partition: partition, | ||
offset: offset, | ||
offset_metadata: offset_metadata | ||
} | ||
end | ||
|
||
context 'when all inputs are valid' do | ||
it { expect(contract_result).to be_success } | ||
end | ||
|
||
context 'when consumer is invalid' do | ||
let(:consumer) { nil } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
|
||
context 'when topic is invalid' do | ||
context 'when topic is empty' do | ||
let(:topic) { '' } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
|
||
context 'when topic is not a string' do | ||
let(:topic) { 123 } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
end | ||
|
||
context 'when partition is invalid' do | ||
context 'when partition is negative' do | ||
let(:partition) { -1 } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
|
||
context 'when partition is not an integer' do | ||
let(:partition) { 'not_an_integer' } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
end | ||
|
||
context 'when offset is invalid' do | ||
context 'when offset is negative' do | ||
let(:offset) { -10 } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
|
||
context 'when offset is not an integer' do | ||
let(:offset) { 'not_an_integer' } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
end | ||
|
||
context 'when offset_metadata is invalid' do | ||
context 'when offset_metadata is not a string or nil' do | ||
let(:offset_metadata) { 123 } | ||
|
||
it { expect(contract_result).not_to be_success } | ||
end | ||
end | ||
end |
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