shamu 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +26 -0
  3. data/.gitignore +2 -1
  4. data/.rubocop.yml +89 -30
  5. data/.yardopts +4 -5
  6. data/Gemfile +24 -12
  7. data/Guardfile +5 -0
  8. data/LABELS.md +22 -0
  9. data/README.md +41 -0
  10. data/Rakefile +12 -0
  11. data/circle.yml +7 -3
  12. data/config.ru +7 -0
  13. data/lib/shamu/active_record.rb +7 -0
  14. data/lib/shamu/attributes/assignment.rb +114 -0
  15. data/lib/shamu/attributes/equality.rb +40 -0
  16. data/lib/shamu/attributes/fluid_assignment.rb +49 -0
  17. data/lib/shamu/attributes/validation.rb +74 -0
  18. data/lib/shamu/attributes.rb +255 -0
  19. data/lib/shamu/auditing/README.md +0 -0
  20. data/lib/shamu/auditing/audit_record.rb +32 -0
  21. data/lib/shamu/auditing/auditing_service.rb +32 -0
  22. data/lib/shamu/auditing/list_scope.rb +22 -0
  23. data/lib/shamu/auditing/logging_auditing_service.rb +16 -0
  24. data/lib/shamu/auditing/support.rb +75 -0
  25. data/lib/shamu/auditing/transaction.rb +58 -0
  26. data/lib/shamu/auditing.rb +12 -0
  27. data/lib/shamu/entities/README.md +1 -0
  28. data/lib/shamu/entities/active_record.rb +123 -0
  29. data/lib/shamu/entities/active_record_soft_destroy.rb +91 -0
  30. data/lib/shamu/entities/entity.rb +196 -0
  31. data/lib/shamu/entities/entity_path.rb +87 -0
  32. data/lib/shamu/entities/identity_cache.rb +64 -0
  33. data/lib/shamu/entities/list.rb +54 -0
  34. data/lib/shamu/entities/list_scope/dates.rb +57 -0
  35. data/lib/shamu/entities/list_scope/paging.rb +51 -0
  36. data/lib/shamu/entities/list_scope/scoped_paging.rb +65 -0
  37. data/lib/shamu/entities/list_scope/sorting.rb +76 -0
  38. data/lib/shamu/entities/list_scope.rb +105 -0
  39. data/lib/shamu/entities/null_entity.rb +88 -0
  40. data/lib/shamu/entities.rb +11 -0
  41. data/lib/shamu/error.rb +23 -5
  42. data/lib/shamu/events/README.md +0 -0
  43. data/lib/shamu/events/active_record/channel.rb +36 -0
  44. data/lib/shamu/events/active_record/message.rb +52 -0
  45. data/lib/shamu/events/active_record/migration.rb +49 -0
  46. data/lib/shamu/events/active_record/runner.rb +28 -0
  47. data/lib/shamu/events/active_record/service.rb +174 -0
  48. data/lib/shamu/events/active_record.rb +13 -0
  49. data/lib/shamu/events/channel_stats.rb +23 -0
  50. data/lib/shamu/events/error.rb +24 -0
  51. data/lib/shamu/events/events_service.rb +136 -0
  52. data/lib/shamu/events/in_memory/async_service.rb +48 -0
  53. data/lib/shamu/events/in_memory/service.rb +97 -0
  54. data/lib/shamu/events/in_memory.rb +10 -0
  55. data/lib/shamu/events/message.rb +38 -0
  56. data/lib/shamu/events/support.rb +60 -0
  57. data/lib/shamu/events.rb +12 -0
  58. data/lib/shamu/features/README.md +0 -0
  59. data/lib/shamu/features/conditions/condition.rb +39 -0
  60. data/lib/shamu/features/conditions/env.rb +37 -0
  61. data/lib/shamu/features/conditions/hosts.rb +25 -0
  62. data/lib/shamu/features/conditions/matching.rb +16 -0
  63. data/lib/shamu/features/conditions/not_matching.rb +16 -0
  64. data/lib/shamu/features/conditions/percentage.rb +44 -0
  65. data/lib/shamu/features/conditions/proc.rb +54 -0
  66. data/lib/shamu/features/conditions/roles.rb +23 -0
  67. data/lib/shamu/features/conditions/schedule_at.rb +27 -0
  68. data/lib/shamu/features/conditions.rb +18 -0
  69. data/lib/shamu/features/config_service.rb +10 -0
  70. data/lib/shamu/features/context.rb +80 -0
  71. data/lib/shamu/features/env_store.rb +88 -0
  72. data/lib/shamu/features/errors.rb +29 -0
  73. data/lib/shamu/features/features_service.rb +168 -0
  74. data/lib/shamu/features/list_scope.rb +30 -0
  75. data/lib/shamu/features/selector.rb +50 -0
  76. data/lib/shamu/features/support.rb +51 -0
  77. data/lib/shamu/features/toggle.rb +149 -0
  78. data/lib/shamu/features/toggle_codec.rb +69 -0
  79. data/lib/shamu/features.rb +16 -0
  80. data/lib/shamu/locale/en.yml +22 -2
  81. data/lib/shamu/logger.rb +13 -0
  82. data/lib/shamu/rack/README.md +0 -0
  83. data/lib/shamu/rack/cookies.rb +115 -0
  84. data/lib/shamu/rack/cookies_middleware.rb +26 -0
  85. data/lib/shamu/rack/query_params.rb +41 -0
  86. data/lib/shamu/rack/query_params_middleware.rb +24 -0
  87. data/lib/shamu/rack.rb +12 -0
  88. data/lib/shamu/rails/controller.rb +131 -0
  89. data/lib/shamu/rails/entity.rb +168 -0
  90. data/lib/shamu/rails/features.rb +13 -0
  91. data/lib/shamu/rails/railtie.rb +30 -0
  92. data/lib/shamu/rails.rb +10 -0
  93. data/lib/shamu/rspec/matchers.rb +44 -0
  94. data/lib/shamu/rspec.rb +1 -0
  95. data/lib/shamu/security/README.md +0 -0
  96. data/lib/shamu/security/active_record_policy.rb +106 -0
  97. data/lib/shamu/security/error.rb +65 -0
  98. data/lib/shamu/security/hashed_value.rb +71 -0
  99. data/lib/shamu/security/no_policy.rb +15 -0
  100. data/lib/shamu/security/policy.rb +289 -0
  101. data/lib/shamu/security/policy_refinement.rb +50 -0
  102. data/lib/shamu/security/policy_rule.rb +59 -0
  103. data/lib/shamu/security/principal.rb +72 -0
  104. data/lib/shamu/security/roles.rb +62 -0
  105. data/lib/shamu/security/roles_service.rb +30 -0
  106. data/lib/shamu/security/support.rb +83 -0
  107. data/lib/shamu/security.rb +43 -0
  108. data/lib/shamu/services/README.md +2 -0
  109. data/lib/shamu/services/active_record.rb +58 -0
  110. data/lib/shamu/services/active_record_crud.rb +378 -0
  111. data/lib/shamu/services/error.rb +24 -0
  112. data/lib/shamu/services/lazy_association.rb +31 -0
  113. data/lib/shamu/services/lazy_transform.rb +97 -0
  114. data/lib/shamu/services/request.rb +122 -0
  115. data/lib/shamu/services/request_support.rb +124 -0
  116. data/lib/shamu/services/result.rb +75 -0
  117. data/lib/shamu/services/service.rb +355 -0
  118. data/lib/shamu/services.rb +12 -0
  119. data/lib/shamu/sessions/README.md +2 -0
  120. data/lib/shamu/sessions/cookie_store.rb +79 -0
  121. data/lib/shamu/sessions/session_store.rb +42 -0
  122. data/lib/shamu/sessions.rb +8 -0
  123. data/lib/shamu/to_bool_extension.rb +57 -0
  124. data/lib/shamu/to_model_id_extension.rb +50 -0
  125. data/lib/shamu/version.rb +10 -4
  126. data/lib/shamu.rb +18 -6
  127. data/shamu.gemspec +21 -10
  128. data/spec/internal/README.md +4 -0
  129. data/spec/internal/config/database.yml +3 -0
  130. data/spec/internal/config/routes.rb +3 -0
  131. data/spec/internal/db/schema.rb +3 -0
  132. data/spec/internal/log/.gitignore +1 -0
  133. data/spec/internal/public/favicon.ico +0 -0
  134. data/spec/lib/shamu/active_record_support.rb +32 -0
  135. data/spec/lib/shamu/attributes/assignment_spec.rb +129 -0
  136. data/spec/lib/shamu/attributes/equality_spec.rb +63 -0
  137. data/spec/lib/shamu/attributes/fluid_assignment_spec.rb +31 -0
  138. data/spec/lib/shamu/attributes/validation_spec.rb +53 -0
  139. data/spec/lib/shamu/attributes_spec.rb +331 -0
  140. data/spec/lib/shamu/auditing/logging_auditing_service_spec.rb +18 -0
  141. data/spec/lib/shamu/auditing/support_spec.rb +41 -0
  142. data/spec/lib/shamu/entities/active_record_soft_destroy_spec.rb +82 -0
  143. data/spec/lib/shamu/entities/active_record_spec.rb +66 -0
  144. data/spec/lib/shamu/entities/entity_path_spec.rb +40 -0
  145. data/spec/lib/shamu/entities/entity_spec.rb +56 -0
  146. data/spec/lib/shamu/entities/identity_cache_spec.rb +69 -0
  147. data/spec/lib/shamu/entities/list_scope/dates_spec.rb +47 -0
  148. data/spec/lib/shamu/entities/list_scope/paging_spec.rb +41 -0
  149. data/spec/lib/shamu/entities/list_scope/scoped_paging_spec.rb +40 -0
  150. data/spec/lib/shamu/entities/list_scope/sorting_spec.rb +59 -0
  151. data/spec/lib/shamu/entities/list_scope_spec.rb +127 -0
  152. data/spec/lib/shamu/entities/list_spec.rb +60 -0
  153. data/spec/lib/shamu/entities/null_entity_spec.rb +94 -0
  154. data/spec/lib/shamu/events/active_record/migration_spec.rb +11 -0
  155. data/spec/lib/shamu/events/active_record/service_spec.rb +139 -0
  156. data/spec/lib/shamu/events/events_service_spec.rb +57 -0
  157. data/spec/lib/shamu/events/in_memory/async_service_spec.rb +37 -0
  158. data/spec/lib/shamu/events/in_memory/service_spec.rb +36 -0
  159. data/spec/lib/shamu/events/message_spec.rb +7 -0
  160. data/spec/lib/shamu/events/support_spec.rb +44 -0
  161. data/spec/lib/shamu/features/conditions/condition_spec.rb +8 -0
  162. data/spec/lib/shamu/features/conditions/env_spec.rb +29 -0
  163. data/spec/lib/shamu/features/conditions/hosts_spec.rb +21 -0
  164. data/spec/lib/shamu/features/conditions/matching_spec.rb +23 -0
  165. data/spec/lib/shamu/features/conditions/percentage_spec.rb +71 -0
  166. data/spec/lib/shamu/features/conditions/proc_spec.rb +28 -0
  167. data/spec/lib/shamu/features/env_store_spec.rb +48 -0
  168. data/spec/lib/shamu/features/features.yml +34 -0
  169. data/spec/lib/shamu/features/features_service_spec.rb +109 -0
  170. data/spec/lib/shamu/features/secondary.yml +5 -0
  171. data/spec/lib/shamu/features/selector_spec.rb +17 -0
  172. data/spec/lib/shamu/features/support_spec.rb +45 -0
  173. data/spec/lib/shamu/features/toggle_codec_spec.rb +28 -0
  174. data/spec/lib/shamu/features/toggle_spec.rb +42 -0
  175. data/spec/lib/shamu/rack/cookies_middleware_spec.rb +33 -0
  176. data/spec/lib/shamu/rack/cookies_spec.rb +43 -0
  177. data/spec/lib/shamu/rack/query_params_middleware_spec.rb +33 -0
  178. data/spec/lib/shamu/rack/query_params_spec.rb +23 -0
  179. data/spec/lib/shamu/rails/controller_spec.rb +74 -0
  180. data/spec/lib/shamu/rails/entity_spec.rb +150 -0
  181. data/spec/lib/shamu/rails/features.yml +13 -0
  182. data/spec/lib/shamu/rails/features_spec.rb +45 -0
  183. data/spec/lib/shamu/security/active_record_policy_spec.rb +38 -0
  184. data/spec/lib/shamu/security/hashed_value_spec.rb +41 -0
  185. data/spec/lib/shamu/security/policy_refinement_spec.rb +61 -0
  186. data/spec/lib/shamu/security/policy_rule_spec.rb +60 -0
  187. data/spec/lib/shamu/security/policy_spec.rb +158 -0
  188. data/spec/lib/shamu/security/roles_spec.rb +46 -0
  189. data/spec/lib/shamu/services/active_record_crud_spec.rb +460 -0
  190. data/spec/lib/shamu/services/active_record_spec.rb +92 -0
  191. data/spec/lib/shamu/services/lazy_association_spec.rb +31 -0
  192. data/spec/lib/shamu/services/lazy_transform_spec.rb +96 -0
  193. data/spec/lib/shamu/services/request_spec.rb +58 -0
  194. data/spec/lib/shamu/services/request_support_spec.rb +129 -0
  195. data/spec/lib/shamu/services/result_spec.rb +37 -0
  196. data/spec/lib/shamu/services/service_spec.rb +307 -0
  197. data/spec/lib/shamu/sessions/cookie_store_spec.rb +44 -0
  198. data/spec/lib/shamu/to_bool_extension_spec.rb +67 -0
  199. data/spec/lib/shamu/to_model_id_extension_spec.rb +54 -0
  200. data/spec/rails_helper.rb +13 -0
  201. data/spec/spec_helper.rb +17 -12
  202. data/spec/support/active_record.rb +17 -0
  203. data/spec/support/database.rb +14 -0
  204. data/spec/support/logger.rb +0 -0
  205. metadata +383 -9
  206. data/spec/lib/shamu_spec.rb +0 -5
  207. /data/{spec/internal/log/test.log → lib/shamu/attributes/README.md} +0 -0
