conflow_spec 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +6 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +15 -0
- data/.travis.yml +17 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +8 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/conflow_spec.gemspec +32 -0
- data/lib/conflow_spec/flow_context.rb +74 -0
- data/lib/conflow_spec/matchers/run_job.rb +109 -0
- data/lib/conflow_spec/matchers.rb +56 -0
- data/lib/conflow_spec/runner.rb +38 -0
- data/lib/conflow_spec/test_flow.rb +44 -0
- data/lib/conflow_spec/version.rb +6 -0
- data/lib/conflow_spec.rb +20 -0
- metadata +177 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 044d258af0bfbf517b7c7a7e34690a02c12b7664411faac186e3a4e184634b74
|
4
|
+
data.tar.gz: 89646fc820ea0bbcac7364849c12d0966e1ca02fcce52baa84348058ad5bf485
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 30e9c47f21d25e3f91556046c9f279f7efa7a83b117ba4c3734785c4bb48d23d593f8d954b5a1b2e00561c90c5d741e8136b64dcb979f6e023df32d906f3815a
|
7
|
+
data.tar.gz: f8f0f3344055383fa942af62f350c617a9aea4a05a880a67993fe14f33124f9fcb8e33365338d953c9592f19715159b35be919634035b2154ee2c9ebfef3911e
|
data/.codeclimate.yml
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
services:
|
5
|
+
- redis-server
|
6
|
+
rvm:
|
7
|
+
- 2.3.6
|
8
|
+
- 2.4.3
|
9
|
+
- 2.5.0
|
10
|
+
before_install: gem install bundler -v 1.16.1
|
11
|
+
before_script:
|
12
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
13
|
+
- chmod +x ./cc-test-reporter
|
14
|
+
- ./cc-test-reporter before-build
|
15
|
+
script: bundle exec rspec
|
16
|
+
after_script:
|
17
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at michal.begejowicz@codesthq.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
conflow_spec (0.1.0)
|
5
|
+
conflow (~> 0.3)
|
6
|
+
rspec (~> 3.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.0)
|
12
|
+
coderay (1.1.2)
|
13
|
+
conflow (0.3.0)
|
14
|
+
redis (~> 4.0)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
docile (1.3.0)
|
17
|
+
json (2.1.0)
|
18
|
+
method_source (0.9.0)
|
19
|
+
parallel (1.12.1)
|
20
|
+
parser (2.5.0.4)
|
21
|
+
ast (~> 2.4.0)
|
22
|
+
powerpack (0.1.1)
|
23
|
+
pry (0.11.3)
|
24
|
+
coderay (~> 1.1.0)
|
25
|
+
method_source (~> 0.9.0)
|
26
|
+
rainbow (3.0.0)
|
27
|
+
rake (12.3.0)
|
28
|
+
redis (4.0.1)
|
29
|
+
rspec (3.7.0)
|
30
|
+
rspec-core (~> 3.7.0)
|
31
|
+
rspec-expectations (~> 3.7.0)
|
32
|
+
rspec-mocks (~> 3.7.0)
|
33
|
+
rspec-core (3.7.1)
|
34
|
+
rspec-support (~> 3.7.0)
|
35
|
+
rspec-expectations (3.7.0)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.7.0)
|
38
|
+
rspec-mocks (3.7.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.7.0)
|
41
|
+
rspec-support (3.7.1)
|
42
|
+
rubocop (0.53.0)
|
43
|
+
parallel (~> 1.10)
|
44
|
+
parser (>= 2.5)
|
45
|
+
powerpack (~> 0.1)
|
46
|
+
rainbow (>= 2.2.2, < 4.0)
|
47
|
+
ruby-progressbar (~> 1.7)
|
48
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
49
|
+
ruby-progressbar (1.9.0)
|
50
|
+
simplecov (0.16.0)
|
51
|
+
docile (~> 1.1)
|
52
|
+
json (>= 1.8, < 3)
|
53
|
+
simplecov-html (~> 0.10.0)
|
54
|
+
simplecov-html (0.10.2)
|
55
|
+
unicode-display_width (1.3.0)
|
56
|
+
|
57
|
+
PLATFORMS
|
58
|
+
ruby
|
59
|
+
|
60
|
+
DEPENDENCIES
|
61
|
+
bundler (~> 1.16)
|
62
|
+
conflow_spec!
|
63
|
+
pry (~> 0.11)
|
64
|
+
rake (~> 12.0)
|
65
|
+
rspec (~> 3.0)
|
66
|
+
rubocop (~> 0.51)
|
67
|
+
simplecov (~> 0.15)
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Michał Begejowicz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# ConflowSpec
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/fanfilmu/conflow_spec.svg?branch=master)](https://travis-ci.org/fanfilmu/conflow_spec) [![Maintainability](https://api.codeclimate.com/v1/badges/0518fa4b23e959ff779c/maintainability)](https://codeclimate.com/github/fanfilmu/conflow_spec/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/0518fa4b23e959ff779c/test_coverage)](https://codeclimate.com/github/fanfilmu/conflow_spec/test_coverage)
|
4
|
+
|
5
|
+
ConflowSpec defines sets of contexts and matchers to easily and responsibly test your [Conflow](https://github.com/fanfilmu/conflow) flows.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "conflow_spec", group: :test
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install conflow_spec
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
`ConflowSpec` automatically integrates with RSpec. Tag your specs describing `Conflow::Flow` classes with `conflow: true` metadata.
|
26
|
+
|
27
|
+
Note that the jobs will not enqueue using your `#queue` method - so you are still responsible for testing this method on your own.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
RSpec.describe MyFlow do
|
31
|
+
describe ".create", conflow: true do
|
32
|
+
subject { described_class.create(id: 115) }
|
33
|
+
|
34
|
+
before { allow_job(DataFetcher, id: 115).to produce([{ type: "User", id: 93 }, { type: "Admin", id: 13 }]) }
|
35
|
+
|
36
|
+
it { is_expected.to run_job(DataFetcher).with_params(id: 115) }
|
37
|
+
|
38
|
+
it { is_expected.to run_job(AdminNotification).with_params(id: 13) }
|
39
|
+
it { is_expected.to_not run_job(AdminNotification).with_params(id: 93) }
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#queue" do
|
43
|
+
# test your queue method (without conflow: true tag!)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
`run_job` checks if a job of given class was enqueued in the flow. To test hooks, use `allow_job` method - it will stub result of job processing.
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
53
|
+
|
54
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fanfilmu/conflow_spec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
63
|
+
|
64
|
+
## Code of Conduct
|
65
|
+
|
66
|
+
Everyone interacting in the ConflowSpec project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fanfilmu/conflow_spec/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "conflow_spec"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "conflow_spec/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "conflow_spec"
|
9
|
+
spec.version = ConflowSpec::VERSION
|
10
|
+
spec.authors = ["Michał Begejowicz"]
|
11
|
+
spec.email = ["michal.begejowicz@codesthq.com"]
|
12
|
+
|
13
|
+
spec.required_ruby_version = "~> 2.3"
|
14
|
+
|
15
|
+
spec.summary = "Helpers for testing flows in RSpec"
|
16
|
+
spec.description = "Helpers for testing flows in RSpec"
|
17
|
+
spec.homepage = "https://github.com/fanfilmu/conflow_spec"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "conflow", "~> 0.3"
|
24
|
+
spec.add_dependency "rspec", "~> 3.0"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "pry", "~> 0.11"
|
28
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "rubocop", "~> 0.51"
|
31
|
+
spec.add_development_dependency "simplecov", "~> 0.15"
|
32
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConflowSpec
|
4
|
+
# Defines context of the spec
|
5
|
+
# - changes {http://www.rubydoc.info/gems/conflow/0.2.0/Conflow/Flow Conflow::Flow} class to queue jobs into special
|
6
|
+
# test queue
|
7
|
+
# - defines worker which collects informations about jobs to be performed
|
8
|
+
# @note In test flows jobs will not be performed - it only tests if the jobs were enqueued with proper parameters
|
9
|
+
module FlowContext
|
10
|
+
extend RSpec::SharedContext
|
11
|
+
|
12
|
+
before { allow(described_class).to receive(:new).and_return(_conflow_spec_test_instance) }
|
13
|
+
|
14
|
+
# Simple struct to hold mocked return values of the jobs
|
15
|
+
JobProductStruct = Struct.new(:job_class, :params, :result) do
|
16
|
+
alias_method :to, :result=
|
17
|
+
end
|
18
|
+
|
19
|
+
# Allows to define returned value by a job (which then can be user by hook attached to the job itself).
|
20
|
+
# @param job_class [Class] Class of the job
|
21
|
+
# @param params [Object] Value to be returned by job when processed
|
22
|
+
# @example
|
23
|
+
# class MyFlow < Conflow::Flow
|
24
|
+
# def configure(id:)
|
25
|
+
# run UpdateJob, params: { id: id }, hook: :send_notifications
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# def send_notifications(emails:)
|
29
|
+
# emails.each { |email| run NotificationJob, params: { email: email } }
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# RSpec.describe MyFlow do
|
34
|
+
# before { allow_job(UpdateJob, id: 110).to produce(emails: ["user1@example.com", "user2@example.com"]) }
|
35
|
+
# subject { described_class.create(id: 110) }
|
36
|
+
#
|
37
|
+
# it { is_expected.to run_job(NotificationJob).with_params(email: "user1@example.com") }
|
38
|
+
# it { is_expected.to run_job(NotificationJob).with_params(email: "user2@example.com") }
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
def allow_job(job_class, params = nil)
|
42
|
+
JobProductStruct.new(job_class, params).tap do |struct|
|
43
|
+
_conflow_spec_test_instance._conflow_spec_returns << struct
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Defines value returned by given job
|
48
|
+
# @see allow_job
|
49
|
+
def produce(result)
|
50
|
+
result
|
51
|
+
end
|
52
|
+
|
53
|
+
# Instance of the TestFlow
|
54
|
+
# @see TestFlow
|
55
|
+
# @api private
|
56
|
+
def _conflow_spec_test_instance
|
57
|
+
@_conflow_spec_test_instance ||= _conflow_spec_test_class.new
|
58
|
+
end
|
59
|
+
|
60
|
+
# Subclass of a described class with overriden queueing logic
|
61
|
+
# @see TestFlow
|
62
|
+
# @api private
|
63
|
+
def _conflow_spec_test_class
|
64
|
+
@_conflow_spec_test_class ||= ConflowSpec::TestFlow.build(described_class, _conflow_spec_queue)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Queue which collects queued jobs
|
68
|
+
# @see TestFlow
|
69
|
+
# @api private
|
70
|
+
def _conflow_spec_queue
|
71
|
+
@_conflow_spec_queue ||= Thread::Queue.new
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConflowSpec
|
4
|
+
module Matchers
|
5
|
+
# Specifies that flow should at some point enqueue specific job.
|
6
|
+
class RunJob
|
7
|
+
include RSpec::Matchers
|
8
|
+
|
9
|
+
attr_reader :job_class, :expected_params, :flow
|
10
|
+
|
11
|
+
# @param job_class [Class] class of the worker expected to be run
|
12
|
+
def initialize(job_class)
|
13
|
+
@job_class = job_class
|
14
|
+
@times = 1
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param flow [Conflow::Flow] flow instance which should enqueue job
|
18
|
+
# @return [Boolean] true if all requirements are fulfilled
|
19
|
+
def matches?(flow)
|
20
|
+
@flow = flow
|
21
|
+
|
22
|
+
relevant_jobs.size == @times
|
23
|
+
end
|
24
|
+
|
25
|
+
# Specifies parameters that the job should be run with.
|
26
|
+
# @param params [Hash] hash of job params
|
27
|
+
# @example
|
28
|
+
# expect(flow).to run_job(MyService).with_params(id: 100)
|
29
|
+
def with_params(params)
|
30
|
+
@expected_params = params
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
# Convinience method to specify job should be enqueued twice
|
35
|
+
# @see times
|
36
|
+
def twice
|
37
|
+
@times = 2
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
# Specifies how many times job should be run
|
42
|
+
# @param num [Integer] number of times
|
43
|
+
# @example
|
44
|
+
# expect(flow).to run_job(MyService).twice
|
45
|
+
def times(num)
|
46
|
+
@times = num
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
# Description of the spec
|
51
|
+
def description
|
52
|
+
[
|
53
|
+
"run job #{job_class}",
|
54
|
+
expected_params && "with parameters #{expected_params}",
|
55
|
+
times_text(@times)
|
56
|
+
].compact.join(" ")
|
57
|
+
end
|
58
|
+
|
59
|
+
# Error message when matcher returns true, but it was expected to return false
|
60
|
+
def failure_message_when_negated
|
61
|
+
[
|
62
|
+
"Expected flow to not",
|
63
|
+
description,
|
64
|
+
"but it did"
|
65
|
+
].join(" ")
|
66
|
+
end
|
67
|
+
|
68
|
+
# Error message when matcher returns false, but it was expected to return true
|
69
|
+
def failure_message
|
70
|
+
[
|
71
|
+
"Expected flow to",
|
72
|
+
description,
|
73
|
+
"but",
|
74
|
+
if relevant_jobs.size.zero?
|
75
|
+
run_with_different_params? ? "it was not run with those parameters" : "it was not run at all"
|
76
|
+
elsif relevant_jobs != @times
|
77
|
+
"it was run #{times_text(relevant_jobs.size)}"
|
78
|
+
end
|
79
|
+
].join(" ")
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def relevant_jobs
|
85
|
+
@relevant_jobs ||= runner.performed_jobs.select do |(type, params)|
|
86
|
+
type == job_class && (!expected_params || params == expected_params)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def run_with_different_params?
|
91
|
+
expected_params && runner.performed_jobs.any? do |(type, params)|
|
92
|
+
type == job_class && params != expected_params
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def runner
|
97
|
+
@runner ||= ConflowSpec::Runner.new(flow).tap(&:run)
|
98
|
+
end
|
99
|
+
|
100
|
+
def times_text(num)
|
101
|
+
case num
|
102
|
+
when 1 then "once"
|
103
|
+
when 2 then "twice"
|
104
|
+
else "#{num} times"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConflowSpec
|
4
|
+
# Collection of matchers used to spec flows
|
5
|
+
module Matchers
|
6
|
+
# Run job matchers checks if job of giben worker class is run in the flow.
|
7
|
+
# @example Verifying simple flow
|
8
|
+
# class MyFlow < Conflow::Flow
|
9
|
+
# def configure(run_other: true)
|
10
|
+
# run RegularJob
|
11
|
+
# run OtherJob if run_other
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# RSpec.describe MyFlow do
|
16
|
+
# subject { described_class.create(run_other: false) }
|
17
|
+
#
|
18
|
+
# it { is_expected.to run_job(RegularJob) }
|
19
|
+
# it { is_expected.to_not run_job(OtherJob) }
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @example Specifying parameters
|
23
|
+
# class MyFlow < Conflow::Flow
|
24
|
+
# def configure(id:)
|
25
|
+
# run UpdateJob, params: { id: id }
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# RSpec.describe MyFlow do
|
30
|
+
# subject { described_class.create(id: 300) }
|
31
|
+
#
|
32
|
+
# it { is_expected.to run_job(UpdateJob).with_params(id: 300) }
|
33
|
+
# it { is_expected.to_not run_job(UpdateJob).with_params(id: 301) }
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# @example Specifying amounts
|
37
|
+
# class MyFlow < Conflow::Flow
|
38
|
+
# def configure(ids:)
|
39
|
+
# ids.each { |id| run UpdateJob, params: { id: id } }
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# RSpec.describe MyFlow do
|
44
|
+
# subject { described_class.create(ids: [300, 301]) }
|
45
|
+
#
|
46
|
+
# it { is_expected.to run_job(UpdateJob).twice } # or .times(2)
|
47
|
+
# it { is_expected.to run_job(UpdateJob).with_params(id: 300) }
|
48
|
+
# it { is_expected.to run_job(UpdateJob).with_params(id: 301) }
|
49
|
+
# end
|
50
|
+
# @param job_class [Class] Worker class that is (not) expected to run
|
51
|
+
# @return [RunJob}] RSpec matcher
|
52
|
+
def run_job(job_class)
|
53
|
+
RunJob.new(job_class)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConflowSpec
|
4
|
+
# Runs the flow and collects informations about it
|
5
|
+
class Runner
|
6
|
+
attr_reader :flow, :performed_jobs
|
7
|
+
|
8
|
+
# @param flow [Conflow::Flow] instance of the flow
|
9
|
+
def initialize(flow)
|
10
|
+
@flow = flow
|
11
|
+
@queue = queue
|
12
|
+
@performed_jobs = []
|
13
|
+
end
|
14
|
+
|
15
|
+
# Performs all jobs in the flow, saving results in {performed_jobs}
|
16
|
+
def run
|
17
|
+
finish(queue.pop) until queue.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def finish(job)
|
23
|
+
performed_jobs << [job.worker_type, job.params.to_h]
|
24
|
+
flow.finish(job, fetch_result(job))
|
25
|
+
end
|
26
|
+
|
27
|
+
def queue
|
28
|
+
flow.class.queue
|
29
|
+
end
|
30
|
+
|
31
|
+
def fetch_result(job)
|
32
|
+
matching_by_class = flow._conflow_spec_returns.select { |struct| struct.job_class == job.worker_type }
|
33
|
+
matching_by_params = matching_by_class.select { |struct| struct.params == job.params.to_h }
|
34
|
+
|
35
|
+
(matching_by_params.first || matching_by_class.first)&.result
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConflowSpec
|
4
|
+
# Module which, when included, will override logic of queueing jobs.
|
5
|
+
# @api private
|
6
|
+
module TestFlow
|
7
|
+
class << self
|
8
|
+
# Creates new class which behaves like *flow_class* (class being tested), but with overriden logic
|
9
|
+
# of queueing jobs.
|
10
|
+
# @param flow_class [Class] Described class
|
11
|
+
# @param queue [Thread::Queue] Queue to which jobs should be pushed
|
12
|
+
# @return [Class] Class with changed queueing logic
|
13
|
+
# @example
|
14
|
+
# flow = ConflowSpec::TestFlow.build(MyFlow, job_queue).new
|
15
|
+
# flow.is_a?(MyFlow) #=> true
|
16
|
+
# flow.name #=> MyFlow
|
17
|
+
def build(flow_class, queue)
|
18
|
+
Class.new(flow_class).tap do |test_class|
|
19
|
+
test_class.prepend(self)
|
20
|
+
test_class.name = flow_class.name
|
21
|
+
test_class.queue = queue
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Adds name and queue attributes on test class
|
26
|
+
def prepended(base)
|
27
|
+
base.singleton_class.instance_exec do
|
28
|
+
attr_accessor :name, :queue
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Queues job pushing it to a Thread::Queue
|
34
|
+
def queue(job)
|
35
|
+
self.class.queue << job
|
36
|
+
end
|
37
|
+
|
38
|
+
# Set of return values of jobs
|
39
|
+
# @api private
|
40
|
+
def _conflow_spec_returns
|
41
|
+
@_conflow_spec_returns ||= Set.new
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/conflow_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "conflow_spec/version"
|
4
|
+
require "conflow"
|
5
|
+
require "rspec"
|
6
|
+
|
7
|
+
require "conflow_spec/test_flow"
|
8
|
+
require "conflow_spec/flow_context"
|
9
|
+
require "conflow_spec/runner"
|
10
|
+
require "conflow_spec/matchers/run_job"
|
11
|
+
require "conflow_spec/matchers"
|
12
|
+
|
13
|
+
# ConflowSpec defines sets of contexts and matchers to easily and responsibly test your Conflow flows.
|
14
|
+
module ConflowSpec
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
config.include ConflowSpec::FlowContext, conflow: true
|
19
|
+
config.include ConflowSpec::Matchers, conflow: true
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: conflow_spec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michał Begejowicz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: conflow
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '12.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '12.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.51'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.51'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.15'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.15'
|
125
|
+
description: Helpers for testing flows in RSpec
|
126
|
+
email:
|
127
|
+
- michal.begejowicz@codesthq.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".codeclimate.yml"
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".rubocop.yml"
|
136
|
+
- ".travis.yml"
|
137
|
+
- CODE_OF_CONDUCT.md
|
138
|
+
- Gemfile
|
139
|
+
- Gemfile.lock
|
140
|
+
- LICENSE.txt
|
141
|
+
- README.md
|
142
|
+
- Rakefile
|
143
|
+
- bin/console
|
144
|
+
- bin/setup
|
145
|
+
- conflow_spec.gemspec
|
146
|
+
- lib/conflow_spec.rb
|
147
|
+
- lib/conflow_spec/flow_context.rb
|
148
|
+
- lib/conflow_spec/matchers.rb
|
149
|
+
- lib/conflow_spec/matchers/run_job.rb
|
150
|
+
- lib/conflow_spec/runner.rb
|
151
|
+
- lib/conflow_spec/test_flow.rb
|
152
|
+
- lib/conflow_spec/version.rb
|
153
|
+
homepage: https://github.com/fanfilmu/conflow_spec
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - "~>"
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '2.3'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.7.3
|
174
|
+
signing_key:
|
175
|
+
specification_version: 4
|
176
|
+
summary: Helpers for testing flows in RSpec
|
177
|
+
test_files: []
|