Module: Deimos::TestHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/deimos/test_helpers.rb,
sig/defs.rbs

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

Instance Method Summary collapse

Class Method Details

.full_integration_test!void

This method returns an undefined value.

Kafka test config with avro schema registry



409
# File 'sig/defs.rbs', line 409

def self.full_integration_test!: () -> void

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/deimos/test_helpers.rb', line 16

def self.included(base)
  super
  base.include Karafka::Testing::RSpec::Helpers

  # Ensure that we only use Karafka.producer, not the producers we set up for multi-broker
  # configs. Only Karafka.producer works with Karafka test helpers.
  RSpec.configure do |config|
    config.before(:each) do
      allow(Deimos).to receive(:producer_for).and_return(Karafka.producer)
    end
  end
end

.kafka_test!void

This method returns an undefined value.

Set the config to the right settings for a kafka test



412
# File 'sig/defs.rbs', line 412

def self.kafka_test!: () -> void

.normalize_message(message) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/deimos/test_helpers.rb', line 85

def self.normalize_message(message)
  return nil if message.nil?

  if message.respond_to?(:to_h)
    message = message.to_h
  end
  if message.respond_to?(:with_indifferent_access)
    message = message.with_indifferent_access
  end
  message
end

.sent_messages::Array[::Hash[untyped, untyped]]

for backwards compatibility

Returns:

  • (::Array[::Hash[untyped, untyped]])


36
37
38
39
40
41
42
43
44
# File 'lib/deimos/test_helpers.rb', line 36

def sent_messages
  Karafka.producer.client.messages.map do |m|
    produced_message = m.except(:label).deep_dup
    Deimos.decode_message(produced_message)
    produced_message[:payload] = Deimos::TestHelpers.normalize_message(produced_message[:payload])
    produced_message[:key] = Deimos::TestHelpers.normalize_message(produced_message[:key])
    produced_message
  end
end

.unit_test!void

This method returns an undefined value.

Set the config to the right settings for a unit test



48
49
50
51
52
# File 'lib/deimos/test_helpers.rb', line 48

def unit_test!
  Deimos.config.schema.backend = :avro_validation
  warn("unit_test! is deprecated and can be replaced by setting Deimos's schema backend " \
       'to `:avro_validation`. All other test behavior is provided by Karafka.')
end

.with_mock_backendsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/deimos/test_helpers.rb', line 54

def with_mock_backends
  Deimos.mock_backends = true
  Deimos.karafka_configs.each do |config|
    config.deserializers[:payload].try(:reset_backend)
    config.deserializers[:key].try(:reset_backend)
  end
  yield
ensure
  Deimos.mock_backends = false
end

Instance Method Details

#clear_kafka_messages!void

This method returns an undefined value.

Clear all sent messages - e.g. if we want to check that particular messages were sent or not sent after a point in time.



172
173
174
175
176
# File 'lib/deimos/test_helpers.rb', line 172

def clear_kafka_messages!
  puts '[Deprecated] clear_kafka_messages! can be replaced with' \
       '`karafka.produced_messages.clear`'
  karafka.produced_messages.clear
end

#sent_messagesArray<Hash>

Returns:

  • (Array<Hash>)


30
31
32
# File 'lib/deimos/test_helpers.rb', line 30

def sent_messages
  self.class.sent_messages
end

#test_consume_batch(handler_class_or_topic, payloads, keys: [], call_original: nil, single: false, partition_keys: []) ⇒ 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

@param handler_class_or_topic — Class which inherits from

@param payloads — the payload to consume

@param keys

@param partition_keys

@param call_original

@param skip_expectation



219
220
221
222
223
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
# File 'lib/deimos/test_helpers.rb', line 219

def test_consume_batch(handler_class_or_topic,
                       payloads,
                       keys: [],
                       call_original: nil,
                       single: false,
                       partition_keys: [])
  unless call_original.nil?
    puts 'test_consume_batch(call_original: true) is deprecated and will be removed' \
         'in the future. You can remove the call_original parameter.'
  end
  karafka.consumer_messages.clear
  topic_name = if handler_class_or_topic.is_a?(String)
    handler_class_or_topic
               else
    Deimos.topic_for_consumer(handler_class_or_topic)
  end

  _validate_consumer_config(topic_name, single)

  consumer = karafka.consumer_for(topic_name)

  Deimos.karafka_config_for(topic: topic_name).each_message(single)

  # don't record messages sent with test_consume_batch
  original_messages = karafka.produced_messages.dup
  payloads.each_with_index do |payload, i|
   karafka.produce(payload, { key: keys[i], partition_key: partition_keys[i], topic: consumer.topic.name })
  end
  if block_given?
    if single
      allow(consumer).to receive(:consume_message) do
       yield(consumer.messages.first.payload, consumer.messages.first.as_json['metadata'])
      end
    else
      allow(consumer).to receive(:consume_batch) do
       yield(consumer.messages)
      end
    end
  end

  # sent_messages should only include messages sent by application code, not this method
  karafka.produced_messages.clear
  karafka.produced_messages.concat(original_messages)

  consumer.consume
end

#test_consume_batch_invalid_messagevoid

This method returns an undefined value.

Check to see that a given message will fail due to validation errors.

@param handler_class

@param payloads

Parameters:

  • handler_class (Class)
  • payloads (::Array[::Hash[untyped, untyped]])


484
# File 'sig/defs.rbs', line 484

def test_consume_batch_invalid_message: (Class handler_class, ::Array[::Hash[untyped, untyped]] payloads) -> void

#test_consume_invalid_messagevoid

This method returns an undefined value.

Check to see that a given message will fail due to validation errors.

@param handler_class

@param payload

Parameters:

  • handler_class (Class)
  • payload (::Hash[untyped, untyped])


451
# File 'sig/defs.rbs', line 451

def test_consume_invalid_message: (Class handler_class, ::Hash[untyped, untyped] payload) -> void

#test_consume_message(handler_class_or_topic, payload, key: nil, call_original: nil, partition_key: nil, &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.

@param handler_class_or_topic — Class which inherits from

@param payload — the payload to consume

@param call_original — if true, allow the consume handler

@param skip_expectation — Set to true to not place any

@param key — the key to use.

@param partition_key — the partition key to use.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/deimos/test_helpers.rb', line 189

def test_consume_message(handler_class_or_topic,
                         payload,
                         key: nil,
                         call_original: nil,
                         partition_key: nil,
                         &block)
  unless call_original.nil?
    puts 'test_consume_message(call_original: true) is deprecated and will be removed' \
         'in the future. You can remove the call_original parameter.'
  end
  test_consume_batch(handler_class_or_topic,
                     [payload],
                     keys: [key],
                     partition_keys: [partition_key],
                     single: true,
                     &block)
end