startback 0.4.5 → 0.5.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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/startback/audit/trailer.rb +145 -0
  3. data/lib/startback/audit.rb +1 -0
  4. data/lib/startback/bus/bunny/async.rb +117 -0
  5. data/lib/startback/bus/bunny.rb +1 -0
  6. data/lib/startback/bus/memory/async.rb +40 -0
  7. data/lib/startback/bus/memory/sync.rb +30 -0
  8. data/lib/startback/bus/memory.rb +2 -0
  9. data/lib/startback/bus.rb +94 -0
  10. data/lib/startback/caching/entity_cache.rb +80 -0
  11. data/lib/startback/caching/store.rb +34 -0
  12. data/lib/startback/context/middleware.rb +1 -1
  13. data/lib/startback/context.rb +93 -4
  14. data/lib/startback/event.rb +43 -0
  15. data/lib/startback/operation.rb +39 -6
  16. data/lib/startback/support/fake_logger.rb +18 -0
  17. data/lib/startback/support/hooks.rb +48 -0
  18. data/lib/startback/support/operation_runner.rb +150 -0
  19. data/lib/startback/support/robustness.rb +153 -0
  20. data/lib/startback/support.rb +3 -0
  21. data/lib/startback/version.rb +2 -2
  22. data/lib/startback/web/api.rb +3 -4
  23. data/lib/startback/web/catch_all.rb +12 -5
  24. data/lib/startback/web/middleware.rb +13 -0
  25. data/lib/startback.rb +2 -0
  26. data/spec/spec_helper.rb +2 -0
  27. data/spec/unit/audit/test_trailer.rb +88 -0
  28. data/spec/unit/bus/memory/test_async.rb +41 -0
  29. data/spec/unit/bus/memory/test_sync.rb +41 -0
  30. data/spec/unit/caching/test_entity_cache.rb +109 -0
  31. data/spec/unit/context/test_abstraction_factory.rb +64 -0
  32. data/spec/unit/support/hooks/test_after_hook.rb +54 -0
  33. data/spec/unit/support/hooks/test_before_hook.rb +54 -0
  34. data/spec/unit/support/operation_runner/test_around_run.rb +157 -0
  35. data/spec/unit/support/operation_runner/test_before_after_call.rb +48 -0
  36. data/spec/unit/support/test_robusteness.rb +209 -0
  37. data/spec/unit/test_context.rb +51 -0
  38. data/spec/unit/test_event.rb +69 -0
  39. data/spec/unit/test_operation.rb +0 -3
  40. metadata +32 -4