@@ -0,0 +1,307 @@
1
+ require "spec_helper"
2
+ require "shamu/services"
3
+
4
+ module ServiceSpec
5
+ class Service < Shamu::Services::Service
6
+ include Shamu::Services::RequestSupport
7
+
8
+ def process( params )
9
+ with_request( params, Request::Change ) do |request|
10
+ request_hook
11
+ next error( :base, "nope" ) if request.level < 0
12
+
13
+ record = OpenStruct.new( request.to_attributes )
14
+ scorpion.fetch ServiceSpec::Entity, { record: record }, {}
15
+ end
16
+ end
17
+
18
+ def request_hook
19
+ end
20
+
21
+ public :entity_lookup_list
22
+ public :entity_list
23
+ public :find_by_lookup
24
+ public :cached_lookup
25
+ public :lookup_association
26
+ public :lazy_association
27
+
28
+ def build_entity( record, records = nil )
29
+ scorpion.fetch ServiceSpec::Entity, { record: record }, {}
30
+ end
31
+ end
32
+
33
+ module Request
34
+ class Change < Shamu::Services::Request
35
+ attribute :level
36
+ attribute :amount, presence: true
37
+ end
38
+ end
39
+
40
+ class Entity < Shamu::Entities::Entity
41
+ model :record
42
+ attribute :id, on: :record
43
+ attribute :level, on: :record
44
+ attribute :amount, on: :record
45
+ end
46
+
47
+ NullEntity = Shamu::Entities::NullEntity.for( Entity )
48
+ end
49
+
50
+ describe Shamu::Services::Service do
51
+
52
+ let( :service ) { scorpion.new ServiceSpec::Service }
53
+
54
+ describe "#entity_list" do
55
+ it "maps each record" do
56
+ expect do |b|
57
+ list = service.entity_list [ double ], &b
58
+ list.to_a
59
+ end.to yield_control
60
+ end
61
+
62
+ it "returns an entity list" do
63
+ expect( service.entity_list( [] ) ).to be_a Shamu::Entities::List
64
+ end
65
+
66
+ it "invokes build_entity if no transformer provided" do
67
+ expect( service ).to receive( :build_entity ).and_call_original
68
+ list = service.entity_list( [{}] )
69
+ list.first
70
+ end
71
+ end
72
+
73
+ describe "#find_by_lookup" do
74
+
75
+ it "raises not found if entity is missing" do
76
+ allow( service ).to receive( :lookup ).and_return [ nil ]
77
+ expect do
78
+ service.send :find_by_lookup, 9
79
+ end.to raise_error Shamu::NotFoundError
80
+ end
81
+
82
+ it "raises not found if enitty is a NullEntity" do
83
+ entity = Shamu::Entities::NullEntity.for( Shamu::Entities::Entity ).new
84
+ allow( service ).to receive( :lookup ).and_return [ entity ]
85
+ expect do
86
+ service.send :find_by_lookup, 9
87
+ end.to raise_error Shamu::NotFoundError
88
+ end
89
+
90
+
91
+ it "returns a single entity if founds" do
92
+ expect( service ).to receive( :lookup ).and_return [ double( id: 1 ) ]
93
+ expect( service.send( :find_by_lookup, 1 ) ).to be_present
94
+ end
95
+ end
96
+
97
+ describe "#entity_lookup_list" do
98
+ let( :record ) { double id: 5, amount: 17 }
99
+ let( :records ) { [ record ] }
100
+
101
+ it "yields for a matching id" do
102
+ expect do |b|
103
+ service.entity_lookup_list( records, [record.id], ServiceSpec::NullEntity, &b )
104
+ end.to yield_control
105
+ end
106
+
107
+ it "returns an Entities::List" do
108
+ expect( service.entity_lookup_list( [], [], ServiceSpec::NullEntity ) ).to be_a Shamu::Entities::List
109
+ end
110
+
111
+ it "matches on id by default" do
112
+ list = service.entity_lookup_list( records, [record.id], ServiceSpec::NullEntity ) do |r|
113
+ scorpion.fetch ServiceSpec::Entity, { record: r }, {}
114
+ end
115
+
116
+ expect( list.first ).to be_present
117
+ end
118
+
119
+ it "matches on id with string numbers" do
120
+ list = service.entity_lookup_list( records, [record.id.to_s], ServiceSpec::NullEntity ) do |r|
121
+ scorpion.fetch ServiceSpec::Entity, { record: r }, {}
122
+ end
123
+
124
+ expect( list.first ).to be_present
125
+ end
126
+
127
+ it "matches on a custom field" do
128
+ list = service.entity_lookup_list( records, [record.amount], ServiceSpec::NullEntity, match: :amount ) do |r|
129
+ scorpion.fetch ServiceSpec::Entity, { record: r }, {}
130
+ end
131
+
132
+ expect( list.first ).to be_present
133
+ end
134
+
135
+ it "matches with a custom proc" do
136
+ matcher = ->( record ) { record.amount }
137
+ list = service.entity_lookup_list( records, [record.amount], ServiceSpec::NullEntity, match: matcher ) do |r|
138
+ scorpion.fetch ServiceSpec::Entity, { record: r }, {}
139
+ end
140
+
141
+ expect( list.first ).to be_present
142
+ end
143
+
144
+ it "returns a NullEntity for a non-matching id" do
145
+ list = service.entity_lookup_list( records, [10], ServiceSpec::NullEntity )
146
+ expect( list.first ).to be_a ServiceSpec::NullEntity
147
+ expect( list.first.id ).to eq 10
148
+ end
149
+
150
+ it "returns a NullEntity for a non-matching custom field" do
151
+ list = service.entity_lookup_list( records, [192], ServiceSpec::NullEntity, match: :amount )
152
+ expect( list.first ).to be_a ServiceSpec::NullEntity
153
+ end
154
+
155
+ it "returns a NullEntity for a non-matching custom matcher" do
156
+ matcher = ->( record ) { record.amount % 17 }
157
+
158
+ list = service.entity_lookup_list( records, [192], ServiceSpec::NullEntity, match: matcher )
159
+ expect( list.first ).to be_a ServiceSpec::NullEntity
160
+ end
161
+ end
162
+
163
+ describe "#cached_lookup" do
164
+
165
+ context "found entities" do
166
+ let( :entities ) do
167
+ Shamu::Entities::List.new [
168
+ ServiceSpec::Entity.new( id: 5, level: 11, amount: "More" ),
169
+ ServiceSpec::Entity.new( id: 11, level: 5, amount: "Less" )
170
+ ]
171
+ end
172
+
173
+ let( :records ) do
174
+ [
175
+ OpenStruct.new( id: 5 ),
176
+ OpenStruct.new( id: 11 )
177
+ ]
178
+ end
179
+ let( :transformer ) { ->(record) { entities.get( record.id ) } }
180
+ let( :lookup ) { ->(ids) { ids.map { |id| entities.get( id ) } } }
181
+
182
+ it "uses existing entities" do
183
+ service.cached_lookup [ 5 ], &lookup
184
+
185
+ expect do |b|
186
+ service.cached_lookup [ 5 ], &b
187
+ end.not_to yield_control
188
+ end
189
+
190
+ it "calls build for uncached entities" do
191
+ service.cached_lookup [ 5 ], &lookup
192
+
193
+ expect do |b|
194
+ service.cached_lookup [ 5, 11 ] do |missing_ids|
195
+ b.to_proc.call( missing_ids )
196
+ lookup.call( missing_ids )
197
+ end
198
+ end.to yield_with_args( [ 11 ] )
199
+ end
200
+
201
+ it "it handles custom matcher" do
202
+ lookup = ->( amounts ) { entities.select { |e| amounts.include?( e.amount ) } }
203
+
204
+ service.cached_lookup [ "More" ], match: :amount, &lookup
205
+
206
+ expect do |b|
207
+ service.cached_lookup [ "More", "Less" ], match: :amount do |missing_amounts|
208
+ b.to_proc.call( missing_amounts )
209
+ lookup.call( missing_amounts )
210
+ end
211
+ end.to yield_with_args( [ "Less" ] )
212
+ end
213
+
214
+ it "it handles custom match block" do
215
+ match = ->( e ) { e.amount.downcase }
216
+ service.cached_lookup ["more"], match: match do |missing_amounts|
217
+ entities.select { |e| missing_amounts.include?( e.amount.downcase ) }
218
+ end
219
+
220
+ expect( service.cached_lookup( ["more"], match: match ).first.amount ).to eq "More"
221
+ end
222
+
223
+ end
224
+
225
+ context "missing entiites" do
226
+ it "caches id" do
227
+ service.cached_lookup( [1] ) do |ids|
228
+ ids.map { |id| Shamu::Entities::NullEntity.for( ServiceSpec::Entity ).new( id: id ) }
229
+ end
230
+
231
+ expect( service.cached_lookup( [1] ).first ).to be_a Shamu::Entities::NullEntity
232
+ end
233
+
234
+ it "caches custom matcher" do
235
+ service.cached_lookup( [1], match: :level ) do |ids|
236
+ ids.map { |id| Shamu::Entities::NullEntity.for( ServiceSpec::Entity ).new( id: id ) }
237
+ end
238
+
239
+ expect( service.cached_lookup( [1], match: :level ).first ).to be_a Shamu::Entities::NullEntity
240
+ end
241
+
242
+ it "caches custom match block" do
243
+ match = ->( e ) { e.id * 2 }
244
+
245
+ service.cached_lookup( [1], match: match ) do |ids|
246
+ ids.map { |id| Shamu::Entities::NullEntity.for( ServiceSpec::Entity ).new( id: id ) }
247
+ end
248
+
249
+ expect( service.cached_lookup( [1], match: match ).first ).to be_a Shamu::Entities::NullEntity
250
+ end
251
+ end
252
+ end
253
+
254
+ describe "#lookup_association" do
255
+ before( :each ) do
256
+ allow( service ).to receive( :lookup ) do |*ids|
257
+ ids.map { |id| ServiceSpec::Entity.null_entity.new( id: id ) }
258
+ end
259
+ end
260
+
261
+ it "returns nil if id is nil" do
262
+ expect( service.lookup_association( nil, service ) ).to be_nil
263
+ end
264
+
265
+ it "yields to get all association links" do
266
+ expect do |b|
267
+ service.lookup_association( 1, service, &b )
268
+ end.to yield_control
269
+ end
270
+
271
+ it "finds assocation from cache" do
272
+ service.lookup_association( 1, service ) do
273
+ [ 1, 2 ]
274
+ end
275
+
276
+ expect do |b|
277
+ service.lookup_association( 1, service, &b )
278
+ end.not_to yield_control
279
+ end
280
+
281
+ it "returns the found entity with no records" do
282
+ result = service.lookup_association( 1, service )
283
+ expect( result ).to be_a ServiceSpec::Entity
284
+ end
285
+
286
+ it "returns the found entity with bulk records" do
287
+ result = service.lookup_association( 1, service ) do
288
+ [ 1, 2 ]
289
+ end
290
+ expect( result ).to be_a ServiceSpec::Entity
291
+ end
292
+ end
293
+
294
+ describe "#lazy_association" do
295
+ before( :each ) do
296
+ allow( service ).to receive( :lookup ) do |*ids|
297
+ ids.map { |id| ServiceSpec::Entity.null_entity.new( id: id ) }
298
+ end
299
+ end
300
+
301
+ it "gets a lazy association" do
302
+ expect( service.lazy_association( 1, service ) ).to be_a Shamu::Services::LazyAssociation
303
+ end
304
+
305
+
306
+ end
307
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe Shamu::Sessions::CookieStore do
4
+
5
+ hunt( :cookies, Shamu::Rack::Cookies )
6
+ let( :store ) { scorpion.new Shamu::Sessions::CookieStore }
7
+
8
+ it "stores signed cookies" do
9
+ expect( cookies ).to receive( :set ).with( "setting", hash_including( value: /[0-9a-f]{40}\$example/ ) )
10
+ expect( store ).to receive( :hash_value ).and_call_original
11
+
12
+ store.set( "setting", "example" )
13
+ end
14
+
15
+ it "reads signed cookies" do
16
+ hashed = store.send( :hash_value, "example" )
17
+ expect( cookies ).to receive( :key? ).with( "setting" ).and_return true
18
+ expect( cookies ).to receive( :get ).with( "setting" ).and_return hashed
19
+ expect( store ).to receive( :verify_hash ).and_call_original
20
+
21
+ expect( store.fetch( "setting" ) ).to eq "example"
22
+ end
23
+
24
+ it "yields if cookie does not exist" do
25
+ expect( cookies ).to receive( :key? ).with( "setting" ).and_return false
26
+
27
+ expect do |b|
28
+ store.fetch( "setting", &b )
29
+ end.to yield_control
30
+ end
31
+
32
+ it "ignores unsigned cookies" do
33
+ expect( cookies ).to receive( :key? ).with( "setting" ).and_return true
34
+ expect( cookies ).to receive( :get ).with( "setting" ).and_return "example"
35
+ expect( store ).to receive( :verify_hash ).and_call_original
36
+
37
+ expect( store.fetch( "setting" ) ).to eq nil
38
+ end
39
+
40
+ it "deletes a cookie" do
41
+ expect( cookies ).to receive( :delete )
42
+ store.delete( "setting" )
43
+ end
44
+ end
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+
3
+
4
+ describe Shamu::ToBoolExtension do
5
+ before( :each ) do
6
+ Shamu::ToBoolExtension.extend!
7
+ end
8
+
9
+ describe Shamu::ToBoolExtension::Strings do
10
+ {
11
+ "1" => true,
12
+ "0" => false,
13
+ "true" => true,
14
+ "false" => false,
15
+ "" => false,
16
+ "True" => true,
17
+ "False" => false,
18
+ "T" => true,
19
+ "F" => false,
20
+ "Yes" => true,
21
+ "No" => false,
22
+ "Y" => true,
23
+ "N" => false,
24
+ }.each do |candidate, expected|
25
+ it "converts '#{ candidate }' to #{ expected }" do
26
+ expect( candidate.to_bool ).to eq expected
27
+ end
28
+ end
29
+
30
+ it "uses default for no-match" do
31
+ expect( "Random".to_bool( :nope ) ).to eq :nope
32
+ end
33
+ end
34
+
35
+ describe Shamu::ToBoolExtension::Integers do
36
+ {
37
+ 1 => true,
38
+ 0 => false
39
+ }.each do |candidate, expected|
40
+ it "converts '#{ candidate }' to #{ expected }" do
41
+ expect( candidate.to_bool ).to eq expected
42
+ end
43
+ end
44
+
45
+ it "uses default for no-match" do
46
+ expect( 100.to_bool( :nope ) ).to eq :nope
47
+ end
48
+ end
49
+
50
+ describe Shamu::ToBoolExtension::Boolean do
51
+ {
52
+ true => true,
53
+ false => false,
54
+ }.each do |candidate, expected|
55
+ it "converts '#{ candidate }' to #{ expected }" do
56
+ expect( candidate.to_bool ).to eq expected
57
+ end
58
+ end
59
+ end
60
+
61
+ describe Shamu::ToBoolExtension::Nil do
62
+ it "uses default" do
63
+ expect( nil.to_bool( :whatever ) ).to eq( :whatever )
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe Shamu::ToModelIdExtension do
4
+ before( :each ) do
5
+ Shamu::ToModelIdExtension.extend!
6
+ end
7
+
8
+ describe Shamu::ToModelIdExtension::Strings do
9
+
10
+ {
11
+ "7432" => 7432,
12
+ " 123" => 123,
13
+ "one" => nil,
14
+ "99 bottles" => nil
15
+ }.each do |candidate, expected|
16
+ it "converts '#{ candidate }' to #{ expected }" do
17
+ expect( candidate.to_model_id ).to eq expected
18
+ end
19
+ end
20
+ end
21
+
22
+ describe Shamu::ToModelIdExtension::Integers do
23
+ it "returns self for an integer" do
24
+ expect( 789.to_model_id ).to eq 789
25
+ end
26
+ end
27
+
28
+ describe Shamu::ToModelIdExtension::Enumerables do
29
+ it "maps array to ids" do
30
+ expect( [ 567 ].to_model_id ).to eq [ 567 ]
31
+ end
32
+ end
33
+
34
+ describe Shamu::ToModelIdExtension::Models do
35
+ use_active_record
36
+
37
+ it "ActiveRecord instances returns their id" do
38
+ instance = ActiveRecordSpec::Favorite.new( id: 48 )
39
+ expect( instance ).to receive( :id ).at_least( :once ).and_call_original
40
+ expect( instance.to_model_id ).to eq instance.id
41
+ end
42
+
43
+ it "Entities instances returns their id" do
44
+ klass = Class.new( Shamu::Entities::Entity ) do
45
+ attribute :id
46
+ end
47
+
48
+ instance = klass.new id: 491
49
+ expect( instance ).to receive( :id ).at_least( :once ).and_call_original
50
+ expect( instance.to_model_id ).to eq instance.id
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ require "combustion"
4
+
5
+ Combustion.initialize! :action_controller do
6
+ require "shamu/rails"
7
+ end
8
+
9
+ require "rspec/rails"
10
+
11
+ RSpec.configure do |config|
12
+ config.use_transactional_fixtures = true
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,18 @@
1
- require 'simplecov'
2
- if ENV['COVERAGE']
1
+ require "simplecov"
2
+ if ENV[ "COVERAGE" ]
3
3
  require "codeclimate-test-reporter"
