blood_contracts 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/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +160 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/blood_contracts.gemspec +34 -0
- data/lib/blood_contracts/runner.rb +183 -0
- data/lib/blood_contracts/storage.rb +80 -0
- data/lib/blood_contracts/suite.rb +34 -0
- data/lib/blood_contracts/version.rb +3 -0
- data/lib/blood_contracts.rb +89 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 234b795bf582f8978bd4ba789c388cbad2539e20
|
4
|
+
data.tar.gz: 46f86eb3b20f692d9fa948fb58e275a94d921012
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7bd966f422b0020d06d981680811c1466859214dbcb90ab346516b403e4216f4cb3355773f697482b75f2b8818c4f419571715d5ee0604aaf975978d31033f4
|
7
|
+
data.tar.gz: a400a28a9d84ab53321a1b13c134b1c0bfcfa40c09bde47b1666bac429461869fabb702ec72bf6222b9634d65398fd66f97c8bb750b51a65e4d7340630c1a380
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
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 sclinede@gmail.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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Sergey Dolganov
|
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,160 @@
|
|
1
|
+
# BloodContracts
|
2
|
+
|
3
|
+
Ruby gem to define and validate behavior of API using contract.
|
4
|
+
|
5
|
+
Possible use-cases:
|
6
|
+
- Automated external API status check (shooting with critical requests and validation that behavior meets the contract);
|
7
|
+
- Automated detection of unexpected external API behavior (Rack::request/response pairs that don't match contract);
|
8
|
+
- Contract definition assistance tool (generate real-a-like requests and iterate through oddities of your system behavior)
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'blood_contracts'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install blood_contracts
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# define contract
|
31
|
+
def contract
|
32
|
+
Hash[
|
33
|
+
success: {
|
34
|
+
check: ->(_input, output) do
|
35
|
+
data = output.data
|
36
|
+
shipping_cost = data.dig(
|
37
|
+
"BkgDetails", "QtdShp", "ShippingCharge"
|
38
|
+
)
|
39
|
+
output.success? && shipping_cost.present?
|
40
|
+
end,
|
41
|
+
threshold: 0.98,
|
42
|
+
},
|
43
|
+
data_missing_error: {
|
44
|
+
check: ->(_input, output) do
|
45
|
+
output.error_codes.present? &&
|
46
|
+
(output.error_codes - ["111"]).empty?
|
47
|
+
end,
|
48
|
+
limit: 0.01,
|
49
|
+
},
|
50
|
+
data_invalid_error: {
|
51
|
+
check: ->(_input, output) do
|
52
|
+
output.error_codes.present? &&
|
53
|
+
(output.error_codes - ["4300", "123454"]).empty?
|
54
|
+
end,
|
55
|
+
limit: 0.01,
|
56
|
+
},
|
57
|
+
strange_weight: {
|
58
|
+
check: ->(input, output) do
|
59
|
+
input.weight > 100 && output.error_codes.empty? && !output.success?
|
60
|
+
end,
|
61
|
+
limit: 0.01,
|
62
|
+
}
|
63
|
+
]
|
64
|
+
end
|
65
|
+
|
66
|
+
# define the API input
|
67
|
+
def generate_data
|
68
|
+
DHL::RequestData.new(
|
69
|
+
data_source.origin_addresses.sample,
|
70
|
+
data_source.destinations.sample,
|
71
|
+
data_source.prices.sample,
|
72
|
+
data_source.products.sample,
|
73
|
+
data_source.weights.sample,
|
74
|
+
data_source.dates.sample.days.since.to_date.to_s(:iso8601),
|
75
|
+
data_source.accounts.sample,
|
76
|
+
).to_h
|
77
|
+
end
|
78
|
+
|
79
|
+
def data_source
|
80
|
+
Hashie::Mash.new(load_fixture("dhl/obfuscated-production-data.yaml"))
|
81
|
+
end
|
82
|
+
|
83
|
+
# initiate contract suite
|
84
|
+
# with default storage (in tmp/blood_contracts/ folder of the project)
|
85
|
+
contract_suite = BloodContract::Suite.new(
|
86
|
+
contract: contract,
|
87
|
+
data_generator: method(:generate_data),
|
88
|
+
)
|
89
|
+
|
90
|
+
# with custom storage backend (e.g. Postgres DB)
|
91
|
+
conn = PG.connect( dbname: "blood_contracts" )
|
92
|
+
conn.exec(<<~SQL);
|
93
|
+
CREATE TABLE runs (
|
94
|
+
created_at timestamp DEFAULT current_timestamp,
|
95
|
+
contract_name text,
|
96
|
+
rules_matched array text[],
|
97
|
+
input text,
|
98
|
+
output text
|
99
|
+
);
|
100
|
+
SQL
|
101
|
+
|
102
|
+
contract_suite = BloodContract::Suite.new(
|
103
|
+
contract: contract,
|
104
|
+
data_generator: method(:generate_data),
|
105
|
+
|
106
|
+
storage_backend: ->(contract_name, rules_matched, input, output) do
|
107
|
+
conn.exec(<<~SQL, contract_name, rules_matched, input, output)
|
108
|
+
INSERT INTO runs (contract_name, rules_matched, input, output) VALUES (?, ?, ?, ?);
|
109
|
+
SQL
|
110
|
+
end
|
111
|
+
)
|
112
|
+
|
113
|
+
# run validation
|
114
|
+
runner = BloodContract::Runner.new(
|
115
|
+
->(input) { DHL::Client.call(input) }
|
116
|
+
suite: contract_suite,
|
117
|
+
time_to_run: 3600, # seconds
|
118
|
+
# or
|
119
|
+
# iterations: 1000
|
120
|
+
).tap(&:call)
|
121
|
+
|
122
|
+
# chech the results
|
123
|
+
runner.valid? # true if behavior was aligned with contract or false in any other case
|
124
|
+
runner.run_stats # stats about each contract rule or exceptions occasions during the run
|
125
|
+
|
126
|
+
```
|
127
|
+
|
128
|
+
## TODO
|
129
|
+
- Add rake task to run contracts validation
|
130
|
+
- Add executable to run contracts validation
|
131
|
+
|
132
|
+
## Possible Features
|
133
|
+
- Store the actual code of the contract rules in Storage (gem 'sourcify')
|
134
|
+
- Store reports in Storage
|
135
|
+
- Export/import contracts to YAML, JSON....
|
136
|
+
- Contracts inheritance (already exists using `Hash#merge`?)
|
137
|
+
- Export `Runner#run_stats` to CSV
|
138
|
+
- Create simple web app, to read the reports
|
139
|
+
|
140
|
+
## Other specific use cases
|
141
|
+
|
142
|
+
For Rack request/response validation use: `blood_contracts-rack`
|
143
|
+
|
144
|
+
## Development
|
145
|
+
|
146
|
+
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.
|
147
|
+
|
148
|
+
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).
|
149
|
+
|
150
|
+
## Contributing
|
151
|
+
|
152
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sclinede/blood_contracts. 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.
|
153
|
+
|
154
|
+
## License
|
155
|
+
|
156
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
157
|
+
|
158
|
+
## Code of Conduct
|
159
|
+
|
160
|
+
Everyone interacting in the BloodContracts project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sclinede/blood_contracts/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "blood_contracts"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "blood_contracts/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "blood_contracts"
|
8
|
+
spec.version = BloodContracts::VERSION
|
9
|
+
spec.authors = ["Sergey Dolganov"]
|
10
|
+
spec.email = ["dolganov@evl.ms"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby gem to define and validate behavior of API using contracts.}
|
13
|
+
spec.description = %q{Ruby gem to define and validate behavior of API using contracts.}
|
14
|
+
spec.homepage = "https://github.com/sclinede/blood_contracts"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
|
21
|
+
# Will be introduced soon
|
22
|
+
# spec.bindir = "exe"
|
23
|
+
# spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "dry-initializer", "~> 2.0"
|
27
|
+
|
28
|
+
# Will be removed soon
|
29
|
+
spec.add_runtime_dependency "activesupport", ">= 3.1"
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
34
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
module BloodContracts
|
2
|
+
class Runner
|
3
|
+
extend Dry::Initializer
|
4
|
+
|
5
|
+
param :checking_proc
|
6
|
+
option :context, optional: true
|
7
|
+
|
8
|
+
option :suite
|
9
|
+
|
10
|
+
option :iterations, ->(v) do
|
11
|
+
v = ENV["iterations"] if ENV["iterations"]
|
12
|
+
v.to_i.positive? ? v.to_i : 1
|
13
|
+
end, default: -> { 1 }
|
14
|
+
|
15
|
+
|
16
|
+
option :time_to_run, ->(v) do
|
17
|
+
v = ENV["duration"] if ENV["duration"]
|
18
|
+
v.to_f if v.to_f.positive?
|
19
|
+
end, optional: true
|
20
|
+
|
21
|
+
option :stop_on_unexpected, default: -> { false }
|
22
|
+
|
23
|
+
def call
|
24
|
+
iterate do
|
25
|
+
unexpected = match_rules(*run) do |input, output, rules|
|
26
|
+
suite.storage.save_run(
|
27
|
+
input: input, output: output, rules: rules, context: context,
|
28
|
+
)
|
29
|
+
end.empty?
|
30
|
+
throw :unexpected_behavior, :stop if stop_on_unexpected && unexpected
|
31
|
+
end
|
32
|
+
valid?
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid?
|
36
|
+
return if stopped_by_unexpected_behavior?
|
37
|
+
return if found_unexpected_behavior?
|
38
|
+
|
39
|
+
last_run_stats = run_stats
|
40
|
+
expectations_checks.all? do |rule, check|
|
41
|
+
percent = last_run_stats[rule.name]&.percent || 0.0
|
42
|
+
check.call(percent, rule)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def found_unexpected_behavior?
|
47
|
+
run_stats.key?(Storage::UNDEFINED_RULE)
|
48
|
+
end
|
49
|
+
|
50
|
+
def failure_message
|
51
|
+
intro = "expected that given Proc would meet the contract:"
|
52
|
+
|
53
|
+
if found_unexpected_behavior?
|
54
|
+
"#{intro}\n#{contract_description}\n"\
|
55
|
+
" during #{iterations} run(s) but got unexpected behavior.\n\n"\
|
56
|
+
"For further investigations open: #{unexpected_behavior_report_path}/"
|
57
|
+
else
|
58
|
+
"#{intro}\n#{contract_description}\n"\
|
59
|
+
" during #{iterations} run(s) but got:\n#{stats_description}\n\n"\
|
60
|
+
"For further investigations open: #{suite.storage.path}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def unexpected_behavior_report_path
|
65
|
+
File.join(suite.storage.path, Storage::UNDEFINED_RULE.to_s)
|
66
|
+
end
|
67
|
+
|
68
|
+
def description
|
69
|
+
"meet the contract:\n#{contract_description} \n"\
|
70
|
+
" during #{iterations} run(s). Stats:\n#{stats_description}\n\n"\
|
71
|
+
"For further investigations open: #{suite.storage.path}\n"
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def run
|
77
|
+
input = suite.data_generator.call
|
78
|
+
[input, checking_proc.call(input)]
|
79
|
+
end
|
80
|
+
|
81
|
+
def stopped_by_unexpected_behavior?
|
82
|
+
@_stopped_by_unexpected_behavior == :stop
|
83
|
+
end
|
84
|
+
|
85
|
+
def stats
|
86
|
+
suite.storage.stats
|
87
|
+
end
|
88
|
+
|
89
|
+
def iterate
|
90
|
+
run_iterations = iterations
|
91
|
+
|
92
|
+
if time_to_run
|
93
|
+
run_iterations = iterations_count_from_time_to_run { yield }
|
94
|
+
@iterations = run_iterations + 1
|
95
|
+
end
|
96
|
+
|
97
|
+
@_stopped_by_unexpected_behavior = catch(:unexpected_behavior) do
|
98
|
+
run_iterations.times { yield }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def iterations_count_from_time_to_run
|
103
|
+
time_per_action = Benchmark.measure { yield }
|
104
|
+
(time_to_run / time_per_action.real).ceil
|
105
|
+
end
|
106
|
+
|
107
|
+
def match_rules(input, output)
|
108
|
+
matched_rules = suite.contract.select do |_name, rule|
|
109
|
+
rule.check.call(input, output)
|
110
|
+
end.keys
|
111
|
+
matched_rules = [Storage::UNDEFINED_RULE] if matched_rules.empty?
|
112
|
+
yield(input, output, matched_rules)
|
113
|
+
|
114
|
+
matched_rules
|
115
|
+
# FIXME: Possible recursion?
|
116
|
+
# Write test about error in the yield (e.g. writing error)
|
117
|
+
rescue => e
|
118
|
+
yield [Storage::UNDEFINED_RULE]
|
119
|
+
raise e
|
120
|
+
end
|
121
|
+
|
122
|
+
def threshold_check(value, rule)
|
123
|
+
value > rule.threshold
|
124
|
+
end
|
125
|
+
|
126
|
+
def limit_check(value, rule)
|
127
|
+
value <= rule.limit
|
128
|
+
end
|
129
|
+
|
130
|
+
def expectations_checks
|
131
|
+
Hash[
|
132
|
+
suite.contract.map do |name, rule|
|
133
|
+
if rule.threshold
|
134
|
+
[rule.merge(name: name), method(:threshold_check)]
|
135
|
+
elsif rule.limit
|
136
|
+
[rule.merge(name: name), method(:limit_check)]
|
137
|
+
else
|
138
|
+
nil
|
139
|
+
end
|
140
|
+
end.compact
|
141
|
+
]
|
142
|
+
end
|
143
|
+
|
144
|
+
def contract_description
|
145
|
+
suite.contract.map do |name, rule|
|
146
|
+
rule_description = " - '#{name}' "
|
147
|
+
if rule.threshold
|
148
|
+
rule_description << <<~TEXT
|
149
|
+
in more then #{(rule.threshold * 100).round(2)}% of cases;
|
150
|
+
TEXT
|
151
|
+
elsif rule.limit
|
152
|
+
rule_description << <<~TEXT
|
153
|
+
in less then #{(rule.limit * 100).round(2)}% of cases;
|
154
|
+
TEXT
|
155
|
+
else
|
156
|
+
next
|
157
|
+
end
|
158
|
+
rule_description
|
159
|
+
end.compact.join
|
160
|
+
end
|
161
|
+
|
162
|
+
def stats_description
|
163
|
+
run_stats.map do |name, occasions|
|
164
|
+
" - '#{name}' happened #{occasions.times} time(s) "\
|
165
|
+
"(#{(occasions.percent * 100).round(2)}% of the time)"
|
166
|
+
end.join("; \n")
|
167
|
+
end
|
168
|
+
|
169
|
+
def run_stats
|
170
|
+
Hash[
|
171
|
+
stats.map do |rule_name, times|
|
172
|
+
[
|
173
|
+
rule_name,
|
174
|
+
Hashie::Mash.new(
|
175
|
+
times: times,
|
176
|
+
percent: (times.to_f / iterations),
|
177
|
+
),
|
178
|
+
]
|
179
|
+
end
|
180
|
+
]
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module BloodContracts
|
2
|
+
class Storage
|
3
|
+
extend Dry::Initializer
|
4
|
+
|
5
|
+
# Split date and time, for more comfortable Dirs navigation
|
6
|
+
option :start_time, default: -> { Time.current.to_s(:number) }
|
7
|
+
option :custom_path, optional: true
|
8
|
+
option :root, default: -> { Rails.root.join(path) }
|
9
|
+
option :stats, default: -> { Hash.new(0) }
|
10
|
+
option :input_writer, optional: true
|
11
|
+
option :output_writer, optional: true
|
12
|
+
|
13
|
+
UNDEFINED_RULE = :__no_tag_match__
|
14
|
+
|
15
|
+
def default_path
|
16
|
+
"./tmp/contract_tests/"
|
17
|
+
end
|
18
|
+
|
19
|
+
def path
|
20
|
+
@path ||= File.join(default_path, custom_path.to_s, start_time)
|
21
|
+
end
|
22
|
+
|
23
|
+
def input_writer=(writer)
|
24
|
+
fail ArgumentError unless writer.respond_to?(:call) ||
|
25
|
+
writer.respond_to?(:to_sym)
|
26
|
+
@input_writer = writer
|
27
|
+
end
|
28
|
+
|
29
|
+
def output_writer=(writer)
|
30
|
+
fail ArgumentError unless writer.respond_to?(:call) ||
|
31
|
+
writer.respond_to?(:to_sym)
|
32
|
+
@output_writer = writer
|
33
|
+
end
|
34
|
+
|
35
|
+
def run_name(tag)
|
36
|
+
run_name = File.join(root, "#{tag}/#{Time.current.to_s(:number)}")
|
37
|
+
FileUtils.mkdir_p File.join(root, "#{tag}")
|
38
|
+
run_name
|
39
|
+
end
|
40
|
+
|
41
|
+
def write(writer, context, input, output)
|
42
|
+
return default_write_pattern(input, output) unless writer
|
43
|
+
writer = context.method(writer) if context && writer.respond_to?(:to_sym)
|
44
|
+
writer.call(input, output)
|
45
|
+
end
|
46
|
+
|
47
|
+
def default_write_pattern(input, output)
|
48
|
+
[
|
49
|
+
"INPUT:",
|
50
|
+
input,
|
51
|
+
"\n#{'=' * 90}\n",
|
52
|
+
"OUTPUT:",
|
53
|
+
output
|
54
|
+
].map(&:to_s).join("\n")
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
# Quick open: `vim -O tmp/contract_tests/<tstamp>/<tag>/<tstamp>.*`
|
59
|
+
def save_run(input:, output:, rules:, context:)
|
60
|
+
Array(rules).each do |rule_name|
|
61
|
+
stats[rule_name] += 1
|
62
|
+
run_name = run_name(rule_name)
|
63
|
+
|
64
|
+
# TODO: Write to HTML
|
65
|
+
input_fname = "#{run_name}.input"
|
66
|
+
output_fname = "#{run_name}.output"
|
67
|
+
File.open(input_fname, "w+") do |f|
|
68
|
+
f << write(input_writer, context, input, output).encode(
|
69
|
+
'UTF-8', invalid: :replace, undef: :replace, replace: '?'
|
70
|
+
)
|
71
|
+
end
|
72
|
+
File.open(output_fname, "w+") do |f|
|
73
|
+
f << write(output_writer, context, input, output).encode(
|
74
|
+
'UTF-8', invalid: :replace, undef: :replace, replace: '?'
|
75
|
+
)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module BloodContracts
|
2
|
+
class Suite
|
3
|
+
extend Dry::Initializer
|
4
|
+
|
5
|
+
option :data_generator, optional: true
|
6
|
+
option :contract, default: -> { Hashie::Mash.new }
|
7
|
+
option :input_writer, optional: true
|
8
|
+
option :output_writer, optional: true
|
9
|
+
option :storage_backend, optional: true
|
10
|
+
option :storage, default: -> { default_storage }
|
11
|
+
|
12
|
+
def data_generator=(generator)
|
13
|
+
fail ArgumentError unless generator.respond_to?(:call)
|
14
|
+
@data_generator = generator
|
15
|
+
end
|
16
|
+
|
17
|
+
def contract=(contract)
|
18
|
+
fail ArgumentError unless contract.respond_to?(:to_h)
|
19
|
+
@contract = Hashie::Mash.new(contract.to_h)
|
20
|
+
end
|
21
|
+
|
22
|
+
def input_writer=(writer)
|
23
|
+
storage.input_writer = writer
|
24
|
+
end
|
25
|
+
|
26
|
+
def output_writer=(writer)
|
27
|
+
storage.output_writer = writer
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_storage
|
31
|
+
Storage.new(input_writer: input_writer, output_writer: output_writer)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "blood_contracts/version"
|
2
|
+
require_relative "blood_contracts/suite"
|
3
|
+
require_relative "blood_contracts/storage"
|
4
|
+
require_relative "blood_contracts/runner"
|
5
|
+
|
6
|
+
module BloodContracts
|
7
|
+
|
8
|
+
# Use https://github.com/razum2um/lurker/blob/master/lib/lurker/spec_helper/rspec.rb
|
9
|
+
if defined?(RSpec) && RSpec.respond_to?(:configure)
|
10
|
+
module MeetContractMatcher
|
11
|
+
extend RSpec::Matchers::DSL
|
12
|
+
|
13
|
+
matcher :meet_contract_rules do |options|
|
14
|
+
match do |actual|
|
15
|
+
raise ArgumentError unless actual.respond_to?(:call)
|
16
|
+
|
17
|
+
@_contract_runner = Runner.new(
|
18
|
+
actual,
|
19
|
+
context: self,
|
20
|
+
suite: build_suite(options),
|
21
|
+
iterations: @_iterations,
|
22
|
+
time_to_run: @_time_to_run,
|
23
|
+
stop_on_unexpected: @_halt_on_unexpected,
|
24
|
+
)
|
25
|
+
@_contract_runner.call
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_suite(options)
|
29
|
+
storage = Storage.new(custom_path: _example_name_to_path)
|
30
|
+
storage.input_writer = _input_writer if _input_writer
|
31
|
+
storage.output_writer = _output_writer if _output_writer
|
32
|
+
|
33
|
+
suite = options[:contract_suite] || Suite.new(storage: storage)
|
34
|
+
|
35
|
+
suite.data_generator = @_generator if @_generator
|
36
|
+
suite.contract = options[:contract] if options[:contract]
|
37
|
+
suite
|
38
|
+
end
|
39
|
+
|
40
|
+
def _example_name_to_path
|
41
|
+
method_missing(:class).
|
42
|
+
name.
|
43
|
+
sub("RSpec::ExampleGroups::", "").
|
44
|
+
snakecase
|
45
|
+
end
|
46
|
+
|
47
|
+
def _input_writer
|
48
|
+
input_writer = @_writers.to_h[:input]
|
49
|
+
input_writer ||= :input_writer if defined? self.input_writer
|
50
|
+
input_writer
|
51
|
+
end
|
52
|
+
|
53
|
+
def _output_writer
|
54
|
+
output_writer = @_writers.to_h[:output]
|
55
|
+
output_writer ||= :output_writer if defined? self.output_writer
|
56
|
+
output_writer
|
57
|
+
end
|
58
|
+
|
59
|
+
supports_block_expectations
|
60
|
+
|
61
|
+
failure_message { @_contract_runner.failure_message }
|
62
|
+
|
63
|
+
description { @_contract_runner.description }
|
64
|
+
|
65
|
+
chain :using_generator do |generator|
|
66
|
+
if generator.respond_to?(:to_sym)
|
67
|
+
@_generator = method(generator.to_sym)
|
68
|
+
else
|
69
|
+
fail ArgumentError unless generator.respond_to?(:call)
|
70
|
+
@_generator = generator
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
chain :during_n_iterations_run do |iterations|
|
75
|
+
@_iterations = Integer(iterations)
|
76
|
+
end
|
77
|
+
|
78
|
+
chain :during_n_seconds_run do |time_to_run|
|
79
|
+
@_time_to_run = Float(time_to_run)
|
80
|
+
end
|
81
|
+
|
82
|
+
chain :halt_on_unexpected do
|
83
|
+
@_halt_on_unexpected = true
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blood_contracts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Dolganov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-initializer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.1'
|
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: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Ruby gem to define and validate behavior of API using contracts.
|
84
|
+
email:
|
85
|
+
- dolganov@evl.ms
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- blood_contracts.gemspec
|
100
|
+
- lib/blood_contracts.rb
|
101
|
+
- lib/blood_contracts/runner.rb
|
102
|
+
- lib/blood_contracts/storage.rb
|
103
|
+
- lib/blood_contracts/suite.rb
|
104
|
+
- lib/blood_contracts/version.rb
|
105
|
+
homepage: https://github.com/sclinede/blood_contracts
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.6.14
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Ruby gem to define and validate behavior of API using contracts.
|
129
|
+
test_files: []
|