@@ -0,0 +1,209 @@
1
+ require 'spec_helper'
2
+ module Startback
3
+ module Support
4
+ describe Robustness do
5
+
6
+ let(:the_logger) {
7
+ FakeLogger.new
8
+ }
9
+
10
+ let(:context_with_logger) {
11
+ Context.new.tap{|c| c.logger = the_logger }
12
+ }
13
+
14
+ describe "monitor" do
15
+ include Robustness
16
+
17
+ it 'works' do
18
+ x = monitor("test", context_with_logger) do
19
+ 12
20
+ end
21
+ expect(x).to eql(12)
22
+ expect(the_logger.last_msg.keys).to eql([:op, :op_took])
23
+ end
24
+
25
+ end # monitor
26
+
27
+ describe "stop_errors" do
28
+ include Robustness
29
+
30
+ it 'works and logs the error' do
31
+ x = nil
32
+ expect {
33
+ x = stop_errors("test", context_with_logger) do
34
+ raise "Test"
35
+ end
36
+ }.not_to raise_error
37
+ expect(x).to be(nil)
38
+ expect(the_logger.last_msg.keys).to eql([:op, :op_took, :error])
39
+ end
40
+
41
+ it 'returns the result if no error' do
42
+ x = nil
43
+ expect {
44
+ x = stop_errors("test", context_with_logger) do
45
+ 12
46
+ end
47
+ }.not_to raise_error
48
+ expect(x).to eql(12)
49
+ expect(the_logger.last_msg).to be_nil
50
+ end
51
+
52
+ end
53
+
54
+ describe "try_max_times" do
55
+ include Robustness
56
+
57
+ it 'fails if n errors are seen' do
58
+ seen = 0
59
+ expect {
60
+ try_max_times(2, "test", context_with_logger) do
61
+ seen += 1
62
+ raise "Test"
63
+ end
64
+ }.to raise_error
65
+ expect(seen).to eql(2)
66
+ expect(the_logger.last_msg.keys).to eql([:op, :op_took, :error])
67
+ end
68
+
69
+ it 'suceeds if an attemps succeeds' do
70
+ seen = 0
71
+ result = nil
72
+ expect {
73
+ result = try_max_times(2, "test", context_with_logger) do
74
+ seen += 1
75
+ raise "Test" if seen == 1
76
+ 12
77
+ end
78
+ }.not_to raise_error
79
+ expect(result).to eql(12)
80
+ expect(seen).to eql(2)
81
+ expect(the_logger.last_msg.keys).to eql([:op, :op_took, :error])
82
+ end
83
+
84
+ it 'suceeds if first attemps succeeds' do
85
+ result = nil
86
+ expect {
87
+ result = try_max_times(2, "test", context_with_logger) do
88
+ 12
89
+ end
90
+ }.not_to raise_error
91
+ expect(result).to eql(12)
92
+ expect(the_logger.last_msg).to be_nil
93
+ end
94
+
95
+ end
96
+
97
+ describe "parse_args" do
98
+ include Robustness::Tools
99
+
100
+ it 'works fine with full op and no context' do
101
+ log_msg_src = {
102
+ op: "AnOp",
103
+ op_data: { foo: "bar" }
104
+ }
105
+ log_msg, logger = parse_args(log_msg_src)
106
+ expect(log_msg).to eql(log_msg_src)
107
+ expect(logger).to be_a(::Logger)
108
+ end
109
+
110
+ it 'works fine with an string and a method name' do
111
+ expected = {
112
+ op: "AClass#method"
113
+ }
114
+ log_msg, logger = parse_args("AClass", "method")
115
+ expect(log_msg).to eql(expected)
116
+ expect(logger).to be_a(::Logger)
117
+ end
118
+
119
+ it 'works fine with a string only' do
120
+ expected = {
121
+ op: "a message"
122
+ }
123
+ log_msg, logger = parse_args("a message")
124
+ expect(log_msg).to eql(expected)
125
+ expect(logger).to be_a(::Logger)
126
+ end
127
+
128
+ it 'works fine with a string and a context with logger' do
129
+ expected = {
130
+ op: "a message"
131
+ }
132
+ log_msg, logger = parse_args("a message", context_with_logger)
133
+ expect(log_msg).to eql(expected)
134
+ expect(logger).to be(the_logger)
135
+ end
136
+
137
+ it 'works fine with a string and an extra hash' do
138
+ expected = {
139
+ op: "a message",
140
+ op_took: 16
141
+ }
142
+ log_msg, logger = parse_args("a message", op_took: 16)
143
+ expect(log_msg).to eql(expected)
144
+ expect(logger).to be_a(::Logger)
145
+ end
146
+
147
+ it 'works fine with a module and method' do
148
+ expected = {
149
+ op: "Startback#foo"
150
+ }
151
+ log_msg, logger = parse_args(Startback, "foo")
152
+ expect(log_msg).to eql(expected)
153
+ expect(logger).to be_a(::Logger)
154
+ end
155
+
156
+ it 'works fine with a class and method' do
157
+ expected = {
158
+ op: "Startback::Event#foo"
159
+ }
160
+ log_msg, logger = parse_args(Startback::Event, "foo")
161
+ expect(log_msg).to eql(expected)
162
+ expect(logger).to be_a(::Logger)
163
+ end
164
+
165
+ it 'works fine with an instance and method' do
166
+ expected = {
167
+ op: "Startback::Event#foo"
168
+ }
169
+ log_msg, logger = parse_args(Startback::Event.new(nil, nil), "foo")
170
+ expect(log_msg).to eql(expected)
171
+ expect(logger).to be_a(::Logger)
172
+ end
173
+
174
+ it 'when a hash is passed as last arg' do
175
+ expected = {
176
+ op: "hello#foo",
177
+ op_took: 16
178
+ }
179
+ log_msg, logger = parse_args("hello", "foo", op_took: 16)
180
+ expect(log_msg).to eql(expected)
181
+ expect(logger).to be_a(::Logger)
182
+ end
183
+ end
184
+
185
+ describe "logger_for" do
186
+ include Robustness::Tools
187
+
188
+ it 'works on a logger' do
189
+ expect(logger_for(the_logger)).to be(the_logger)
190
+ end
191
+
192
+ it 'works on a Context responding to logger' do
193
+ expect(logger_for(context_with_logger)).to be(the_logger)
194
+ end
195
+
196
+ it 'works on an object having a Context responding to logger' do
197
+ x = OpenStruct.new(context: context_with_logger)
198
+ expect(logger_for(x)).to eql(the_logger)
199
+ end
200
+
201
+ it 'works on an object having a Context but no logger' do
202
+ x = OpenStruct.new(context: Context.new)
203
+ expect(logger_for(x)).to be_a(::Logger)
204
+ end
205
+ end
206
+
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ module Startback
4
+ describe Context do
5
+
6
+ it "has a to_json that dumps it" do
7
+ expect(Context.new.to_json).to eql("{}")
8
+ end
9
+
10
+ class SubContext < Context
11
+ attr_accessor :foo
12
+ h_factory do |c,h|
13
+ c.foo = h["foo"]
14
+ end
15
+ h_dump do |h|
16
+ h.merge!("foo" => foo)
17
+ end
18
+ end
19
+
20
+ class SubContext
21
+ attr_accessor :bar
22
+ h_factory do |c,h|
23
+ c.bar = h["bar"]
24
+ end
25
+ h_dump do |h|
26
+ h.merge!("bar" => bar)
27
+ end
28
+ end
29
+
30
+ it 'allows installing factories' do
31
+ expect(Context.h_factories).to be_empty
32
+ expect(SubContext.h_factories.size).to eql(2)
33
+ end
34
+
35
+ it 'has a `to_h` information contract that works as expected' do
36
+ context = SubContext.new.tap{|c|
37
+ c.foo = "Hello"
38
+ c.bar = "World"
39
+ }
40
+ expect(context.to_h).to eql({ "foo" => "Hello", "bar" => "World" })
41
+ end
42
+
43
+ it 'has a `h` information contract that works as expected' do
44
+ context = SubContext.h({ "foo" => "Hello", "bar" => "World" })
45
+ expect(context).to be_a(SubContext)
46
+ expect(context.foo).to eql("Hello")
47
+ expect(context.bar).to eql("World")
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ module Startback
4
+ describe Event do
5
+
6
+ subject{
7
+ Event.new("user_changed", { "foo" => "bar" })
8
+ }
9
+
10
+ it 'presents an ostruct on top of its data' do
11
+ expect(subject.data.foo).to eql("bar")
12
+ end
13
+
14
+ describe "the json information contract" do
15
+
16
+ JSON_SRC = <<-JSON.gsub(/\s+/, "")
17
+ {
18
+ "type": "user_changed",
19
+ "data": {
20
+ "foo": "bar"
21
+ }
22
+ }
23
+ JSON
24
+
25
+ it 'has a to_json method that works as expected' do
26
+ expect(subject.to_json).to eql(JSON_SRC)
27
+ end
28
+
29
+ it 'has a to_json that dumps the context if any' do
30
+ evt = Event.new("user_changed", { "foo" => "bar" }, { "baz": "context" })
31
+ expect(evt.to_json).to eql(<<-JSON.gsub(/\s+/, ""))
32
+ {
33
+ "type": "user_changed",
34
+ "data": {
35
+ "foo": "bar"
36
+ },
37
+ "context": {
38
+ "baz": "context"
39
+ }
40
+ }
41
+ JSON
42
+ end
43
+
44
+
45
+ it 'has a json class method that works as expected' do
46
+ evt = Event.json(JSON_SRC)
47
+ expect(evt).to be_a(Event)
48
+ expect(evt.type).to eql("user_changed")
49
+ expect(evt.data).to eql(subject.data)
50
+ end
51
+
52
+ it 'accepts an explicit context in the world' do
53
+ evt = Event.json(JSON_SRC, context: 12)
54
+ expect(evt.context).to eql(12)
55
+ end
56
+
57
+ it 'accepts an context factory in the world' do
58
+ cf = ->(arg) {
59
+ expect(arg).to eql(JSON.parse(JSON_SRC))
60
+ 12
61
+ }
62
+ evt = Event.json(JSON_SRC, context_factory: cf)
63
+ expect(evt.context).to eql(12)
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end # module Startback
@@ -21,10 +21,7 @@ module Startback
21
21
  foo1.foo = :bar1
