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
-
.sent_messages ⇒ Array<Hash>
for backwards compatibility.
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
Instance Method Details
#_frk_failure_message(topic, message, key = nil, partition_key = nil, was_negated = false) ⇒ Object
:nodoc:
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/deimos/test_helpers.rb', line 84 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.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/deimos/test_helpers.rb', line 71 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.
149 150 151 |
# File 'lib/deimos/test_helpers.rb', line 149 def Deimos::Backends::Test..clear end |
#stub_batch_consumer(_klass) ⇒ Object
64 65 66 |
# File 'lib/deimos/test_helpers.rb', line 64 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
59 60 61 |
# File 'lib/deimos/test_helpers.rb', line 59 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
54 55 56 |
# File 'lib/deimos/test_helpers.rb', line 54 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
49 50 51 |
# File 'lib/deimos/test_helpers.rb', line 49 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
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 |
# File 'lib/deimos/test_helpers.rb', line 224 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 expectation = expect(handler).to receive(:consume_batch). with(payloads, anything, &block) expectation.and_call_original if call_original 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.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/deimos/test_helpers.rb', line 280 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.
210 211 212 213 214 215 |
# File 'lib/deimos/test_helpers.rb', line 210 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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/deimos/test_helpers.rb', line 166 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 expectation = expect(handler).to receive(:consume). with(payload, anything, &block) expectation.and_call_original if call_original end Phobos::Actions::ProcessMessage.new( listener: listener, message: , listener_metadata: { topic: 'my-topic' } ).send(:process_message, payload) end |