rohbau 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,305 @@
1
+ module Rohbau
2
+ module SharedSpecs
3
+ DefaultGateway = Proc.new do
4
+
5
+ describe 'add' do
6
+ it 'inserts an entity' do
7
+ subject.add entity
8
+ assert_equal 1, subject.size
9
+ end
10
+
11
+ it 'returns a copied entity' do
12
+ new_entity = subject.add entity
13
+ refute_equal new_entity.object_id, entity.object_id
14
+ end
15
+
16
+ it 'assigns a string uid to the entity' do
17
+ new_entity = subject.add entity
18
+ refute_nil new_entity.uid
19
+ assert_kind_of String, new_entity.uid
20
+ end
21
+
22
+ it 'raises an error if entity is nil' do
23
+ assert_raises ArgumentError do
24
+ subject.add nil
25
+ end
26
+ end
27
+
28
+ it 'raises an error if entity has already an uid' do
29
+ entity.uid = '45'
30
+ assert_raises ArgumentError do
31
+ subject.add entity
32
+ end
33
+ end
34
+ end
35
+
36
+ describe 'bulk_add' do
37
+ it 'inserts a collection of entities' do
38
+ entities = [entity, updated_entity]
39
+
40
+ subject.bulk_add entities
41
+ assert_equal 2, subject.size
42
+ end
43
+
44
+ it 'returns a collection copied entities' do
45
+ entities = [entity]
46
+
47
+ new_entities = subject.bulk_add entities
48
+ refute_equal new_entities[0].object_id, entities[0].object_id
49
+ end
50
+
51
+ it 'assigns a string uid to the entities' do
52
+ entities = [entity]
53
+
54
+ new_entities = subject.bulk_add entities
55
+ refute_nil new_entities[0].uid
56
+ assert_kind_of String, new_entities[0].uid
57
+ end
58
+
59
+ it 'raises an error if entities are nil' do
60
+ assert_raises ArgumentError do
61
+ subject.bulk_add [nil]
62
+ end
63
+ end
64
+
65
+ it 'raises an error if entity has already an uid' do
66
+ entity.uid = '45'
67
+ assert_raises ArgumentError do
68
+ subject.bulk_add [entity]
69
+ end
70
+ end
71
+ end
72
+
73
+ describe 'all' do
74
+
75
+ it 'returns an empty array' do
76
+ assert_equal [], subject.all
77
+ end
78
+
79
+ it 'returns added entities' do
80
+ entities = [entity, updated_entity].map do |e|
81
+ subject.add e
82
+ end
83
+
84
+ assert_equal entities, subject.all
85
+ end
86
+
87
+ end
88
+
89
+ describe 'get' do
90
+ it 'returns nothing if uid not found' do
91
+ assert_equal nil, subject.get('23')
92
+ end
93
+
94
+ it 'raises an error if uid is an invalid value' do
95
+ ["", nil, 22].each do |value|
96
+ assert_raises ArgumentError do
97
+ subject.get(value)
98
+ end
99
+ end
100
+ end
101
+
102
+ it 'returns the entity for a known uid' do
103
+ entities = [entity, updated_entity].map do |e|
104
+ subject.add e
105
+ end
106
+
107
+ assert_equal entities.first, subject.get(entities.first.uid)
108
+ assert_equal entities.last, subject.get(entities.last.uid)
109
+ end
110
+ end
111
+
112
+ describe 'entity duplication' do
113
+ it 'returns a copy of the stored entity on get' do
114
+ uid = subject.add(entity).uid
115
+
116
+ e = subject.get(uid)
117
+ e.uid = uid + '1'
118
+
119
+ assert_equal uid, subject.get(uid).uid
120
+ end
121
+
122
+ it 'returns a copy of the stored entity on add' do
123
+ result = subject.add(entity)
124
+ uid = result.uid
125
+
126
+ result.uid = uid + '1'
127
+
128
+ assert_equal uid, subject.get(uid).uid
129
+ end
130
+
131
+ it 'adds a copy as stored entity on add' do
132
+ uid = subject.add(entity).uid
133
+
134
+ entity.uid = uid + '1'
135
+
136
+ assert_equal uid, subject.get(uid).uid
137
+ end
138
+
139
+ it 'returns copies of the stored entities on all' do
140
+ uid = subject.add(entity).uid
141
+
142
+ e = subject.all.first
143
+ e.uid = uid + '1'
144
+
145
+ assert_equal uid, subject.get(uid).uid
146
+ end
147
+
148
+ it 'returns a copy of the stored entity on update' do
149
+ stored = subject.add(entity)
150
+ uid = stored.uid
151
+
152
+ result = subject.update(stored)
153
+
154
+ result.uid = uid + '1'
155
+
156
+ assert_equal uid, subject.get(uid).uid
157
+ end
158
+
159
+ it 'returns copies of the stored entities on bulk_add' do
160
+ entities = [entity]
161
+
162
+ results = subject.bulk_add(entities)
163
+ uid = results[0].uid
164
+
165
+ results[0].uid = uid + '1'
166
+
167
+ assert_equal uid, subject.get(uid).uid
168
+ end
169
+
170
+ it 'adds copies as stored entities on bulk_add' do
171
+ entities = [entity]
172
+
173
+ results = subject.bulk_add(entities)
174
+ uid = results[0].uid
175
+
176
+ entity.uid = uid + '1'
177
+
178
+ assert_equal uid, subject.get(uid).uid
179
+ end
180
+ end
181
+
182
+ describe 'update' do
183
+ it 'fails if nil is given' do
184
+ assert_raises ArgumentError do
185
+ subject.update(nil)
186
+ end
187
+ end
188
+
189
+ it 'fails if uid is nil' do
190
+ assert_raises ArgumentError do
191
+ entity.uid = nil
192
+ subject.update(entity)
193
+ end
194
+ end
195
+
196
+ it 'fails if uid is not known' do
197
+ assert_raises ArgumentError do
198
+ entity.uid = "22"
199
+ subject.update(entity)
200
+ end
201
+ end
202
+
203
+ it 'updates and returns the entity' do
204
+ added_entity = subject.add(entity)
205
+ uid = added_entity.uid
206
+
207
+ updated_entity.uid = uid
208
+ result = subject.update(updated_entity)
209
+
210
+ assert_equal subject.get(uid), result
211
+ refute_equal added_entity, result
212
+ end
213
+ end
214
+
215
+ describe 'delete' do
216
+ it 'fails if nil is given' do
217
+ assert_raises ArgumentError do
218
+ subject.delete(nil)
219
+ end
220
+ end
221
+
222
+ it 'fails if uid is not known' do
223
+ assert_raises ArgumentError do
224
+ subject.delete('22')
225
+ end
226
+ end
227
+
228
+ it 'deletes the entity' do
229
+ uid = subject.add(entity).uid
230
+
231
+ assert subject.get(uid)
232
+
233
+ subject.delete(uid)
234
+
235
+ refute subject.get(uid)
236
+ end
237
+
238
+ it 'returns deleted entity' do
239
+ uid = subject.add(entity).uid
240
+
241
+ persisted_entity = subject.get(uid)
242
+
243
+ assert_equal persisted_entity, subject.delete(uid)
244
+ end
245
+
246
+ it 'removes the entity from the collection' do
247
+ uid = subject.add(entity).uid
248
+ subject.add(updated_entity)
249
+
250
+ subject.delete(uid)
251
+
252
+ assert_equal 1, subject.all.size
253
+ refute_includes subject.all.map(&:uid), uid
254
+ end
255
+ end
256
+
257
+ describe 'bulk_delete' do
258
+ it 'raises an error if nil is given' do
259
+ assert_raises ArgumentError do
260
+ subject.bulk_delete([nil])
261
+ end
262
+ end
263
+
264
+ it 'raises an error if uid is not known' do
265
+ assert_raises ArgumentError do
266
+ subject.bulk_delete(['22'])
267
+ end
268
+ end
269
+
270
+ it 'deletes the entities' do
271
+ uid = subject.add(entity).uid
272
+ uids = [uid]
273
+
274
+ assert subject.get(uid)
275
+
276
+ subject.bulk_delete(uids)
277
+
278
+ refute subject.get(uid)
279
+ end
280
+
281
+ it 'returns deleted entity' do
282
+ uid = subject.add(entity).uid
283
+ uids = [uid]
284
+
285
+ persisted_entity = subject.get(uid)
286
+
287
+ assert_equal persisted_entity, subject.bulk_delete(uids)[0]
288
+ end
289
+
290
+ it 'removes the entity from the collection' do
291
+ uid = subject.add(entity).uid
292
+ uids = [uid]
293
+
294
+ subject.add(updated_entity).uid
295
+
296
+ subject.bulk_delete(uids)
297
+
298
+ assert_equal 1, subject.all.size
299
+ refute_includes subject.all.map(&:uid), uid
300
+ end
301
+ end
302
+
303
+ end
304
+ end
305
+ end
@@ -0,0 +1,25 @@
1
+ require 'bound'
2
+
3
+ module Rohbau
4
+
5
+ class UseCase
6
+
7
+ def self.call(request, input = nil)
8
+ args = [request]
9
+ args << input if input
10
+
11
+ new(*args).call
12
+ end
13
+
14
+ def initialize(request)
15
+ @request = request
16
+ end
17
+
18
+ protected
19
+
20
+ def service(service_name)
21
+ @request.services.public_send service_name
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ module Rohbau
2
+ VERSION = "0.1.0"
3
+ end
data/lib/rohbau.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "rohbau/version"
2
+
3
+ module Rohbau
4
+ # Your code goes here...
5
+ end
data/rohbau.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rohbau/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rohbau"
8
+ spec.version = Rohbau::VERSION
9
+ spec.authors = [
10
+ "Jakob Holderbaum",
11
+ "Peter Suschlik",
12
+ "Dax Defranco",
13
+ "Andreas Busold",
14
+ "Jan Owiesniak"
15
+ ]
16
+ spec.email = [
17
+ "rohbau@jakob.io",
18
+ "ps@neopoly.de",
19
+ "dd@neopoly.de",
20
+ "ab@neopoly.de",
21
+ "jo@neopoly.de"
22
+ ]
23
+ spec.summary = %q{Provides a set of patterns used in Domain Driven Design}
24
+ spec.homepage = "http://www.neopoly.de/"
25
+ spec.license = "MIT"
26
+
27
+ spec.files = `git ls-files`.split($/)
28
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency 'thread_safe', '~> 0.3'
33
+ spec.add_dependency 'bound', '~> 2.1'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.7'
36
+ spec.add_development_dependency 'rake', '~> 10.3'
37
+ spec.add_development_dependency 'minitest', '~> 5.4'
38
+ spec.add_development_dependency 'simplecov'
39
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+ require 'rohbau/event_tube'
3
+
4
+ describe Rohbau::EventTube do
5
+ let(:tube) { Class.new(Rohbau::EventTube)}
6
+ let(:event_class) { Struct.new(:arg1) }
7
+ let(:event) { event_class.new(22) }
8
+
9
+ describe 'without subscribers' do
10
+ it 'can publish' do
11
+ tube.publish :my_event, event
12
+ end
13
+ end
14
+
15
+ describe 'with subscriber' do
16
+ before do
17
+ @calls = []
18
+ tube.subscribe :my_event do |event|
19
+ @calls << event
20
+ end
21
+ end
22
+
23
+ it 'can publish' do
24
+ tube.publish :my_event, event
25
+ assert_equal [event], @calls
26
+ end
27
+ end
28
+
29
+ describe 'with multiple subscribers' do
30
+ before do
31
+ @calls = []
32
+
33
+ tube.subscribe :my_event do |event|
34
+ @calls << event
35
+ end
36
+
37
+ tube.subscribe :my_event do |event|
38
+ @calls << event
39
+ end
40
+ end
41
+
42
+ it 'processes them all on publish' do
43
+ tube.publish :my_event, event
44
+ assert_equal [event,event], @calls
45
+ end
46
+ end
47
+
48
+ describe 'reset' do
49
+ before do
50
+ @calls = []
51
+ tube.subscribe :my_event do |event|
52
+ @calls << event
53
+ end
54
+ end
55
+
56
+ it 'does nothin on publish' do
57
+ tube.reset
58
+ tube.publish :my_event, event
59
+ assert_equal [], @calls
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,72 @@
1
+ require 'minitest/mock'
2
+
3
+ require 'spec_helper'
4
+ require 'rohbau/runtime_loader'
5
+
6
+ describe Rohbau::RuntimeLoader do
7
+ ExampleClass = Class.new
8
+
9
+ let(:my_runtime_loader) do
10
+ Class.new(Rohbau::RuntimeLoader)
11
+ end
12
+
13
+ before do
14
+ @initializer_result = my_runtime_loader.new(ExampleClass)
15
+ end
16
+
17
+ it 'starts up a given class' do
18
+ assert_kind_of ExampleClass, my_runtime_loader.instance
19
+ end
20
+
21
+ it 'has a running predicate after init' do
22
+ assert_predicate my_runtime_loader, :running?
23
+ end
24
+
25
+ it 'returns the loader class on new' do
26
+ assert_equal my_runtime_loader, @initializer_result
27
+ end
28
+
29
+ describe 'termination' do
30
+ it 'has no running predicate afterwards' do
31
+ my_runtime_loader.terminate
32
+ refute_predicate my_runtime_loader, :running?
33
+ end
34
+
35
+ it 'sets instance to nil' do
36
+ my_runtime_loader.terminate
37
+ assert_equal nil, my_runtime_loader.instance
38
+ end
39
+
40
+ it 'calls terminate on instance, if it responds to it' do
41
+ mocked_instance = inject_mocked_instance_into_my_runtime_loader
42
+ mocked_instance.expect(:terminate, nil)
43
+
44
+ my_runtime_loader.terminate
45
+
46
+ assert mocked_instance.verify
47
+ end
48
+
49
+ it 'is initializeable after termination' do
50
+ my_runtime_loader.terminate
51
+ my_runtime_loader.new(ExampleClass)
52
+ end
53
+ end
54
+
55
+ it 'does not instanciate twice' do
56
+ instance = my_runtime_loader.instance
57
+
58
+ my_runtime_loader.new(ExampleClass)
59
+
60
+ assert_same instance, my_runtime_loader.instance
61
+ end
62
+
63
+
64
+ def inject_mocked_instance_into_my_runtime_loader
65
+ instance = MiniTest::Mock.new
66
+
67
+ def my_runtime_loader.instance
68
+ @__mocked_instance
69
+ end
70
+ my_runtime_loader.instance_variable_set :@__mocked_instance, instance
71
+ end
72
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+ require 'rohbau/service_factory'
3
+
4
+ describe Rohbau::ServiceFactory do
5
+
6
+ let(:factory_class) do
7
+ Class.new(Rohbau::ServiceFactory)
8
+ end
9
+
10
+ let(:factory) do
11
+ factory_class.new(runtime)
12
+ end
13
+
14
+ let(:runtime) do
15
+ Object.new
16
+ end
17
+
18
+ it 'needs a runtime instance to get instanciated' do
19
+ raised = assert_raises RuntimeError do
20
+ factory_class.new(nil)
21
+ end
22
+
23
+ assert_match(/Runtime/, raised.message)
24
+ end
25
+
26
+ describe 'external dependency compliance' do
27
+ it 'is reached if there are none' do
28
+ assert_predicate factory_class, :external_dependencies_complied?
29
+ end
30
+
31
+ describe 'with external dependencies' do
32
+ before do
33
+ factory_class.external_dependencies :service1, :service2
34
+ end
35
+
36
+ it 'is not reached without any registered external service' do
37
+ refute_predicate factory_class, :external_dependencies_complied?
38
+ end
39
+
40
+ it 'is not reached with partially registers services' do
41
+ factory_class.register(:service1) { }
42
+ refute_predicate factory_class, :external_dependencies_complied?
43
+ end
44
+
45
+ it 'is reached if all required dependencies are registered' do
46
+ factory_class.register(:service1) { }
47
+ factory_class.register(:service2) { }
48
+ assert_predicate factory_class, :external_dependencies_complied?
49
+ end
50
+ end
51
+ end
52
+
53
+ describe 'service registration' do
54
+ describe "with one service registered" do
55
+ before do
56
+ factory_class.register :test_service do
57
+ Struct.new(:a).new(22)
58
+ end
59
+ end
60
+
61
+ it 'allows access via a named method' do
62
+ assert_kind_of Struct, factory.test_service
63
+ assert_equal 22, factory.test_service.a
64
+ end
65
+
66
+ it 'caches service instances' do
67
+ identity_of_first_call = factory.test_service.object_id
68
+ identity_of_second_call = factory.test_service.object_id
69
+ assert_equal identity_of_first_call, identity_of_second_call
70
+ end
71
+
72
+ it "can unregister services" do
73
+ factory_class.unregister(:test_service)
74
+
75
+ assert_raises(NoMethodError) {factory.test_service}
76
+ end
77
+
78
+ describe "with more than one service registered" do
79
+ before do
80
+ factory_class.register :test_service do
81
+ Struct.new(:a).new(23)
82
+ end
83
+ end
84
+
85
+ it "uses the most recently defined service" do
86
+ assert_kind_of Struct, factory.test_service
87
+ refute_equal 22, factory.test_service.a
88
+ assert_equal 23, factory.test_service.a
89
+ end
90
+
91
+ it "uses the default implementation when unregistered" do
92
+ factory_class.unregister(:test_service)
93
+
94
+ assert_kind_of Struct, factory.test_service
95
+ assert_equal 22, factory.test_service.a
96
+ end
97
+
98
+ it "removes the service if unregistered twice" do
99
+ factory_class.unregister(:test_service)
100
+ factory_class.unregister(:test_service)
101
+
102
+ assert_raises(NoMethodError) {factory.test_service}
103
+ end
104
+ end
105
+ end
106
+
107
+ end
108
+
109
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'rohbau/shared_spec'
3
+
4
+ describe Rohbau::SharedSpec do
5
+
6
+ before do
7
+ Rohbau::SharedSpec::SpecIndex.reset
8
+ end
9
+
10
+ let(:shared_spec_class) { Class.new(Rohbau::SharedSpec) }
11
+
12
+ it 'provides access the stored blocks' do
13
+ blk = -> {}
14
+
15
+ shared_spec_class.for :test1, &blk
16
+
17
+ assert_equal Proc.new(&blk), shared_spec_class.get(:test1)
18
+ end
19
+
20
+ describe 'index' do
21
+ let(:second_shared_spec_class) { Class.new(Rohbau::SharedSpec) }
22
+ let(:index) { Rohbau::SharedSpec::SpecIndex }
23
+
24
+ it 'collects all inherited specs' do
25
+ assert_equal [
26
+ shared_spec_class,
27
+ second_shared_spec_class
28
+ ], index.all
29
+ end
30
+
31
+ it 'looks up spec on all registered children' do
32
+ blk1 = -> {}
33
+ blk2 = -> {}
34
+
35
+ shared_spec_class.for :test1, &blk1
36
+ second_shared_spec_class.for :test2, &blk2
37
+
38
+ assert_equal Proc.new(&blk1), index.get(:test1)
39
+ assert_equal Proc.new(&blk2), index.get(:test2)
40
+ assert_equal nil, index.get(:something)
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,12 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+
4
+ if ENV['CODECLIMATE_REPO_TOKEN']
5
+ require "codeclimate-test-reporter"
6
+ CodeClimate::TestReporter.start
7
+ end
8
+
9
+ if ENV['coverage']
10
+ require 'simplecov'
11
+ SimpleCov.start
12
+ end