22
22
 
23
23
  foo2 = foo1.bind({ db: :bar })
24
- expect(foo1 == foo2).to eql(false)
25
24
  expect(foo2.foo).to eql(:bar1)
26
-
27
- expect(->(){ foo1.db }).to raise_error(NoMethodError)
28
25
  expect(foo2.db).to eql(:bar)
29
26
  end
30
27
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: startback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-24 00:00:00.000000000 Z
11
+ date: 2019-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '0.6'
117
+ version: '0.7'
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '0.6'
124
+ version: '0.7'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: path
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -146,9 +146,20 @@ files:
146
146
  - README.md
147
147
  - Rakefile
148
148
  - lib/startback.rb
149
+ - lib/startback/audit.rb
150
+ - lib/startback/audit/trailer.rb
151
+ - lib/startback/bus.rb
152
+ - lib/startback/bus/bunny.rb
153
+ - lib/startback/bus/bunny/async.rb
154
+ - lib/startback/bus/memory.rb
155
+ - lib/startback/bus/memory/async.rb
156
+ - lib/startback/bus/memory/sync.rb
157
+ - lib/startback/caching/entity_cache.rb
158
+ - lib/startback/caching/store.rb
149
159
  - lib/startback/context.rb
150
160
  - lib/startback/context/middleware.rb
