Module: Deimos::TestHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/deimos/test_helpers.rb
Overview
Include this module in your RSpec spec_helper to stub out external dependencies and add methods to use to test encoding/decoding.
Class Method Summary collapse
-
.full_integration_test! ⇒ Object
Kafka test config with avro schema registry.
-
.kafka_test! ⇒ Object
Set the config to the right settings for a kafka test.
-
.sent_messages ⇒ Array<Hash>
for backwards compatibility.
-
.unit_test! ⇒ Object
Set the config to the right settings for a unit test.
Instance Method Summary collapse
-
#_frk_failure_message(topic, message, key = nil, partition_key = nil, was_negated = false) ⇒ Object
:nodoc:.
-
#_hash_diff(hash1, hash2) ⇒ Object
get the difference of 2 hashes.
-
#clear_kafka_messages! ⇒ Object
Clear all sent messages - e.g.
- #stub_batch_consumer(_klass) ⇒ Object deprecated Deprecated.
- #stub_consumer(_klass) ⇒ Object deprecated Deprecated.
- #stub_producer(_klass) ⇒ Object deprecated Deprecated.
- #stub_producers_and_consumers! ⇒ Object deprecated Deprecated.
-
#test_consume_batch(handler_class_or_topic, payloads, keys: [], partition_keys: [], call_original: false, skip_expectation: false, &block) ⇒ Object
Test that a given handler will consume a given batch payload correctly, i.e.
-
#test_consume_batch_invalid_message(handler_class, payloads) ⇒ Object
Check to see that a given message will fail due to validation errors.
-
#test_consume_invalid_message(handler_class, payload) ⇒ Object
Check to see that a given message will fail due to validation errors.
-
#test_consume_message(handler_class_or_topic, payload, call_original: false, key: nil, partition_key: nil, skip_expectation: false, &block) ⇒ Object
Test that a given handler will consume a given payload correctly, i.e.
Class Method Details
.full_integration_test! ⇒ Object
Kafka test config with avro schema registry
34 35 36 37 38 39 |
# File 'lib/deimos/test_helpers.rb', line 34 def full_integration_test! Deimos.configure do |deimos_config| deimos_config.producers.backend = :kafka deimos_config.schema.backend = :avro_schema_registry end end |
.kafka_test! ⇒ Object
Set the config to the right settings for a kafka test
42 43 44 45 46 47 |
# File 'lib/deimos/test_helpers.rb', line 42 def kafka_test! Deimos.configure do |deimos_config| deimos_config.producers.backend = :kafka deimos_config.schema.backend = :avro_validation end end |
.sent_messages ⇒ Array<Hash>
for backwards compatibility
18 19 20 |
# File 'lib/deimos/test_helpers.rb', line 18 def Deimos::Backends::Test. end |
.unit_test! ⇒ Object
Set the config to the right settings for a unit test
23 24 25 26 27 28 29 30 31 |
# File 'lib/deimos/test_helpers.rb', line 23 def unit_test! Deimos.configure do |deimos_config| deimos_config.logger = Logger.new(STDOUT) deimos_config.consumers.reraise_errors = true deimos_config.kafka.seed_brokers ||= ['test_broker'] deimos_config.schema.backend = Deimos.schema_backend_class.mock_backend deimos_config.producers.backend = :test end end |
Instance Method Details
#_frk_failure_message(topic, message, key = nil, partition_key = nil, was_negated = false) ⇒ Object
:nodoc:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/deimos/test_helpers.rb', line 100 def (topic, , key=nil, partition_key=nil, was_negated=false) = Deimos::Backends::Test.. select { |m| m[:topic] == topic }. map { |m| m.except(:topic) } = '' diff = nil min_hash_diff = nil if .any? = .map(&:inspect).join("\n") min_hash_diff = .min_by { |m| _hash_diff(m, ).keys.size } diff = RSpec::Expectations.differ. diff_as_object(, min_hash_diff[:payload]) end description = if .respond_to?(:description) .description elsif .nil? 'nil' else end str = "Expected #{topic} #{'not ' if was_negated}to have sent #{description}" str += " with key #{key}" if key str += " with partition key #{partition_key}" if partition_key str += "\nClosest message received: #{min_hash_diff}" if min_hash_diff str += "\nDiff: #{diff}" if diff str + "\nAll Messages received:\n#{}" end |
#_hash_diff(hash1, hash2) ⇒ Object
get the difference of 2 hashes.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/deimos/test_helpers.rb', line 87 def _hash_diff(hash1, hash2) if hash1.nil? || !hash1.is_a?(Hash) hash2 elsif hash2.nil? || !hash2.is_a?(Hash) hash1 else hash1.dup. delete_if { |k, v| hash2[k] == v }. merge!(hash2.dup.delete_if { |k, _v| hash1.key?(k) }) end end |
#clear_kafka_messages! ⇒ Object
Clear all sent messages - e.g. if we want to check that particular messages were sent or not sent after a point in time.
165 166 167 |
# File 'lib/deimos/test_helpers.rb', line 165 def Deimos::Backends::Test..clear end |
#stub_batch_consumer(_klass) ⇒ Object
80 81 82 |
# File 'lib/deimos/test_helpers.rb', line 80 def stub_batch_consumer(_klass) warn('Stubbing batch consumers is no longer necessary and this method will be removed in 3.0') end |
#stub_consumer(_klass) ⇒ Object
75 76 77 |
# File 'lib/deimos/test_helpers.rb', line 75 def stub_consumer(_klass) warn('Stubbing consumers is no longer necessary and this method will be removed in 3.0') end |
#stub_producer(_klass) ⇒ Object
70 71 72 |
# File 'lib/deimos/test_helpers.rb', line 70 def stub_producer(_klass) warn('Stubbing producers is no longer necessary and this method will be removed in 3.0') end |
#stub_producers_and_consumers! ⇒ Object
65 66 67 |
# File 'lib/deimos/test_helpers.rb', line 65 def stub_producers_and_consumers! warn('stub_producers_and_consumers! is no longer necessary and this method will be removed in 3.0') end |
#test_consume_batch(handler_class_or_topic, payloads, keys: [], partition_keys: [], call_original: false, skip_expectation: false, &block) ⇒ Object
Test that a given handler will consume a given batch payload correctly, i.e. that the schema is correct. If a block is given, that block will be executed when `consume` is called. Otherwise it will just confirm that `consume` is called at all. Deimos::Consumer or the topic as a string
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/deimos/test_helpers.rb', line 242 def test_consume_batch(handler_class_or_topic, payloads, keys: [], partition_keys: [], call_original: false, skip_expectation: false, &block) if call_original && block_given? raise 'Cannot have both call_original and be given a block!' end topic_name = 'my-topic' handler_class = if handler_class_or_topic.is_a?(String) _get_handler_class_from_topic(handler_class_or_topic) else handler_class_or_topic end handler = handler_class.new allow(handler_class).to receive(:new).and_return(handler) listener = double('listener', handler_class: handler_class, encoding: nil) = payloads.zip(keys, partition_keys).map do |payload, key, partition_key| key ||= _key_from_consumer(handler_class) double('message', 'key' => key, 'partition_key' => partition_key, 'partition' => 1, 'offset' => 1, 'headers' => {}, 'value' => payload) end batch = double('fetched_batch', 'messages' => , 'topic' => topic_name, 'partition' => 1, 'offset_lag' => 0) unless skip_expectation _handler_expectation(:consume_batch, payloads, handler, call_original, &block) end action = Phobos::Actions::ProcessBatchInline.new( listener: listener, batch: batch, metadata: { topic: topic_name } ) allow(action).to receive(:backoff_interval).and_return(0) allow(action).to receive(:handle_error) { |e| raise e } action.send(:execute) end |
#test_consume_batch_invalid_message(handler_class, payloads) ⇒ Object
Check to see that a given message will fail due to validation errors.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/deimos/test_helpers.rb', line 300 def (handler_class, payloads) topic_name = 'my-topic' handler = handler_class.new allow(handler_class).to receive(:new).and_return(handler) listener = double('listener', handler_class: handler_class, encoding: nil) = payloads.map do |payload| key ||= _key_from_consumer(handler_class) double('message', 'key' => key, 'partition' => 1, 'offset' => 1, 'value' => payload) end batch = double('fetched_batch', 'messages' => , 'topic' => topic_name, 'partition' => 1, 'offset_lag' => 0) action = Phobos::Actions::ProcessBatchInline.new( listener: listener, batch: batch, metadata: { topic: topic_name } ) allow(action).to receive(:backoff_interval).and_return(0) allow(action).to receive(:handle_error) { |e| raise e } expect { action.send(:execute) }. to raise_error end |
#test_consume_invalid_message(handler_class, payload) ⇒ Object
Check to see that a given message will fail due to validation errors.
228 229 230 231 232 233 |
# File 'lib/deimos/test_helpers.rb', line 228 def (handler_class, payload) expect { handler_class.decoder.validate(payload, schema: handler_class.decoder.schema) }.to raise_error(Avro::SchemaValidator::ValidationError) end |
#test_consume_message(handler_class_or_topic, payload, call_original: false, key: nil, partition_key: nil, skip_expectation: false, &block) ⇒ Object
Test that a given handler will consume a given payload correctly, i.e. that the schema is correct. If a block is given, that block will be executed when `consume` is called. Otherwise it will just confirm that `consume` is called at all. Deimos::Consumer or the topic as a string to continue as normal. Not compatible with a block. expectations on the consumer. Primarily used internally to Deimos.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/deimos/test_helpers.rb', line 182 def (handler_class_or_topic, payload, call_original: false, key: nil, partition_key: nil, skip_expectation: false, &block) raise 'Cannot have both call_original and be given a block!' if call_original && block_given? payload&.stringify_keys! handler_class = if handler_class_or_topic.is_a?(String) _get_handler_class_from_topic(handler_class_or_topic) else handler_class_or_topic end handler = handler_class.new allow(handler_class).to receive(:new).and_return(handler) listener = double('listener', handler_class: handler_class, encoding: nil) key ||= _key_from_consumer(handler_class) = double('message', 'key' => key, 'partition_key' => partition_key, 'partition' => 1, 'offset' => 1, 'headers' => {}, 'value' => payload) unless skip_expectation _handler_expectation(:consume, payload, handler, call_original, &block) end Phobos::Actions::ProcessMessage.new( listener: listener, message: , listener_metadata: { topic: 'my-topic' } ).send(:process_message, payload) end |