hoodoo 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (216) hide show
  1. checksums.yaml +7 -0
  2. data/bin/hoodoo +5 -0
  3. data/lib/hoodoo.rb +27 -0
  4. data/lib/hoodoo/active.rb +32 -0
  5. data/lib/hoodoo/active/active_model/uuid_validator.rb +45 -0
  6. data/lib/hoodoo/active/active_record/base.rb +81 -0
  7. data/lib/hoodoo/active/active_record/creator.rb +134 -0
  8. data/lib/hoodoo/active/active_record/dated.rb +343 -0
  9. data/lib/hoodoo/active/active_record/error_mapping.rb +351 -0
  10. data/lib/hoodoo/active/active_record/finder.rb +606 -0
  11. data/lib/hoodoo/active/active_record/search_helper.rb +189 -0
  12. data/lib/hoodoo/active/active_record/secure.rb +431 -0
  13. data/lib/hoodoo/active/active_record/support.rb +106 -0
  14. data/lib/hoodoo/active/active_record/translated.rb +87 -0
  15. data/lib/hoodoo/active/active_record/uuid.rb +80 -0
  16. data/lib/hoodoo/active/active_record/writer.rb +321 -0
  17. data/lib/hoodoo/client.rb +23 -0
  18. data/lib/hoodoo/client/augmented_array.rb +29 -0
  19. data/lib/hoodoo/client/augmented_base.rb +168 -0
  20. data/lib/hoodoo/client/augmented_hash.rb +23 -0
  21. data/lib/hoodoo/client/client.rb +354 -0
  22. data/lib/hoodoo/client/endpoint/endpoint.rb +427 -0
  23. data/lib/hoodoo/client/endpoint/endpoints/amqp.rb +180 -0
  24. data/lib/hoodoo/client/endpoint/endpoints/auto_session.rb +194 -0
  25. data/lib/hoodoo/client/endpoint/endpoints/http.rb +203 -0
  26. data/lib/hoodoo/client/endpoint/endpoints/http_based.rb +367 -0
  27. data/lib/hoodoo/client/endpoint/endpoints/not_found.rb +59 -0
  28. data/lib/hoodoo/client/headers.rb +269 -0
  29. data/lib/hoodoo/communicators.rb +23 -0
  30. data/lib/hoodoo/communicators/fast.rb +44 -0
  31. data/lib/hoodoo/communicators/pool.rb +601 -0
  32. data/lib/hoodoo/communicators/slow.rb +84 -0
  33. data/lib/hoodoo/data.rb +51 -0
  34. data/lib/hoodoo/data/resources/caller.rb +39 -0
  35. data/lib/hoodoo/data/resources/errors.rb +28 -0
  36. data/lib/hoodoo/data/resources/log.rb +31 -0
  37. data/lib/hoodoo/data/resources/session.rb +26 -0
  38. data/lib/hoodoo/data/types/error_primitive.rb +27 -0
  39. data/lib/hoodoo/data/types/permissions.rb +40 -0
  40. data/lib/hoodoo/data/types/permissions_defaults.rb +32 -0
  41. data/lib/hoodoo/data/types/permissions_full.rb +28 -0
  42. data/lib/hoodoo/data/types/permissions_resources.rb +31 -0
  43. data/lib/hoodoo/discovery.rb +20 -0
  44. data/lib/hoodoo/errors.rb +19 -0
  45. data/lib/hoodoo/errors/error_descriptions.rb +229 -0
  46. data/lib/hoodoo/errors/errors.rb +322 -0
  47. data/lib/hoodoo/generator.rb +139 -0
  48. data/lib/hoodoo/logger.rb +23 -0
  49. data/lib/hoodoo/logger/fast_writer.rb +27 -0
  50. data/lib/hoodoo/logger/flattener_mixin.rb +36 -0
  51. data/lib/hoodoo/logger/logger.rb +387 -0
  52. data/lib/hoodoo/logger/slow_writer.rb +49 -0
  53. data/lib/hoodoo/logger/writer_mixin.rb +52 -0
  54. data/lib/hoodoo/logger/writers/file_writer.rb +45 -0
  55. data/lib/hoodoo/logger/writers/log_entries_dot_com_writer.rb +64 -0
  56. data/lib/hoodoo/logger/writers/stream_writer.rb +43 -0
  57. data/lib/hoodoo/middleware.rb +33 -0
  58. data/lib/hoodoo/presenters.rb +45 -0
  59. data/lib/hoodoo/presenters/base.rb +281 -0
  60. data/lib/hoodoo/presenters/base_dsl.rb +519 -0
  61. data/lib/hoodoo/presenters/common_resource_fields.rb +31 -0
  62. data/lib/hoodoo/presenters/embedding.rb +232 -0
  63. data/lib/hoodoo/presenters/types/array.rb +118 -0
  64. data/lib/hoodoo/presenters/types/boolean.rb +26 -0
  65. data/lib/hoodoo/presenters/types/date.rb +26 -0
  66. data/lib/hoodoo/presenters/types/date_time.rb +26 -0
  67. data/lib/hoodoo/presenters/types/decimal.rb +47 -0
  68. data/lib/hoodoo/presenters/types/enum.rb +55 -0
  69. data/lib/hoodoo/presenters/types/field.rb +158 -0
  70. data/lib/hoodoo/presenters/types/float.rb +26 -0
  71. data/lib/hoodoo/presenters/types/hash.rb +361 -0
  72. data/lib/hoodoo/presenters/types/integer.rb +26 -0
  73. data/lib/hoodoo/presenters/types/object.rb +117 -0
  74. data/lib/hoodoo/presenters/types/string.rb +53 -0
  75. data/lib/hoodoo/presenters/types/tags.rb +24 -0
  76. data/lib/hoodoo/presenters/types/text.rb +26 -0
  77. data/lib/hoodoo/presenters/types/uuid.rb +54 -0
  78. data/lib/hoodoo/services.rb +34 -0
  79. data/lib/hoodoo/services/discovery/discoverers/by_consul.rb +66 -0
  80. data/lib/hoodoo/services/discovery/discoverers/by_convention.rb +173 -0
  81. data/lib/hoodoo/services/discovery/discoverers/by_drb/by_drb.rb +195 -0
  82. data/lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb +166 -0
  83. data/lib/hoodoo/services/discovery/discoverers/by_drb/drb_server_start.rb +37 -0
  84. data/lib/hoodoo/services/discovery/discovery.rb +186 -0
  85. data/lib/hoodoo/services/discovery/results/for_amqp.rb +58 -0
  86. data/lib/hoodoo/services/discovery/results/for_http.rb +85 -0
  87. data/lib/hoodoo/services/discovery/results/for_local.rb +85 -0
  88. data/lib/hoodoo/services/discovery/results/for_remote.rb +57 -0
  89. data/lib/hoodoo/services/middleware/amqp_log_message.rb +186 -0
  90. data/lib/hoodoo/services/middleware/amqp_log_writer.rb +119 -0
  91. data/lib/hoodoo/services/middleware/endpoints/inter_resource_local.rb +130 -0
  92. data/lib/hoodoo/services/middleware/endpoints/inter_resource_remote.rb +202 -0
  93. data/lib/hoodoo/services/middleware/exception_reporting/base_reporter.rb +105 -0
  94. data/lib/hoodoo/services/middleware/exception_reporting/exception_reporting.rb +115 -0
  95. data/lib/hoodoo/services/middleware/exception_reporting/reporters/airbrake_reporter.rb +64 -0
  96. data/lib/hoodoo/services/middleware/exception_reporting/reporters/raygun_reporter.rb +63 -0
  97. data/lib/hoodoo/services/middleware/interaction.rb +127 -0
  98. data/lib/hoodoo/services/middleware/middleware.rb +2705 -0
  99. data/lib/hoodoo/services/middleware/rack_monkey_patch.rb +73 -0
  100. data/lib/hoodoo/services/services/context.rb +153 -0
  101. data/lib/hoodoo/services/services/implementation.rb +132 -0
  102. data/lib/hoodoo/services/services/interface.rb +934 -0
  103. data/lib/hoodoo/services/services/permissions.rb +250 -0
  104. data/lib/hoodoo/services/services/request.rb +189 -0
  105. data/lib/hoodoo/services/services/response.rb +316 -0
  106. data/lib/hoodoo/services/services/service.rb +141 -0
  107. data/lib/hoodoo/services/services/session.rb +729 -0
  108. data/lib/hoodoo/utilities.rb +12 -0
  109. data/lib/hoodoo/utilities/string_inquirer.rb +54 -0
  110. data/lib/hoodoo/utilities/utilities.rb +380 -0
  111. data/lib/hoodoo/utilities/uuid.rb +44 -0
  112. data/lib/hoodoo/version.rb +17 -0
  113. data/spec/active/active_record/base_spec.rb +57 -0
  114. data/spec/active/active_record/creator_spec.rb +88 -0
  115. data/spec/active/active_record/dated_spec.rb +248 -0
  116. data/spec/active/active_record/error_mapping_spec.rb +360 -0
  117. data/spec/active/active_record/finder_spec.rb +744 -0
  118. data/spec/active/active_record/search_helper_spec.rb +384 -0
  119. data/spec/active/active_record/secure_spec.rb +435 -0
  120. data/spec/active/active_record/support_spec.rb +225 -0
  121. data/spec/active/active_record/translated_spec.rb +19 -0
  122. data/spec/active/active_record/uuid_spec.rb +72 -0
  123. data/spec/active/active_record/writer_spec.rb +272 -0
  124. data/spec/alchemy/alchemy-amq.rb +33 -0
  125. data/spec/client/augmented_array_spec.rb +15 -0
  126. data/spec/client/augmented_base_spec.rb +50 -0
  127. data/spec/client/augmented_hash_spec.rb +15 -0
  128. data/spec/client/client_spec.rb +955 -0
  129. data/spec/client/endpoint/endpoint_spec.rb +70 -0
  130. data/spec/client/endpoint/endpoints/amqp_spec.rb +16 -0
  131. data/spec/client/endpoint/endpoints/auto_session_spec.rb +9 -0
  132. data/spec/client/endpoint/endpoints/http_based_spec.rb +9 -0
  133. data/spec/client/endpoint/endpoints/http_spec.rb +103 -0
  134. data/spec/client/endpoint/endpoints/not_found_spec.rb +35 -0
  135. data/spec/client/headers_spec.rb +172 -0
  136. data/spec/communicators/fast_spec.rb +9 -0
  137. data/spec/communicators/pool_spec.rb +339 -0
  138. data/spec/communicators/slow_spec.rb +15 -0
  139. data/spec/data/resources/caller_spec.rb +156 -0
  140. data/spec/data/resources/errors_spec.rb +22 -0
  141. data/spec/data/resources/log_spec.rb +20 -0
  142. data/spec/data/resources/session_spec.rb +15 -0
  143. data/spec/data/types/error_primitive_spec.rb +15 -0
  144. data/spec/data/types/permissions_defaults_spec.rb +25 -0
  145. data/spec/data/types/permissions_full_spec.rb +44 -0
  146. data/spec/data/types/permissions_resources_spec.rb +34 -0
  147. data/spec/data/types/permissions_spec.rb +37 -0
  148. data/spec/errors/error_descriptions_spec.rb +98 -0
  149. data/spec/errors/errors_spec.rb +346 -0
  150. data/spec/integration/service_actions_spec.rb +112 -0
  151. data/spec/logger/fast_writer_spec.rb +18 -0
  152. data/spec/logger/logger_spec.rb +259 -0
  153. data/spec/logger/slow_writer_spec.rb +144 -0
  154. data/spec/logger/writers/file_writer_spec.rb +37 -0
  155. data/spec/logger/writers/log_entries_dot_com_writer_spec.rb +29 -0
  156. data/spec/logger/writers/stream_writer_spec.rb +38 -0
  157. data/spec/presenters/base_dsl_spec.rb +111 -0
  158. data/spec/presenters/base_spec.rb +871 -0
  159. data/spec/presenters/common_resource_fields_spec.rb +30 -0
  160. data/spec/presenters/embedding_spec.rb +87 -0
  161. data/spec/presenters/types/array_spec.rb +249 -0
  162. data/spec/presenters/types/boolean_spec.rb +51 -0
  163. data/spec/presenters/types/date_spec.rb +57 -0
  164. data/spec/presenters/types/date_time_spec.rb +59 -0
  165. data/spec/presenters/types/decimal_spec.rb +58 -0
  166. data/spec/presenters/types/enum_spec.rb +71 -0
  167. data/spec/presenters/types/field_spec.rb +77 -0
  168. data/spec/presenters/types/float_spec.rb +50 -0
  169. data/spec/presenters/types/hash_spec.rb +1069 -0
  170. data/spec/presenters/types/integer_spec.rb +50 -0
  171. data/spec/presenters/types/object_spec.rb +177 -0
  172. data/spec/presenters/types/string_spec.rb +65 -0
  173. data/spec/presenters/types/tags_spec.rb +56 -0
  174. data/spec/presenters/types/text_spec.rb +50 -0
  175. data/spec/presenters/types/uuid_spec.rb +46 -0
  176. data/spec/presenters/walk_spec.rb +198 -0
  177. data/spec/services/discovery/discoverers/by_consul_spec.rb +29 -0
  178. data/spec/services/discovery/discoverers/by_convention_spec.rb +67 -0
  179. data/spec/services/discovery/discoverers/by_drb/by_drb_spec.rb +80 -0
  180. data/spec/services/discovery/discoverers/by_drb/drb_server_spec.rb +205 -0
  181. data/spec/services/discovery/discovery_spec.rb +73 -0
  182. data/spec/services/discovery/results/for_amqp_spec.rb +17 -0
  183. data/spec/services/discovery/results/for_http_spec.rb +37 -0
  184. data/spec/services/discovery/results/for_local_spec.rb +21 -0
  185. data/spec/services/discovery/results/for_remote_spec.rb +15 -0
  186. data/spec/services/middleware/amqp_log_message_spec.rb +60 -0
  187. data/spec/services/middleware/amqp_log_writer_spec.rb +95 -0
  188. data/spec/services/middleware/endpoints/inter_resource_local_spec.rb +9 -0
  189. data/spec/services/middleware/endpoints/inter_resource_remote_spec.rb +9 -0
  190. data/spec/services/middleware/exception_reporting/base_reporter_spec.rb +16 -0
  191. data/spec/services/middleware/exception_reporting/exception_reporting_spec.rb +92 -0
  192. data/spec/services/middleware/exception_reporting/reporters/airbrake_reporter_spec.rb +24 -0
  193. data/spec/services/middleware/exception_reporting/reporters/raygun_reporter_spec.rb +23 -0
  194. data/spec/services/middleware/middleware_cors_spec.rb +93 -0
  195. data/spec/services/middleware/middleware_create_update_spec.rb +489 -0
  196. data/spec/services/middleware/middleware_dated_at_spec.rb +186 -0
  197. data/spec/services/middleware/middleware_exotic_communication_spec.rb +560 -0
  198. data/spec/services/middleware/middleware_logging_spec.rb +356 -0
  199. data/spec/services/middleware/middleware_multi_local_spec.rb +1094 -0
  200. data/spec/services/middleware/middleware_multi_remote_spec.rb +1440 -0
  201. data/spec/services/middleware/middleware_permissions_spec.rb +1014 -0
  202. data/spec/services/middleware/middleware_public_spec.rb +238 -0
  203. data/spec/services/middleware/middleware_spec.rb +1569 -0
  204. data/spec/services/middleware/string_inquirer_spec.rb +30 -0
  205. data/spec/services/services/application_spec.rb +74 -0
  206. data/spec/services/services/context_spec.rb +48 -0
  207. data/spec/services/services/implementation_spec.rb +45 -0
  208. data/spec/services/services/interface_spec.rb +262 -0
  209. data/spec/services/services/permissions_spec.rb +249 -0
  210. data/spec/services/services/request_spec.rb +95 -0
  211. data/spec/services/services/response_spec.rb +250 -0
  212. data/spec/services/services/session_spec.rb +432 -0
  213. data/spec/spec_helper.rb +298 -0
  214. data/spec/utilities/utilities_spec.rb +537 -0
  215. data/spec/utilities/uuid_spec.rb +20 -0
  216. metadata +615 -0