151
161
  - lib/startback/errors.rb
162
+ - lib/startback/event.rb
152
163
  - lib/startback/ext.rb
153
164
  - lib/startback/ext/date_time.rb
154
165
  - lib/startback/ext/time.rb
@@ -156,7 +167,11 @@ files:
156
167
  - lib/startback/operation/error_operation.rb
157
168
  - lib/startback/operation/multi_operation.rb
158
169
  - lib/startback/support.rb
170
+ - lib/startback/support/fake_logger.rb
171
+ - lib/startback/support/hooks.rb
159
172
  - lib/startback/support/logger.rb
173
+ - lib/startback/support/operation_runner.rb
174
+ - lib/startback/support/robustness.rb
160
175
  - lib/startback/version.rb
161
176
  - lib/startback/web/api.rb
162
177
  - lib/startback/web/auto_caching.rb
@@ -166,9 +181,22 @@ files:
166
181
  - lib/startback/web/magic_assets.rb
167
182
  - lib/startback/web/magic_assets/ng_html_transformer.rb
168
183
  - lib/startback/web/magic_assets/rake_tasks.rb
184
+ - lib/startback/web/middleware.rb
169
185
  - lib/startback/web/shield.rb
170
186
  - spec/spec_helper.rb
187
+ - spec/unit/audit/test_trailer.rb
188
+ - spec/unit/bus/memory/test_async.rb
189
+ - spec/unit/bus/memory/test_sync.rb
190
+ - spec/unit/caching/test_entity_cache.rb
191
+ - spec/unit/context/test_abstraction_factory.rb
171
192
  - spec/unit/context/test_middleware.rb
193
+ - spec/unit/support/hooks/test_after_hook.rb
194
+ - spec/unit/support/hooks/test_before_hook.rb
195
+ - spec/unit/support/operation_runner/test_around_run.rb
196
+ - spec/unit/support/operation_runner/test_before_after_call.rb
197
+ - spec/unit/support/test_robusteness.rb
198
+ - spec/unit/test_context.rb
199
+ - spec/unit/test_event.rb
172
200
  - spec/unit/test_operation.rb
173
201
  - spec/unit/test_support.rb
174
202
  - spec/unit/web/fixtures/assets/app/hello.es6