Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUILD-619: Test helpers don't automatically reset to test config #120

Merged
merged 12 commits into from
Aug 12, 2021
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## UNRELEASED
### Roadmap :car:

- TestHelper does not automatically reset Deimos config before each test. [#120](https://github.com/flipp-oss/deimos/pull/120)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should call out in bold that this is a breaking change.


## 1.10.0 - 2021-03-22

Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Built on Phobos and hence Ruby-Kafka.
* [Running Consumers](#running-consumers)
* [Metrics](#metrics)
* [Testing](#testing)
* [Test Helpers](#test-helpers)
* [Integration Test Helpers](#integration-test-helpers)
* [Utilities](#utilities)
* [Contributing](#contributing)
Expand Down Expand Up @@ -946,6 +947,32 @@ require 'deimos/test_helpers'
# Can pass a file path, a string or a hash into this:
Deimos::TestHelpers.schemas_compatible?(schema1, schema2)
```
### Test Helpers

There are helper methods available to configure Deimos for different types of testing scenarios.
Currently there is a helper defined for unit tests and for testing Kafka releated code. You can use it as follows:

```ruby
# The following can be added to a rpsec file so that each unit
# test can have the same settings every time it is run
around(:each) do |example|
Deimos::TestHelpers.unit_test!
example.run
Deimos.config.reset
end


# Similarly you can use the Kafka test helper
around(:each) do |example|
Deimos::TestHelpers.kafka_test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing !

example.run
Deimos.config.reset
end
```

With the help of these helper methods, rspec examples can be written without having to tinker with Deimos settings.
This also prevents Deimos setting changes from leaking in to other examples.
dorner marked this conversation as resolved.
Show resolved Hide resolved


### Integration Test Helpers

Expand Down
29 changes: 19 additions & 10 deletions lib/deimos/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@ class << self
def sent_messages
Deimos::Backends::Test.sent_messages
end

# Set the config to the right settings for a unit test
def unit_test!
dorner marked this conversation as resolved.
Show resolved Hide resolved
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

# Set the config to the right settings for a kafka test
def kafka_test!
Deimos.configure do |deimos_config|
deimos_config.producers.backend = :kafka
deimos_config.schema.backend = :avro_validation
end
dorner marked this conversation as resolved.
Show resolved Hide resolved
end
end

included do

RSpec.configure do |config|

config.before(:suite) do
Deimos.configure do |d_config|
d_config.logger = Logger.new(STDOUT)
d_config.consumers.reraise_errors = true
d_config.kafka.seed_brokers ||= ['test_broker']
d_config.schema.backend = Deimos.schema_backend_class.mock_backend
d_config.producers.backend = :test
end
end

config.prepend_before(:each) do
client = double('client').as_null_object
allow(client).to receive(:time) do |*_args, &block|
Expand Down