@@ -0,0 +1,744 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+
4
+ describe Hoodoo::ActiveRecord::Finder do
5
+ before :all do
6
+ spec_helper_silence_stdout() do
7
+ ActiveRecord::Migration.create_table( :r_spec_model_finder_tests, :id => false ) do | t |
8
+ t.text :id
9
+ t.text :uuid
10
+ t.text :code
11
+ t.text :field_one
12
+ t.text :field_two
13
+ t.text :field_three
14
+
15
+ t.timestamps
16
+ end
17
+ end
18
+
19
+ class RSpecModelFinderTest < ActiveRecord::Base
20
+ include Hoodoo::ActiveRecord::Finder
21
+
22
+ self.primary_key = :id
23
+ acquire_with :uuid, :code
24
+
25
+ # These forms follow quite closely the RDoc comments in
26
+ # the finder.rb source.
27
+
28
+ ARRAY_MATCH = Proc.new { | attr, value |
29
+ [ { attr => [ value ].flatten } ]
30
+ }
31
+
32
+ # Pretend security scoping that only finds things with
33
+ # a UUID and code coming in from the session, for secured
34
+ # searches. Note intentional mix of Symbols and Strings.
35
+
36
+ secure_with(
37
+ 'uuid' => :authorised_uuids, # Array
38
+ :code => 'authorised_code' # Single item
39
+ )
40
+
41
+ # Deliberate mixture of symbols and strings.
42
+
43
+ search_with(
44
+ 'field_one' => nil,
45
+ :field_two => Proc.new { | attr, value |
46
+ [ "#{ attr } ILIKE ?", value ]
47
+ },
48
+ :field_three => ARRAY_MATCH
49
+ )
50
+
51
+ filter_with(
52
+ :field_one => ARRAY_MATCH,
53
+ :field_two => nil,
54
+ 'field_three' => Proc.new { | attr, value |
55
+ [ "#{ attr } ILIKE ?", value ]
56
+ }
57
+ )
58
+ end
59
+
60
+ class RSpecModelFinderTestWithDating < ActiveRecord::Base
61
+ self.primary_key = :id
62
+ self.table_name = :r_spec_model_finder_tests
63
+
64
+ include Hoodoo::ActiveRecord::Finder
65
+ include Hoodoo::ActiveRecord::Dated
66
+ end
67
+
68
+ class RSpecModelFinderTestWithHelpers < ActiveRecord::Base
69
+ include Hoodoo::ActiveRecord::Finder
70
+
71
+ self.primary_key = :id
72
+
73
+ self.table_name = :r_spec_model_finder_tests
74
+ sh = Hoodoo::ActiveRecord::Finder::SearchHelper
75
+
76
+ search_with(
77
+ 'mapped_code' => sh.cs_match( 'code' ),
78
+ :mapped_field_one => sh.ci_match_generic( 'field_one' ),
79
+ :wild_field_one => sh.ciaw_match_generic( 'field_one '),
80
+ :field_two => sh.cs_match_csv(),
81
+ 'field_three' => sh.cs_match_array()
82
+ )
83
+
84
+ filter_with(
85
+ 'mapped_code' => sh.cs_match( 'code' ),
86
+ :mapped_field_one => sh.ci_match_postgres( 'field_one' ),
87
+ :wild_field_one => sh.ciaw_match_postgres( 'field_one '),
88
+ :field_two => sh.cs_match_csv(),
89
+ 'field_three' => sh.cs_match_array()
90
+ )
91
+ end
92
+ end
93
+
94
+ before :each do
95
+ @a = RSpecModelFinderTest.new
96
+ @a.id = "one"
97
+ @a.code = 'A' # Must be set else SQLite fails to find this if you search for "code != 'C'" (!)
98
+ @a.field_one = 'group 1'
99
+ @a.field_two = 'two a'
100
+ @a.field_three = 'three a'
101
+ @a.save!
102
+ @id = @a.id
103
+
104
+ @b = RSpecModelFinderTest.new
105
+ @b.id = "two"
106
+ @b.uuid = Hoodoo::UUID.generate
107
+ @b.code = 'B'
108
+ @b.field_one = 'group 1'
109
+ @b.field_two = 'two b'
110
+ @b.field_three = 'three b'
111
+ @b.save!
112
+ @uuid = @b.uuid
113
+
114
+ @c = RSpecModelFinderTest.new
115
+ @c.id = "three"
116
+ @c.code = 'C'
117
+ @c.field_one = 'group 2'
118
+ @c.field_two = 'two c'
119
+ @c.field_three = 'three c'
120
+ @c.save!
121
+ @code = @c.code
122
+
123
+ @a_wh = RSpecModelFinderTestWithHelpers.find( @a.id )
124
+ @b_wh = RSpecModelFinderTestWithHelpers.find( @b.id )
125
+ @c_wh = RSpecModelFinderTestWithHelpers.find( @c.id )
126
+
127
+ @list_params = Hoodoo::Services::Request::ListParameters.new
128
+ end
129
+
130
+ # ==========================================================================
131
+
132
+ context 'acquire' do
133
+ it 'finds from the class' do
134
+ found = RSpecModelFinderTest.acquire( @id )
135
+ expect( found ).to eq(@a)
136
+
137
+ found = RSpecModelFinderTest.acquire( @uuid )
138
+ expect( found ).to eq(@b)
139
+
140
+ found = RSpecModelFinderTest.acquire( @code )
141
+ expect( found ).to eq(@c)
142
+ end
143
+
144
+ it 'finds with a chain' do
145
+ finder = RSpecModelFinderTest.where( :field_one => 'group 1' )
146
+
147
+ found = finder.acquire( @id )
148
+ expect( found ).to eq(@a)
149
+
150
+ found = finder.acquire( @uuid )
151
+ expect( found ).to eq(@b)
152
+
153
+ found = finder.acquire( @code )
154
+ expect( found ).to eq(nil) # Not in 'group 1'
155
+ end
156
+ end
157
+
158
+ # ==========================================================================
159
+
160
+ context 'acquire_in' do
161
+ before :each do
162
+ @scoped_1 = RSpecModelFinderTest.new
163
+ @scoped_1.id = 'id 1'
164
+ @scoped_1.uuid = 'uuid 1'
165
+ @scoped_1.code = 'code 1'
166
+ @scoped_1.field_one = 'scoped 1'
167
+ @scoped_1.save!
168
+
169
+ @scoped_2 = RSpecModelFinderTest.new
170
+ @scoped_2.id = 'id 2'
171
+ @scoped_2.uuid = 'uuid 1'
172
+ @scoped_2.code = 'code 2'
173
+ @scoped_2.field_one = 'scoped 2'
174
+ @scoped_2.save!
175
+
176
+ @scoped_3 = RSpecModelFinderTest.new
177
+ @scoped_3.id = 'id 3'
178
+ @scoped_3.uuid = 'uuid 2'
179
+ @scoped_3.code = 'code 2'
180
+ @scoped_3.field_one = 'scoped 3'
181
+ @scoped_3.save!
182
+
183
+ # Get a good-enough-for-test interaction which has a context
184
+ # that contains a Session we can modify.
185
+
186
+ @interaction = Hoodoo::Services::Middleware::Interaction.new( {}, nil )
187
+ @interaction.context = Hoodoo::Services::Context.new(
188
+ Hoodoo::Services::Session.new,
189
+ @interaction.context.request,
190
+ @interaction.context.response,
191
+ @interaction
192
+ )
193
+
194
+ @context = @interaction.context
195
+ @session = @interaction.context.session
196
+ end
197
+
198
+ it 'finds with secure scopes from the class' do
199
+ @session.scoping = { :authorised_uuids => [ 'uuid 1' ], :authorised_code => 'code 1' }
200
+
201
+ @context.request.uri_path_components = [ @scoped_1.id ]
202
+ found = RSpecModelFinderTest.acquire_in( @context )
203
+ expect( found ).to eq( @scoped_1 )
204
+
205
+ @context.request.uri_path_components = [ @scoped_2.id ]
206
+ found = RSpecModelFinderTest.acquire_in( @context )
207
+ expect( found ).to be_nil
208
+
209
+ @context.request.uri_path_components = [ @scoped_3.id ]
210
+ found = RSpecModelFinderTest.acquire_in( @context )
211
+ expect( found ).to be_nil
212
+
213
+ @session.scoping.authorised_code = 'code 2'
214
+
215
+ @context.request.uri_path_components = [ @scoped_1.id ]
216
+ found = RSpecModelFinderTest.acquire_in( @context )
217
+ expect( found ).to be_nil
218
+
219
+ @context.request.uri_path_components = [ @scoped_2.id ]
220
+ found = RSpecModelFinderTest.acquire_in( @context )
221
+ expect( found ).to eq( @scoped_2 )
222
+
223
+ @context.request.uri_path_components = [ @scoped_3.id ]
224
+ found = RSpecModelFinderTest.acquire_in( @context )
225
+ expect( found ).to be_nil
226
+
227
+ @session.scoping.authorised_uuids = [ 'uuid 2' ]
228
+
229
+ @context.request.uri_path_components = [ @scoped_1.id ]
230
+ found = RSpecModelFinderTest.acquire_in( @context )
231
+ expect( found ).to be_nil
232
+
233
+ @context.request.uri_path_components = [ @scoped_2.id ]
234
+ found = RSpecModelFinderTest.acquire_in( @context )
235
+ expect( found ).to be_nil
236
+
237
+ @context.request.uri_path_components = [ @scoped_3.id ]
238
+ found = RSpecModelFinderTest.acquire_in( @context )
239
+ expect( found ).to eq( @scoped_3 )
240
+
241
+ @session.scoping.authorised_uuids = [ 'uuid 1', 'uuid 2' ]
242
+
243
+ @context.request.uri_path_components = [ @scoped_1.id ]
244
+ found = RSpecModelFinderTest.acquire_in( @context )
245
+ expect( found ).to be_nil
246
+
247
+ @context.request.uri_path_components = [ @scoped_2.id ]
248
+ found = RSpecModelFinderTest.acquire_in( @context )
249
+ expect( found ).to eq( @scoped_2 )
250
+
251
+ @context.request.uri_path_components = [ @scoped_3.id ]
252
+ found = RSpecModelFinderTest.acquire_in( @context )
253
+ expect( found ).to eq( @scoped_3 )
254
+ end
255
+
256
+ it 'finds with secure scopes with a chain' do
257
+ @session.scoping = { :authorised_uuids => [ 'uuid 1' ], :authorised_code => 'code 1' }
258
+
259
+ @context.request.uri_path_components = [ @scoped_1.id ]
260
+ found = RSpecModelFinderTest.where( :field_one => @scoped_1.field_one ).acquire_in( @context )
261
+ expect( found ).to eq( @scoped_1 )
262
+
263
+ @context.request.uri_path_components = [ @scoped_1.id ]
264
+ found = RSpecModelFinderTest.where( :field_one => @scoped_1.field_one + '!' ).acquire_in( @context )
265
+ expect( found ).to be_nil
266
+
267
+ @context.request.uri_path_components = [ @scoped_2.id ]
268
+ found = RSpecModelFinderTest.where( :field_one => @scoped_2.field_one ).acquire_in( @context )
269
+ expect( found ).to be_nil
270
+
271
+ @context.request.uri_path_components = [ @scoped_3.id ]
272
+ found = RSpecModelFinderTest.where( :field_one => @scoped_3.field_one ).acquire_in( @context )
273
+ expect( found ).to be_nil
274
+
275
+ @session.scoping.authorised_uuids = [ 'uuid 1', 'uuid 2' ]
276
+ @session.scoping.authorised_code = 'code 2'
277
+
278
+ @context.request.uri_path_components = [ @scoped_1.id ]
279
+ found = RSpecModelFinderTest.where( :field_one => @scoped_1.field_one ).acquire_in( @context )
280
+ expect( found ).to be_nil
281
+
282
+ @context.request.uri_path_components = [ @scoped_2.id ]
283
+ found = RSpecModelFinderTest.where( :field_one => @scoped_2.field_one ).acquire_in( @context )
284
+ expect( found ).to eq( @scoped_2 )
285
+
286
+ @context.request.uri_path_components = [ @scoped_3.id ]
287
+ found = RSpecModelFinderTest.where( :field_one => @scoped_3.field_one ).acquire_in( @context )
288
+ expect( found ).to eq( @scoped_3 )
289
+
290
+ @context.request.uri_path_components = [ @scoped_3.id ]
291
+ found = RSpecModelFinderTest.where( :field_one => @scoped_3.field_one + '!' ).acquire_in( @context )
292
+ expect( found ).to be_nil
293
+ end
294
+ end
295
+
296
+ # ==========================================================================
297
+
298
+ context 'lists' do
299
+ it 'lists with pages, offsets and counts' do
300
+ @list_params.offset = 1 # 0 is first record
301
+ @list_params.limit = 1
302
+
303
+ finder = RSpecModelFinderTest.order( :field_three => :asc ).list( @list_params )
304
+ expect( finder ).to eq([@b])
305
+ expect( finder.dataset_size).to eq(3)
306
+
307
+ @list_params.offset = 1
308
+ @list_params.limit = 2
309
+
310
+ finder = RSpecModelFinderTest.order( :field_three => :asc ).list( @list_params )
311
+ expect( finder ).to eq([@b, @c])
312
+ expect( finder.dataset_size).to eq(3)
313
+ end
314
+ end
315
+
316
+ # ==========================================================================
317
+
318
+ context 'search' do
319
+ it 'searches without chain' do
320
+ @list_params.search_data = {
321
+ 'field_one' => 'group 1'
322
+ }
323
+
324
+ finder = RSpecModelFinderTest.list( @list_params )
325
+ expect( finder ).to eq([@b, @a])
326
+
327
+ @list_params.search_data = {
328
+ 'field_one' => 'group 2'
329
+ }
330
+
331
+ finder = RSpecModelFinderTest.list( @list_params )
332
+ expect( finder ).to eq([@c])
333
+
334
+ @list_params.search_data = {
335
+ 'field_two' => 'TWO_A'
336
+ }
337
+
338
+ finder = RSpecModelFinderTest.list( @list_params )
339
+ expect( finder ).to eq([@a])
340
+
341
+ @list_params.search_data = {
342
+ 'field_three' => [ 'three a', 'three c' ]
343
+ }
344
+
345
+ finder = RSpecModelFinderTest.list( @list_params )
346
+ expect( finder ).to eq([@c, @a])
347
+
348
+ @list_params.search_data = {
349
+ 'field_two' => 'two c',
350
+ 'field_three' => [ 'three a', 'three c' ]
351
+ }
352
+
353
+ finder = RSpecModelFinderTest.list( @list_params )
354
+ expect( finder ).to eq([@c])
355
+ end
356
+
357
+ it 'searches with chain' do
358
+ constraint = RSpecModelFinderTest.where( :field_one => 'group 1' )
359
+
360
+ @list_params.search_data = {
361
+ 'field_one' => 'group 1'
362
+ }
363
+
364
+ finder = constraint.list( @list_params )
365
+ expect( finder ).to eq([@b, @a])
366
+
367
+ @list_params.search_data = {
368
+ 'field_one' => 'group 2'
369
+ }
370
+
371
+ finder = constraint.list( @list_params )
372
+ expect( finder ).to eq([])
373
+
374
+ @list_params.search_data = {
375
+ 'field_two' => 'TWO_A'
376
+ }
377
+
378
+ finder = constraint.list( @list_params )
379
+ expect( finder ).to eq([@a])
380
+
381
+ @list_params.search_data = {
382
+ 'field_three' => [ 'three a', 'three c' ]
383
+ }
384
+
385
+ finder = constraint.list( @list_params )
386
+ expect( finder ).to eq([@a])
387
+
388
+ @list_params.search_data = {
389
+ 'field_two' => 'two c',
390
+ 'field_three' => [ 'three a', 'three c' ]
391
+ }
392
+
393
+ finder = constraint.list( @list_params )
394
+ expect( finder ).to eq([])
395
+ end
396
+ end
397
+
398
+ # ==========================================================================
399
+
400
+ context 'helper-based search' do
401
+ it 'finds by mapped code' do
402
+ @list_params.search_data = {
403
+ 'mapped_code' => @code
404
+ }
405
+
406
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
407
+ expect( finder ).to eq( [ @c_wh ] )
408
+ end
409
+
410
+ it 'finds by mapped field-one' do
411
+ @list_params.search_data = {
412
+ 'mapped_field_one' => :'grOUp 1'
413
+ }
414
+
415
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
416
+ expect( finder ).to eq( [ @b_wh, @a_wh ] )
417
+ end
418
+
419
+ it 'finds by mapped, wildcard field-one' do
420
+ @list_params.search_data = {
421
+ 'wild_field_one' => :'oUP '
422
+ }
423
+
424
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
425
+ expect( finder ).to eq( [ @c_wh, @b_wh, @a_wh ] )
426
+
427
+ @list_params.search_data = {
428
+ 'wild_field_one' => :'o!p '
429
+ }
430
+
431
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
432
+ expect( finder ).to eq( [] )
433
+ end
434
+
435
+ it 'finds by comma-separated list' do
436
+ @list_params.search_data = {
437
+ 'field_two' => 'two a,something else,two c,more'
438
+ }
439
+
440
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
441
+ expect( finder ).to eq( [ @c_wh, @a_wh ] )
442
+ end
443
+
444
+ it 'finds by Array' do
445
+ @list_params.search_data = {
446
+ 'field_three' => [ 'hello', :'three b', 'three c', :there ]
447
+ }
448
+
449
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
450
+ expect( finder ).to eq( [ @c_wh, @b_wh ] )
451
+ end
452
+ end
453
+
454
+ # ==========================================================================
455
+
456
+ context 'filter' do
457
+ it 'filters without chain' do
458
+ @list_params.filter_data = {
459
+ 'field_two' => 'two a'
460
+ }
461
+
462
+ finder = RSpecModelFinderTest.list( @list_params )
463
+ expect( finder ).to eq([@c, @b])
464
+
465
+ @list_params.filter_data = {
466
+ 'field_three' => 'three c'
467
+ }
468
+
469
+ finder = RSpecModelFinderTest.list( @list_params )
470
+ expect( finder ).to eq([@b, @a])
471
+
472
+ @list_params.filter_data = {
473
+ 'field_one' => [ 'group 1', 'group 2' ]
474
+ }
475
+
476
+ finder = RSpecModelFinderTest.list( @list_params )
477
+ expect( finder ).to eq([])
478
+
479
+ @list_params.filter_data = {
480
+ 'field_one' => [ 'group 2' ]
481
+ }
482
+
483
+ finder = RSpecModelFinderTest.list( @list_params )
484
+ expect( finder ).to eq([@b, @a])
485
+
486
+ @list_params.filter_data = {
487
+ 'field_one' => [ 'group 2' ],
488
+ 'field_three' => 'three a'
489
+ }
490
+
491
+ finder = RSpecModelFinderTest.list( @list_params )
492
+ expect( finder ).to eq([@b])
493
+ end
494
+
495
+ it 'filters with chain' do
496
+
497
+ # Remember, the constraint is *inclusive* unlike all the
498
+ # subsequent filters which *exclude*.
499
+
500
+ constraint = RSpecModelFinderTest.where( :field_one => 'group 2' )
501
+
502
+ @list_params.filter_data = {
503
+ 'field_two' => 'two a'
504
+ }
505
+
506
+ finder = constraint.list( @list_params )
507
+ expect( finder ).to eq([@c])
508
+
509
+ @list_params.filter_data = {
510
+ 'field_three' => 'three c'
511
+ }
512
+
513
+ finder = constraint.list( @list_params )
514
+ expect( finder ).to eq([])
515
+
516
+ @list_params.filter_data = {
517
+ 'field_one' => [ 'group 1', 'group 2' ]
518
+ }
519
+
520
+ finder = constraint.list( @list_params )
521
+ expect( finder ).to eq([])
522
+
523
+ @list_params.filter_data = {
524
+ 'field_one' => [ 'group 2' ]
525
+ }
526
+
527
+ finder = constraint.list( @list_params )
528
+ expect( finder ).to eq([])
529
+
530
+ @list_params.filter_data = {
531
+ 'field_one' => [ 'group 2' ],
532
+ 'field_three' => 'three a'
533
+ }
534
+
535
+ finder = constraint.list( @list_params )
536
+ expect( finder ).to eq([])
537
+ end
538
+ end
539
+
540
+ # ==========================================================================
541
+
542
+ # This set of copy-and-modify tests based on the helper-based search tests
543
+ # earlier seems somewhat redundant, but should anyone accidentally decouple
544
+ # the search/filter back-end processing and introduce some sort of error at
545
+ # a finder-level, the tests here have a chance of catching that.
546
+
547
+ context 'helper-based filtering' do
548
+ it 'filters by mapped code' do
549
+ @list_params.filter_data = {
550
+ 'mapped_code' => @code
551
+ }
552
+
553
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
554
+ expect( finder ).to eq( [ @b_wh, @a_wh ] )
555
+ end
556
+
557
+ it 'filters by mapped field-one' do
558
+ @list_params.filter_data = {
559
+ 'mapped_field_one' => :'grOUp 1'
560
+ }
561
+
562
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
563
+ expect( finder ).to eq( [ @c_wh ] )
564
+ end
565
+
566
+ it 'filters by mapped, wildcard field-one' do
567
+ @list_params.filter_data = {
568
+ 'wild_field_one' => :'oUP '
569
+ }
570
+
571
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
572
+ expect( finder ).to eq( [] )
573
+
574
+ @list_params.filter_data = {
575
+ 'wild_field_one' => :'o!p '
576
+ }
577
+
578
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
579
+ expect( finder ).to eq( [ @c_wh, @b_wh, @a_wh ] )
580
+ end
581
+
582
+
583
+ it 'filters by comma-separated list' do
584
+ @list_params.filter_data = {
585
+ 'field_two' => 'two a,something else,two c,more'
586
+ }
587
+
588
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
589
+ expect( finder ).to eq( [ @b_wh ] )
590
+ end
591
+
592
+ it 'filters by Array' do
593
+ @list_params.filter_data = {
594
+ 'field_three' => [ 'hello', :'three b', 'three c', :there ]
595
+ }
596
+
597
+ finder = RSpecModelFinderTestWithHelpers.list( @list_params )
598
+ expect( finder ).to eq( [ @a_wh ] )
599
+ end
600
+ end
601
+
602
+ # ==========================================================================
603
+
604
+ context 'list_in' do
605
+ before :each do
606
+ @scoped_1 = RSpecModelFinderTest.new
607
+ @scoped_1.id = 'id 1'
608
+ @scoped_1.uuid = 'uuid 1'
609
+ @scoped_1.code = 'code 1'
610
+ @scoped_1.field_one = 'scoped 1'
611
+ @scoped_1.save!
612
+
613
+ @scoped_2 = RSpecModelFinderTest.new
614
+ @scoped_2.id = 'id 2'
615
+ @scoped_2.uuid = 'uuid 1'
616
+ @scoped_2.code = 'code 2'
617
+ @scoped_2.field_one = 'scoped 2'
618
+ @scoped_2.save!
619
+
620
+ @scoped_3 = RSpecModelFinderTest.new
621
+ @scoped_3.id = 'id 3'
622
+ @scoped_3.uuid = 'uuid 2'
623
+ @scoped_3.code = 'code 2'
624
+ @scoped_3.field_one = 'scoped 3'
625
+ @scoped_3.save!
626
+
627
+ # Get a good-enough-for-test interaction which has a context
628
+ # that contains a Session we can modify.
629
+
630
+ @interaction = Hoodoo::Services::Middleware::Interaction.new( {}, nil )
631
+ @interaction.context = Hoodoo::Services::Context.new(
632
+ Hoodoo::Services::Session.new,
633
+ @interaction.context.request,
634
+ @interaction.context.response,
635
+ @interaction
636
+ )
637
+
638
+ @context = @interaction.context
639
+ @session = @interaction.context.session
640
+ end
641
+
642
+ it 'lists with secure scopes from the class' do
643
+ @session.scoping = { :authorised_uuids => [ 'uuid 1' ], :authorised_code => 'code 1' }
644
+
645
+ list = RSpecModelFinderTest.list_in( @context )
646
+ expect( list ).to eq( [ @scoped_1 ] )
647
+
648
+ @session.scoping.authorised_code = 'code 2'
649
+
650
+ list = RSpecModelFinderTest.list_in( @context )
651
+ expect( list ).to eq( [ @scoped_2 ] )
652
+
653
+ @session.scoping.authorised_uuids = [ 'uuid 2' ]
654
+
655
+ list = RSpecModelFinderTest.list_in( @context )
656
+ expect( list ).to eq( [ @scoped_3 ] )
657
+
658
+ @session.scoping.authorised_uuids = [ 'uuid 1', 'uuid 2' ]
659
+
660
+ # OK, so these test 'with a chain' too... It's just convenient to (re-)cover
661
+ # that aspect here.
662
+
663
+ list = RSpecModelFinderTest.list_in( @context ).reorder( 'field_one' => 'asc' )
664
+ expect( list ).to eq( [ @scoped_2, @scoped_3 ] )
665
+
666
+ list = RSpecModelFinderTest.list_in( @context ).reorder( 'field_one' => 'desc' )
667
+ expect( list ).to eq( [ @scoped_3, @scoped_2 ] )
668
+
669
+ list = RSpecModelFinderTest.reorder( 'field_one' => 'asc' ).list_in( @context )
670
+ expect( list ).to eq( [ @scoped_2, @scoped_3 ] )
671
+
672
+ list = RSpecModelFinderTest.reorder( 'field_one' => 'desc' ).list_in( @context )
673
+ expect( list ).to eq( [ @scoped_3, @scoped_2 ] )
674
+ end
675
+
676
+ it 'finds with secure scopes with a chain' do
677
+ @session.scoping = { :authorised_uuids => [ 'uuid 1' ], :authorised_code => 'code 1' }
678
+
679
+ list = RSpecModelFinderTest.where( :field_one => @scoped_1.field_one ).list_in( @context )
680
+ expect( list ).to eq( [ @scoped_1 ] )
681
+
682
+ list = RSpecModelFinderTest.where( :field_one => @scoped_1.field_one + '!' ).list_in( @context )
683
+ expect( list ).to eq( [] )
684
+
685
+ list = RSpecModelFinderTest.list_in( @context ).where( :field_one => @scoped_1.field_one )
686
+ expect( list ).to eq( [ @scoped_1 ] )
687
+
688
+ list = RSpecModelFinderTest.list_in( @context ).where( :field_one => @scoped_1.field_one + '!' )
689
+ expect( list ).to eq( [] )
690
+
691
+ @session.scoping.authorised_uuids = [ 'uuid 1', 'uuid 2' ]
692
+ @session.scoping.authorised_code = 'code 2'
693
+
694
+ list = RSpecModelFinderTest.where( :field_one => [ @scoped_1.field_one, @scoped_2.field_one ] ).list_in( @context )
695
+ expect( list ).to eq( [ @scoped_2 ] )
696
+
697
+ list = RSpecModelFinderTest.list_in( @context ).where( :field_one => [ @scoped_1.field_one, @scoped_2.field_one ] )
698
+ expect( list ).to eq( [ @scoped_2 ] )
699
+
700
+ list = RSpecModelFinderTest.where( :field_one => [ @scoped_2.field_one, @scoped_3.field_one ] ).list_in( @context ).reorder( 'field_one' => 'asc' )
701
+ expect( list ).to eq( [ @scoped_2, @scoped_3 ] )
702
+
703
+ list = RSpecModelFinderTest.list_in( @context ).reorder( 'field_one' => 'asc' ).where( :field_one => [ @scoped_2.field_one, @scoped_3.field_one ] )
704
+ expect( list ).to eq( [ @scoped_2, @scoped_3 ] )
705
+ end
706
+ end
707
+
708
+ # ==========================================================================
709
+
710
+ context 'deprecated' do
711
+ it '#polymorphic_find calls #acquire' do
712
+ expect( $stderr ).to receive( :puts ).once
713
+ expect( RSpecModelFinderTest ).to receive( :acquire ).once.with( 21 )
714
+ RSpecModelFinderTest.polymorphic_find( RSpecModelFinderTest, 21 )
715
+ end
716
+
717
+ it '#polymorphic_id_fields calls #acquire_with' do
718
+ expect( $stderr ).to receive( :puts ).once
719
+ expect( RSpecModelFinderTest ).to receive( :acquire_with ).once.with( :uuid, :code )
720
+ RSpecModelFinderTest.polymorphic_id_fields( :uuid, :code )
721
+ end
722
+
723
+ it '#list_finder calls #list' do
724
+ params = { :search => { :field_one => 'one' } }
725
+ expect( $stderr ).to receive( :puts ).once
726
+ expect( RSpecModelFinderTest ).to receive( :list ).once.with( params )
727
+ RSpecModelFinderTest.list_finder( params )
728
+ end
729
+
730
+ it '#list_search_map calls #search_with' do
731
+ params = { :foo => nil, :bar => nil }
732
+ expect( $stderr ).to receive( :puts ).once
733
+ expect( RSpecModelFinderTest ).to receive( :search_with ).once.with( params )
734
+ RSpecModelFinderTest.list_search_map( params )
735
+ end
736
+
737
+ it '#list_filter_map calls #filter_with' do
738
+ params = { :foo => nil, :bar => nil }
739
+ expect( $stderr ).to receive( :puts ).once
740
+ expect( RSpecModelFinderTest ).to receive( :filter_with ).once.with( params )
741
+ RSpecModelFinderTest.list_filter_map( params )
742
+ end
743
+ end
744
+ end