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
- .included(base) ⇒ Object
- .normalize_message(m) ⇒ Object
- .sent_messages ⇒ Array<Hash>
-
.unit_test! ⇒ void
Set the config to the right settings for a unit test.
Instance Method Summary collapse
-
#clear_kafka_messages! ⇒ void
Clear all sent messages - e.g.
- #sent_messages ⇒ Array<Hash>
-
#test_consume_batch(handler_class_or_topic, payloads, keys: [], call_original: nil, single: false, partition_keys: []) ⇒ void
Test that a given handler will consume a given batch payload correctly, i.e.
-
#test_consume_message(handler_class_or_topic, payload, key: nil, call_original: nil, partition_key: nil) ⇒ void
Test that a given handler will consume a given payload correctly, i.e.
Class Method Details
.included(base) ⇒ Object
15 16 17 18 |
# File 'lib/deimos/test_helpers.rb', line 15 def self.included(base) super base.include Karafka::Testing::RSpec::Helpers end |
.normalize_message(m) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/deimos/test_helpers.rb', line 63 def self.(m) return nil if m.nil? if m.respond_to?(:to_h) m = m.to_h end if m.respond_to?(:with_indifferent_access) m = m.with_indifferent_access end m end |
.sent_messages ⇒ Array<Hash>
27 28 29 30 31 32 33 34 35 |
# File 'lib/deimos/test_helpers.rb', line 27 def Karafka.producer.client..map do |m| = m.except(:label).deep_dup Deimos.() [:payload] = Deimos::TestHelpers.([:payload]) [:key] = Deimos::TestHelpers.([:key]) end end |
.unit_test! ⇒ void
This method returns an undefined value.
Set the config to the right settings for a unit test
39 40 41 42 |
# File 'lib/deimos/test_helpers.rb', line 39 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 |
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.
126 127 128 129 |
# File 'lib/deimos/test_helpers.rb', line 126 def puts "[Deprecated] clear_kafka_messages! can be replaced with `karafka.produced_messages.clear`" karafka..clear end |
#sent_messages ⇒ Array<Hash>
21 22 23 |
# File 'lib/deimos/test_helpers.rb', line 21 def self.class. end |
#test_consume_batch(handler_class_or_topic, payloads, keys: [], call_original: nil, single: false, partition_keys: []) ⇒ void
This method returns an undefined value.
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
165 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 |
# File 'lib/deimos/test_helpers.rb', line 165 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 consumer = nil topic_name = nil if handler_class_or_topic.is_a?(String) topic_name = handler_class_or_topic consumer = karafka.consumer_for(topic_name) else topic_name = Deimos.topic_for_consumer(handler_class_or_topic) consumer = karafka.consumer_for(topic_name) end Deimos.karafka_config_for(topic: topic_name).(single) 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? allow_any_instance_of(consumer_class).to receive(:consume_batch) do yield end end consumer.consume end |
#test_consume_message(handler_class_or_topic, payload, key: nil, call_original: nil, partition_key: nil) ⇒ void
This method returns an undefined value.
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
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/deimos/test_helpers.rb', line 142 def (handler_class_or_topic, payload, key: nil, call_original: nil, partition_key: nil) 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) end |