flu_shot 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +20 -0
- data/.gitignore +3 -0
- data/.travis.yml +2 -0
- data/Gemfile.lock +26 -0
- data/README.md +38 -4
- data/Rakefile +2 -2
- data/flu_shot.gemspec +2 -0
- data/lib/flu_shot/config.rb +11 -0
- data/lib/flu_shot/prescription.rb +28 -0
- data/lib/flu_shot/sneeze.rb +9 -0
- data/lib/flu_shot/storage/memory.rb +19 -0
- data/lib/flu_shot/storage/redis.rb +28 -0
- data/lib/flu_shot/vaccine.rb +63 -0
- data/lib/flu_shot/version.rb +1 -1
- data/lib/flu_shot.rb +51 -1
- data/specs/flu_shot_spec.rb +59 -0
- data/specs/integration/inject_spec.rb +34 -0
- data/specs/lib/config_spec.rb +24 -0
- data/specs/lib/prescription_spec.rb +32 -0
- data/specs/lib/receipt_spec.rb +8 -0
- data/specs/lib/sneeze_spec.rb +14 -0
- data/specs/lib/storage/memory_spec.rb +21 -0
- data/specs/lib/storage/redis_spec.rb +29 -0
- data/specs/lib/vaccine_spec.rb +58 -0
- data/specs/spec_helper.rb +13 -0
- metadata +48 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d252d5c120bc8b41e6f80b754e2d08727250075f76ef3dca4815f5681d7dcfbd
|
4
|
+
data.tar.gz: 0b1f124319d00aa3f4f3c01ed9c068620c231f8b91f12f70a7938d1dfefe7f43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1f13c53db4a9ea6f28e0f90aa9bcb40091d53120e454254ac034f0bdd14162aaf378ff22707840b22a9885f46ca3ea3ce1baa9acc0521aab0f58ef72af29cdb
|
7
|
+
data.tar.gz: c1087095009ae80804ffedcf1a811adc31c177e915acb655b3578cf746341c9d1efc1986f48a057ae97af39f949089690319363e606bb574853512a93f6949c8
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v2
|
12
|
+
- name: Set up Ruby 2.4.9
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.4.9
|
16
|
+
- name: Build and test with Rake
|
17
|
+
run: |
|
18
|
+
gem install bundler
|
19
|
+
bundle install --jobs 4 --retry 3
|
20
|
+
bundle exec rake test
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
flu_shot (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.14.0)
|
10
|
+
mocha (1.11.2)
|
11
|
+
rake (10.5.0)
|
12
|
+
redis (4.1.3)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
bundler (~> 1.17)
|
19
|
+
flu_shot!
|
20
|
+
minitest (~> 5.0)
|
21
|
+
mocha (~> 1.11.0)
|
22
|
+
rake (~> 10.0)
|
23
|
+
redis (~> 4.1.0)
|
24
|
+
|
25
|
+
BUNDLED WITH
|
26
|
+
1.17.2
|
data/README.md
CHANGED
@@ -1,8 +1,43 @@
|
|
1
1
|
# FluShot
|
2
2
|
|
3
|
-
|
3
|
+
<img style="float: right;" src="https://raw.githubusercontent.com/Nucc/flu_shot/assets/logo.png"/>
|
4
4
|
|
5
|
-
|
5
|
+
FluShot is a Chaos Testing tool for Ruby applications. It can inject unexpected behaviors into your system, like adding extra latency to a network request, simulating infinite loops, raising exceptions.
|
6
|
+
|
7
|
+
**The project is currently in `work in progress`**
|
8
|
+
|
9
|
+
Idea:
|
10
|
+
|
11
|
+
First we specify the area where the flu shot needs to be injected:
|
12
|
+
|
13
|
+
```
|
14
|
+
class UserController < ApplicationController
|
15
|
+
def show
|
16
|
+
FluShot.inject(:user_controller_show) do
|
17
|
+
User.find(params[:user_id])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
We can specify vaccines that has a certain behaviour, like adding extra latency:
|
24
|
+
```
|
25
|
+
class Latency < FluShot::Vaccine
|
26
|
+
label :latency
|
27
|
+
|
28
|
+
def initialize(params = {})
|
29
|
+
sleep(rand(params[:max] - params[:min]) + params[:min])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
Finally we need to create a prescription for the `user_controller_show` and inject the `latency` vaccine:
|
35
|
+
|
36
|
+
```
|
37
|
+
FluShot::Prescription.for(:user_controller_show) do |prescription|
|
38
|
+
prescription.add(:latency, {min: 1000, max: 3000})
|
39
|
+
end
|
40
|
+
```
|
6
41
|
|
7
42
|
## Installation
|
8
43
|
|
@@ -22,7 +57,6 @@ Or install it yourself as:
|
|
22
57
|
|
23
58
|
## Usage
|
24
59
|
|
25
|
-
TODO: Write usage instructions here
|
26
60
|
|
27
61
|
## Development
|
28
62
|
|
@@ -32,7 +66,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
66
|
|
33
67
|
## Contributing
|
34
68
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
69
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nucc/flu_shot. 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.
|
36
70
|
|
37
71
|
## License
|
38
72
|
|
data/Rakefile
CHANGED
@@ -2,9 +2,9 @@ require "bundler/gem_tasks"
|
|
2
2
|
require "rake/testtask"
|
3
3
|
|
4
4
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs << "
|
5
|
+
t.libs << "specs"
|
6
6
|
t.libs << "lib"
|
7
|
-
t.test_files = FileList["
|
7
|
+
t.test_files = FileList["specs/**/*_spec.rb"]
|
8
8
|
end
|
9
9
|
|
10
10
|
task :default => :test
|
data/flu_shot.gemspec
CHANGED
@@ -39,4 +39,6 @@ Gem::Specification.new do |spec|
|
|
39
39
|
spec.add_development_dependency "bundler", "~> 1.17"
|
40
40
|
spec.add_development_dependency "rake", "~> 10.0"
|
41
41
|
spec.add_development_dependency "minitest", "~> 5.0"
|
42
|
+
spec.add_development_dependency "mocha", "~> 1.11.0"
|
43
|
+
spec.add_development_dependency "redis", "~> 4.1.0"
|
42
44
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module FluShot
|
2
|
+
class Prescription
|
3
|
+
def self.spec(name, &block)
|
4
|
+
block.call(self.new(name)) if block_given?
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(name)
|
8
|
+
@name = name
|
9
|
+
self.class.prescriptions.add(@name, [])
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(vaccine, params = {})
|
13
|
+
current = self.class.prescriptions.get(@name)
|
14
|
+
current << { vaccine: vaccine, params: params }
|
15
|
+
self.class.prescriptions.add(@name, current)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.for(name)
|
19
|
+
prescriptions.get(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def self.prescriptions
|
25
|
+
Config.storage
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module FluShot
|
2
|
+
module Storage
|
3
|
+
class Memory
|
4
|
+
def initialize
|
5
|
+
@storage = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(prescription_name, vaccines)
|
9
|
+
@storage[prescription_name] ||= []
|
10
|
+
vaccines = [vaccines] unless vaccines.is_a?(Array)
|
11
|
+
@storage[prescription_name] = vaccines
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(prescription_name)
|
15
|
+
@storage[prescription_name] || []
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module FluShot
|
5
|
+
module Storage
|
6
|
+
class Redis
|
7
|
+
def initialize(connection)
|
8
|
+
@connection = connection
|
9
|
+
end
|
10
|
+
|
11
|
+
def add(prescription, vaccines)
|
12
|
+
vaccines = [vaccines] unless vaccines.is_a?(Array)
|
13
|
+
@connection.hset('flu_shot', prescription.to_s, YAML.dump(vaccines))
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(name)
|
17
|
+
value = @connection.hget('flu_shot', name)
|
18
|
+
value ? YAML.load(value) : []
|
19
|
+
end
|
20
|
+
|
21
|
+
def reset
|
22
|
+
@connection.hgetall('flu_shot').each_key do |x|
|
23
|
+
@connection.hdel('flu_shot', x)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# The idea here is to do something like this in the code:
|
2
|
+
#
|
3
|
+
# ... your business layer ...
|
4
|
+
# FluShot.inject(:user_profile_fetch)
|
5
|
+
#
|
6
|
+
# Define a latency vaccine that you can inject into your business layer
|
7
|
+
# and adds a random delay between 1 and 3 seconds.
|
8
|
+
#
|
9
|
+
# class Latency < FluShot::Vaccine
|
10
|
+
# label :latency
|
11
|
+
#
|
12
|
+
# def initialize(params)
|
13
|
+
# sleep(rand(params[:max] - params[:min]) + params[:min])
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# FluShot.on :user_profile_fetch do
|
18
|
+
# FluShot::Vaccine.use(:latency, {min: 1000, max: 3000})
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
#
|
22
|
+
module FluShot
|
23
|
+
class Vaccine
|
24
|
+
attr_reader :attributes
|
25
|
+
|
26
|
+
def initialize(attributes = {})
|
27
|
+
@attributes = attributes
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.label(name = nil)
|
31
|
+
if name.nil?
|
32
|
+
if defined?(@vaccine_name)
|
33
|
+
@vaccine_name
|
34
|
+
end || :unknown
|
35
|
+
else
|
36
|
+
@vaccine_name = name
|
37
|
+
vaccines[name] = self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.registered
|
42
|
+
self.vaccines.keys
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.find(name)
|
46
|
+
self.vaccines[name]
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.use(name, params = {})
|
50
|
+
find(name).new(params)
|
51
|
+
end
|
52
|
+
|
53
|
+
def label
|
54
|
+
self.class.label
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def self.vaccines
|
60
|
+
Thread.current[:vaccines] ||= {}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/flu_shot/version.rb
CHANGED
data/lib/flu_shot.rb
CHANGED
@@ -1,6 +1,56 @@
|
|
1
1
|
require "flu_shot/version"
|
2
|
+
require 'flu_shot/vaccine'
|
3
|
+
require 'flu_shot/prescription'
|
4
|
+
require 'flu_shot/config'
|
5
|
+
require 'flu_shot/sneeze'
|
6
|
+
|
2
7
|
|
3
8
|
module FluShot
|
9
|
+
module Storage
|
10
|
+
autoload :Memory, 'flu_shot/storage/memory'
|
11
|
+
autoload :Redis, 'flu_shot/storage/redis'
|
12
|
+
end
|
13
|
+
|
4
14
|
class Error < StandardError; end
|
5
|
-
|
15
|
+
|
16
|
+
def self.inject(name, &block)
|
17
|
+
if !before_filter.call
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
21
|
+
Prescription.for(name).each do |prescription|
|
22
|
+
Vaccine.find(prescription[:vaccine]).new(prescription[:params])
|
23
|
+
end
|
24
|
+
|
25
|
+
# @test
|
26
|
+
_before_init
|
27
|
+
block_given? ? block.call : nil
|
28
|
+
|
29
|
+
rescue FluShot::Sneeze => sneeze
|
30
|
+
raise sneeze.wrapped_exception
|
31
|
+
|
32
|
+
rescue
|
33
|
+
# @test
|
34
|
+
_exception_muted
|
35
|
+
block_given? ? block.call : nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.inject_only_if(&block)
|
39
|
+
Thread.current[:before_filter] = block
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def self.before_filter
|
45
|
+
Thread.current[:before_filter] ||= Proc.new { true }
|
46
|
+
Thread.current[:before_filter]
|
47
|
+
end
|
48
|
+
|
49
|
+
# @test
|
50
|
+
def self._before_init
|
51
|
+
end
|
52
|
+
|
53
|
+
# @test
|
54
|
+
def self._exception_muted
|
55
|
+
end
|
6
56
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe FluShot do
|
4
|
+
it 'has a VERSION number' do
|
5
|
+
refute_nil ::FluShot::VERSION
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#inject' do
|
9
|
+
it 'requires a name' do
|
10
|
+
assert_nil FluShot.inject('name')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'executes the given block' do
|
14
|
+
ret = FluShot.inject('name') do
|
15
|
+
'hello'
|
16
|
+
end
|
17
|
+
|
18
|
+
assert_equal 'hello', ret
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'executes the given block even if exception occurs' do
|
22
|
+
FluShot.stubs(:_before_init).raises(StandardError, 'should be catched')
|
23
|
+
ret = FluShot.inject('name') do
|
24
|
+
'hello'
|
25
|
+
end
|
26
|
+
|
27
|
+
assert_equal 'hello', ret
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'can be switched off' do
|
31
|
+
FluShot.inject_only_if do
|
32
|
+
false
|
33
|
+
end
|
34
|
+
FluShot.expects(:_before_init).never
|
35
|
+
FluShot.inject('name')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#inject_only_if' do
|
40
|
+
it 'does not aborts when exception occurs in it' do
|
41
|
+
FluShot.inject_only_if do
|
42
|
+
raise 'it should be catched'
|
43
|
+
end
|
44
|
+
FluShot.expects(:_before_init).never
|
45
|
+
FluShot.expects(:_exception_muted).once
|
46
|
+
FluShot.inject('name')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'sneeze' do
|
51
|
+
it 'is not rescued' do
|
52
|
+
FluShot.stubs(:_before_init).raises(FluShot::Sneeze.new(StandardError.new), 'should not be catched')
|
53
|
+
|
54
|
+
assert_raises StandardError do
|
55
|
+
FluShot.inject('name')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe :inject do
|
4
|
+
|
5
|
+
class Rand123 < FluShot::Vaccine
|
6
|
+
label :injection_test_vaccine
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
test
|
10
|
+
end
|
11
|
+
|
12
|
+
def test
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'executes all the vaccines from a prescription' do
|
17
|
+
FluShot::Prescription.spec(:injection_name) do |p|
|
18
|
+
p.add(:injection_test_vaccine, {})
|
19
|
+
end
|
20
|
+
|
21
|
+
Rand123.any_instance.expects(:test).once
|
22
|
+
FluShot.inject(:injection_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'works with Redis store' do
|
26
|
+
FluShot::Config.storage = FluShot::Storage::Redis.new(Redis.new)
|
27
|
+
FluShot::Prescription.spec(:injection_name) do |p|
|
28
|
+
p.add(:injection_test_vaccine, {})
|
29
|
+
end
|
30
|
+
|
31
|
+
Rand123.any_instance.expects(:test).once
|
32
|
+
FluShot.inject(:injection_name)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FluShot::Config do
|
4
|
+
|
5
|
+
describe '#storage' do
|
6
|
+
|
7
|
+
before do
|
8
|
+
FluShot::Config.storage = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns memory storage by default' do
|
12
|
+
assert FluShot::Config.storage.is_a?(FluShot::Storage::Memory)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns the same storage instance' do
|
16
|
+
assert_equal FluShot::Config.storage.object_id, FluShot::Config.storage.object_id
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'can register other storage objects too' do
|
20
|
+
FluShot::Config.storage = FluShot::Storage::Redis.new(Redis.new)
|
21
|
+
assert FluShot::Config.storage.is_a?(FluShot::Storage::Redis)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FluShot::Prescription do
|
4
|
+
describe '#spec' do
|
5
|
+
|
6
|
+
class VaccineMock < FluShot::Vaccine
|
7
|
+
label :vaccine_label
|
8
|
+
|
9
|
+
def initialize(*params)
|
10
|
+
params
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'registers vaccines' do
|
15
|
+
FluShot::Prescription.spec(:name) do |order|
|
16
|
+
order.add(:vaccine_label)
|
17
|
+
end
|
18
|
+
|
19
|
+
assert_equal({:vaccine => :vaccine_label, :params => {}}, FluShot::Prescription.for(:name).first)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'remembers to the ingridients of the vaccines' do
|
23
|
+
FluShot::Prescription.spec(:name) do |p|
|
24
|
+
p.add(:vaccine_label, {param: 1})
|
25
|
+
p.add(:vaccine_label, {param: 2})
|
26
|
+
end
|
27
|
+
|
28
|
+
assert_equal({:vaccine => :vaccine_label, :params => {:param => 1}}, FluShot::Prescription.for(:name).first)
|
29
|
+
assert_equal 2, FluShot::Prescription.for(:name).size
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FluShot::Sneeze do
|
4
|
+
it 'can be raised as an exception' do
|
5
|
+
assert_raises do
|
6
|
+
raise FluShot::Sneeze.new
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'can wrap another exception' do
|
11
|
+
sneeze = FluShot::Sneeze.new(StandardError.new)
|
12
|
+
assert_equal StandardError, sneeze.wrapped_exception.class
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FluShot::Storage::Memory do
|
4
|
+
before do
|
5
|
+
@store = FluShot::Storage::Memory.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'returns [] when prescription is missing' do
|
9
|
+
assert_equal [], @store.get('prescription_name')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'stores vaccines for a prescription' do
|
13
|
+
@store.add('prescription_name', {vaccine: 'vaccine_name'})
|
14
|
+
assert_equal 'vaccine_name', @store.get('prescription_name').first[:vaccine]
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'stores vaccine with parameters' do
|
18
|
+
@store.add('prescription_name', {vaccine: 'vaccine_name', params: {param: 'value'}})
|
19
|
+
assert_equal 'value', @store.get('prescription_name').first[:params][:param]
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FluShot::Storage::Redis do
|
4
|
+
before do
|
5
|
+
connection = Redis.new
|
6
|
+
@store = FluShot::Storage::Redis.new(connection)
|
7
|
+
@store.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns [] when prescription is missing' do
|
11
|
+
assert_equal [], @store.get('prescription_name')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'stores vaccine for a prescription' do
|
15
|
+
@store.add('prescription_name', {vaccine: 'vaccine_name'})
|
16
|
+
assert_equal 'vaccine_name', @store.get('prescription_name').first[:vaccine]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'stores vaccine with parameters' do
|
20
|
+
@store.add('prescription_name', {vaccine: 'vaccine_name', params: {param: 'value'}})
|
21
|
+
assert_equal 'value', @store.get('prescription_name').first[:params][:param]
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'can reset itself' do
|
25
|
+
@store.add('prescription_name', {param: 1})
|
26
|
+
@store.reset
|
27
|
+
assert_equal [], @store.get('prescription_name')
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FluShot::Vaccine do
|
4
|
+
it 'has an unknown label by default' do
|
5
|
+
vaccine = FluShot::Vaccine.new
|
6
|
+
assert_equal :unknown, vaccine.label
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'has a label that registers itself in a repo' do
|
10
|
+
Class.new(FluShot::Vaccine) { label :latency }
|
11
|
+
assert FluShot::Vaccine.registered.include?(:latency)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'can have attributes' do
|
15
|
+
vaccine = FluShot::Vaccine.new({min: 1000, max: 3000})
|
16
|
+
assert_equal 1000, vaccine.attributes[:min]
|
17
|
+
assert_equal 3000, vaccine.attributes[:max]
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#find' do
|
21
|
+
it 'can initialize a vaccine from the label' do
|
22
|
+
class Vaccine1 < FluShot::Vaccine
|
23
|
+
label :vaccine1
|
24
|
+
end
|
25
|
+
assert_equal Vaccine1, FluShot::Vaccine.find(:vaccine1)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#use' do
|
30
|
+
it 'finds and executes the vaccine' do
|
31
|
+
class Vaccine2 < FluShot::Vaccine
|
32
|
+
attr_reader :test_value
|
33
|
+
label :latency
|
34
|
+
|
35
|
+
def initialize(params = {})
|
36
|
+
@test_value = :ok
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_equal :ok, FluShot::Vaccine.use(:latency).test_value
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'finds and executes the vaccine with attributes' do
|
44
|
+
class Vaccine3 < FluShot::Vaccine
|
45
|
+
attr_reader :min, :max
|
46
|
+
label :latency
|
47
|
+
|
48
|
+
def initialize(params = {})
|
49
|
+
@min = params[:min]
|
50
|
+
@max = params[:max]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
assert_equal 1000, FluShot::Vaccine.use(:latency, {min: 1000, max: 3000}).min
|
55
|
+
assert_equal 3000, FluShot::Vaccine.use(:latency, {min: 1000, max: 3000}).max
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "flu_shot"
|
3
|
+
|
4
|
+
require "minitest/autorun"
|
5
|
+
require "minitest/spec"
|
6
|
+
require "mocha/minitest"
|
7
|
+
|
8
|
+
class Minitest::Spec
|
9
|
+
before :each do
|
10
|
+
FluShot::Config.storage = FluShot::Storage::Memory.new
|
11
|
+
FluShot.inject_only_if { true }
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flu_shot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Laszlo Papp
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,34 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mocha
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.11.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.11.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: redis
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.1.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.1.0
|
55
83
|
description: ''
|
56
84
|
email:
|
57
85
|
- nucc@bteam.hu
|
@@ -59,10 +87,12 @@ executables: []
|
|
59
87
|
extensions: []
|
60
88
|
extra_rdoc_files: []
|
61
89
|
files:
|
90
|
+
- ".github/workflows/ruby.yml"
|
62
91
|
- ".gitignore"
|
63
92
|
- ".travis.yml"
|
64
93
|
- CODE_OF_CONDUCT.md
|
65
94
|
- Gemfile
|
95
|
+
- Gemfile.lock
|
66
96
|
- LICENSE.txt
|
67
97
|
- README.md
|
68
98
|
- Rakefile
|
@@ -70,7 +100,23 @@ files:
|
|
70
100
|
- bin/setup
|
71
101
|
- flu_shot.gemspec
|
72
102
|
- lib/flu_shot.rb
|
103
|
+
- lib/flu_shot/config.rb
|
104
|
+
- lib/flu_shot/prescription.rb
|
105
|
+
- lib/flu_shot/sneeze.rb
|
106
|
+
- lib/flu_shot/storage/memory.rb
|
107
|
+
- lib/flu_shot/storage/redis.rb
|
108
|
+
- lib/flu_shot/vaccine.rb
|
73
109
|
- lib/flu_shot/version.rb
|
110
|
+
- specs/flu_shot_spec.rb
|
111
|
+
- specs/integration/inject_spec.rb
|
112
|
+
- specs/lib/config_spec.rb
|
113
|
+
- specs/lib/prescription_spec.rb
|
114
|
+
- specs/lib/receipt_spec.rb
|
115
|
+
- specs/lib/sneeze_spec.rb
|
116
|
+
- specs/lib/storage/memory_spec.rb
|
117
|
+
- specs/lib/storage/redis_spec.rb
|
118
|
+
- specs/lib/vaccine_spec.rb
|
119
|
+
- specs/spec_helper.rb
|
74
120
|
homepage: https://github.com/Nucc/flu_shot
|
75
121
|
licenses:
|
76
122
|
- MIT
|