4
4
  CodeClimate::TestReporter.start
5
5
  else
6
6
  SimpleCov.start
7
7
  end
8
- require 'pry'
9
- require 'bundler/setup'
8
+ require "pry"
9
+ require "bundler/setup"
10
10
 
11
- require 'shamu'
11
+ require "shamu"
12
+ require "shamu/rspec"
13
+ require "scorpion/rspec"
14
+ require "rspec/wait"
15
+ require "rspec/its"
12
16
 
13
17
  root_path = File.expand_path( "../..", __FILE__ )
14
18
 
@@ -20,16 +24,17 @@ RSpec.configure do |config|
20
24
 
21
25
  config.order = "random"
22
26
 
27
+ config.filter_gems_from_backtrace "activesupport", "actionpack", "actionview", "scorpion-ioc", "rspec-wait"
28
+
23
29
  config.filter_run focus: true
24
30
  config.filter_run_excluding :broken => true
25
31
  config.run_all_when_everything_filtered = true
26
32
 
27
- config.before(:each) { GC.disable }
28
- config.after(:each) { GC.enable }
33
+ config.include Scorpion::Rspec::Helper
34
+ config.extend Support::ActiveRecord
29
35
 
30
- config.before( :each, type: :model ) do
31
- [ Todo, Author ].each do |model|
32
- model.destroy_all
33
- end
36
+ config.before(:all) do
37
+ Shamu::Security.private_key = SecureRandom.base64( 128 )
34
38
  end
35
- end
39
+
40
+ end
@@ -0,0 +1,17 @@
1
+ module Support
2
+ module ActiveRecord
3
+ def use_active_record
4
+ require "active_record"
5
+ require_relative "../lib/shamu/active_record_support"
6
+
7
+ before( :each ) do
8
+ ActiveRecordSpec::FavoriteMigration.verbose = false
9
+ ActiveRecordSpec::FavoriteMigration.up
10
+ end
11
+
12
+ after( :each ) do
13
+ ActiveRecordSpec::FavoriteMigration.down
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require "active_record"
2
+
3
+ # Prevent kaminari warning since we're not using a framework
4
+ module Sinatra
5
+ end
6
+
7
+ require "kaminari"
8
+ Kaminari::Hooks.init
9
+
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ adapter: "sqlite3",
13
+ database: "test.db"
14
+ )
File without changes