mark_mapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.rdoc +39 -0
  4. data/examples/attr_accessible.rb +24 -0
  5. data/examples/attr_protected.rb +24 -0
  6. data/examples/cache_key.rb +26 -0
  7. data/examples/custom_types.rb +26 -0
  8. data/examples/identity_map.rb +30 -0
  9. data/examples/identity_map/automatic.rb +2 -0
  10. data/examples/keys.rb +42 -0
  11. data/examples/modifiers/set.rb +27 -0
  12. data/examples/plugins.rb +40 -0
  13. data/examples/querying.rb +39 -0
  14. data/examples/sample_app.rb +43 -0
  15. data/examples/scopes.rb +56 -0
  16. data/examples/validating/embedded_docs.rb +31 -0
  17. data/lib/mark_mapper.rb +125 -0
  18. data/lib/mark_mapper/config.rb +90 -0
  19. data/lib/mark_mapper/connection.rb +60 -0
  20. data/lib/mark_mapper/criteria_hash.rb +194 -0
  21. data/lib/mark_mapper/document.rb +46 -0
  22. data/lib/mark_mapper/embedded_document.rb +32 -0
  23. data/lib/mark_mapper/exceptions.rb +33 -0
  24. data/lib/mark_mapper/extensions/array.rb +27 -0
  25. data/lib/mark_mapper/extensions/boolean.rb +45 -0
  26. data/lib/mark_mapper/extensions/date.rb +29 -0
  27. data/lib/mark_mapper/extensions/duplicable.rb +86 -0
  28. data/lib/mark_mapper/extensions/float.rb +18 -0
  29. data/lib/mark_mapper/extensions/hash.rb +26 -0
  30. data/lib/mark_mapper/extensions/integer.rb +27 -0
  31. data/lib/mark_mapper/extensions/kernel.rb +11 -0
  32. data/lib/mark_mapper/extensions/nil_class.rb +18 -0
  33. data/lib/mark_mapper/extensions/object.rb +30 -0
  34. data/lib/mark_mapper/extensions/object_id.rb +18 -0
  35. data/lib/mark_mapper/extensions/set.rb +20 -0
  36. data/lib/mark_mapper/extensions/string.rb +31 -0
  37. data/lib/mark_mapper/extensions/symbol.rb +87 -0
  38. data/lib/mark_mapper/extensions/time.rb +29 -0
  39. data/lib/mark_mapper/locale/en.yml +5 -0
  40. data/lib/mark_mapper/middleware/identity_map.rb +41 -0
  41. data/lib/mark_mapper/normalizers/criteria_hash_key.rb +17 -0
  42. data/lib/mark_mapper/normalizers/criteria_hash_value.rb +66 -0
  43. data/lib/mark_mapper/normalizers/fields_value.rb +26 -0
  44. data/lib/mark_mapper/normalizers/hash_key.rb +19 -0
  45. data/lib/mark_mapper/normalizers/integer.rb +19 -0
  46. data/lib/mark_mapper/normalizers/options_hash_value.rb +83 -0
  47. data/lib/mark_mapper/normalizers/sort_value.rb +55 -0
  48. data/lib/mark_mapper/options_hash.rb +103 -0
  49. data/lib/mark_mapper/pagination.rb +6 -0
  50. data/lib/mark_mapper/pagination/collection.rb +32 -0
  51. data/lib/mark_mapper/pagination/paginator.rb +46 -0
  52. data/lib/mark_mapper/plugins.rb +22 -0
  53. data/lib/mark_mapper/plugins/accessible.rb +61 -0
  54. data/lib/mark_mapper/plugins/active_model.rb +18 -0
  55. data/lib/mark_mapper/plugins/associations.rb +96 -0
  56. data/lib/mark_mapper/plugins/associations/base.rb +98 -0
  57. data/lib/mark_mapper/plugins/associations/belongs_to_association.rb +63 -0
  58. data/lib/mark_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +35 -0
  59. data/lib/mark_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
  60. data/lib/mark_mapper/plugins/associations/collection.rb +29 -0
  61. data/lib/mark_mapper/plugins/associations/embedded_collection.rb +44 -0
  62. data/lib/mark_mapper/plugins/associations/in_array_proxy.rb +133 -0
  63. data/lib/mark_mapper/plugins/associations/many_association.rb +63 -0
  64. data/lib/mark_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  65. data/lib/mark_mapper/plugins/associations/many_documents_proxy.rb +142 -0
  66. data/lib/mark_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  67. data/lib/mark_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  68. data/lib/mark_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  69. data/lib/mark_mapper/plugins/associations/one_as_proxy.rb +22 -0
  70. data/lib/mark_mapper/plugins/associations/one_association.rb +48 -0
  71. data/lib/mark_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
  72. data/lib/mark_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
  73. data/lib/mark_mapper/plugins/associations/one_proxy.rb +95 -0
  74. data/lib/mark_mapper/plugins/associations/proxy.rb +138 -0
  75. data/lib/mark_mapper/plugins/associations/single_association.rb +46 -0
  76. data/lib/mark_mapper/plugins/caching.rb +21 -0
  77. data/lib/mark_mapper/plugins/callbacks.rb +42 -0
  78. data/lib/mark_mapper/plugins/clone.rb +24 -0
  79. data/lib/mark_mapper/plugins/counter_cache.rb +97 -0
  80. data/lib/mark_mapper/plugins/dirty.rb +61 -0
  81. data/lib/mark_mapper/plugins/document.rb +41 -0
  82. data/lib/mark_mapper/plugins/dumpable.rb +22 -0
  83. data/lib/mark_mapper/plugins/dynamic_querying.rb +45 -0
  84. data/lib/mark_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  85. data/lib/mark_mapper/plugins/embedded_callbacks.rb +81 -0
  86. data/lib/mark_mapper/plugins/embedded_document.rb +53 -0
  87. data/lib/mark_mapper/plugins/equality.rb +23 -0
  88. data/lib/mark_mapper/plugins/identity_map.rb +144 -0
  89. data/lib/mark_mapper/plugins/indexable.rb +86 -0
  90. data/lib/mark_mapper/plugins/inspect.rb +16 -0
  91. data/lib/mark_mapper/plugins/keys.rb +470 -0
  92. data/lib/mark_mapper/plugins/keys/key.rb +134 -0
  93. data/lib/mark_mapper/plugins/keys/static.rb +45 -0
  94. data/lib/mark_mapper/plugins/logger.rb +18 -0
  95. data/lib/mark_mapper/plugins/modifiers.rb +140 -0
  96. data/lib/mark_mapper/plugins/pagination.rb +16 -0
  97. data/lib/mark_mapper/plugins/partial_updates.rb +77 -0
  98. data/lib/mark_mapper/plugins/persistence.rb +79 -0
  99. data/lib/mark_mapper/plugins/protected.rb +45 -0
  100. data/lib/mark_mapper/plugins/querying.rb +173 -0
  101. data/lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb +75 -0
  102. data/lib/mark_mapper/plugins/rails.rb +79 -0
  103. data/lib/mark_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
  104. data/lib/mark_mapper/plugins/sci.rb +82 -0
  105. data/lib/mark_mapper/plugins/scopes.rb +28 -0
  106. data/lib/mark_mapper/plugins/serialization.rb +109 -0
  107. data/lib/mark_mapper/plugins/timestamps.rb +29 -0
  108. data/lib/mark_mapper/plugins/touch.rb +18 -0
  109. data/lib/mark_mapper/plugins/userstamps.rb +18 -0
  110. data/lib/mark_mapper/plugins/validations.rb +96 -0
  111. data/lib/mark_mapper/query.rb +278 -0
  112. data/lib/mark_mapper/railtie.rb +52 -0
  113. data/lib/mark_mapper/railtie/database.rake +65 -0
  114. data/lib/mark_mapper/translation.rb +10 -0
  115. data/lib/mark_mapper/version.rb +4 -0
  116. data/lib/rails/generators/mark_mapper/config/config_generator.rb +37 -0
  117. data/lib/rails/generators/mark_mapper/config/templates/marklogic.yml +19 -0
  118. data/lib/rails/generators/mark_mapper/model/model_generator.rb +40 -0
  119. data/lib/rails/generators/mark_mapper/model/templates/model.rb +17 -0
  120. data/spec/config/mark_mapper.yml +6 -0
  121. data/spec/examples_spec.rb +25 -0
  122. data/spec/functional/accessible_spec.rb +198 -0
  123. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +64 -0
  124. data/spec/functional/associations/belongs_to_proxy_spec.rb +255 -0
  125. data/spec/functional/associations/in_array_proxy_spec.rb +349 -0
  126. data/spec/functional/associations/many_documents_as_proxy_spec.rb +230 -0
  127. data/spec/functional/associations/many_documents_proxy_spec.rb +968 -0
  128. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +238 -0
  129. data/spec/functional/associations/many_embedded_proxy_spec.rb +288 -0
  130. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +302 -0
  131. data/spec/functional/associations/one_as_proxy_spec.rb +489 -0
  132. data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +207 -0
  133. data/spec/functional/associations/one_embedded_proxy_spec.rb +100 -0
  134. data/spec/functional/associations/one_proxy_spec.rb +406 -0
  135. data/spec/functional/associations_spec.rb +48 -0
  136. data/spec/functional/caching_spec.rb +75 -0
  137. data/spec/functional/callbacks_spec.rb +330 -0
  138. data/spec/functional/counter_cache_spec.rb +235 -0
  139. data/spec/functional/dirty_spec.rb +316 -0
  140. data/spec/functional/document_spec.rb +310 -0
  141. data/spec/functional/dumpable_spec.rb +24 -0
  142. data/spec/functional/dynamic_querying_spec.rb +75 -0
  143. data/spec/functional/embedded_document_spec.rb +316 -0
  144. data/spec/functional/equality_spec.rb +20 -0
  145. data/spec/functional/extensions_spec.rb +16 -0
  146. data/spec/functional/identity_map_spec.rb +483 -0
  147. data/spec/functional/keys_spec.rb +339 -0
  148. data/spec/functional/logger_spec.rb +20 -0
  149. data/spec/functional/modifiers_spec.rb +446 -0
  150. data/spec/functional/options_hash_spec.rb +41 -0
  151. data/spec/functional/pagination_spec.rb +89 -0
  152. data/spec/functional/partial_updates_spec.rb +530 -0
  153. data/spec/functional/protected_spec.rb +199 -0
  154. data/spec/functional/querying_spec.rb +984 -0
  155. data/spec/functional/rails_spec.rb +55 -0
  156. data/spec/functional/sci_spec.rb +374 -0
  157. data/spec/functional/scopes_spec.rb +204 -0
  158. data/spec/functional/static_keys_spec.rb +153 -0
  159. data/spec/functional/timestamps_spec.rb +97 -0
  160. data/spec/functional/touch_spec.rb +125 -0
  161. data/spec/functional/userstamps_spec.rb +46 -0
  162. data/spec/functional/validations_spec.rb +416 -0
  163. data/spec/quality_spec.rb +51 -0
  164. data/spec/spec_helper.rb +150 -0
  165. data/spec/support/matchers.rb +15 -0
  166. data/spec/support/models.rb +256 -0
  167. data/spec/symbol_operator_spec.rb +70 -0
  168. data/spec/symbol_spec.rb +9 -0
  169. data/spec/unit/associations/base_spec.rb +146 -0
  170. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  171. data/spec/unit/associations/many_association_spec.rb +64 -0
  172. data/spec/unit/associations/one_association_spec.rb +48 -0
  173. data/spec/unit/associations/proxy_spec.rb +103 -0
  174. data/spec/unit/clone_spec.rb +79 -0
  175. data/spec/unit/config_generator_spec.rb +24 -0
  176. data/spec/unit/criteria_hash_spec.rb +218 -0
  177. data/spec/unit/document_spec.rb +251 -0
  178. data/spec/unit/dynamic_finder_spec.rb +125 -0
  179. data/spec/unit/embedded_document_spec.rb +676 -0
  180. data/spec/unit/equality_spec.rb +38 -0
  181. data/spec/unit/exceptions_spec.rb +12 -0
  182. data/spec/unit/extensions_spec.rb +368 -0
  183. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  184. data/spec/unit/inspect_spec.rb +47 -0
  185. data/spec/unit/key_spec.rb +276 -0
  186. data/spec/unit/keys_spec.rb +155 -0
  187. data/spec/unit/mark_mapper_spec.rb +37 -0
  188. data/spec/unit/model_generator_spec.rb +45 -0
  189. data/spec/unit/normalizers/criteria_hash_key_spec.rb +37 -0
  190. data/spec/unit/normalizers/criteria_hash_value_spec.rb +200 -0
  191. data/spec/unit/normalizers/fields_value_spec.rb +45 -0
  192. data/spec/unit/normalizers/hash_key_spec.rb +15 -0
  193. data/spec/unit/normalizers/integer_spec.rb +24 -0
  194. data/spec/unit/normalizers/options_hash_value_spec.rb +99 -0
  195. data/spec/unit/normalizers/sort_value_spec.rb +98 -0
  196. data/spec/unit/options_hash_spec.rb +64 -0
  197. data/spec/unit/pagination/collection_spec.rb +30 -0
  198. data/spec/unit/pagination/paginator_spec.rb +118 -0
  199. data/spec/unit/pagination_spec.rb +11 -0
  200. data/spec/unit/plugins_spec.rb +89 -0
  201. data/spec/unit/query_spec.rb +837 -0
  202. data/spec/unit/rails_compatibility_spec.rb +40 -0
  203. data/spec/unit/rails_reflect_on_association_spec.rb +118 -0
  204. data/spec/unit/rails_spec.rb +188 -0
  205. data/spec/unit/serialization_spec.rb +169 -0
  206. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  207. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  208. data/spec/unit/time_zones_spec.rb +44 -0
  209. data/spec/unit/translation_spec.rb +27 -0
  210. data/spec/unit/validations_spec.rb +588 -0
  211. metadata +307 -0
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Equality" do
4
+ context "Case equality" do
5
+ before do
6
+ @klass = Class.new do
7
+ include MarkMapper::Plugins::Equality
8
+ end
9
+ @subklass = Class.new(@klass)
10
+
11
+ @faker = Class.new do
12
+ def initialize(faked)
13
+ @faked = faked
14
+ end
15
+
16
+ def is_a?(klass)
17
+ @faked.is_a? klass
18
+ end
19
+ end
20
+ end
21
+
22
+ it "should work with regular instance" do
23
+ @klass.should === @klass.new
24
+ end
25
+
26
+ it "should work with instances of subclasses" do
27
+ @klass.should === @subklass.new
28
+ end
29
+
30
+ it "should work with a faker class" do
31
+ @klass.should === @faker.new(@klass.new)
32
+ end
33
+
34
+ it "should not work with other instances" do
35
+ @klass.should_not === 1
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Extensions" do
4
+ context "DocumentNotValid" do
5
+ it "should have document reader method" do
6
+ doc_class = Doc()
7
+ instance = doc_class.new
8
+ exception = MarkMapper::DocumentNotValid.new(instance)
9
+ exception.document.should == instance
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,368 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Support" do
4
+ context "Array.to_marklogic" do
5
+ it "should convert value to_a" do
6
+ Array.to_marklogic([1, 2, 3, 4]).should == [1, 2, 3, 4]
7
+ Array.to_marklogic('1').should == ['1']
8
+ Array.to_marklogic({'1' => '2', '3' => '4'}).should include(['1', '2'], ['3', '4'])
9
+ end
10
+ end
11
+
12
+ context "Array.from_marklogic" do
13
+ it "should be array if array" do
14
+ Array.from_marklogic([1, 2]).should == [1, 2]
15
+ end
16
+
17
+ it "should be empty array if nil" do
18
+ Array.from_marklogic(nil).should == []
19
+ end
20
+ end
21
+
22
+ context "Boolean.to_marklogic" do
23
+ it "should be true for true" do
24
+ Boolean.to_marklogic(true).should be_truthy
25
+ end
26
+
27
+ it "should be false for false" do
28
+ Boolean.to_marklogic(false).should be_falsey
29
+ end
30
+
31
+ it "should handle odd assortment of other values" do
32
+ Boolean.to_marklogic('true').should be_truthy
33
+ Boolean.to_marklogic('t').should be_truthy
34
+ Boolean.to_marklogic('1').should be_truthy
35
+ Boolean.to_marklogic(1).should be_truthy
36
+
37
+ Boolean.to_marklogic('false').should be_falsey
38
+ Boolean.to_marklogic('f').should be_falsey
39
+ Boolean.to_marklogic('0').should be_falsey
40
+ Boolean.to_marklogic(0).should be_falsey
41
+ end
42
+
43
+ it "should be nil for nil" do
44
+ Boolean.to_marklogic(nil).should be_nil
45
+ end
46
+ end
47
+
48
+ context "Boolean.from_marklogic" do
49
+ it "should be true for true" do
50
+ Boolean.from_marklogic(true).should be_truthy
51
+ end
52
+
53
+ it "should be false for false" do
54
+ Boolean.from_marklogic(false).should be_falsey
55
+ end
56
+
57
+ it "should be nil for nil" do
58
+ Boolean.from_marklogic(nil).should be_nil
59
+ end
60
+ end
61
+
62
+ context "Date.to_marklogic" do
63
+ it "should be time if string" do
64
+ date = Date.to_marklogic('2009-10-01')
65
+ date.should == Time.utc(2009, 10, 1)
66
+ date.should == date
67
+ date.month.should == 10
68
+ date.day.should == 1
69
+ date.year.should == 2009
70
+ end
71
+
72
+ it "should be time if date" do
73
+ Date.to_marklogic(Date.new(2009, 10, 1)).should == Time.utc(2009, 10, 1)
74
+ end
75
+
76
+ it "should be date if time" do
77
+ Date.to_marklogic(Time.parse("2009-10-1T12:30:00")).should == Time.utc(2009, 10, 1)
78
+ end
79
+
80
+ it "should be nil if bogus string" do
81
+ Date.to_marklogic('jdsafop874').should be_nil
82
+ end
83
+
84
+ it "should be nil if empty string" do
85
+ Date.to_marklogic('').should be_nil
86
+ end
87
+ end
88
+
89
+ context "Date.from_marklogic" do
90
+ it "should be date if date" do
91
+ date = Date.new(2009, 10, 1)
92
+ from_date = Date.from_marklogic(date)
93
+ from_date.should == date
94
+ from_date.month.should == 10
95
+ from_date.day.should == 1
96
+ from_date.year.should == 2009
97
+ end
98
+
99
+ it "should be date if time" do
100
+ time = Time.now
101
+ Date.from_marklogic(time).should == time.to_date
102
+ end
103
+
104
+ it "should be nil if nil" do
105
+ Date.from_marklogic(nil).should be_nil
106
+ end
107
+ end
108
+
109
+ context "Float.to_marklogic" do
110
+ it "should convert value to_f" do
111
+ [21, 21.0, '21'].each do |value|
112
+ Float.to_marklogic(value).should == 21.0
113
+ end
114
+ end
115
+
116
+ it "should leave nil values nil" do
117
+ Float.to_marklogic(nil).should == nil
118
+ end
119
+
120
+ it "should leave blank values nil" do
121
+ Float.to_marklogic('').should == nil
122
+ end
123
+ end
124
+
125
+ context "Hash.from_marklogic" do
126
+ it "should convert hash to hash with indifferent access" do
127
+ hash = Hash.from_marklogic(:foo => 'bar')
128
+ hash[:foo].should == 'bar'
129
+ hash['foo'].should == 'bar'
130
+ end
131
+
132
+ it "should be hash if nil" do
133
+ hash = Hash.from_marklogic(nil)
134
+ hash.should == {}
135
+ hash.is_a?(HashWithIndifferentAccess).should be_truthy
136
+ end
137
+ end
138
+
139
+ context "Hash.to_marklogic instance method" do
140
+ it "should have instance method that returns self" do
141
+ hash = HashWithIndifferentAccess.new('foo' => 'bar')
142
+ hash.to_marklogic.should == {'foo' => 'bar'}
143
+ end
144
+ end
145
+
146
+ context "Integer.to_marklogic" do
147
+ it "should convert value to integer" do
148
+ [21, 21.0, '21'].each do |value|
149
+ Integer.to_marklogic(value).should == 21
150
+ end
151
+ end
152
+
153
+ it "should convert value from marklogic to integer" do
154
+ [21, 21.0, '21'].each do |value|
155
+ Integer.from_marklogic(value).should == 21
156
+ end
157
+ end
158
+
159
+ it "should convert nil to nil" do
160
+ Integer.to_marklogic(nil).should be_nil
161
+ end
162
+
163
+ it "should convert nil to nil" do
164
+ Integer.from_marklogic(nil).should be_nil
165
+ end
166
+
167
+ it "should work fine with big integers" do
168
+ [9223372036854775807, '9223372036854775807'].each do |value|
169
+ Integer.to_marklogic(value).should == 9223372036854775807
170
+ end
171
+ end
172
+ end
173
+
174
+ context "NilClass#from_marklogic" do
175
+ it "should return nil" do
176
+ nil.from_marklogic(nil).should be_nil
177
+ end
178
+ end
179
+
180
+ context "NilClass#to_marklogic" do
181
+ it "should return nil" do
182
+ nil.to_marklogic(nil).should be_nil
183
+ end
184
+ end
185
+
186
+ context "ObjectId#to_marklogic" do
187
+ it "should call class to_marklogic with self" do
188
+ object = Object.new
189
+ expect(object.class).to receive(:to_marklogic).with(object)
190
+ object.to_marklogic
191
+ end
192
+ end
193
+
194
+ context "ObjectId.to_marklogic" do
195
+ it "should return nil for nil" do
196
+ ObjectId.to_marklogic(nil).should be_nil
197
+ end
198
+
199
+ it "should return nil if blank string" do
200
+ ObjectId.to_marklogic('').should be_nil
201
+ end
202
+
203
+ it "should return value if object id" do
204
+ id = MarkLogic::ObjectId.new
205
+ ObjectId.to_marklogic(id).should be(id)
206
+ end
207
+
208
+ it "should return value" do
209
+ Object.to_marklogic(21).should == 21
210
+ Object.to_marklogic('21').should == '21'
211
+ Object.to_marklogic(9223372036854775807).should == 9223372036854775807
212
+ end
213
+ end
214
+
215
+ context "ObjectId.from_marklogic" do
216
+ it "should return value" do
217
+ Object.from_marklogic(21).should == 21
218
+ Object.from_marklogic('21').should == '21'
219
+ Object.from_marklogic(9223372036854775807).should == 9223372036854775807
220
+
221
+ id = MarkLogic::ObjectId.new
222
+ ObjectId.from_marklogic(id).should == id
223
+ end
224
+ end
225
+
226
+ context "Set.to_marklogic" do
227
+ it "should convert value to_a" do
228
+ Set.to_marklogic(Set.new([1,2,3])).should == [1,2,3]
229
+ end
230
+
231
+ it "should convert to empty array if nil" do
232
+ Set.to_marklogic(nil).should == []
233
+ end
234
+ end
235
+
236
+ context "Set.from_marklogic" do
237
+ it "should be a set if array" do
238
+ Set.from_marklogic([1,2,3]).should == Set.new([1,2,3])
239
+ end
240
+
241
+ it "should be empty set if nil" do
242
+ Set.from_marklogic(nil).should == Set.new([])
243
+ end
244
+ end
245
+
246
+ context "String.to_marklogic" do
247
+ it "should convert value to_s" do
248
+ [21, '21'].each do |value|
249
+ String.to_marklogic(value).should == '21'
250
+ end
251
+ end
252
+
253
+ it "should be nil if nil" do
254
+ String.to_marklogic(nil).should be_nil
255
+ end
256
+ end
257
+
258
+ context "String.from_marklogic" do
259
+ it "should be string if value present" do
260
+ String.from_marklogic('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
261
+ end
262
+
263
+ it "should return nil if nil" do
264
+ String.from_marklogic(nil).should be_nil
265
+ end
266
+
267
+ it "should return empty string if blank" do
268
+ String.from_marklogic('').should == ''
269
+ end
270
+ end
271
+
272
+ context "Time.to_marklogic without Time.zone" do
273
+ before do
274
+ Time.zone = nil
275
+ end
276
+
277
+ it "should be nil if blank string" do
278
+ Time.to_marklogic('').should be_nil
279
+ end
280
+
281
+ it "should not be nil if nil" do
282
+ Time.to_marklogic(nil).should be_nil
283
+ end
284
+ end
285
+
286
+ context "Time.to_marklogic with Time.zone" do
287
+ it "should be nil if blank string" do
288
+ Time.zone = 'Hawaii'
289
+ Time.to_marklogic('').should be_nil
290
+ Time.zone = nil
291
+ end
292
+
293
+ it "should be nil if nil" do
294
+ Time.zone = 'Hawaii'
295
+ Time.to_marklogic(nil).should be_nil
296
+ Time.zone = nil
297
+ end
298
+ end
299
+
300
+ context "Time.from_marklogic without Time.zone" do
301
+ it "should be time" do
302
+ time = Time.now
303
+ Time.from_marklogic(time).should == time
304
+ end
305
+
306
+ it "should be nil if nil" do
307
+ Time.from_marklogic(nil).should be_nil
308
+ end
309
+ end
310
+
311
+ context "Time.from_marklogic with Time.zone" do
312
+ it "should be time in Time.zone" do
313
+ Time.zone = 'Hawaii'
314
+
315
+ time = Time.from_marklogic(Time.utc(2009, 10, 1))
316
+ time.should == Time.zone.local(2009, 9, 30, 14)
317
+ # time.is_a?(ActiveSupport::TimeWithZone).should be_truthy
318
+
319
+ Time.zone = nil
320
+ end
321
+
322
+ it "should be nil if nil" do
323
+ Time.zone = 'Hawaii'
324
+ Time.from_marklogic(nil).should be_nil
325
+ Time.zone = nil
326
+ end
327
+ end
328
+
329
+ context "ObjectId" do
330
+ context "#as_json" do
331
+ it "should convert object id to string" do
332
+ id = MarkLogic::ObjectId.new
333
+ id.as_json.should == id.to_s
334
+ end
335
+ end
336
+
337
+ context "#to_json" do
338
+ it "should convert object id to string" do
339
+ id = MarkLogic::ObjectId.new
340
+ id.to_json.should == %Q("#{id}")
341
+ end
342
+ end
343
+ end
344
+
345
+ context "Symbol.to_marklogic" do
346
+ it "should convert value to_sym" do
347
+ Symbol.to_marklogic('asdfasdfasdf').should == :asdfasdfasdf
348
+ end
349
+
350
+ it "should convert string if not string" do
351
+ Symbol.to_marklogic(123).should == :'123'
352
+ end
353
+
354
+ it "should return nil for nil" do
355
+ Symbol.to_marklogic(nil).should be_nil
356
+ end
357
+ end
358
+
359
+ context "Symbol.from_marklogic" do
360
+ it "should convert value to_sym" do
361
+ Symbol.from_marklogic(:asdfasdfasdf).should == :asdfasdfasdf
362
+ end
363
+
364
+ it "should return nil for nil" do
365
+ Symbol.from_marklogic(nil).should be_nil
366
+ end
367
+ end
368
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ module IdentityMapSpec
5
+ describe "IdentityMapMiddleware" do
6
+ include Rack::Test::Methods
7
+
8
+ def app
9
+ @app ||= Rack::Builder.new do
10
+ use MarkMapper::Middleware::IdentityMap
11
+
12
+ map "/" do
13
+ run lambda {|env| [200, {}, []] }
14
+ end
15
+
16
+ map "/fail" do
17
+ run lambda {|env| raise "FAIL!" }
18
+ end
19
+ end.to_app
20
+ end
21
+
22
+ context "" do
23
+ before do
24
+ @enabled = MarkMapper::Plugins::IdentityMap.enabled
25
+ MarkMapper::Plugins::IdentityMap.enabled = false
26
+ end
27
+
28
+ after do
29
+ MarkMapper::Plugins::IdentityMap.enabled = @enabled
30
+ end
31
+
32
+ it "should delegate" do
33
+ called = false
34
+ mw = MarkMapper::Middleware::IdentityMap.new lambda { |env|
35
+ called = true
36
+ [200, {}, nil]
37
+ }
38
+ mw.call({})
39
+ called.should be_truthy
40
+ end
41
+
42
+ it "should enable identity map during delegation" do
43
+ mw = MarkMapper::Middleware::IdentityMap.new lambda { |env|
44
+ MarkMapper::Plugins::IdentityMap.should be_enabled
45
+ [200, {}, nil]
46
+ }
47
+ mw.call({})
48
+ end
49
+
50
+ class Enum < Struct.new(:iter)
51
+ def each(&b)
52
+ iter.call(&b)
53
+ end
54
+ end
55
+
56
+ it "should enable IM for body each" do
57
+ mw = MarkMapper::Middleware::IdentityMap.new lambda { |env|
58
+ [200, {}, Enum.new(lambda { |&b|
59
+ MarkMapper::Plugins::IdentityMap.should be_enabled
60
+ b.call "hello"
61
+ })]
62
+ }
63
+ body = mw.call({}).last
64
+ body.each { |x| x.should eql('hello') }
65
+ end
66
+
67
+ it "should disable IM after body close" do
68
+ mw = MarkMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
69
+ body = mw.call({}).last
70
+ MarkMapper::Plugins::IdentityMap.should be_enabled
71
+ body.close
72
+ MarkMapper::Plugins::IdentityMap.should_not be_enabled
73
+ end
74
+
75
+ it "should clear IM after body close" do
76
+ mw = MarkMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
77
+ body = mw.call({}).last
78
+
79
+ MarkMapper::Plugins::IdentityMap.repository['hello'] = 'world'
80
+ MarkMapper::Plugins::IdentityMap.repository.should_not be_empty
81
+
82
+ body.close
83
+
84
+ MarkMapper::Plugins::IdentityMap.repository.should be_empty
85
+ end
86
+
87
+ context "with a successful request" do
88
+ it "should clear the identity map" do
89
+ expect(MarkMapper::Plugins::IdentityMap).to receive(:clear).twice
90
+ get '/'
91
+ end
92
+ end
93
+
94
+ context "when the request raises an error" do
95
+ it "should clear the identity map" do
96
+ expect(MarkMapper::Plugins::IdentityMap).to receive(:clear).once
97
+ get '/fail' rescue nil
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ # describe "IdentityMapMiddleware" do
104
+ # include Rack::Test::Methods
105
+
106
+ # def app
107
+ # @app ||= Rack::Builder.new do
108
+ # use MarkMapper::Middleware::IdentityMap
109
+ # map "/" do
110
+ # run lambda {|env| [200, {}, []] }
111
+ # end
112
+ # map "/fail" do
113
+ # run lambda {|env| raise "FAIL!" }
114
+ # end
115
+ # end.to_app
116
+ # end
117
+
118
+ # context "with a successful request" do
119
+ # it "should clear the identity map" do
120
+ # expect(MarkMapper::Plugins::IdentityMap).to receive(:clear).twice
121
+ # get '/'
122
+ # end
123
+ # end
124
+
125
+ # context "when the request raises an error" do
126
+ # it "should clear the identity map" do
127
+ # expect(MarkMapper::Plugins::IdentityMap).to receive(:clear).twice
128
+ # get '/fail' rescue nil
129
+ # end
130
+ # end
131
+
132
+
133
+ # end
134
+ end