mongoid-ultra 9.0.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (1086) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +156 -0
  4. data/Rakefile +34 -0
  5. data/lib/config/locales/en.yml +706 -0
  6. data/lib/mongoid/association/accessors.rb +430 -0
  7. data/lib/mongoid/association/bindable.rb +227 -0
  8. data/lib/mongoid/association/builders.rb +86 -0
  9. data/lib/mongoid/association/constrainable.rb +42 -0
  10. data/lib/mongoid/association/depending.rb +128 -0
  11. data/lib/mongoid/association/eager.rb +159 -0
  12. data/lib/mongoid/association/eager_loadable.rb +58 -0
  13. data/lib/mongoid/association/embedded/batchable.rb +396 -0
  14. data/lib/mongoid/association/embedded/cyclic.rb +102 -0
  15. data/lib/mongoid/association/embedded/eager.rb +22 -0
  16. data/lib/mongoid/association/embedded/embedded_in/binding.rb +74 -0
  17. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +38 -0
  18. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +124 -0
  19. data/lib/mongoid/association/embedded/embedded_in.rb +141 -0
  20. data/lib/mongoid/association/embedded/embeds_many/binding.rb +42 -0
  21. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +46 -0
  22. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +584 -0
  23. data/lib/mongoid/association/embedded/embeds_many.rb +183 -0
  24. data/lib/mongoid/association/embedded/embeds_one/binding.rb +43 -0
  25. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +53 -0
  26. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +183 -0
  27. data/lib/mongoid/association/embedded/embeds_one.rb +150 -0
  28. data/lib/mongoid/association/embedded.rb +7 -0
  29. data/lib/mongoid/association/macros.rb +228 -0
  30. data/lib/mongoid/association/many.rb +202 -0
  31. data/lib/mongoid/association/marshalable.rb +31 -0
  32. data/lib/mongoid/association/nested/many.rb +192 -0
  33. data/lib/mongoid/association/nested/nested_buildable.rb +66 -0
  34. data/lib/mongoid/association/nested/one.rb +154 -0
  35. data/lib/mongoid/association/nested.rb +15 -0
  36. data/lib/mongoid/association/one.rb +53 -0
  37. data/lib/mongoid/association/options.rb +127 -0
  38. data/lib/mongoid/association/proxy.rb +200 -0
  39. data/lib/mongoid/association/referenced/auto_save.rb +74 -0
  40. data/lib/mongoid/association/referenced/belongs_to/binding.rb +83 -0
  41. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +48 -0
  42. data/lib/mongoid/association/referenced/belongs_to/eager.rb +73 -0
  43. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +124 -0
  44. data/lib/mongoid/association/referenced/belongs_to.rb +226 -0
  45. data/lib/mongoid/association/referenced/counter_cache.rb +152 -0
  46. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +72 -0
  47. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
  48. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +53 -0
  49. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +345 -0
  50. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +277 -0
  51. data/lib/mongoid/association/referenced/has_many/binding.rb +36 -0
  52. data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
  53. data/lib/mongoid/association/referenced/has_many/eager.rb +44 -0
  54. data/lib/mongoid/association/referenced/has_many/enumerable.rb +490 -0
  55. data/lib/mongoid/association/referenced/has_many/proxy.rb +552 -0
  56. data/lib/mongoid/association/referenced/has_many.rb +251 -0
  57. data/lib/mongoid/association/referenced/has_one/binding.rb +42 -0
  58. data/lib/mongoid/association/referenced/has_one/buildable.rb +74 -0
  59. data/lib/mongoid/association/referenced/has_one/eager.rb +36 -0
  60. data/lib/mongoid/association/referenced/has_one/proxy.rb +108 -0
  61. data/lib/mongoid/association/referenced/has_one.rb +184 -0
  62. data/lib/mongoid/association/referenced/syncable.rb +155 -0
  63. data/lib/mongoid/association/referenced.rb +9 -0
  64. data/lib/mongoid/association/reflections.rb +69 -0
  65. data/lib/mongoid/association/relatable.rb +489 -0
  66. data/lib/mongoid/association.rb +135 -0
  67. data/lib/mongoid/atomic/modifiers.rb +297 -0
  68. data/lib/mongoid/atomic/paths/embedded/many.rb +60 -0
  69. data/lib/mongoid/atomic/paths/embedded/one.rb +40 -0
  70. data/lib/mongoid/atomic/paths/embedded.rb +27 -0
  71. data/lib/mongoid/atomic/paths/root.rb +36 -0
  72. data/lib/mongoid/atomic/paths.rb +4 -0
  73. data/lib/mongoid/atomic.rb +332 -0
  74. data/lib/mongoid/attributes/dynamic.rb +141 -0
  75. data/lib/mongoid/attributes/nested.rb +93 -0
  76. data/lib/mongoid/attributes/processing.rb +139 -0
  77. data/lib/mongoid/attributes/projector.rb +119 -0
  78. data/lib/mongoid/attributes/readonly.rb +73 -0
  79. data/lib/mongoid/attributes.rb +368 -0
  80. data/lib/mongoid/cacheable.rb +33 -0
  81. data/lib/mongoid/changeable.rb +520 -0
  82. data/lib/mongoid/clients/factory.rb +115 -0
  83. data/lib/mongoid/clients/options.rb +119 -0
  84. data/lib/mongoid/clients/sessions.rb +146 -0
  85. data/lib/mongoid/clients/storage_options.rb +76 -0
  86. data/lib/mongoid/clients/validators/storage.rb +46 -0
  87. data/lib/mongoid/clients/validators.rb +3 -0
  88. data/lib/mongoid/clients.rb +79 -0
  89. data/lib/mongoid/collection_configurable.rb +58 -0
  90. data/lib/mongoid/composable.rb +132 -0
  91. data/lib/mongoid/config/defaults.rb +45 -0
  92. data/lib/mongoid/config/encryption.rb +203 -0
  93. data/lib/mongoid/config/environment.rb +82 -0
  94. data/lib/mongoid/config/introspection.rb +150 -0
  95. data/lib/mongoid/config/options.rb +86 -0
  96. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  97. data/lib/mongoid/config/validators/client.rb +128 -0
  98. data/lib/mongoid/config/validators/option.rb +24 -0
  99. data/lib/mongoid/config/validators.rb +5 -0
  100. data/lib/mongoid/config.rb +380 -0
  101. data/lib/mongoid/contextual/aggregable/memory.rb +122 -0
  102. data/lib/mongoid/contextual/aggregable/mongo.rb +140 -0
  103. data/lib/mongoid/contextual/aggregable/none.rb +65 -0
  104. data/lib/mongoid/contextual/aggregable.rb +17 -0
  105. data/lib/mongoid/contextual/atomic.rb +245 -0
  106. data/lib/mongoid/contextual/command.rb +32 -0
  107. data/lib/mongoid/contextual/geo_near.rb +236 -0
  108. data/lib/mongoid/contextual/map_reduce.rb +230 -0
  109. data/lib/mongoid/contextual/memory.rb +770 -0
  110. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  111. data/lib/mongoid/contextual/mongo.rb +1065 -0
  112. data/lib/mongoid/contextual/none.rb +326 -0
  113. data/lib/mongoid/contextual/queryable.rb +24 -0
  114. data/lib/mongoid/contextual.rb +66 -0
  115. data/lib/mongoid/copyable.rb +115 -0
  116. data/lib/mongoid/criteria/findable.rb +143 -0
  117. data/lib/mongoid/criteria/includable.rb +93 -0
  118. data/lib/mongoid/criteria/inspectable.rb +24 -0
  119. data/lib/mongoid/criteria/marshalable.rb +55 -0
  120. data/lib/mongoid/criteria/modifiable.rb +235 -0
  121. data/lib/mongoid/criteria/options.rb +24 -0
  122. data/lib/mongoid/criteria/permission.rb +71 -0
  123. data/lib/mongoid/criteria/queryable/aggregable.rb +109 -0
  124. data/lib/mongoid/criteria/queryable/expandable.rb +68 -0
  125. data/lib/mongoid/criteria/queryable/extensions/array.rb +151 -0
  126. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +55 -0
  127. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +33 -0
  128. data/lib/mongoid/criteria/queryable/extensions/date.rb +63 -0
  129. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +52 -0
  130. data/lib/mongoid/criteria/queryable/extensions/hash.rb +163 -0
  131. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +75 -0
  132. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +73 -0
  133. data/lib/mongoid/criteria/queryable/extensions/object.rb +182 -0
  134. data/lib/mongoid/criteria/queryable/extensions/range.rb +102 -0
  135. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +72 -0
  136. data/lib/mongoid/criteria/queryable/extensions/set.rb +33 -0
  137. data/lib/mongoid/criteria/queryable/extensions/string.rb +112 -0
  138. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +64 -0
  139. data/lib/mongoid/criteria/queryable/extensions/time.rb +60 -0
  140. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +66 -0
  141. data/lib/mongoid/criteria/queryable/extensions.rb +25 -0
  142. data/lib/mongoid/criteria/queryable/key.rb +177 -0
  143. data/lib/mongoid/criteria/queryable/macroable.rb +26 -0
  144. data/lib/mongoid/criteria/queryable/mergeable.rb +434 -0
  145. data/lib/mongoid/criteria/queryable/optional.rb +380 -0
  146. data/lib/mongoid/criteria/queryable/options.rb +136 -0
  147. data/lib/mongoid/criteria/queryable/pipeline.rb +106 -0
  148. data/lib/mongoid/criteria/queryable/selectable.rb +952 -0
  149. data/lib/mongoid/criteria/queryable/selector.rb +289 -0
  150. data/lib/mongoid/criteria/queryable/smash.rb +128 -0
  151. data/lib/mongoid/criteria/queryable/storable.rb +237 -0
  152. data/lib/mongoid/criteria/queryable.rb +90 -0
  153. data/lib/mongoid/criteria/scopable.rb +166 -0
  154. data/lib/mongoid/criteria/translator.rb +45 -0
  155. data/lib/mongoid/criteria.rb +570 -0
  156. data/lib/mongoid/deprecable.rb +36 -0
  157. data/lib/mongoid/deprecation.rb +25 -0
  158. data/lib/mongoid/document.rb +389 -0
  159. data/lib/mongoid/encryptable.rb +49 -0
  160. data/lib/mongoid/equality.rb +63 -0
  161. data/lib/mongoid/errors/ambiguous_relationship.rb +50 -0
  162. data/lib/mongoid/errors/attribute_not_loaded.rb +33 -0
  163. data/lib/mongoid/errors/callback.rb +24 -0
  164. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  165. data/lib/mongoid/errors/criteria_argument_required.rb +18 -0
  166. data/lib/mongoid/errors/delete_restriction.rb +26 -0
  167. data/lib/mongoid/errors/document_not_destroyed.rb +22 -0
  168. data/lib/mongoid/errors/document_not_found.rb +125 -0
  169. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  170. data/lib/mongoid/errors/empty_config_file.rb +25 -0
  171. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  172. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +19 -0
  173. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  174. data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +32 -0
  175. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  176. data/lib/mongoid/errors/invalid_config_file.rb +25 -0
  177. data/lib/mongoid/errors/invalid_config_option.rb +26 -0
  178. data/lib/mongoid/errors/invalid_dependent_strategy.rb +31 -0
  179. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +24 -0
  180. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +23 -0
  181. data/lib/mongoid/errors/invalid_elem_match_operator.rb +32 -0
  182. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +25 -0
  183. data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +25 -0
  184. data/lib/mongoid/errors/invalid_expression_operator.rb +27 -0
  185. data/lib/mongoid/errors/invalid_field.rb +65 -0
  186. data/lib/mongoid/errors/invalid_field_operator.rb +32 -0
  187. data/lib/mongoid/errors/invalid_field_option.rb +34 -0
  188. data/lib/mongoid/errors/invalid_field_type.rb +26 -0
  189. data/lib/mongoid/errors/invalid_find.rb +18 -0
  190. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  191. data/lib/mongoid/errors/invalid_includes.rb +31 -0
  192. data/lib/mongoid/errors/invalid_index.rb +27 -0
  193. data/lib/mongoid/errors/invalid_options.rb +27 -0
  194. data/lib/mongoid/errors/invalid_path.rb +20 -0
  195. data/lib/mongoid/errors/invalid_persistence_option.rb +26 -0
  196. data/lib/mongoid/errors/invalid_query.rb +40 -0
  197. data/lib/mongoid/errors/invalid_relation.rb +61 -0
  198. data/lib/mongoid/errors/invalid_relation_option.rb +28 -0
  199. data/lib/mongoid/errors/invalid_scope.rb +23 -0
  200. data/lib/mongoid/errors/invalid_session_nesting.rb +16 -0
  201. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +39 -0
  202. data/lib/mongoid/errors/invalid_storage_options.rb +26 -0
  203. data/lib/mongoid/errors/invalid_storage_parent.rb +27 -0
  204. data/lib/mongoid/errors/invalid_time.rb +21 -0
  205. data/lib/mongoid/errors/invalid_transaction_nesting.rb +16 -0
  206. data/lib/mongoid/errors/inverse_not_found.rb +28 -0
  207. data/lib/mongoid/errors/mixed_client_configuration.rb +27 -0
  208. data/lib/mongoid/errors/mixed_relations.rb +31 -0
  209. data/lib/mongoid/errors/mongoid_error.rb +91 -0
  210. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +27 -0
  211. data/lib/mongoid/errors/no_client_config.rb +21 -0
  212. data/lib/mongoid/errors/no_client_database.rb +26 -0
  213. data/lib/mongoid/errors/no_client_hosts.rb +26 -0
  214. data/lib/mongoid/errors/no_clients_config.rb +19 -0
  215. data/lib/mongoid/errors/no_default_client.rb +22 -0
  216. data/lib/mongoid/errors/no_environment.rb +18 -0
  217. data/lib/mongoid/errors/no_map_reduce_output.rb +23 -0
  218. data/lib/mongoid/errors/no_metadata.rb +20 -0
  219. data/lib/mongoid/errors/no_parent.rb +23 -0
  220. data/lib/mongoid/errors/readonly_attribute.rb +24 -0
  221. data/lib/mongoid/errors/readonly_document.rb +21 -0
  222. data/lib/mongoid/errors/rollback.rb +14 -0
  223. data/lib/mongoid/errors/scope_overwrite.rb +22 -0
  224. data/lib/mongoid/errors/sessions_not_supported.rb +16 -0
  225. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +22 -0
  226. data/lib/mongoid/errors/transaction_error.rb +22 -0
  227. data/lib/mongoid/errors/transactions_not_supported.rb +16 -0
  228. data/lib/mongoid/errors/unknown_attribute.rb +24 -0
  229. data/lib/mongoid/errors/unknown_model.rb +24 -0
  230. data/lib/mongoid/errors/unsaved_document.rb +20 -0
  231. data/lib/mongoid/errors/unsupported_javascript.rb +26 -0
  232. data/lib/mongoid/errors/validations.rb +30 -0
  233. data/lib/mongoid/errors.rb +73 -0
  234. data/lib/mongoid/evolvable.rb +18 -0
  235. data/lib/mongoid/extensions/array.rb +173 -0
  236. data/lib/mongoid/extensions/big_decimal.rb +90 -0
  237. data/lib/mongoid/extensions/binary.rb +42 -0
  238. data/lib/mongoid/extensions/boolean.rb +26 -0
  239. data/lib/mongoid/extensions/date.rb +87 -0
  240. data/lib/mongoid/extensions/date_time.rb +60 -0
  241. data/lib/mongoid/extensions/decimal128.rb +36 -0
  242. data/lib/mongoid/extensions/false_class.rb +35 -0
  243. data/lib/mongoid/extensions/float.rb +55 -0
  244. data/lib/mongoid/extensions/hash.rb +248 -0
  245. data/lib/mongoid/extensions/integer.rb +63 -0
  246. data/lib/mongoid/extensions/module.rb +27 -0
  247. data/lib/mongoid/extensions/nil_class.rb +30 -0
  248. data/lib/mongoid/extensions/object.rb +250 -0
  249. data/lib/mongoid/extensions/object_id.rb +49 -0
  250. data/lib/mongoid/extensions/range.rb +108 -0
  251. data/lib/mongoid/extensions/raw_value.rb +32 -0
  252. data/lib/mongoid/extensions/regexp.rb +34 -0
  253. data/lib/mongoid/extensions/set.rb +57 -0
  254. data/lib/mongoid/extensions/string.rb +171 -0
  255. data/lib/mongoid/extensions/symbol.rb +38 -0
  256. data/lib/mongoid/extensions/time.rb +105 -0
  257. data/lib/mongoid/extensions/time_with_zone.rb +74 -0
  258. data/lib/mongoid/extensions/true_class.rb +35 -0
  259. data/lib/mongoid/extensions.rb +27 -0
  260. data/lib/mongoid/factory.rb +134 -0
  261. data/lib/mongoid/fields/encrypted.rb +41 -0
  262. data/lib/mongoid/fields/foreign_key.rb +166 -0
  263. data/lib/mongoid/fields/localized.rb +99 -0
  264. data/lib/mongoid/fields/standard.rb +241 -0
  265. data/lib/mongoid/fields/validators/macro.rb +125 -0
  266. data/lib/mongoid/fields/validators.rb +3 -0
  267. data/lib/mongoid/fields.rb +851 -0
  268. data/lib/mongoid/findable.rb +243 -0
  269. data/lib/mongoid/indexable/specification.rb +138 -0
  270. data/lib/mongoid/indexable/validators/options.rb +110 -0
  271. data/lib/mongoid/indexable.rb +148 -0
  272. data/lib/mongoid/inspectable.rb +52 -0
  273. data/lib/mongoid/interceptable.rb +320 -0
  274. data/lib/mongoid/loadable.rb +82 -0
  275. data/lib/mongoid/loggable.rb +66 -0
  276. data/lib/mongoid/matchable.rb +23 -0
  277. data/lib/mongoid/matcher/all.rb +22 -0
  278. data/lib/mongoid/matcher/and.rb +21 -0
  279. data/lib/mongoid/matcher/bits.rb +41 -0
  280. data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
  281. data/lib/mongoid/matcher/bits_all_set.rb +20 -0
  282. data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
  283. data/lib/mongoid/matcher/bits_any_set.rb +20 -0
  284. data/lib/mongoid/matcher/elem_match.rb +36 -0
  285. data/lib/mongoid/matcher/elem_match_expression.rb +20 -0
  286. data/lib/mongoid/matcher/eq.rb +11 -0
  287. data/lib/mongoid/matcher/eq_impl.rb +62 -0
  288. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +25 -0
  289. data/lib/mongoid/matcher/exists.rb +15 -0
  290. data/lib/mongoid/matcher/expression.rb +35 -0
  291. data/lib/mongoid/matcher/expression_operator.rb +19 -0
  292. data/lib/mongoid/matcher/field_expression.rb +62 -0
  293. data/lib/mongoid/matcher/field_operator.rb +54 -0
  294. data/lib/mongoid/matcher/gt.rb +17 -0
  295. data/lib/mongoid/matcher/gte.rb +17 -0
  296. data/lib/mongoid/matcher/in.rb +25 -0
  297. data/lib/mongoid/matcher/lt.rb +17 -0
  298. data/lib/mongoid/matcher/lte.rb +17 -0
  299. data/lib/mongoid/matcher/mod.rb +17 -0
  300. data/lib/mongoid/matcher/ne.rb +16 -0
  301. data/lib/mongoid/matcher/nin.rb +11 -0
  302. data/lib/mongoid/matcher/nor.rb +25 -0
  303. data/lib/mongoid/matcher/not.rb +29 -0
  304. data/lib/mongoid/matcher/or.rb +21 -0
  305. data/lib/mongoid/matcher/regex.rb +41 -0
  306. data/lib/mongoid/matcher/size.rb +26 -0
  307. data/lib/mongoid/matcher/type.rb +99 -0
  308. data/lib/mongoid/matcher.rb +115 -0
  309. data/lib/mongoid/persistable/creatable.rb +184 -0
  310. data/lib/mongoid/persistable/deletable.rb +136 -0
  311. data/lib/mongoid/persistable/destroyable.rb +80 -0
  312. data/lib/mongoid/persistable/incrementable.rb +35 -0
  313. data/lib/mongoid/persistable/logical.rb +36 -0
  314. data/lib/mongoid/persistable/maxable.rb +36 -0
  315. data/lib/mongoid/persistable/minable.rb +36 -0
  316. data/lib/mongoid/persistable/multipliable.rb +35 -0
  317. data/lib/mongoid/persistable/poppable.rb +36 -0
  318. data/lib/mongoid/persistable/pullable.rb +50 -0
  319. data/lib/mongoid/persistable/pushable.rb +66 -0
  320. data/lib/mongoid/persistable/renamable.rb +37 -0
  321. data/lib/mongoid/persistable/savable.rb +59 -0
  322. data/lib/mongoid/persistable/settable.rb +93 -0
  323. data/lib/mongoid/persistable/unsettable.rb +36 -0
  324. data/lib/mongoid/persistable/updatable.rb +233 -0
  325. data/lib/mongoid/persistable/upsertable.rb +82 -0
  326. data/lib/mongoid/persistable.rb +317 -0
  327. data/lib/mongoid/persistence_context.rb +275 -0
  328. data/lib/mongoid/positional.rb +72 -0
  329. data/lib/mongoid/query_cache.rb +64 -0
  330. data/lib/mongoid/railtie.rb +122 -0
  331. data/lib/mongoid/railties/console_sandbox.rb +41 -0
  332. data/lib/mongoid/railties/controller_runtime.rb +88 -0
  333. data/lib/mongoid/railties/database.rake +93 -0
  334. data/lib/mongoid/reloadable.rb +94 -0
  335. data/lib/mongoid/scopable.rb +323 -0
  336. data/lib/mongoid/selectable.rb +49 -0
  337. data/lib/mongoid/serializable.rb +170 -0
  338. data/lib/mongoid/shardable.rb +154 -0
  339. data/lib/mongoid/stateful.rb +155 -0
  340. data/lib/mongoid/stringified_symbol.rb +52 -0
  341. data/lib/mongoid/tasks/database.rake +51 -0
  342. data/lib/mongoid/tasks/database.rb +214 -0
  343. data/lib/mongoid/tasks/encryption.rake +19 -0
  344. data/lib/mongoid/tasks/encryption.rb +102 -0
  345. data/lib/mongoid/threaded/lifecycle.rb +150 -0
  346. data/lib/mongoid/threaded.rb +396 -0
  347. data/lib/mongoid/timestamps/created/short.rb +20 -0
  348. data/lib/mongoid/timestamps/created.rb +34 -0
  349. data/lib/mongoid/timestamps/short.rb +11 -0
  350. data/lib/mongoid/timestamps/timeless.rb +95 -0
  351. data/lib/mongoid/timestamps/updated/short.rb +20 -0
  352. data/lib/mongoid/timestamps/updated.rb +44 -0
  353. data/lib/mongoid/timestamps.rb +17 -0
  354. data/lib/mongoid/touchable.rb +228 -0
  355. data/lib/mongoid/traversable.rb +337 -0
  356. data/lib/mongoid/utils.rb +22 -0
  357. data/lib/mongoid/validatable/associated.rb +47 -0
  358. data/lib/mongoid/validatable/format.rb +21 -0
  359. data/lib/mongoid/validatable/length.rb +21 -0
  360. data/lib/mongoid/validatable/localizable.rb +29 -0
  361. data/lib/mongoid/validatable/macros.rb +89 -0
  362. data/lib/mongoid/validatable/presence.rb +81 -0
  363. data/lib/mongoid/validatable/queryable.rb +29 -0
  364. data/lib/mongoid/validatable/uniqueness.rb +304 -0
  365. data/lib/mongoid/validatable.rb +163 -0
  366. data/lib/mongoid/version.rb +5 -0
  367. data/lib/mongoid/warnings.rb +30 -0
  368. data/lib/mongoid.rb +157 -0
  369. data/lib/rails/generators/mongoid/config/config_generator.rb +36 -0
  370. data/lib/rails/generators/mongoid/config/templates/mongoid.rb +24 -0
  371. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +137 -0
  372. data/lib/rails/generators/mongoid/model/model_generator.rb +25 -0
  373. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +19 -0
  374. data/lib/rails/generators/mongoid_generator.rb +29 -0
  375. data/lib/rails/mongoid.rb +27 -0
  376. data/spec/README.md +33 -0
  377. data/spec/config/mongoid.yml +70 -0
  378. data/spec/config/mongoid_with_schema_map_uuid.yml +27 -0
  379. data/spec/integration/app_spec.rb +377 -0
  380. data/spec/integration/associations/belongs_to_spec.rb +33 -0
  381. data/spec/integration/associations/embedded_dirty_spec.rb +57 -0
  382. data/spec/integration/associations/embedded_spec.rb +351 -0
  383. data/spec/integration/associations/embeds_many_spec.rb +219 -0
  384. data/spec/integration/associations/embeds_one_spec.rb +41 -0
  385. data/spec/integration/associations/foreign_key_spec.rb +107 -0
  386. data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
  387. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +21 -0
  388. data/spec/integration/associations/has_many_spec.rb +128 -0
  389. data/spec/integration/associations/has_one_spec.rb +272 -0
  390. data/spec/integration/associations/nested_attributes_assignment_spec.rb +115 -0
  391. data/spec/integration/associations/reverse_population_spec.rb +34 -0
  392. data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
  393. data/spec/integration/associations/scope_option_spec.rb +101 -0
  394. data/spec/integration/atomic/modifiers_spec.rb +116 -0
  395. data/spec/integration/bson_regexp_raw_spec.rb +19 -0
  396. data/spec/integration/callbacks_models.rb +155 -0
  397. data/spec/integration/callbacks_spec.rb +557 -0
  398. data/spec/integration/contextual/empty_spec.rb +141 -0
  399. data/spec/integration/criteria/alias_query_spec.rb +97 -0
  400. data/spec/integration/criteria/date_field_spec.rb +40 -0
  401. data/spec/integration/criteria/default_scope_spec.rb +72 -0
  402. data/spec/integration/criteria/logical_spec.rb +105 -0
  403. data/spec/integration/criteria/range_spec.rb +357 -0
  404. data/spec/integration/criteria/raw_value_spec.rb +525 -0
  405. data/spec/integration/criteria/time_with_zone_spec.rb +142 -0
  406. data/spec/integration/discriminator_key_spec.rb +391 -0
  407. data/spec/integration/discriminator_value_spec.rb +206 -0
  408. data/spec/integration/document_spec.rb +51 -0
  409. data/spec/integration/dots_and_dollars_spec.rb +277 -0
  410. data/spec/integration/encryption_spec.rb +81 -0
  411. data/spec/integration/i18n_fallbacks_spec.rb +74 -0
  412. data/spec/integration/matcher_examples_spec.rb +765 -0
  413. data/spec/integration/matcher_operator_data/all.yml +140 -0
  414. data/spec/integration/matcher_operator_data/and.yml +93 -0
  415. data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
  416. data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
  417. data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
  418. data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
  419. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  420. data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
  421. data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
  422. data/spec/integration/matcher_operator_data/eq.yml +191 -0
  423. data/spec/integration/matcher_operator_data/exists.yml +213 -0
  424. data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
  425. data/spec/integration/matcher_operator_data/gt.yml +132 -0
  426. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  427. data/spec/integration/matcher_operator_data/gte.yml +132 -0
  428. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  429. data/spec/integration/matcher_operator_data/implicit.yml +331 -0
  430. data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
  431. data/spec/integration/matcher_operator_data/in.yml +210 -0
  432. data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
  433. data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
  434. data/spec/integration/matcher_operator_data/lt.yml +132 -0
  435. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  436. data/spec/integration/matcher_operator_data/lte.yml +132 -0
  437. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  438. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  439. data/spec/integration/matcher_operator_data/multiple.yml +29 -0
  440. data/spec/integration/matcher_operator_data/ne.yml +150 -0
  441. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  442. data/spec/integration/matcher_operator_data/nin.yml +114 -0
  443. data/spec/integration/matcher_operator_data/nor.yml +126 -0
  444. data/spec/integration/matcher_operator_data/not.yml +196 -0
  445. data/spec/integration/matcher_operator_data/or.yml +137 -0
  446. data/spec/integration/matcher_operator_data/regex.yml +174 -0
  447. data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
  448. data/spec/integration/matcher_operator_data/size.yml +174 -0
  449. data/spec/integration/matcher_operator_data/type.yml +70 -0
  450. data/spec/integration/matcher_operator_data/type_array.yml +16 -0
  451. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  452. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  453. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  454. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  455. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  456. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  457. data/spec/integration/matcher_operator_data/type_decimal.yml +41 -0
  458. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  459. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  460. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  461. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  462. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  463. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  464. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  465. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  466. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  467. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  468. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  469. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  470. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  471. data/spec/integration/matcher_operator_spec.rb +122 -0
  472. data/spec/integration/matcher_spec.rb +237 -0
  473. data/spec/integration/persistence/range_field_spec.rb +350 -0
  474. data/spec/integration/server_query_spec.rb +141 -0
  475. data/spec/integration/shardable_spec.rb +148 -0
  476. data/spec/integration/stringified_symbol_field_spec.rb +203 -0
  477. data/spec/lite_spec_helper.rb +77 -0
  478. data/spec/mongoid/association/accessors_spec.rb +1049 -0
  479. data/spec/mongoid/association/auto_save_spec.rb +403 -0
  480. data/spec/mongoid/association/builders_spec.rb +255 -0
  481. data/spec/mongoid/association/constrainable_spec.rb +117 -0
  482. data/spec/mongoid/association/counter_cache_spec.rb +452 -0
  483. data/spec/mongoid/association/depending_spec.rb +959 -0
  484. data/spec/mongoid/association/eager_spec.rb +331 -0
  485. data/spec/mongoid/association/embedded/cyclic_spec.rb +180 -0
  486. data/spec/mongoid/association/embedded/dirty_spec.rb +67 -0
  487. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +174 -0
  488. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +90 -0
  489. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +646 -0
  490. data/spec/mongoid/association/embedded/embedded_in_spec.rb +914 -0
  491. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +56 -0
  492. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +218 -0
  493. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +4858 -0
  494. data/spec/mongoid/association/embedded/embeds_many_models.rb +241 -0
  495. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +63 -0
  496. data/spec/mongoid/association/embedded/embeds_many_spec.rb +932 -0
  497. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +79 -0
  498. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +106 -0
  499. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +1038 -0
  500. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +8 -0
  501. data/spec/mongoid/association/embedded/embeds_one_models.rb +77 -0
  502. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
  503. data/spec/mongoid/association/embedded/embeds_one_spec.rb +984 -0
  504. data/spec/mongoid/association/macros_spec.rb +1116 -0
  505. data/spec/mongoid/association/nested/many_spec.rb +232 -0
  506. data/spec/mongoid/association/nested/one_spec.rb +253 -0
  507. data/spec/mongoid/association/options_spec.rb +1323 -0
  508. data/spec/mongoid/association/polymorphic_spec.rb +162 -0
  509. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +244 -0
  510. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +292 -0
  511. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +412 -0
  512. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +1388 -0
  513. data/spec/mongoid/association/referenced/belongs_to_models.rb +11 -0
  514. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
  515. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2030 -0
  516. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +180 -0
  517. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +148 -0
  518. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +196 -0
  519. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +76 -0
  520. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +3812 -0
  521. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +92 -0
  522. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
  523. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1104 -0
  524. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +155 -0
  525. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +251 -0
  526. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +290 -0
  527. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +2232 -0
  528. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
  529. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +4118 -0
  530. data/spec/mongoid/association/referenced/has_many_models.rb +97 -0
  531. data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
  532. data/spec/mongoid/association/referenced/has_many_spec.rb +1273 -0
  533. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +133 -0
  534. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +159 -0
  535. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +193 -0
  536. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +1248 -0
  537. data/spec/mongoid/association/referenced/has_one_models.rb +113 -0
  538. data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
  539. data/spec/mongoid/association/referenced/has_one_spec.rb +1383 -0
  540. data/spec/mongoid/association/reflections_spec.rb +92 -0
  541. data/spec/mongoid/association/syncable_spec.rb +513 -0
  542. data/spec/mongoid/association_spec.rb +191 -0
  543. data/spec/mongoid/atomic/modifiers_spec.rb +505 -0
  544. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +120 -0
  545. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +112 -0
  546. data/spec/mongoid/atomic/paths/root_spec.rb +50 -0
  547. data/spec/mongoid/atomic/paths_spec.rb +350 -0
  548. data/spec/mongoid/atomic_spec.rb +412 -0
  549. data/spec/mongoid/attributes/dynamic_spec.rb +152 -0
  550. data/spec/mongoid/attributes/nested_spec.rb +5000 -0
  551. data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
  552. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  553. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  554. data/spec/mongoid/attributes/projector_spec.rb +40 -0
  555. data/spec/mongoid/attributes/readonly_spec.rb +271 -0
  556. data/spec/mongoid/attributes_spec.rb +2709 -0
  557. data/spec/mongoid/cacheable_spec.rb +114 -0
  558. data/spec/mongoid/changeable_spec.rb +2189 -0
  559. data/spec/mongoid/clients/factory_spec.rb +471 -0
  560. data/spec/mongoid/clients/options_spec.rb +521 -0
  561. data/spec/mongoid/clients/sessions_spec.rb +297 -0
  562. data/spec/mongoid/clients/transactions_spec.rb +1077 -0
  563. data/spec/mongoid/clients/transactions_spec_models.rb +114 -0
  564. data/spec/mongoid/clients_spec.rb +1218 -0
  565. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  566. data/spec/mongoid/composable_spec.rb +33 -0
  567. data/spec/mongoid/config/defaults_spec.rb +92 -0
  568. data/spec/mongoid/config/encryption_spec.rb +150 -0
  569. data/spec/mongoid/config/environment_spec.rb +175 -0
  570. data/spec/mongoid/config/introspection_spec.rb +113 -0
  571. data/spec/mongoid/config/options_spec.rb +75 -0
  572. data/spec/mongoid/config_spec.rb +861 -0
  573. data/spec/mongoid/contextual/aggregable/memory_spec.rb +573 -0
  574. data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
  575. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +62 -0
  576. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +595 -0
  577. data/spec/mongoid/contextual/aggregable/none_spec.rb +48 -0
  578. data/spec/mongoid/contextual/atomic_spec.rb +1142 -0
  579. data/spec/mongoid/contextual/geo_near_spec.rb +471 -0
  580. data/spec/mongoid/contextual/map_reduce_spec.rb +459 -0
  581. data/spec/mongoid/contextual/memory_spec.rb +2931 -0
  582. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +193 -0
  583. data/spec/mongoid/contextual/mongo_spec.rb +4534 -0
  584. data/spec/mongoid/contextual/none_spec.rb +170 -0
  585. data/spec/mongoid/copyable_spec.rb +1142 -0
  586. data/spec/mongoid/copyable_spec_models.rb +47 -0
  587. data/spec/mongoid/criteria/findable_spec.rb +1132 -0
  588. data/spec/mongoid/criteria/includable_spec.rb +1492 -0
  589. data/spec/mongoid/criteria/includable_spec_models.rb +54 -0
  590. data/spec/mongoid/criteria/inspectable_spec.rb +29 -0
  591. data/spec/mongoid/criteria/marshalable_spec.rb +47 -0
  592. data/spec/mongoid/criteria/modifiable_spec.rb +1812 -0
  593. data/spec/mongoid/criteria/options_spec.rb +31 -0
  594. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +372 -0
  595. data/spec/mongoid/criteria/queryable/expandable_spec.rb +61 -0
  596. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +513 -0
  597. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +169 -0
  598. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +215 -0
  599. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +352 -0
  600. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +434 -0
  601. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +67 -0
  602. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +314 -0
  603. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +67 -0
  604. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +79 -0
  605. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +119 -0
  606. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +110 -0
  607. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +370 -0
  608. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +91 -0
  609. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +92 -0
  610. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +41 -0
  611. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +264 -0
  612. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +110 -0
  613. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +406 -0
  614. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +387 -0
  615. data/spec/mongoid/criteria/queryable/key_spec.rb +96 -0
  616. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +182 -0
  617. data/spec/mongoid/criteria/queryable/optional_spec.rb +1332 -0
  618. data/spec/mongoid/criteria/queryable/options_spec.rb +362 -0
  619. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +214 -0
  620. data/spec/mongoid/criteria/queryable/queryable_spec.rb +139 -0
  621. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2538 -0
  622. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +38 -0
  623. data/spec/mongoid/criteria/queryable/selectable_spec.rb +2327 -0
  624. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +589 -0
  625. data/spec/mongoid/criteria/queryable/selector_spec.rb +990 -0
  626. data/spec/mongoid/criteria/queryable/smash_spec.rb +32 -0
  627. data/spec/mongoid/criteria/queryable/storable_spec.rb +203 -0
  628. data/spec/mongoid/criteria/scopable_spec.rb +601 -0
  629. data/spec/mongoid/criteria/translator_spec.rb +132 -0
  630. data/spec/mongoid/criteria_projection_spec.rb +406 -0
  631. data/spec/mongoid/criteria_spec.rb +3238 -0
  632. data/spec/mongoid/document_fields_spec.rb +262 -0
  633. data/spec/mongoid/document_persistence_context_spec.rb +32 -0
  634. data/spec/mongoid/document_query_spec.rb +89 -0
  635. data/spec/mongoid/document_spec.rb +1365 -0
  636. data/spec/mongoid/equality_spec.rb +241 -0
  637. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +31 -0
  638. data/spec/mongoid/errors/attribute_not_loaded_spec.rb +31 -0
  639. data/spec/mongoid/errors/callback_spec.rb +31 -0
  640. data/spec/mongoid/errors/delete_restriction_spec.rb +31 -0
  641. data/spec/mongoid/errors/document_not_destroyed_spec.rb +35 -0
  642. data/spec/mongoid/errors/document_not_found_spec.rb +182 -0
  643. data/spec/mongoid/errors/invalid_collection_spec.rb +38 -0
  644. data/spec/mongoid/errors/invalid_config_file_spec.rb +31 -0
  645. data/spec/mongoid/errors/invalid_config_option_spec.rb +31 -0
  646. data/spec/mongoid/errors/invalid_field_option_spec.rb +31 -0
  647. data/spec/mongoid/errors/invalid_field_spec.rb +39 -0
  648. data/spec/mongoid/errors/invalid_field_type_spec.rb +55 -0
  649. data/spec/mongoid/errors/invalid_find_spec.rb +31 -0
  650. data/spec/mongoid/errors/invalid_includes_spec.rb +42 -0
  651. data/spec/mongoid/errors/invalid_index_spec.rb +31 -0
  652. data/spec/mongoid/errors/invalid_options_spec.rb +31 -0
  653. data/spec/mongoid/errors/invalid_path_spec.rb +25 -0
  654. data/spec/mongoid/errors/invalid_relation_spec.rb +39 -0
  655. data/spec/mongoid/errors/invalid_scope_spec.rb +31 -0
  656. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +19 -0
  657. data/spec/mongoid/errors/invalid_storage_options_spec.rb +31 -0
  658. data/spec/mongoid/errors/invalid_time_spec.rb +31 -0
  659. data/spec/mongoid/errors/inverse_not_found_spec.rb +31 -0
  660. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +31 -0
  661. data/spec/mongoid/errors/mixed_relations_spec.rb +31 -0
  662. data/spec/mongoid/errors/mongoid_error_spec.rb +83 -0
  663. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +31 -0
  664. data/spec/mongoid/errors/no_client_config_spec.rb +31 -0
  665. data/spec/mongoid/errors/no_client_database_spec.rb +31 -0
  666. data/spec/mongoid/errors/no_client_hosts_spec.rb +31 -0
  667. data/spec/mongoid/errors/no_clients_config_spec.rb +31 -0
  668. data/spec/mongoid/errors/no_environment_spec.rb +31 -0
  669. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +31 -0
  670. data/spec/mongoid/errors/no_metadata_spec.rb +25 -0
  671. data/spec/mongoid/errors/no_parent_spec.rb +31 -0
  672. data/spec/mongoid/errors/readonly_attribute_spec.rb +31 -0
  673. data/spec/mongoid/errors/readonly_document_spec.rb +31 -0
  674. data/spec/mongoid/errors/scope_overwrite_spec.rb +31 -0
  675. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +31 -0
  676. data/spec/mongoid/errors/unknown_attribute_spec.rb +31 -0
  677. data/spec/mongoid/errors/unsaved_document_spec.rb +39 -0
  678. data/spec/mongoid/errors/unsupported_javascript_spec.rb +31 -0
  679. data/spec/mongoid/errors/validations_spec.rb +47 -0
  680. data/spec/mongoid/extensions/array_spec.rb +620 -0
  681. data/spec/mongoid/extensions/big_decimal_spec.rb +908 -0
  682. data/spec/mongoid/extensions/binary_spec.rb +97 -0
  683. data/spec/mongoid/extensions/boolean_spec.rb +137 -0
  684. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +339 -0
  685. data/spec/mongoid/extensions/date_spec.rb +131 -0
  686. data/spec/mongoid/extensions/date_time_spec.rb +107 -0
  687. data/spec/mongoid/extensions/decimal128_spec.rb +46 -0
  688. data/spec/mongoid/extensions/false_class_spec.rb +44 -0
  689. data/spec/mongoid/extensions/float_spec.rb +140 -0
  690. data/spec/mongoid/extensions/hash_spec.rb +518 -0
  691. data/spec/mongoid/extensions/integer_spec.rb +135 -0
  692. data/spec/mongoid/extensions/module_spec.rb +44 -0
  693. data/spec/mongoid/extensions/nil_class_spec.rb +13 -0
  694. data/spec/mongoid/extensions/object_id_spec.rb +948 -0
  695. data/spec/mongoid/extensions/object_spec.rb +288 -0
  696. data/spec/mongoid/extensions/range_spec.rb +364 -0
  697. data/spec/mongoid/extensions/raw_value_spec.rb +66 -0
  698. data/spec/mongoid/extensions/regexp_spec.rb +97 -0
  699. data/spec/mongoid/extensions/set_spec.rb +141 -0
  700. data/spec/mongoid/extensions/string_spec.rb +387 -0
  701. data/spec/mongoid/extensions/stringified_symbol_spec.rb +84 -0
  702. data/spec/mongoid/extensions/symbol_spec.rb +71 -0
  703. data/spec/mongoid/extensions/time_spec.rb +706 -0
  704. data/spec/mongoid/extensions/time_with_zone_spec.rb +361 -0
  705. data/spec/mongoid/extensions/true_class_spec.rb +44 -0
  706. data/spec/mongoid/factory_spec.rb +534 -0
  707. data/spec/mongoid/fields/foreign_key_spec.rb +718 -0
  708. data/spec/mongoid/fields/localized_spec.rb +571 -0
  709. data/spec/mongoid/fields/standard_spec.rb +168 -0
  710. data/spec/mongoid/fields_spec.rb +2062 -0
  711. data/spec/mongoid/findable_spec.rb +929 -0
  712. data/spec/mongoid/indexable/specification_spec.rb +247 -0
  713. data/spec/mongoid/indexable_spec.rb +962 -0
  714. data/spec/mongoid/inspectable_spec.rb +86 -0
  715. data/spec/mongoid/interceptable_spec.rb +2373 -0
  716. data/spec/mongoid/interceptable_spec_models.rb +307 -0
  717. data/spec/mongoid/loading_spec.rb +109 -0
  718. data/spec/mongoid/loggable_spec.rb +23 -0
  719. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  720. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
  721. data/spec/mongoid/matcher/extract_attribute_spec.rb +36 -0
  722. data/spec/mongoid/mongoizable_spec.rb +285 -0
  723. data/spec/mongoid/persistable/creatable_spec.rb +662 -0
  724. data/spec/mongoid/persistable/deletable_spec.rb +580 -0
  725. data/spec/mongoid/persistable/destroyable_spec.rb +652 -0
  726. data/spec/mongoid/persistable/incrementable_spec.rb +278 -0
  727. data/spec/mongoid/persistable/logical_spec.rb +196 -0
  728. data/spec/mongoid/persistable/maxable_spec.rb +146 -0
  729. data/spec/mongoid/persistable/minable_spec.rb +146 -0
  730. data/spec/mongoid/persistable/multipliable_spec.rb +226 -0
  731. data/spec/mongoid/persistable/poppable_spec.rb +167 -0
  732. data/spec/mongoid/persistable/pullable_spec.rb +330 -0
  733. data/spec/mongoid/persistable/pushable_spec.rb +425 -0
  734. data/spec/mongoid/persistable/renamable_spec.rb +187 -0
  735. data/spec/mongoid/persistable/savable_spec.rb +780 -0
  736. data/spec/mongoid/persistable/settable_spec.rb +581 -0
  737. data/spec/mongoid/persistable/unsettable_spec.rb +207 -0
  738. data/spec/mongoid/persistable/updatable_spec.rb +752 -0
  739. data/spec/mongoid/persistable/upsertable_spec.rb +228 -0
  740. data/spec/mongoid/persistable_spec.rb +321 -0
  741. data/spec/mongoid/persistence_context_spec.rb +695 -0
  742. data/spec/mongoid/positional_spec.rb +223 -0
  743. data/spec/mongoid/query_cache_middleware_spec.rb +50 -0
  744. data/spec/mongoid/query_cache_spec.rb +832 -0
  745. data/spec/mongoid/railties/console_sandbox_spec.rb +43 -0
  746. data/spec/mongoid/relations/proxy_spec.rb +126 -0
  747. data/spec/mongoid/reloadable_spec.rb +629 -0
  748. data/spec/mongoid/scopable_spec.rb +1312 -0
  749. data/spec/mongoid/selectable_spec.rb +136 -0
  750. data/spec/mongoid/serializable_spec.rb +942 -0
  751. data/spec/mongoid/shardable_models.rb +75 -0
  752. data/spec/mongoid/shardable_spec.rb +296 -0
  753. data/spec/mongoid/stateful_spec.rb +271 -0
  754. data/spec/mongoid/tasks/database_rake_spec.rb +370 -0
  755. data/spec/mongoid/tasks/database_spec.rb +308 -0
  756. data/spec/mongoid/tasks/encryption_spec.rb +147 -0
  757. data/spec/mongoid/threaded_spec.rb +343 -0
  758. data/spec/mongoid/timestamps/created/short_spec.rb +53 -0
  759. data/spec/mongoid/timestamps/created_spec.rb +46 -0
  760. data/spec/mongoid/timestamps/timeless_spec.rb +149 -0
  761. data/spec/mongoid/timestamps/updated/short_spec.rb +92 -0
  762. data/spec/mongoid/timestamps/updated_spec.rb +84 -0
  763. data/spec/mongoid/timestamps_spec.rb +498 -0
  764. data/spec/mongoid/timestamps_spec_models.rb +67 -0
  765. data/spec/mongoid/touchable_spec.rb +1403 -0
  766. data/spec/mongoid/touchable_spec_models.rb +153 -0
  767. data/spec/mongoid/traversable_spec.rb +1395 -0
  768. data/spec/mongoid/validatable/associated_spec.rb +208 -0
  769. data/spec/mongoid/validatable/format_spec.rb +85 -0
  770. data/spec/mongoid/validatable/length_spec.rb +225 -0
  771. data/spec/mongoid/validatable/numericality_spec.rb +32 -0
  772. data/spec/mongoid/validatable/presence_spec.rb +592 -0
  773. data/spec/mongoid/validatable/uniqueness_spec.rb +2548 -0
  774. data/spec/mongoid/validatable_spec.rb +327 -0
  775. data/spec/mongoid/warnings_spec.rb +35 -0
  776. data/spec/mongoid_spec.rb +98 -0
  777. data/spec/rails/controller_extension/controller_runtime_spec.rb +112 -0
  778. data/spec/rails/mongoid_spec.rb +53 -0
  779. data/spec/shared/LICENSE +20 -0
  780. data/spec/shared/bin/get-mongodb-download-url +17 -0
  781. data/spec/shared/bin/s3-copy +45 -0
  782. data/spec/shared/bin/s3-upload +69 -0
  783. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  784. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  785. data/spec/shared/lib/mrss/constraints.rb +378 -0
  786. data/spec/shared/lib/mrss/docker_runner.rb +291 -0
  787. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  788. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  789. data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
  790. data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
  791. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  792. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  793. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  794. data/spec/shared/lib/mrss/utils.rb +15 -0
  795. data/spec/shared/share/Dockerfile.erb +325 -0
  796. data/spec/shared/share/haproxy-1.conf +16 -0
  797. data/spec/shared/share/haproxy-2.conf +17 -0
  798. data/spec/shared/shlib/config.sh +27 -0
  799. data/spec/shared/shlib/distro.sh +74 -0
  800. data/spec/shared/shlib/server.sh +392 -0
  801. data/spec/shared/shlib/set_env.sh +169 -0
  802. data/spec/spec_helper.rb +210 -0
  803. data/spec/support/authorization.rb +15 -0
  804. data/spec/support/client_registry.rb +9 -0
  805. data/spec/support/constraints.rb +77 -0
  806. data/spec/support/crypt/models.rb +43 -0
  807. data/spec/support/crypt.rb +79 -0
  808. data/spec/support/expectations.rb +32 -0
  809. data/spec/support/feature_sandbox.rb +71 -0
  810. data/spec/support/helpers.rb +11 -0
  811. data/spec/support/immutable_ids.rb +118 -0
  812. data/spec/support/macros.rb +129 -0
  813. data/spec/support/models/account.rb +38 -0
  814. data/spec/support/models/acolyte.rb +19 -0
  815. data/spec/support/models/actor.rb +20 -0
  816. data/spec/support/models/actress.rb +4 -0
  817. data/spec/support/models/address.rb +87 -0
  818. data/spec/support/models/address_component.rb +7 -0
  819. data/spec/support/models/address_number.rb +8 -0
  820. data/spec/support/models/agency.rb +7 -0
  821. data/spec/support/models/agent.rb +18 -0
  822. data/spec/support/models/album.rb +20 -0
  823. data/spec/support/models/alert.rb +9 -0
  824. data/spec/support/models/animal.rb +28 -0
  825. data/spec/support/models/answer.rb +8 -0
  826. data/spec/support/models/appointment.rb +9 -0
  827. data/spec/support/models/armrest.rb +9 -0
  828. data/spec/support/models/array_field.rb +7 -0
  829. data/spec/support/models/article.rb +14 -0
  830. data/spec/support/models/artist.rb +90 -0
  831. data/spec/support/models/artwork.rb +6 -0
  832. data/spec/support/models/audible_sound.rb +3 -0
  833. data/spec/support/models/audio.rb +7 -0
  834. data/spec/support/models/augmentation.rb +25 -0
  835. data/spec/support/models/author.rb +8 -0
  836. data/spec/support/models/baby.rb +6 -0
  837. data/spec/support/models/band.rb +45 -0
  838. data/spec/support/models/bar.rb +12 -0
  839. data/spec/support/models/basic.rb +8 -0
  840. data/spec/support/models/bed.rb +3 -0
  841. data/spec/support/models/big_palette.rb +4 -0
  842. data/spec/support/models/birthday.rb +15 -0
  843. data/spec/support/models/bolt.rb +7 -0
  844. data/spec/support/models/bomb.rb +6 -0
  845. data/spec/support/models/book.rb +19 -0
  846. data/spec/support/models/breed.rb +6 -0
  847. data/spec/support/models/browser.rb +8 -0
  848. data/spec/support/models/building.rb +10 -0
  849. data/spec/support/models/building_address.rb +9 -0
  850. data/spec/support/models/bus.rb +9 -0
  851. data/spec/support/models/business.rb +7 -0
  852. data/spec/support/models/callback_test.rb +11 -0
  853. data/spec/support/models/canvas.rb +27 -0
  854. data/spec/support/models/car.rb +3 -0
  855. data/spec/support/models/cat.rb +10 -0
  856. data/spec/support/models/catalog.rb +24 -0
  857. data/spec/support/models/category.rb +10 -0
  858. data/spec/support/models/child.rb +6 -0
  859. data/spec/support/models/child_doc.rb +24 -0
  860. data/spec/support/models/church.rb +8 -0
  861. data/spec/support/models/circle.rb +5 -0
  862. data/spec/support/models/circuit.rb +6 -0
  863. data/spec/support/models/circus.rb +12 -0
  864. data/spec/support/models/code.rb +15 -0
  865. data/spec/support/models/coding/pull_request.rb +11 -0
  866. data/spec/support/models/coding.rb +3 -0
  867. data/spec/support/models/comment.rb +18 -0
  868. data/spec/support/models/company.rb +7 -0
  869. data/spec/support/models/consumption_period.rb +9 -0
  870. data/spec/support/models/contextable_item.rb +7 -0
  871. data/spec/support/models/contractor.rb +7 -0
  872. data/spec/support/models/cookie.rb +8 -0
  873. data/spec/support/models/country_code.rb +12 -0
  874. data/spec/support/models/courier_job.rb +6 -0
  875. data/spec/support/models/cover.rb +10 -0
  876. data/spec/support/models/crate.rb +12 -0
  877. data/spec/support/models/customer.rb +10 -0
  878. data/spec/support/models/customer_address.rb +11 -0
  879. data/spec/support/models/deed.rb +7 -0
  880. data/spec/support/models/definition.rb +11 -0
  881. data/spec/support/models/delegating_patient.rb +15 -0
  882. data/spec/support/models/description.rb +13 -0
  883. data/spec/support/models/dictionary.rb +18 -0
  884. data/spec/support/models/division.rb +12 -0
  885. data/spec/support/models/doctor.rb +14 -0
  886. data/spec/support/models/dog.rb +9 -0
  887. data/spec/support/models/dokument.rb +8 -0
  888. data/spec/support/models/draft.rb +11 -0
  889. data/spec/support/models/dragon.rb +6 -0
  890. data/spec/support/models/driver.rb +9 -0
  891. data/spec/support/models/drug.rb +8 -0
  892. data/spec/support/models/dungeon.rb +6 -0
  893. data/spec/support/models/edit.rb +7 -0
  894. data/spec/support/models/email.rb +8 -0
  895. data/spec/support/models/employer.rb +7 -0
  896. data/spec/support/models/entry.rb +8 -0
  897. data/spec/support/models/eraser.rb +3 -0
  898. data/spec/support/models/even.rb +9 -0
  899. data/spec/support/models/event.rb +24 -0
  900. data/spec/support/models/exhibition.rb +6 -0
  901. data/spec/support/models/exhibitor.rb +8 -0
  902. data/spec/support/models/explosion.rb +6 -0
  903. data/spec/support/models/eye.rb +11 -0
  904. data/spec/support/models/eye_bowl.rb +11 -0
  905. data/spec/support/models/face.rb +10 -0
  906. data/spec/support/models/fanatic.rb +8 -0
  907. data/spec/support/models/favorite.rb +14 -0
  908. data/spec/support/models/filesystem.rb +7 -0
  909. data/spec/support/models/fire_hydrant.rb +8 -0
  910. data/spec/support/models/firefox.rb +6 -0
  911. data/spec/support/models/fish.rb +9 -0
  912. data/spec/support/models/folder.rb +9 -0
  913. data/spec/support/models/folder_item.rb +11 -0
  914. data/spec/support/models/fruits.rb +36 -0
  915. data/spec/support/models/game.rb +21 -0
  916. data/spec/support/models/ghost.rb +9 -0
  917. data/spec/support/models/guitar.rb +4 -0
  918. data/spec/support/models/hole.rb +12 -0
  919. data/spec/support/models/home.rb +6 -0
  920. data/spec/support/models/house.rb +8 -0
  921. data/spec/support/models/html_writer.rb +5 -0
  922. data/spec/support/models/id_key.rb +8 -0
  923. data/spec/support/models/idnodef.rb +7 -0
  924. data/spec/support/models/image.rb +24 -0
  925. data/spec/support/models/implant.rb +27 -0
  926. data/spec/support/models/instrument.rb +8 -0
  927. data/spec/support/models/item.rb +10 -0
  928. data/spec/support/models/jar.rb +9 -0
  929. data/spec/support/models/kaleidoscope.rb +8 -0
  930. data/spec/support/models/kangaroo.rb +6 -0
  931. data/spec/support/models/label.rb +52 -0
  932. data/spec/support/models/language.rb +7 -0
  933. data/spec/support/models/lat_lng.rb +17 -0
  934. data/spec/support/models/league.rb +13 -0
  935. data/spec/support/models/learner.rb +4 -0
  936. data/spec/support/models/line_item.rb +8 -0
  937. data/spec/support/models/location.rb +10 -0
  938. data/spec/support/models/login.rb +10 -0
  939. data/spec/support/models/manufacturer.rb +9 -0
  940. data/spec/support/models/meat.rb +6 -0
  941. data/spec/support/models/membership.rb +7 -0
  942. data/spec/support/models/message.rb +13 -0
  943. data/spec/support/models/minim.rb +6 -0
  944. data/spec/support/models/mixed_drink.rb +6 -0
  945. data/spec/support/models/mop.rb +24 -0
  946. data/spec/support/models/movie.rb +15 -0
  947. data/spec/support/models/my_hash.rb +4 -0
  948. data/spec/support/models/name.rb +26 -0
  949. data/spec/support/models/name_only.rb +8 -0
  950. data/spec/support/models/node.rb +7 -0
  951. data/spec/support/models/note.rb +19 -0
  952. data/spec/support/models/nut.rb +7 -0
  953. data/spec/support/models/odd.rb +9 -0
  954. data/spec/support/models/order.rb +12 -0
  955. data/spec/support/models/ordered_post.rb +13 -0
  956. data/spec/support/models/ordered_preference.rb +8 -0
  957. data/spec/support/models/oscar.rb +16 -0
  958. data/spec/support/models/other_owner_object.rb +4 -0
  959. data/spec/support/models/override.rb +18 -0
  960. data/spec/support/models/ownable.rb +8 -0
  961. data/spec/support/models/owner.rb +10 -0
  962. data/spec/support/models/pack.rb +5 -0
  963. data/spec/support/models/page.rb +18 -0
  964. data/spec/support/models/page_question.rb +6 -0
  965. data/spec/support/models/palette.rb +9 -0
  966. data/spec/support/models/parent.rb +7 -0
  967. data/spec/support/models/parent_doc.rb +8 -0
  968. data/spec/support/models/passport.rb +22 -0
  969. data/spec/support/models/patient.rb +11 -0
  970. data/spec/support/models/pdf_writer.rb +5 -0
  971. data/spec/support/models/pencil.rb +3 -0
  972. data/spec/support/models/person.rb +231 -0
  973. data/spec/support/models/pet.rb +25 -0
  974. data/spec/support/models/pet_owner.rb +8 -0
  975. data/spec/support/models/phone.rb +13 -0
  976. data/spec/support/models/piano.rb +4 -0
  977. data/spec/support/models/pizza.rb +9 -0
  978. data/spec/support/models/player.rb +39 -0
  979. data/spec/support/models/post.rb +52 -0
  980. data/spec/support/models/post_genre.rb +8 -0
  981. data/spec/support/models/powerup.rb +25 -0
  982. data/spec/support/models/preference.rb +11 -0
  983. data/spec/support/models/princess.rb +12 -0
  984. data/spec/support/models/product.rb +20 -0
  985. data/spec/support/models/profile.rb +17 -0
  986. data/spec/support/models/pronunciation.rb +7 -0
  987. data/spec/support/models/pub.rb +8 -0
  988. data/spec/support/models/publication/encyclopedia.rb +11 -0
  989. data/spec/support/models/publication/review.rb +13 -0
  990. data/spec/support/models/publication.rb +4 -0
  991. data/spec/support/models/purchase.rb +6 -0
  992. data/spec/support/models/purchased_item.rb +10 -0
  993. data/spec/support/models/question.rb +10 -0
  994. data/spec/support/models/quiz.rb +9 -0
  995. data/spec/support/models/rating.rb +10 -0
  996. data/spec/support/models/record.rb +54 -0
  997. data/spec/support/models/registry.rb +7 -0
  998. data/spec/support/models/role.rb +9 -0
  999. data/spec/support/models/root_category.rb +6 -0
  1000. data/spec/support/models/sandbox/app/models/app_models_message.rb +3 -0
  1001. data/spec/support/models/sandbox/lib/models/lib_models_message.rb +3 -0
  1002. data/spec/support/models/sandbox/sandbox_message.rb +3 -0
  1003. data/spec/support/models/sandbox/sandbox_user.rb +3 -0
  1004. data/spec/support/models/sandbox/subdir/sandbox_comment.rb +3 -0
  1005. data/spec/support/models/sandwich.rb +11 -0
  1006. data/spec/support/models/scheduler.rb +9 -0
  1007. data/spec/support/models/school.rb +14 -0
  1008. data/spec/support/models/scribe.rb +7 -0
  1009. data/spec/support/models/sealer.rb +7 -0
  1010. data/spec/support/models/seat.rb +24 -0
  1011. data/spec/support/models/seo.rb +9 -0
  1012. data/spec/support/models/series.rb +7 -0
  1013. data/spec/support/models/server.rb +15 -0
  1014. data/spec/support/models/service.rb +24 -0
  1015. data/spec/support/models/shape.rb +14 -0
  1016. data/spec/support/models/shelf.rb +7 -0
  1017. data/spec/support/models/shield.rb +18 -0
  1018. data/spec/support/models/shipment_address.rb +5 -0
  1019. data/spec/support/models/shipping_container.rb +7 -0
  1020. data/spec/support/models/shipping_pack.rb +5 -0
  1021. data/spec/support/models/shirt.rb +11 -0
  1022. data/spec/support/models/shop.rb +8 -0
  1023. data/spec/support/models/short_agent.rb +6 -0
  1024. data/spec/support/models/short_quiz.rb +7 -0
  1025. data/spec/support/models/simple.rb +7 -0
  1026. data/spec/support/models/slave.rb +8 -0
  1027. data/spec/support/models/song.rb +10 -0
  1028. data/spec/support/models/sound.rb +7 -0
  1029. data/spec/support/models/spacer.rb +7 -0
  1030. data/spec/support/models/square.rb +6 -0
  1031. data/spec/support/models/staff.rb +9 -0
  1032. data/spec/support/models/store_as_dup_test1.rb +7 -0
  1033. data/spec/support/models/store_as_dup_test2.rb +7 -0
  1034. data/spec/support/models/store_as_dup_test3.rb +7 -0
  1035. data/spec/support/models/store_as_dup_test4.rb +7 -0
  1036. data/spec/support/models/strategy.rb +5 -0
  1037. data/spec/support/models/student.rb +14 -0
  1038. data/spec/support/models/sub_item.rb +5 -0
  1039. data/spec/support/models/subscription.rb +7 -0
  1040. data/spec/support/models/survey.rb +7 -0
  1041. data/spec/support/models/symptom.rb +8 -0
  1042. data/spec/support/models/system_role.rb +7 -0
  1043. data/spec/support/models/tag.rb +10 -0
  1044. data/spec/support/models/target.rb +7 -0
  1045. data/spec/support/models/template.rb +7 -0
  1046. data/spec/support/models/thing.rb +11 -0
  1047. data/spec/support/models/threadlocker.rb +7 -0
  1048. data/spec/support/models/title.rb +5 -0
  1049. data/spec/support/models/tool.rb +10 -0
  1050. data/spec/support/models/topping.rb +7 -0
  1051. data/spec/support/models/toy.rb +9 -0
  1052. data/spec/support/models/track.rb +40 -0
  1053. data/spec/support/models/translation.rb +7 -0
  1054. data/spec/support/models/tree.rb +11 -0
  1055. data/spec/support/models/truck.rb +7 -0
  1056. data/spec/support/models/updatable.rb +7 -0
  1057. data/spec/support/models/user.rb +23 -0
  1058. data/spec/support/models/user_account.rb +12 -0
  1059. data/spec/support/models/validation_callback.rb +12 -0
  1060. data/spec/support/models/vehicle.rb +18 -0
  1061. data/spec/support/models/version.rb +7 -0
  1062. data/spec/support/models/vertex.rb +8 -0
  1063. data/spec/support/models/vet_visit.rb +7 -0
  1064. data/spec/support/models/video.rb +15 -0
  1065. data/spec/support/models/video_game.rb +3 -0
  1066. data/spec/support/models/washer.rb +7 -0
  1067. data/spec/support/models/weapon.rb +25 -0
  1068. data/spec/support/models/wiki_page.rb +17 -0
  1069. data/spec/support/models/word.rb +17 -0
  1070. data/spec/support/models/word_origin.rb +13 -0
  1071. data/spec/support/models/writer.rb +13 -0
  1072. data/spec/support/rails_mock.rb +31 -0
  1073. data/spec/support/schema_maps/schema_map_aws.json +17 -0
  1074. data/spec/support/schema_maps/schema_map_aws_key_alt_names.json +12 -0
  1075. data/spec/support/schema_maps/schema_map_azure.json +17 -0
  1076. data/spec/support/schema_maps/schema_map_azure_key_alt_names.json +12 -0
  1077. data/spec/support/schema_maps/schema_map_gcp.json +17 -0
  1078. data/spec/support/schema_maps/schema_map_gcp_key_alt_names.json +12 -0
  1079. data/spec/support/schema_maps/schema_map_kmip.json +17 -0
  1080. data/spec/support/schema_maps/schema_map_kmip_key_alt_names.json +12 -0
  1081. data/spec/support/schema_maps/schema_map_local.json +18 -0
  1082. data/spec/support/schema_maps/schema_map_local_key_alt_names.json +12 -0
  1083. data/spec/support/shared/time.rb +40 -0
  1084. data/spec/support/sinatra_mock.rb +6 -0
  1085. data/spec/support/spec_config.rb +80 -0
  1086. metadata +1944 -0
@@ -0,0 +1,4534 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Mongoid::Contextual::Mongo do
6
+
7
+ [ :blank?, :empty? ].each do |method|
8
+
9
+ describe "##{method}" do
10
+
11
+ before do
12
+ Band.create!(name: "Depeche Mode")
13
+ end
14
+
15
+ context "when the count is zero" do
16
+
17
+ let(:criteria) do
18
+ Band.where(name: "New Order")
19
+ end
20
+
21
+ let(:context) do
22
+ described_class.new(criteria)
23
+ end
24
+
25
+ it "returns true" do
26
+ expect(context.send(method)).to be true
27
+ end
28
+ end
29
+
30
+ context "when the count is greater than zero" do
31
+
32
+ let(:criteria) do
33
+ Band.where(name: "Depeche Mode")
34
+ end
35
+
36
+ let(:context) do
37
+ described_class.new(criteria)
38
+ end
39
+
40
+ it "returns false" do
41
+ expect(context.send(method)).to be false
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#count" do
48
+
49
+ let!(:depeche) do
50
+ Band.create!(name: "Depeche Mode")
51
+ end
52
+
53
+ let!(:new_order) do
54
+ Band.create!(name: "New Order")
55
+ end
56
+
57
+ let(:criteria) do
58
+ Band.where(name: "Depeche Mode")
59
+ end
60
+
61
+ context "when no arguments are provided" do
62
+
63
+ let(:context) do
64
+ described_class.new(criteria)
65
+ end
66
+
67
+ it "returns the number of documents that match" do
68
+ expect(context.count).to eq(1)
69
+ end
70
+ end
71
+
72
+ context "when the query cache is enabled" do
73
+ query_cache_enabled
74
+
75
+ let(:context) do
76
+ described_class.new(criteria)
77
+ end
78
+
79
+ it "only executes the count query once" do
80
+ expect_query(1) do
81
+ 2.times { expect(context.count).to eq(1) }
82
+ end
83
+ end
84
+ end
85
+
86
+ context "when provided a block" do
87
+
88
+ let(:context) do
89
+ described_class.new(criteria)
90
+ end
91
+
92
+ let(:count) do
93
+ context.count do |doc|
94
+ doc.likes.nil?
95
+ end
96
+ end
97
+
98
+ it "returns the number of documents that match" do
99
+ expect(count).to eq(1)
100
+ end
101
+
102
+ context "and a limit true" do
103
+
104
+ before do
105
+ 2.times { Band.create!(name: "Depeche Mode", likes: 1) }
106
+ end
107
+
108
+ let(:count) do
109
+ context.count(true) do |doc|
110
+ doc.likes.nil?
111
+ end
112
+ end
113
+
114
+ it "returns the number of documents that match" do
115
+ expect(count).to eq(1)
116
+ end
117
+ end
118
+ end
119
+
120
+ context "when provided limit" do
121
+
122
+ before do
123
+ 2.times { Band.create!(name: "Depeche Mode") }
124
+ end
125
+
126
+ let(:context) do
127
+ described_class.new(criteria)
128
+ end
129
+
130
+ let(:count) do
131
+ context.count(limit: 2)
132
+ end
133
+
134
+ it "returns the number of documents that match" do
135
+ expect(count).to eq(2)
136
+ end
137
+ end
138
+
139
+ context 'when a collation is specified' do
140
+ min_server_version '3.4'
141
+
142
+ let(:context) do
143
+ described_class.new(criteria)
144
+ end
145
+
146
+ context 'when the collation is specified on the criteria' do
147
+
148
+ let(:criteria) do
149
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
150
+ end
151
+
152
+ let(:count) do
153
+ context.count
154
+ end
155
+
156
+ it 'applies the collation' do
157
+ expect(count).to eq(1)
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ describe "#estimated_count" do
164
+
165
+ let!(:depeche) do
166
+ Band.create!(name: "Depeche Mode")
167
+ end
168
+
169
+ let!(:new_order) do
170
+ Band.create!(name: "New Order")
171
+ end
172
+
173
+ let(:criteria) do
174
+ Band.where
175
+ end
176
+
177
+ context "when not providing options" do
178
+ it 'returns the correct count' do
179
+ expect(criteria.estimated_count).to eq(2)
180
+ end
181
+ end
182
+
183
+ context "when providing options" do
184
+ it 'returns the correct count' do
185
+ expect(criteria.estimated_count(maxTimeMS: 1000)).to eq(2)
186
+ end
187
+ end
188
+
189
+ context "when the query cache is enabled" do
190
+ query_cache_enabled
191
+
192
+ let(:context) do
193
+ described_class.new(criteria)
194
+ end
195
+
196
+ it "the results are not cached" do
197
+ expect_query(2) do
198
+ 2.times do
199
+ context.estimated_count
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ context "when the criteria contains a selector", :focus do
206
+ let(:criteria) do
207
+ Band.where(name: "New Order")
208
+ end
209
+
210
+ context "when not providing options" do
211
+ it 'raises an error' do
212
+ expect do
213
+ criteria.estimated_count
214
+ end.to raise_error(Mongoid::Errors::InvalidEstimatedCountCriteria)
215
+ end
216
+ end
217
+
218
+ context "when providing options" do
219
+ it 'raises an error' do
220
+ expect do
221
+ criteria.estimated_count(maxTimeMS: 1000)
222
+ end.to raise_error(Mongoid::Errors::InvalidEstimatedCountCriteria)
223
+ end
224
+ end
225
+ end
226
+
227
+ context "when including a default scope" do
228
+
229
+ let(:criteria) do
230
+ Band.where(name: "New Order")
231
+ end
232
+
233
+ before do
234
+ 5.times { Band.create! }
235
+ Band.default_scope ->{ criteria }
236
+ end
237
+
238
+ after do
239
+ Band.default_scoping = nil
240
+ end
241
+
242
+ it 'raises an error' do
243
+ expect do
244
+ Band.estimated_count
245
+ end.to raise_error(Mongoid::Errors::InvalidEstimatedCountScoping)
246
+ end
247
+
248
+ it "does not raise an error on unscoped" do
249
+ expect do
250
+ expect(Band.unscoped.estimated_count).to eq(5)
251
+ end
252
+ end
253
+ end
254
+ end
255
+
256
+
257
+
258
+ [ :delete, :delete_all ].each do |method|
259
+
260
+ describe "##{method}" do
261
+
262
+ let!(:depeche_mode) do
263
+ Band.create!(name: "Depeche Mode")
264
+ end
265
+
266
+ let!(:new_order) do
267
+ Band.create!(name: "New Order")
268
+ end
269
+
270
+ context "when the selector is contraining" do
271
+
272
+ let(:criteria) do
273
+ Band.where(name: "Depeche Mode")
274
+ end
275
+
276
+ let(:context) do
277
+ described_class.new(criteria)
278
+ end
279
+
280
+ let!(:deleted) do
281
+ context.send(method)
282
+ end
283
+
284
+ it "deletes the matching documents" do
285
+ expect(Band.find(new_order.id)).to eq(new_order)
286
+ end
287
+
288
+ it "deletes the correct number of documents" do
289
+ expect(Band.count).to eq(1)
290
+ end
291
+
292
+ it "returns the number of documents deleted" do
293
+ expect(deleted).to eq(1)
294
+ end
295
+
296
+ context 'when the criteria has a collation' do
297
+ min_server_version '3.4'
298
+
299
+ let(:criteria) do
300
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
301
+ end
302
+
303
+ let(:context) do
304
+ described_class.new(criteria)
305
+ end
306
+
307
+ let!(:deleted) do
308
+ context.send(method)
309
+ end
310
+
311
+ it "deletes the matching documents" do
312
+ expect(Band.find(new_order.id)).to eq(new_order)
313
+ end
314
+
315
+ it "deletes the correct number of documents" do
316
+ expect(Band.count).to eq(1)
317
+ end
318
+
319
+ it "returns the number of documents deleted" do
320
+ expect(deleted).to eq(1)
321
+ end
322
+ end
323
+ end
324
+
325
+ context "when the selector is not contraining" do
326
+
327
+ let(:criteria) do
328
+ Band.all
329
+ end
330
+
331
+ let(:context) do
332
+ described_class.new(criteria)
333
+ end
334
+
335
+ before do
336
+ context.send(method)
337
+ end
338
+
339
+ it "deletes all the documents" do
340
+ expect(Band.count).to eq(0)
341
+ end
342
+ end
343
+
344
+ context 'when the write concern is unacknowledged' do
345
+
346
+ let(:criteria) do
347
+ Band.all
348
+ end
349
+
350
+ let!(:deleted) do
351
+ criteria.with(write: { w: 0 }) do |crit|
352
+ crit.send(method)
353
+ end
354
+ end
355
+
356
+ it 'returns 0' do
357
+ expect(deleted).to eq(0)
358
+ end
359
+ end
360
+ end
361
+ end
362
+
363
+ [ :destroy, :destroy_all ].each do |method|
364
+
365
+ describe "##{method}" do
366
+
367
+ let!(:depeche_mode) do
368
+ Band.create!(name: "Depeche Mode")
369
+ end
370
+
371
+ let!(:new_order) do
372
+ Band.create!(name: "New Order")
373
+ end
374
+
375
+ context "when the selector is contraining" do
376
+
377
+ let(:criteria) do
378
+ Band.where(name: "Depeche Mode")
379
+ end
380
+
381
+ let(:context) do
382
+ described_class.new(criteria)
383
+ end
384
+
385
+ let!(:destroyed) do
386
+ context.send(method)
387
+ end
388
+
389
+ it "destroys the matching documents" do
390
+ expect(Band.find(new_order.id)).to eq(new_order)
391
+ end
392
+
393
+ it "destroys the correct number of documents" do
394
+ expect(Band.count).to eq(1)
395
+ end
396
+
397
+ it "returns the number of documents destroyed" do
398
+ expect(destroyed).to eq(1)
399
+ end
400
+
401
+ context 'when the criteria has a collation' do
402
+ min_server_version '3.4'
403
+
404
+ let(:criteria) do
405
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
406
+ end
407
+
408
+ let(:context) do
409
+ described_class.new(criteria)
410
+ end
411
+
412
+ let!(:destroyed) do
413
+ context.send(method)
414
+ end
415
+
416
+ it "destroys the matching documents" do
417
+ expect(Band.find(new_order.id)).to eq(new_order)
418
+ end
419
+
420
+ it "destroys the correct number of documents" do
421
+ expect(Band.count).to eq(1)
422
+ end
423
+
424
+ it "returns the number of documents destroyed" do
425
+ expect(destroyed).to eq(1)
426
+ end
427
+ end
428
+ end
429
+
430
+ context "when the selector is not contraining" do
431
+
432
+ let(:criteria) do
433
+ Band.all
434
+ end
435
+
436
+ let(:context) do
437
+ described_class.new(criteria)
438
+ end
439
+
440
+ before do
441
+ context.send(method)
442
+ end
443
+
444
+ it "destroys all the documents" do
445
+ expect(Band.count).to eq(0)
446
+ end
447
+ end
448
+ end
449
+
450
+ context 'when the write concern is unacknowledged' do
451
+
452
+ before do
453
+ 2.times { Band.create! }
454
+ end
455
+
456
+ let(:criteria) do
457
+ Band.all
458
+ end
459
+
460
+ let!(:deleted) do
461
+ criteria.with(write: { w: 0 }) do |crit|
462
+ crit.send(method)
463
+ end
464
+ end
465
+
466
+ it 'returns 0' do
467
+ expect(deleted).to eq(0)
468
+ end
469
+ end
470
+ end
471
+
472
+ describe "#distinct" do
473
+
474
+ before do
475
+ Band.create!(name: "Depeche Mode", years: 30, sales: "1E2")
476
+ Band.create!(name: "New Order", years: 25, sales: "2E3")
477
+ Band.create!(name: "10,000 Maniacs", years: 20, sales: "1E2")
478
+ end
479
+
480
+ context "when limiting the result set" do
481
+
482
+ let(:criteria) do
483
+ Band.where(name: "Depeche Mode")
484
+ end
485
+
486
+ let(:context) do
487
+ described_class.new(criteria)
488
+ end
489
+
490
+ it "returns the distinct matching fields" do
491
+ expect(context.distinct(:name)).to eq([ "Depeche Mode" ])
492
+ end
493
+ end
494
+
495
+ context "when not limiting the result set" do
496
+
497
+ let(:criteria) do
498
+ Band.criteria
499
+ end
500
+
501
+ let(:context) do
502
+ described_class.new(criteria)
503
+ end
504
+
505
+ it "returns the distinct field values" do
506
+ expect(context.distinct(:name).sort).to eq([ "10,000 Maniacs", "Depeche Mode", "New Order" ].sort)
507
+ end
508
+ end
509
+
510
+ context "when providing an aliased field" do
511
+
512
+ let(:criteria) do
513
+ Band.criteria
514
+ end
515
+
516
+ let(:context) do
517
+ described_class.new(criteria)
518
+ end
519
+
520
+ it "returns the distinct field values" do
521
+ expect(context.distinct(:years).sort).to eq([ 20, 25, 30 ])
522
+ end
523
+ end
524
+
525
+ context 'when a collation is specified' do
526
+ min_server_version '3.4'
527
+
528
+ before do
529
+ Band.create!(name: 'DEPECHE MODE')
530
+ end
531
+
532
+ let(:context) do
533
+ described_class.new(criteria)
534
+ end
535
+
536
+ let(:expected_results) do
537
+ ["10,000 Maniacs", "Depeche Mode", "New Order"]
538
+ end
539
+
540
+ let(:criteria) do
541
+ Band.where({}).collation(locale: 'en_US', strength: 2)
542
+ end
543
+
544
+ it 'applies the collation' do
545
+ expect(context.distinct(:name).sort).to eq(expected_results.sort)
546
+ end
547
+ end
548
+
549
+ context "when providing a demongoizable field" do
550
+ let(:criteria) do
551
+ Band.criteria
552
+ end
553
+
554
+ let(:context) do
555
+ described_class.new(criteria)
556
+ end
557
+
558
+ it "returns the non-demongoized distinct field values" do
559
+ expect(context.distinct(:sales).sort).to eq([ BigDecimal("1E2"), BigDecimal("2E3") ])
560
+ end
561
+ end
562
+
563
+ context "when getting a localized field" do
564
+ with_default_i18n_configs
565
+
566
+ before do
567
+ I18n.locale = :en
568
+ d = Dictionary.create!(description: 'english-text')
569
+ I18n.locale = :de
570
+ d.description = 'deutsch-text'
571
+ d.save!
572
+ end
573
+
574
+ let(:criteria) do
575
+ Dictionary.criteria
576
+ end
577
+
578
+ let(:context) do
579
+ described_class.new(criteria)
580
+ end
581
+
582
+ context "when getting the field without _translations" do
583
+ it "gets the demongoized localized field" do
584
+ expect(context.distinct(:description)).to eq([ 'deutsch-text' ])
585
+ end
586
+ end
587
+
588
+ context "when getting the field with _translations" do
589
+ it "gets the full hash" do
590
+ expect(context.distinct(:description_translations)).to eq([ { "de" => "deutsch-text", "en" => "english-text" } ])
591
+ end
592
+ end
593
+
594
+ context 'when plucking a specific locale' do
595
+
596
+ let(:distinct) do
597
+ context.distinct(:'description.de')
598
+ end
599
+
600
+ it 'returns the specific translation' do
601
+ expect(distinct).to eq([ "deutsch-text" ])
602
+ end
603
+ end
604
+
605
+ context 'when plucking a specific locale from _translations field' do
606
+
607
+ let(:distinct) do
608
+ context.distinct(:'description_translations.de')
609
+ end
610
+
611
+ it 'returns the specific translations' do
612
+ expect(distinct).to eq(['deutsch-text'])
613
+ end
614
+ end
615
+
616
+ context 'when fallbacks are enabled with a locale list' do
617
+ require_fallbacks
618
+ with_default_i18n_configs
619
+
620
+ before do
621
+ I18n.fallbacks[:he] = [ :en ]
622
+ end
623
+
624
+ let(:distinct) do
625
+ context.distinct(:description).first
626
+ end
627
+
628
+ it "correctly uses the fallback" do
629
+ I18n.locale = :en
630
+ Dictionary.create!(description: 'english-text')
631
+ I18n.locale = :he
632
+ distinct.should == "english-text"
633
+ end
634
+ end
635
+
636
+ context "when the localized field is embedded" do
637
+ with_default_i18n_configs
638
+
639
+ before do
640
+ p = Passport.new
641
+ I18n.locale = :en
642
+ p.name = "Neil"
643
+ I18n.locale = :he
644
+ p.name = "Nissim"
645
+
646
+ Person.create!(passport: p, employer_id: 12345)
647
+ end
648
+
649
+ let(:criteria) do
650
+ Person.where(employer_id: 12345)
651
+ end
652
+
653
+ let(:context) do
654
+ described_class.new(criteria)
655
+ end
656
+
657
+ let(:distinct) do
658
+ context.distinct("pass.name").first
659
+ end
660
+
661
+ let(:distinct_translations) do
662
+ context.distinct("pass.name_translations").first
663
+ end
664
+
665
+ let(:distinct_translations_field) do
666
+ context.distinct("pass.name_translations.en").first
667
+ end
668
+
669
+ it "returns the translation for the current locale" do
670
+ expect(distinct).to eq("Nissim")
671
+ end
672
+
673
+ it "returns the full _translation hash" do
674
+ expect(distinct_translations).to eq({ "en" => "Neil", "he" => "Nissim" })
675
+ end
676
+
677
+ it "returns the translation for the requested locale" do
678
+ expect(distinct_translations_field).to eq("Neil")
679
+ end
680
+ end
681
+ end
682
+
683
+ context "when getting an embedded field" do
684
+
685
+ let(:label) { Label.new(sales: "1E2") }
686
+ let!(:band) { Band.create!(label: label) }
687
+ let(:criteria) { Band.where(_id: band.id) }
688
+ let(:context) { described_class.new(criteria) }
689
+
690
+ it "returns the distinct matching fields" do
691
+ expect(context.distinct("label.sales")).to eq([ BigDecimal("1E2") ])
692
+ end
693
+ end
694
+ end
695
+
696
+ describe "#tally" do
697
+ let(:fans1) { [ Fanatic.new(age:1), Fanatic.new(age:2) ] }
698
+ let(:fans2) { [ Fanatic.new(age:1), Fanatic.new(age:2) ] }
699
+ let(:fans3) { [ Fanatic.new(age:1), Fanatic.new(age:3) ] }
700
+
701
+ let(:genres1) { [ { x: 1, y: { z: 1 } }, { x: 2, y: { z: 2 } }, { y: 3 } ]}
702
+ let(:genres2) { [ { x: 1, y: { z: 1 } }, { x: 2, y: { z: 2 } }, { y: 4 } ]}
703
+ let(:genres3) { [ { x: 1, y: { z: 1 } }, { x: 3, y: { z: 3 } }, { y: 5 } ]}
704
+
705
+ let(:label1) { Label.new(name: "Atlantic") }
706
+ let(:label2) { Label.new(name: "Atlantic") }
707
+ let(:label3) { Label.new(name: "Columbia") }
708
+
709
+ before do
710
+ Band.create!(origin: "tally", name: "Depeche Mode", years: 30, sales: "1E2", label: label1, genres: genres1)
711
+ Band.create!(origin: "tally", name: "New Order", years: 30, sales: "2E3", label: label2, genres: genres2)
712
+ Band.create!(origin: "tally", name: "10,000 Maniacs", years: 30, sales: "1E2", label: label3, genres: genres3)
713
+ Band.create!(origin: "tally2", fanatics: fans1, genres: [1, 2])
714
+ Band.create!(origin: "tally2", fanatics: fans2, genres: [1, 2])
715
+ Band.create!(origin: "tally2", fanatics: fans3, genres: [1, 3])
716
+ end
717
+
718
+ let(:criteria) { Band.where(origin: "tally") }
719
+
720
+ context "when tallying a string" do
721
+ let(:tally) do
722
+ criteria.tally(:name)
723
+ end
724
+
725
+ it "returns the correct hash" do
726
+ expect(tally).to eq("Depeche Mode" => 1, "New Order" => 1, "10,000 Maniacs" => 1)
727
+ end
728
+ end
729
+
730
+ context "using an aliased field" do
731
+ let(:tally) do
732
+ criteria.tally(:years)
733
+ end
734
+
735
+ it "returns the correct hash" do
736
+ expect(tally).to eq(30 => 3)
737
+ end
738
+ end
739
+
740
+ context "when tallying a demongoizable field" do
741
+ let(:tally) do
742
+ criteria.tally(:sales)
743
+ end
744
+
745
+ it "returns the correct hash" do
746
+ expect(tally).to eq(BigDecimal("1E2") => 2, BigDecimal("2E3") => 1)
747
+ end
748
+ end
749
+
750
+ context "when tallying a localized field" do
751
+ with_default_i18n_configs
752
+
753
+ before do
754
+ I18n.locale = :en
755
+ d1 = Dictionary.create!(description: 'en1')
756
+ d2 = Dictionary.create!(description: 'en1')
757
+ d3 = Dictionary.create!(description: 'en1')
758
+ d4 = Dictionary.create!(description: 'en2')
759
+ I18n.locale = :de
760
+ d1.description = 'de1'
761
+ d2.description = 'de1'
762
+ d3.description = 'de2'
763
+ d4.description = 'de3'
764
+ d1.save!
765
+ d2.save!
766
+ d3.save!
767
+ d4.save!
768
+ I18n.locale = :en
769
+ end
770
+
771
+ context "when getting the demongoized field" do
772
+ let(:tallied) do
773
+ Dictionary.tally(:description)
774
+ end
775
+
776
+ it "returns the translation for the current locale" do
777
+ expect(tallied).to eq("en1" => 3, "en2" => 1)
778
+ end
779
+ end
780
+
781
+ context "when getting a specific locale" do
782
+ let(:tallied) do
783
+ Dictionary.tally("description.de")
784
+ end
785
+
786
+ it "returns the translation for the the specific locale" do
787
+ expect(tallied).to eq("de1" => 2, "de2" => 1, "de3" => 1)
788
+ end
789
+ end
790
+
791
+ context "when getting the full hash" do
792
+ let(:tallied) do
793
+ Dictionary.tally("description_translations")
794
+ end
795
+
796
+ it "returns the correct hash" do
797
+ expect(tallied).to eq(
798
+ {"de" => "de1", "en" => "en1" } => 2,
799
+ {"de" => "de2", "en" => "en1" } => 1,
800
+ {"de" => "de3", "en" => "en2" } => 1
801
+ )
802
+ end
803
+ end
804
+ end
805
+
806
+ context "when tallying an embedded localized field" do
807
+ with_default_i18n_configs
808
+
809
+ before do
810
+ I18n.locale = :en
811
+ address1a = Address.new(name: "en1")
812
+ address1b = Address.new(name: "en2")
813
+ address2a = Address.new(name: "en1")
814
+ address2b = Address.new(name: "en3")
815
+ I18n.locale = :de
816
+ address1a.name = "de1"
817
+ address1b.name = "de2"
818
+ address2a.name = "de1"
819
+ address2b.name = "de3"
820
+ Person.create!(addresses: [ address1a, address1b ])
821
+ Person.create!(addresses: [ address2a, address2b ])
822
+ I18n.locale = :en
823
+ end
824
+
825
+ context "when getting the demongoized field" do
826
+ let(:tallied) do
827
+ Person.tally("addresses.name")
828
+ end
829
+
830
+ it "returns the translation for the current locale" do
831
+ expect(tallied).to eq(
832
+ [ "en1", "en2" ] => 1,
833
+ [ "en1", "en3" ] => 1,
834
+ )
835
+ end
836
+ end
837
+
838
+ context "when getting a specific locale" do
839
+ let(:tallied) do
840
+ Person.tally("addresses.name.de")
841
+ end
842
+
843
+ it "returns the translation for the the specific locale" do
844
+ expect(tallied).to eq(
845
+ [ "de1", "de2" ] => 1,
846
+ [ "de1", "de3" ] => 1,
847
+ )
848
+ end
849
+ end
850
+
851
+ context "when getting the full hash" do
852
+ let(:tallied) do
853
+ Person.tally("addresses.name_translations")
854
+ end
855
+
856
+ it "returns the correct hash" do
857
+ expect(tallied).to eq(
858
+ [{ "de" => "de1", "en" => "en1" }, { "de" => "de2", "en" => "en2" }] => 1,
859
+ [{ "de" => "de1", "en" => "en1" }, { "de" => "de3", "en" => "en3" }] => 1,
860
+ )
861
+ end
862
+ end
863
+
864
+ end
865
+
866
+ context "when tallying an embedded field" do
867
+ let(:tally) do
868
+ criteria.tally("label.name")
869
+ end
870
+
871
+ it "returns the correct hash" do
872
+ expect(tally).to eq("Atlantic" => 2, "Columbia" => 1)
873
+ end
874
+ end
875
+
876
+ context "when tallying an element in an embeds_many field" do
877
+ let(:criteria) { Band.where(origin: "tally2") }
878
+
879
+ let(:tally) do
880
+ criteria.tally("fanatics.age")
881
+ end
882
+
883
+ it "returns the correct hash" do
884
+ expect(tally).to eq(
885
+ [1, 2] => 2,
886
+ [1, 3] => 1
887
+ )
888
+ end
889
+ end
890
+
891
+ context "when tallying an embeds_many field" do
892
+ let(:criteria) { Band.where(origin: "tally2") }
893
+
894
+ let(:tally) do
895
+ criteria.tally("fanatics")
896
+ end
897
+
898
+ it "returns the correct hash" do
899
+ expect(tally).to eq(
900
+ fans1.map(&:attributes) => 1,
901
+ fans2.map(&:attributes) => 1,
902
+ fans3.map(&:attributes) => 1,
903
+ )
904
+ end
905
+ end
906
+
907
+ context "when tallying a field of type array" do
908
+ let(:criteria) { Band.where(origin: "tally2") }
909
+
910
+ let(:tally) do
911
+ criteria.tally("genres")
912
+ end
913
+
914
+ it "returns the correct hash" do
915
+ expect(tally).to eq(
916
+ [1, 2] => 2,
917
+ [1, 3] => 1
918
+ )
919
+ end
920
+ end
921
+
922
+ context "when tallying an element from an array of hashes" do
923
+ let(:criteria) { Band.where(origin: "tally") }
924
+
925
+ let(:tally) do
926
+ criteria.tally("genres.x")
927
+ end
928
+
929
+ it "returns the correct hash without the nil keys" do
930
+ expect(tally).to eq(
931
+ [1, 2] => 2,
932
+ [1, 3] => 1
933
+ )
934
+ end
935
+ end
936
+
937
+ context "when tallying an element from an array of hashes; with duplicate" do
938
+
939
+ before do
940
+ Band.create!(origin: "tally", genres: [ { x: 1 }, {x: 1} ] )
941
+ end
942
+
943
+ let(:criteria) { Band.where(origin: "tally") }
944
+
945
+ let(:tally) do
946
+ criteria.tally("genres.x")
947
+ end
948
+
949
+ it "returns the correct hash without the nil keys" do
950
+ expect(tally).to eq(
951
+ [1, 2] => 2,
952
+ [1, 3] => 1,
953
+ [1, 1] => 1,
954
+ )
955
+ end
956
+ end
957
+
958
+ context "when tallying an aliased field of type array" do
959
+
960
+ before do
961
+ Person.create!(array: [ 1, 2 ])
962
+ Person.create!(array: [ 1, 3 ])
963
+ end
964
+
965
+ let(:tally) do
966
+ Person.tally("array")
967
+ end
968
+
969
+ it "returns the correct hash" do
970
+ expect(tally).to eq(
971
+ [1, 2] => 1,
972
+ [1, 3] => 1
973
+ )
974
+ end
975
+ end
976
+
977
+ context "when going multiple levels deep in arrays" do
978
+ let(:criteria) { Band.where(origin: "tally") }
979
+
980
+ let(:tally) do
981
+ criteria.tally("genres.y.z")
982
+ end
983
+
984
+ it "returns the correct hash" do
985
+ expect(tally).to eq(
986
+ [1, 2] => 2,
987
+ [1, 3] => 1
988
+ )
989
+ end
990
+ end
991
+
992
+ context "when going multiple levels deep in an array" do
993
+ let(:criteria) { Band.where(origin: "tally") }
994
+
995
+ let(:tally) do
996
+ criteria.tally("genres.y.z")
997
+ end
998
+
999
+ it "returns the correct hash" do
1000
+ expect(tally).to eq(
1001
+ [1, 2] => 2,
1002
+ [1, 3] => 1
1003
+ )
1004
+ end
1005
+ end
1006
+
1007
+ context "when tallying deeply nested arrays/embedded associations" do
1008
+
1009
+ before do
1010
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
1011
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
1012
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))) ])
1013
+ end
1014
+
1015
+ let(:tally) do
1016
+ Person.tally("addresses.code.deepest.array.y.z")
1017
+ end
1018
+
1019
+ it "returns the correct hash" do
1020
+ expect(tally).to eq(
1021
+ [ [ 1, 2 ] ] => 2,
1022
+ [ [ 1, 3 ] ] => 1
1023
+ )
1024
+ end
1025
+ end
1026
+
1027
+ context "when tallying deeply nested arrays/embedded associations" do
1028
+
1029
+ before do
1030
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))),
1031
+ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
1032
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))),
1033
+ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
1034
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))),
1035
+ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))) ])
1036
+ end
1037
+
1038
+ let(:tally) do
1039
+ Person.tally("addresses.code.deepest.array.y.z")
1040
+ end
1041
+
1042
+ it "returns the correct hash" do
1043
+ expect(tally).to eq(
1044
+ [ [ 1, 2 ], [ 1, 2 ] ] => 2,
1045
+ [ [ 1, 3 ], [ 1, 3 ] ] => 1
1046
+ )
1047
+ end
1048
+ end
1049
+
1050
+ context "when some keys are missing" do
1051
+ before do
1052
+ 3.times { Band.create!(origin: "tally") }
1053
+ end
1054
+
1055
+ let(:tally) do
1056
+ criteria.tally(:name)
1057
+ end
1058
+
1059
+ it "returns the correct hash" do
1060
+ expect(tally).to eq(
1061
+ "Depeche Mode" => 1,
1062
+ "New Order" => 1,
1063
+ "10,000 Maniacs" => 1,
1064
+ nil => 3
1065
+ )
1066
+ end
1067
+ end
1068
+
1069
+ context "when the first element is an embeds_one" do
1070
+ before do
1071
+ Person.create!(name: Name.new(translations: [ Translation.new(language: 1), Translation.new(language: 2) ]))
1072
+ Person.create!(name: Name.new(translations: [ Translation.new(language: 1), Translation.new(language: 2) ]))
1073
+ Person.create!(name: Name.new(translations: [ Translation.new(language: 1), Translation.new(language: 3) ]))
1074
+ end
1075
+
1076
+ let(:tally) do
1077
+ Person.tally("name.translations.language")
1078
+ end
1079
+
1080
+ it "returns the correct hash" do
1081
+ expect(tally).to eq(
1082
+ [1, 2] => 2,
1083
+ [1, 3] => 1
1084
+ )
1085
+ end
1086
+ end
1087
+
1088
+ context "when tallying demongoizable values from typeless fields" do
1089
+
1090
+ let!(:person1) { Person.create!(ssn: /hello/) }
1091
+ let!(:person2) { Person.create!(ssn: BSON::Decimal128.new("1")) }
1092
+ let(:tally) { Person.tally("ssn") }
1093
+
1094
+ context "< BSON 5" do
1095
+ max_bson_version '4.99.99'
1096
+
1097
+ it "stores the correct types in the database" do
1098
+ Person.find(person1.id).attributes["ssn"].should be_a BSON::Regexp::Raw
1099
+ Person.find(person2.id).attributes["ssn"].should be_a BSON::Decimal128
1100
+ end
1101
+
1102
+ it "tallies the correct type" do
1103
+ tally.keys.map(&:class).sort do |a,b|
1104
+ a.to_s <=> b.to_s
1105
+ end.should == [BSON::Decimal128, BSON::Regexp::Raw]
1106
+ end
1107
+ end
1108
+
1109
+ context ">= BSON 5" do
1110
+ min_bson_version "5.0"
1111
+
1112
+ it "stores the correct types in the database" do
1113
+ Person.find(person1.id).ssn.should be_a BSON::Regexp::Raw
1114
+ Person.find(person2.id).ssn.should be_a BigDeimal
1115
+ end
1116
+
1117
+ it "tallies the correct type" do
1118
+ tally.keys.map(&:class).sort do |a,b|
1119
+ a.to_s <=> b.to_s
1120
+ end.should == [BigDecimal, BSON::Regexp::Raw]
1121
+ end
1122
+ end
1123
+ end
1124
+ end
1125
+
1126
+ describe "#each" do
1127
+
1128
+ before do
1129
+ Band.create!(name: "Depeche Mode")
1130
+ end
1131
+
1132
+ let(:criteria) do
1133
+ Band.where(name: "Depeche Mode")
1134
+ end
1135
+
1136
+ let(:context) do
1137
+ described_class.new(criteria)
1138
+ end
1139
+
1140
+ context 'when the criteria has a collation' do
1141
+ min_server_version '3.4'
1142
+
1143
+ let(:criteria) do
1144
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
1145
+ end
1146
+
1147
+ it "yields mongoid documents to the block" do
1148
+ context.each do |doc|
1149
+ expect(doc).to be_a(Mongoid::Document)
1150
+ end
1151
+ end
1152
+
1153
+ it "iterates over the matching documents" do
1154
+ context.each do |doc|
1155
+ expect(doc.name).to eq("Depeche Mode")
1156
+ end
1157
+ end
1158
+
1159
+ it "returns self" do
1160
+ expect(context.each{}).to be(context)
1161
+ end
1162
+ end
1163
+
1164
+ context "when providing a block" do
1165
+
1166
+ it "yields mongoid documents to the block" do
1167
+ context.each do |doc|
1168
+ expect(doc).to be_a(Mongoid::Document)
1169
+ end
1170
+ end
1171
+
1172
+ it "iterates over the matching documents" do
1173
+ context.each do |doc|
1174
+ expect(doc.name).to eq("Depeche Mode")
1175
+ end
1176
+ end
1177
+
1178
+ it "returns self" do
1179
+ expect(context.each{}).to be(context)
1180
+ end
1181
+ end
1182
+
1183
+ context "when no block is provided" do
1184
+
1185
+ let(:enum) do
1186
+ context.each
1187
+ end
1188
+
1189
+ it "returns an enumerator" do
1190
+ expect(enum).to be_a(Enumerator)
1191
+ end
1192
+
1193
+ context "when iterating over the enumerator" do
1194
+
1195
+ context "when iterating with each" do
1196
+
1197
+ it "yields mongoid documents to the block" do
1198
+ enum.each do |doc|
1199
+ expect(doc).to be_a(Mongoid::Document)
1200
+ end
1201
+ end
1202
+ end
1203
+
1204
+ context "when iterating with next" do
1205
+
1206
+ before do
1207
+ 10.times { |i| Band.create!(name: "Test #{i}") }
1208
+ end
1209
+
1210
+ let(:criteria) do
1211
+ Band.batch_size(5)
1212
+ end
1213
+
1214
+ it "yields mongoid documents" do
1215
+ expect(enum.next).to be_a(Mongoid::Document)
1216
+ end
1217
+
1218
+ it "does not load all documents" do
1219
+ subscriber = Mrss::EventSubscriber.new
1220
+ context.view.client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
1221
+
1222
+ enum.next
1223
+
1224
+ find_events = subscriber.all_events.select do |evt|
1225
+ evt.command_name == 'find'
1226
+ end
1227
+ expect(find_events.length).to be(2)
1228
+ get_more_events = subscriber.all_events.select do |evt|
1229
+ evt.command_name == 'getMore'
1230
+ end
1231
+ expect(get_more_events.length).to be(0)
1232
+ ensure
1233
+ context.view.client.unsubscribe(Mongo::Monitoring::COMMAND, subscriber)
1234
+ end
1235
+ end
1236
+ end
1237
+ end
1238
+
1239
+ context 'when the criteria has a parent document' do
1240
+
1241
+ before do
1242
+ Post.create!(person: person)
1243
+ Post.create!(person: person)
1244
+ Post.create!(person: person)
1245
+ end
1246
+
1247
+ let(:person) do
1248
+ Person.new
1249
+ end
1250
+
1251
+ let(:criteria) do
1252
+ person.posts.all
1253
+ end
1254
+
1255
+ let(:persons) do
1256
+ criteria.collect(&:person)
1257
+ end
1258
+
1259
+ it 'sets the same parent object on each related object' do
1260
+ expect(persons.uniq.size).to eq(1)
1261
+ end
1262
+ end
1263
+ end
1264
+
1265
+ describe "#eager_load" do
1266
+
1267
+ let(:criteria) do
1268
+ Person.includes(:game)
1269
+ end
1270
+
1271
+ let(:context) do
1272
+ described_class.new(criteria)
1273
+ end
1274
+
1275
+ context "when no documents are returned" do
1276
+
1277
+ let(:game_association) do
1278
+ Person.reflect_on_association(:game)
1279
+ end
1280
+
1281
+ it "does not make any additional database queries" do
1282
+ expect(game_association).to receive(:eager_load).never
1283
+ context.send(:eager_load, [])
1284
+ end
1285
+ end
1286
+ end
1287
+
1288
+ describe "#exists?" do
1289
+
1290
+ let!(:band) do
1291
+ Band.create!(name: "Depeche Mode", active: true)
1292
+ end
1293
+
1294
+ context "when not passing options" do
1295
+
1296
+ context "when the count is zero" do
1297
+
1298
+ let(:criteria) do
1299
+ Band.where(name: "New Order")
1300
+ end
1301
+
1302
+ let(:context) do
1303
+ described_class.new(criteria)
1304
+ end
1305
+
1306
+ it "returns false" do
1307
+ expect(context).to_not be_exists
1308
+ end
1309
+ end
1310
+
1311
+ context "when the count is greater than zero" do
1312
+
1313
+ let(:criteria) do
1314
+ Band.where(name: "Depeche Mode")
1315
+ end
1316
+
1317
+ let(:context) do
1318
+ described_class.new(criteria)
1319
+ end
1320
+
1321
+ it "returns true" do
1322
+ expect(context).to be_exists
1323
+ end
1324
+ end
1325
+
1326
+ context "when caching is not enabled" do
1327
+
1328
+ let(:criteria) do
1329
+ Band.where(name: "Depeche Mode")
1330
+ end
1331
+
1332
+ let(:context) do
1333
+ described_class.new(criteria)
1334
+ end
1335
+
1336
+ context "when exists? already called and query cache is enabled" do
1337
+ query_cache_enabled
1338
+
1339
+ before do
1340
+ context.exists?
1341
+ end
1342
+
1343
+ it "does not hit the database again" do
1344
+ expect_no_queries do
1345
+ expect(context).to be_exists
1346
+ end
1347
+ end
1348
+ end
1349
+ end
1350
+ end
1351
+
1352
+ context "when passing an _id" do
1353
+
1354
+ context "when its of type BSON::ObjectId" do
1355
+
1356
+ context "when calling it on the class" do
1357
+
1358
+ it "returns true" do
1359
+ expect(Band.exists?(band._id)).to be true
1360
+ end
1361
+ end
1362
+
1363
+ context "when calling it on a criteria that includes the object" do
1364
+
1365
+ it "returns true" do
1366
+ expect(Band.where(name: band.name).exists?(band._id)).to be true
1367
+ end
1368
+ end
1369
+
1370
+ context "when calling it on a criteria that does not include the object" do
1371
+
1372
+ it "returns false" do
1373
+ expect(Band.where(name: "bogus").exists?(band._id)).to be false
1374
+ end
1375
+ end
1376
+
1377
+ context "when the id does not exist" do
1378
+
1379
+ it "returns false" do
1380
+ expect(Band.exists?(BSON::ObjectId.new)).to be false
1381
+ end
1382
+ end
1383
+ end
1384
+
1385
+ context "when its of type String" do
1386
+
1387
+ context "when the id exists" do
1388
+
1389
+ it "returns true" do
1390
+ expect(Band.exists?(band._id.to_s)).to be true
1391
+ end
1392
+ end
1393
+
1394
+ context "when the id does not exist" do
1395
+
1396
+ it "returns false" do
1397
+ expect(Band.exists?(BSON::ObjectId.new.to_s)).to be false
1398
+ end
1399
+ end
1400
+ end
1401
+ end
1402
+
1403
+ context "when passing a hash" do
1404
+
1405
+ context "when calling it on the class" do
1406
+
1407
+ it "returns true" do
1408
+ expect(Band.exists?(name: band.name)).to be true
1409
+ end
1410
+ end
1411
+
1412
+ context "when calling it on a criteria that includes the object" do
1413
+
1414
+ it "returns true" do
1415
+ expect(Band.where(active: true).exists?(name: band.name)).to be true
1416
+ end
1417
+ end
1418
+
1419
+ context "when calling it on a criteria that does not include the object" do
1420
+
1421
+ it "returns false" do
1422
+ expect(Band.where(active: false).exists?(name: band.name)).to be false
1423
+ end
1424
+ end
1425
+
1426
+ context "when the conditions don't match" do
1427
+
1428
+ it "returns false" do
1429
+ expect(Band.exists?(name: "bogus")).to be false
1430
+ end
1431
+ end
1432
+ end
1433
+
1434
+ context "when passing false" do
1435
+
1436
+ it "returns false" do
1437
+ expect(Band.exists?(false)).to be false
1438
+ end
1439
+ end
1440
+
1441
+ context "when passing nil" do
1442
+
1443
+ it "returns false" do
1444
+ expect(Band.exists?(nil)).to be false
1445
+ end
1446
+ end
1447
+
1448
+ context "when the limit is 0" do
1449
+
1450
+ it "returns false" do
1451
+ expect(Band.limit(0).exists?).to be false
1452
+ end
1453
+ end
1454
+
1455
+ context "when the criteria limit is 0" do
1456
+
1457
+ it "returns false" do
1458
+ expect(Band.criteria.limit(0).exists?).to be false
1459
+ end
1460
+ end
1461
+ end
1462
+
1463
+ describe "#explain" do
1464
+
1465
+ let(:criteria) do
1466
+ Band.where(name: "Depeche Mode")
1467
+ end
1468
+
1469
+ let(:context) do
1470
+ described_class.new(criteria)
1471
+ end
1472
+
1473
+ it "returns the criteria explain path" do
1474
+ expect(context.explain).to_not be_empty
1475
+ end
1476
+ end
1477
+
1478
+ describe "#find_one_and_replace" do
1479
+
1480
+ let!(:depeche) do
1481
+ Band.create!(name: "Depeche Mode")
1482
+ end
1483
+
1484
+ let!(:tool) do
1485
+ Band.create!(name: "Tool")
1486
+ end
1487
+
1488
+ context "when the selector matches" do
1489
+
1490
+ context "when not providing options" do
1491
+
1492
+ let(:criteria) do
1493
+ Band.where(name: "Depeche Mode")
1494
+ end
1495
+
1496
+ let(:context) do
1497
+ described_class.new(criteria)
1498
+ end
1499
+
1500
+ let!(:result) do
1501
+ context.find_one_and_replace(name: 'FKA Twigs')
1502
+ end
1503
+
1504
+ it "returns the first matching document" do
1505
+ expect(result).to eq(depeche)
1506
+ end
1507
+
1508
+ it "updates the document in the database" do
1509
+ expect(depeche.reload.name).to eq('FKA Twigs')
1510
+ end
1511
+ end
1512
+
1513
+ context "when sorting" do
1514
+
1515
+ let(:criteria) do
1516
+ Band.desc(:name)
1517
+ end
1518
+
1519
+ let(:context) do
1520
+ described_class.new(criteria)
1521
+ end
1522
+
1523
+ let!(:result) do
1524
+ context.find_one_and_replace(likes: 1)
1525
+ end
1526
+
1527
+ it "returns the first matching document" do
1528
+ expect(result).to eq(tool)
1529
+ end
1530
+
1531
+ it "updates the document in the database" do
1532
+ expect(tool.reload.likes).to eq(1)
1533
+ expect(tool.reload.name).to be_nil
1534
+ end
1535
+ end
1536
+
1537
+ context "when limiting fields" do
1538
+
1539
+ let(:criteria) do
1540
+ Band.only(:_id)
1541
+ end
1542
+
1543
+ let(:context) do
1544
+ described_class.new(criteria)
1545
+ end
1546
+
1547
+ let!(:result) do
1548
+ context.find_one_and_replace(name: 'FKA Twigs', likes: 1)
1549
+ end
1550
+
1551
+ it "returns the first matching document" do
1552
+ expect(result).to eq(depeche)
1553
+ end
1554
+
1555
+ it "limits the returned fields" do
1556
+ expect(result.name).to be_nil
1557
+ end
1558
+
1559
+ it "updates the document in the database" do
1560
+ expect(depeche.reload.likes).to eq(1)
1561
+ end
1562
+ end
1563
+
1564
+ context "when returning new" do
1565
+
1566
+ let(:criteria) do
1567
+ Band.where(name: "Depeche Mode")
1568
+ end
1569
+
1570
+ let(:context) do
1571
+ described_class.new(criteria)
1572
+ end
1573
+
1574
+ let!(:result) do
1575
+ context.find_one_and_replace({ likes: 1 }, return_document: :after)
1576
+ end
1577
+
1578
+ it "returns the first matching document" do
1579
+ expect(result).to eq(depeche)
1580
+ end
1581
+
1582
+ it "returns the updated document" do
1583
+ expect(result.name).to be_nil
1584
+ expect(result.likes).to eq(1)
1585
+ end
1586
+ end
1587
+
1588
+ context 'when a collation is specified on the criteria' do
1589
+ min_server_version '3.4'
1590
+
1591
+ let(:criteria) do
1592
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
1593
+ end
1594
+
1595
+ let(:context) do
1596
+ described_class.new(criteria)
1597
+ end
1598
+
1599
+ let!(:result) do
1600
+ context.find_one_and_replace({ likes: 1 }, return_document: :after)
1601
+ end
1602
+
1603
+ it "returns the first matching document" do
1604
+ expect(result).to eq(depeche)
1605
+ end
1606
+
1607
+ it "returns the updated document" do
1608
+ expect(result.likes).to eq(1)
1609
+ expect(result.name).to be_nil
1610
+ end
1611
+ end
1612
+ end
1613
+
1614
+ context "when the selector does not match" do
1615
+
1616
+ let(:criteria) do
1617
+ Band.where(name: "DEPECHE MODE")
1618
+ end
1619
+
1620
+ let(:context) do
1621
+ described_class.new(criteria)
1622
+ end
1623
+
1624
+ let(:result) do
1625
+ context.find_one_and_replace(name: 'FKA Twigs')
1626
+ end
1627
+
1628
+ it "returns nil" do
1629
+ expect(result).to be_nil
1630
+ end
1631
+ end
1632
+ end
1633
+
1634
+ describe "#find_one_and_update" do
1635
+
1636
+ let!(:depeche) do
1637
+ Band.create!(name: "Depeche Mode")
1638
+ end
1639
+
1640
+ let!(:tool) do
1641
+ Band.create!(name: "Tool")
1642
+ end
1643
+
1644
+ context "when the selector matches" do
1645
+
1646
+ context "when not providing options" do
1647
+
1648
+ let(:criteria) do
1649
+ Band.where(name: "Depeche Mode")
1650
+ end
1651
+
1652
+ let(:context) do
1653
+ described_class.new(criteria)
1654
+ end
1655
+
1656
+ let!(:result) do
1657
+ context.find_one_and_update("$inc" => { likes: 1 })
1658
+ end
1659
+
1660
+ it "returns the first matching document" do
1661
+ expect(result).to eq(depeche)
1662
+ end
1663
+
1664
+ it "updates the document in the database" do
1665
+ expect(depeche.reload.likes).to eq(1)
1666
+ end
1667
+ end
1668
+
1669
+ context "when sorting" do
1670
+
1671
+ let(:criteria) do
1672
+ Band.desc(:name)
1673
+ end
1674
+
1675
+ let(:context) do
1676
+ described_class.new(criteria)
1677
+ end
1678
+
1679
+ let!(:result) do
1680
+ context.find_one_and_update("$inc" => { likes: 1 })
1681
+ end
1682
+
1683
+ it "returns the first matching document" do
1684
+ expect(result).to eq(tool)
1685
+ end
1686
+
1687
+ it "updates the document in the database" do
1688
+ expect(tool.reload.likes).to eq(1)
1689
+ end
1690
+ end
1691
+
1692
+ context "when limiting fields" do
1693
+
1694
+ let(:criteria) do
1695
+ Band.only(:_id)
1696
+ end
1697
+
1698
+ let(:context) do
1699
+ described_class.new(criteria)
1700
+ end
1701
+
1702
+ let!(:result) do
1703
+ context.find_one_and_update("$inc" => { likes: 1 })
1704
+ end
1705
+
1706
+ it "returns the first matching document" do
1707
+ expect(result).to eq(depeche)
1708
+ end
1709
+
1710
+ it "limits the returned fields" do
1711
+ expect(result.name).to be_nil
1712
+ end
1713
+
1714
+ it "updates the document in the database" do
1715
+ expect(depeche.reload.likes).to eq(1)
1716
+ end
1717
+ end
1718
+
1719
+ context "when returning new" do
1720
+
1721
+ let(:criteria) do
1722
+ Band.where(name: "Depeche Mode")
1723
+ end
1724
+
1725
+ let(:context) do
1726
+ described_class.new(criteria)
1727
+ end
1728
+
1729
+ let!(:result) do
1730
+ context.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
1731
+ end
1732
+
1733
+ it "returns the first matching document" do
1734
+ expect(result).to eq(depeche)
1735
+ end
1736
+
1737
+ it "returns the updated document" do
1738
+ expect(result.likes).to eq(1)
1739
+ end
1740
+ end
1741
+
1742
+ context 'when a collation is specified on the criteria' do
1743
+ min_server_version '3.4'
1744
+
1745
+ let(:criteria) do
1746
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
1747
+ end
1748
+
1749
+ let(:context) do
1750
+ described_class.new(criteria)
1751
+ end
1752
+
1753
+ let!(:result) do
1754
+ context.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
1755
+ end
1756
+
1757
+ it "returns the first matching document" do
1758
+ expect(result).to eq(depeche)
1759
+ end
1760
+
1761
+ it "returns the updated document" do
1762
+ expect(result.likes).to eq(1)
1763
+ end
1764
+ end
1765
+ end
1766
+
1767
+ context "when the selector does not match" do
1768
+
1769
+ let(:criteria) do
1770
+ Band.where(name: "Placebo")
1771
+ end
1772
+
1773
+ let(:context) do
1774
+ described_class.new(criteria)
1775
+ end
1776
+
1777
+ let(:result) do
1778
+ context.find_one_and_update("$inc" => { likes: 1 })
1779
+ end
1780
+
1781
+ it "returns nil" do
1782
+ expect(result).to be_nil
1783
+ end
1784
+ end
1785
+ end
1786
+
1787
+ describe "#find_one_and_delete" do
1788
+
1789
+ let!(:depeche) do
1790
+ Band.create!(name: "Depeche Mode")
1791
+ end
1792
+
1793
+ let(:criteria) do
1794
+ Band.where(name: "Depeche Mode")
1795
+ end
1796
+
1797
+ let(:context) do
1798
+ described_class.new(criteria)
1799
+ end
1800
+
1801
+ let!(:result) do
1802
+ context.find_one_and_delete
1803
+ end
1804
+
1805
+ context 'when the selector matches a document' do
1806
+
1807
+ it "returns the first matching document" do
1808
+ expect(result).to eq(depeche)
1809
+ end
1810
+
1811
+ it "deletes the document from the database" do
1812
+ expect {
1813
+ depeche.reload
1814
+ }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class Band with id\(s\)/)
1815
+ end
1816
+
1817
+ context 'when a collation is specified on the criteria' do
1818
+ min_server_version '3.4'
1819
+
1820
+ let(:criteria) do
1821
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
1822
+ end
1823
+
1824
+ let(:context) do
1825
+ described_class.new(criteria)
1826
+ end
1827
+
1828
+ let!(:result) do
1829
+ context.find_one_and_delete
1830
+ end
1831
+
1832
+ it "returns the first matching document" do
1833
+ expect(result).to eq(depeche)
1834
+ end
1835
+
1836
+ it "deletes the document from the database" do
1837
+ expect {
1838
+ depeche.reload
1839
+ }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class Band with id\(s\)/)
1840
+ end
1841
+ end
1842
+ end
1843
+
1844
+ context 'when the selector does not match a document' do
1845
+
1846
+ let(:criteria) do
1847
+ Band.where(name: "Placebo")
1848
+ end
1849
+
1850
+ let(:context) do
1851
+ described_class.new(criteria)
1852
+ end
1853
+
1854
+ let(:result) do
1855
+ context.find_one_and_delete
1856
+ end
1857
+
1858
+ it "returns nil" do
1859
+ expect(result).to be_nil
1860
+ end
1861
+ end
1862
+ end
1863
+
1864
+ [ :first, :one ].each do |method|
1865
+
1866
+ describe "##{method}" do
1867
+
1868
+ let!(:depeche_mode) do
1869
+ Band.create!(name: "Depeche Mode")
1870
+ end
1871
+
1872
+ let!(:new_order) do
1873
+ Band.create!(name: "New Order")
1874
+ end
1875
+
1876
+ let!(:rolling_stones) do
1877
+ Band.create!(name: "The Rolling Stones")
1878
+ end
1879
+
1880
+ context "when the context is not cached" do
1881
+
1882
+ let(:criteria) do
1883
+ Band.where(name: "Depeche Mode")
1884
+ end
1885
+
1886
+ let(:context) do
1887
+ described_class.new(criteria)
1888
+ end
1889
+
1890
+ it "returns the first matching document" do
1891
+ expect(context.send(method)).to eq(depeche_mode)
1892
+ end
1893
+
1894
+ context 'when the criteria has a collation' do
1895
+ min_server_version '3.4'
1896
+
1897
+ let(:criteria) do
1898
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
1899
+ end
1900
+
1901
+ it "returns the first matching document" do
1902
+ expect(context.send(method)).to eq(depeche_mode)
1903
+ end
1904
+ end
1905
+ end
1906
+
1907
+ context "when using .desc" do
1908
+
1909
+ let(:criteria) do
1910
+ Band.desc(:name)
1911
+ end
1912
+
1913
+ let(:context) do
1914
+ described_class.new(criteria)
1915
+ end
1916
+
1917
+ context "when there is sort on the context" do
1918
+
1919
+ it "follows the main sort" do
1920
+ expect(context.send(method)).to eq(rolling_stones)
1921
+ end
1922
+ end
1923
+
1924
+ context "when subsequently calling #last" do
1925
+
1926
+ it "returns the correct document" do
1927
+ expect(context.send(method)).to eq(rolling_stones)
1928
+ expect(context.last).to eq(depeche_mode)
1929
+ end
1930
+ end
1931
+ end
1932
+
1933
+ context 'when the criteria has no sort' do
1934
+
1935
+ let(:criteria) do
1936
+ Band.all
1937
+ end
1938
+
1939
+ let(:context) do
1940
+ described_class.new(criteria)
1941
+ end
1942
+
1943
+
1944
+ it 'applies a sort on _id' do
1945
+ expect(context.send(method)).to eq(depeche_mode)
1946
+ end
1947
+
1948
+ context 'when calling #last' do
1949
+
1950
+ it 'returns the last document, sorted by _id' do
1951
+ expect(context.send(method)).to eq(depeche_mode)
1952
+ expect(context.last).to eq(rolling_stones)
1953
+ end
1954
+ end
1955
+ end
1956
+
1957
+ context 'when the criteria has a sort' do
1958
+
1959
+ let(:criteria) do
1960
+ Band.desc(:name)
1961
+ end
1962
+
1963
+ let(:context) do
1964
+ described_class.new(criteria)
1965
+ end
1966
+
1967
+ it 'applies the criteria sort' do
1968
+ expect(context.send(method)).to eq(rolling_stones)
1969
+ end
1970
+
1971
+ context 'when calling #last' do
1972
+
1973
+ it 'applies the criteria sort' do
1974
+ expect(context.send(method)).to eq(rolling_stones)
1975
+ expect(context.last).to eq(depeche_mode)
1976
+ end
1977
+ end
1978
+ end
1979
+
1980
+ context "when using .sort" do
1981
+
1982
+ let(:criteria) do
1983
+ Band.all.sort(:name => -1).criteria
1984
+ end
1985
+
1986
+ let(:context) do
1987
+ described_class.new(criteria)
1988
+ end
1989
+
1990
+ context "when there is sort on the context" do
1991
+
1992
+ it "follows the main sort" do
1993
+ expect(context.send(method)).to eq(rolling_stones)
1994
+ end
1995
+ end
1996
+
1997
+ context "when subsequently calling #last" do
1998
+
1999
+ it "returns the correct document" do
2000
+ expect(context.send(method)).to eq(rolling_stones)
2001
+ expect(context.last).to eq(depeche_mode)
2002
+ end
2003
+ end
2004
+ end
2005
+
2006
+ context "when the query cache is enabled" do
2007
+ query_cache_enabled
2008
+
2009
+ let(:criteria) do
2010
+ Band.where(name: "Depeche Mode")
2011
+ end
2012
+
2013
+ let(:context) do
2014
+ described_class.new(criteria)
2015
+ end
2016
+
2017
+ context "when first method was called before" do
2018
+
2019
+ before do
2020
+ context.first
2021
+ end
2022
+
2023
+ it "returns the first document without touching the database" do
2024
+ expect_no_queries do
2025
+ expect(context.send(method)).to eq(depeche_mode)
2026
+ end
2027
+ end
2028
+ end
2029
+ end
2030
+
2031
+ context "when including a limit" do
2032
+
2033
+ context "when the context is not cached" do
2034
+
2035
+ let(:context) do
2036
+ described_class.new(criteria)
2037
+ end
2038
+
2039
+ context "when the limit is 1" do
2040
+ let(:criteria) do
2041
+ Band.criteria
2042
+ end
2043
+
2044
+ let(:docs) do
2045
+ context.send(method, 1)
2046
+ end
2047
+
2048
+ it "returns an array of documents" do
2049
+ expect(docs).to eq([ depeche_mode ])
2050
+ end
2051
+ end
2052
+
2053
+ context "when the limit is >1" do
2054
+ let(:criteria) do
2055
+ Band.criteria
2056
+ end
2057
+
2058
+ let(:docs) do
2059
+ context.send(method, 2)
2060
+ end
2061
+
2062
+ it "returns the number of documents in order" do
2063
+ expect(docs).to eq([ depeche_mode, new_order ])
2064
+ end
2065
+ end
2066
+
2067
+ context 'when the criteria has a collation' do
2068
+ min_server_version '3.4'
2069
+
2070
+ let(:criteria) do
2071
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
2072
+ end
2073
+
2074
+ it "returns the first matching document" do
2075
+ expect(context.send(method, 1)).to eq([ depeche_mode ])
2076
+ end
2077
+ end
2078
+ end
2079
+
2080
+ context "when the query cache is enabled" do
2081
+
2082
+ let(:context) do
2083
+ described_class.new(criteria)
2084
+ end
2085
+
2086
+ context "when calling first beforehand" do
2087
+ query_cache_enabled
2088
+
2089
+ let(:context) do
2090
+ described_class.new(criteria)
2091
+ end
2092
+
2093
+ let(:criteria) do
2094
+ Band.all
2095
+ end
2096
+
2097
+ before do
2098
+ context.first(before_limit)
2099
+ end
2100
+
2101
+ let(:docs) do
2102
+ context.send(method, limit)
2103
+ end
2104
+
2105
+ context "when getting all of the documents before" do
2106
+ let(:before_limit) { 3 }
2107
+
2108
+ context "when getting all of the documents" do
2109
+ let(:limit) { 3 }
2110
+
2111
+ it "returns all documents without touching the database" do
2112
+ expect_no_queries do
2113
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2114
+ end
2115
+ end
2116
+ end
2117
+
2118
+ context "when getting fewer documents" do
2119
+ let(:limit) { 2 }
2120
+
2121
+ it "returns the correct documents without touching the database" do
2122
+ expect_no_queries do
2123
+ expect(docs).to eq([ depeche_mode, new_order ])
2124
+ end
2125
+ end
2126
+ end
2127
+ end
2128
+
2129
+ context "when getting fewer documents before" do
2130
+ let(:before_limit) { 2 }
2131
+
2132
+ context "when getting the same number of documents" do
2133
+ let(:limit) { 2 }
2134
+
2135
+ it "returns the correct documents without touching the database" do
2136
+ expect_no_queries do
2137
+ expect(docs).to eq([ depeche_mode, new_order ])
2138
+ end
2139
+ end
2140
+ end
2141
+
2142
+ context "when getting more documents" do
2143
+ let(:limit) { 3 }
2144
+
2145
+ it "returns the correct documents and touches the database" do
2146
+ expect_query(1) do
2147
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2148
+ end
2149
+ end
2150
+ end
2151
+ end
2152
+
2153
+ context "when getting one document before" do
2154
+ let(:before_limit) { 1 }
2155
+
2156
+ context "when getting one document" do
2157
+ let(:limit) { 1 }
2158
+
2159
+ it "returns the correct documents without touching the database" do
2160
+ expect_no_queries do
2161
+ expect(docs).to eq([ depeche_mode ])
2162
+ end
2163
+ end
2164
+ end
2165
+
2166
+ context "when getting more than one document" do
2167
+ let(:limit) { 3 }
2168
+
2169
+ it "returns the correct documents and touches the database" do
2170
+ expect_query(1) do
2171
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2172
+ end
2173
+ end
2174
+ end
2175
+ end
2176
+ end
2177
+ end
2178
+ end
2179
+
2180
+ context "when calling #first then #last and the query cache is enabled" do
2181
+ query_cache_enabled
2182
+
2183
+ let(:context) do
2184
+ described_class.new(criteria)
2185
+ end
2186
+
2187
+ let(:criteria) do
2188
+ Band.all
2189
+ end
2190
+
2191
+ before do
2192
+ context.first(before_limit)
2193
+ end
2194
+
2195
+ let(:docs) do
2196
+ context.last(limit)
2197
+ end
2198
+
2199
+ context "when getting one from the beginning and one from the end" do
2200
+ let(:before_limit) { 2 }
2201
+ let(:limit) { 1 }
2202
+
2203
+ it "gets the correct document and hits the database" do
2204
+ expect_query(1) do
2205
+ expect(docs).to eq([rolling_stones])
2206
+ end
2207
+ end
2208
+ end
2209
+ end
2210
+ end
2211
+ end
2212
+
2213
+ describe "#last" do
2214
+ let!(:depeche_mode) do
2215
+ Band.create!(name: "Depeche Mode")
2216
+ end
2217
+
2218
+ let!(:new_order) do
2219
+ Band.create!(name: "New Order")
2220
+ end
2221
+
2222
+ let!(:rolling_stones) do
2223
+ Band.create!(name: "The Rolling Stones")
2224
+ end
2225
+
2226
+ context "when the context is not cached" do
2227
+
2228
+ let(:criteria) do
2229
+ Band.where(name: "Depeche Mode")
2230
+ end
2231
+
2232
+ let(:context) do
2233
+ described_class.new(criteria)
2234
+ end
2235
+
2236
+ it "returns the last matching document" do
2237
+ expect(context.last).to eq(depeche_mode)
2238
+ end
2239
+
2240
+ context 'when the criteria has a collation' do
2241
+ min_server_version '3.4'
2242
+
2243
+ let(:criteria) do
2244
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
2245
+ end
2246
+
2247
+ it "returns the last matching document" do
2248
+ expect(context.last).to eq(depeche_mode)
2249
+ end
2250
+ end
2251
+ end
2252
+
2253
+ context "when using .desc" do
2254
+
2255
+ let(:criteria) do
2256
+ Band.desc(:name)
2257
+ end
2258
+
2259
+ let(:context) do
2260
+ described_class.new(criteria)
2261
+ end
2262
+
2263
+ context "when there is sort on the context" do
2264
+
2265
+ it "follows the main sort" do
2266
+ expect(context.last).to eq(depeche_mode)
2267
+ end
2268
+ end
2269
+
2270
+ context "when subsequently calling #first" do
2271
+
2272
+ it "returns the correct document" do
2273
+ expect(context.last).to eq(depeche_mode)
2274
+ expect(context.first).to eq(rolling_stones)
2275
+ end
2276
+ end
2277
+ end
2278
+
2279
+ context 'when the criteria has no sort' do
2280
+
2281
+ let(:criteria) do
2282
+ Band.all
2283
+ end
2284
+
2285
+ let(:context) do
2286
+ described_class.new(criteria)
2287
+ end
2288
+
2289
+ it 'applies a sort on _id' do
2290
+ expect(context.last).to eq(rolling_stones)
2291
+ end
2292
+
2293
+ context 'when calling #first' do
2294
+
2295
+ it 'returns the first document, sorted by _id' do
2296
+ expect(context.last).to eq(rolling_stones)
2297
+ expect(context.first).to eq(depeche_mode)
2298
+ end
2299
+ end
2300
+ end
2301
+
2302
+ context 'when the criteria has a sort' do
2303
+
2304
+ let(:criteria) do
2305
+ Band.desc(:name)
2306
+ end
2307
+
2308
+ let(:context) do
2309
+ described_class.new(criteria)
2310
+ end
2311
+
2312
+
2313
+ it 'applies the criteria sort' do
2314
+ expect(context.last).to eq(depeche_mode)
2315
+ end
2316
+
2317
+ context 'when calling #first' do
2318
+
2319
+ it 'applies the criteria sort' do
2320
+ expect(context.last).to eq(depeche_mode)
2321
+ expect(context.first).to eq(rolling_stones)
2322
+ end
2323
+ end
2324
+ end
2325
+
2326
+ context "when using .sort" do
2327
+
2328
+ let(:criteria) do
2329
+ Band.all.sort(:name => -1).criteria
2330
+ end
2331
+
2332
+ let(:context) do
2333
+ described_class.new(criteria)
2334
+ end
2335
+
2336
+ context "when there is sort on the context" do
2337
+
2338
+ it "follows the main sort" do
2339
+ expect(context.last).to eq(depeche_mode)
2340
+ end
2341
+ end
2342
+
2343
+ context "when subsequently calling #first" do
2344
+
2345
+ it "returns the correct document" do
2346
+ expect(context.last).to eq(depeche_mode)
2347
+ expect(context.first).to eq(rolling_stones)
2348
+ end
2349
+ end
2350
+ end
2351
+
2352
+ context "when the query cache is enabled" do
2353
+ query_cache_enabled
2354
+
2355
+ let(:criteria) do
2356
+ Band.where(name: "Depeche Mode")
2357
+ end
2358
+
2359
+ let(:context) do
2360
+ described_class.new(criteria)
2361
+ end
2362
+
2363
+ context "when last method was called before" do
2364
+
2365
+ before do
2366
+ context.last
2367
+ end
2368
+
2369
+ it "returns the last document without touching the database" do
2370
+ expect_no_queries do
2371
+ expect(context.last).to eq(depeche_mode)
2372
+ end
2373
+ end
2374
+ end
2375
+ end
2376
+
2377
+ context "when including a limit" do
2378
+
2379
+ context "when the context is not cached" do
2380
+
2381
+ let(:context) do
2382
+ described_class.new(criteria)
2383
+ end
2384
+
2385
+ context "when the limit is 1" do
2386
+ let(:criteria) do
2387
+ Band.criteria
2388
+ end
2389
+
2390
+ let(:docs) do
2391
+ context.last(1)
2392
+ end
2393
+
2394
+ it "returns an array of documents" do
2395
+ expect(docs).to eq([ rolling_stones ])
2396
+ end
2397
+ end
2398
+
2399
+ context "when the limit is >1" do
2400
+ let(:criteria) do
2401
+ Band.criteria
2402
+ end
2403
+
2404
+ let(:docs) do
2405
+ context.last(2)
2406
+ end
2407
+
2408
+ it "returns the number of documents in order" do
2409
+ expect(docs).to eq([ new_order, rolling_stones ])
2410
+ end
2411
+ end
2412
+
2413
+ context 'when the criteria has a collation' do
2414
+ min_server_version '3.4'
2415
+
2416
+ let(:criteria) do
2417
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
2418
+ end
2419
+
2420
+ it "returns the first matching document" do
2421
+ expect(context.last(1)).to eq([ depeche_mode ])
2422
+ end
2423
+ end
2424
+ end
2425
+
2426
+ context "when the context is cached" do
2427
+
2428
+ let(:context) do
2429
+ described_class.new(criteria)
2430
+ end
2431
+
2432
+ context "when query cache is enabled" do
2433
+ query_cache_enabled
2434
+
2435
+ let(:context) do
2436
+ described_class.new(criteria)
2437
+ end
2438
+
2439
+ let(:criteria) do
2440
+ Band.all
2441
+ end
2442
+
2443
+ before do
2444
+ context.last(before_limit)
2445
+ end
2446
+
2447
+ let(:docs) do
2448
+ context.last(limit)
2449
+ end
2450
+
2451
+ context "when getting all of the documents before" do
2452
+ let(:before_limit) { 3 }
2453
+
2454
+ context "when getting all of the documents" do
2455
+ let(:limit) { 3 }
2456
+
2457
+ it "returns all documents without touching the db" do
2458
+ expect_no_queries do
2459
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2460
+ end
2461
+ end
2462
+ end
2463
+
2464
+ context "when getting fewer documents" do
2465
+ let(:limit) { 2 }
2466
+
2467
+ it "returns the correct documents without touching the db" do
2468
+ expect_no_queries do
2469
+ expect(docs).to eq([ new_order, rolling_stones ])
2470
+ end
2471
+ end
2472
+ end
2473
+ end
2474
+
2475
+ context "when getting fewer documents before" do
2476
+ let(:before_limit) { 2 }
2477
+
2478
+ context "when getting the same number of documents" do
2479
+ let(:limit) { 2 }
2480
+
2481
+ it "returns the correct documents without touching the db" do
2482
+ expect_no_queries do
2483
+ expect(docs).to eq([ new_order, rolling_stones ])
2484
+ end
2485
+ end
2486
+ end
2487
+
2488
+ context "when getting more documents" do
2489
+ let(:limit) { 3 }
2490
+
2491
+ it "returns the correct documents and touches the database" do
2492
+ expect_query(1) do
2493
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2494
+ end
2495
+ end
2496
+ end
2497
+ end
2498
+
2499
+ context "when getting one document before" do
2500
+ let(:before_limit) { 1 }
2501
+
2502
+ context "when getting one document" do
2503
+ let(:limit) { 1 }
2504
+
2505
+ it "returns the correct documents without touching the database" do
2506
+ expect_no_queries do
2507
+ expect(docs).to eq([ rolling_stones ])
2508
+ end
2509
+ end
2510
+ end
2511
+
2512
+ context "when getting more than one document" do
2513
+ let(:limit) { 3 }
2514
+
2515
+ it "returns the correct documents and touches the database" do
2516
+ expect_query(1) do
2517
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2518
+ end
2519
+ end
2520
+ end
2521
+ end
2522
+ end
2523
+ end
2524
+ end
2525
+
2526
+ context "when calling #last then #first and the query cache is enabled" do
2527
+ query_cache_enabled
2528
+
2529
+ let(:context) do
2530
+ described_class.new(criteria)
2531
+ end
2532
+
2533
+ let(:criteria) do
2534
+ Band.all
2535
+ end
2536
+
2537
+ before do
2538
+ context.last(before_limit)
2539
+ end
2540
+
2541
+ let(:docs) do
2542
+ context.first(limit)
2543
+ end
2544
+
2545
+ context "when getting one from the beginning and one from the end" do
2546
+ let(:before_limit) { 2 }
2547
+ let(:limit) { 1 }
2548
+
2549
+ it "hits the database" do
2550
+ expect_query(1) do
2551
+ docs
2552
+ end
2553
+ end
2554
+
2555
+ it "gets the correct document" do
2556
+ expect(docs).to eq([ depeche_mode ])
2557
+ end
2558
+ end
2559
+ end
2560
+ end
2561
+
2562
+ describe "#initialize" do
2563
+
2564
+ let(:criteria) do
2565
+ Band.where(name: "Depeche Mode").no_timeout
2566
+ end
2567
+
2568
+ let(:context) do
2569
+ described_class.new(criteria)
2570
+ end
2571
+
2572
+ it "sets the criteria" do
2573
+ expect(context.criteria).to eq(criteria)
2574
+ end
2575
+
2576
+ it "sets the klass" do
2577
+ expect(context.klass).to eq(Band)
2578
+ end
2579
+
2580
+ it "sets the view" do
2581
+ expect(context.view).to be_a(Mongo::Collection::View)
2582
+ end
2583
+
2584
+ it "sets the view selector" do
2585
+ expect(context.view.selector).to eq({ "name" => "Depeche Mode" })
2586
+ end
2587
+ end
2588
+
2589
+ [ :length, :size ].each do |method|
2590
+
2591
+ describe "##{method}" do
2592
+
2593
+ before do
2594
+ Band.create!(name: "Depeche Mode")
2595
+ Band.create!(name: "New Order")
2596
+ end
2597
+
2598
+ context "when the criteria has a limit" do
2599
+
2600
+ let(:criteria) do
2601
+ Band.limit(1)
2602
+ end
2603
+
2604
+ let(:context) do
2605
+ described_class.new(criteria)
2606
+ end
2607
+
2608
+ context "when broken_view_options is false" do
2609
+ driver_config_override :broken_view_options, false
2610
+
2611
+ it "returns the number of documents that match" do
2612
+ expect(context.send(method)).to eq(1)
2613
+ end
2614
+ end
2615
+
2616
+ context "when broken_view_options is true" do
2617
+ driver_config_override :broken_view_options, true
2618
+
2619
+ it "returns the number of documents that match" do
2620
+ expect(context.send(method)).to eq(2)
2621
+ end
2622
+ end
2623
+
2624
+ context "when calling more than once with different limits" do
2625
+ driver_config_override :broken_view_options, false
2626
+
2627
+ it "does not cache the value" do
2628
+ expect(context.limit(1).send(method)).to eq(1)
2629
+ expect(context.limit(2).send(method)).to eq(2)
2630
+ end
2631
+ end
2632
+ end
2633
+
2634
+ context "when the criteria has no limit" do
2635
+
2636
+ let(:criteria) do
2637
+ Band.where(name: "Depeche Mode")
2638
+ end
2639
+
2640
+ let(:context) do
2641
+ described_class.new(criteria)
2642
+ end
2643
+
2644
+ it "returns the number of documents that match" do
2645
+ expect(context.send(method)).to eq(1)
2646
+ end
2647
+
2648
+ context "when calling more than once with different skips" do
2649
+ driver_config_override :broken_view_options, false
2650
+
2651
+ it "does not cache the value" do
2652
+ expect(context.skip(0).send(method)).to eq(1)
2653
+ expect(context.skip(1).send(method)).to eq(0)
2654
+ end
2655
+ end
2656
+
2657
+ context "when the results have been iterated over" do
2658
+
2659
+ before do
2660
+ context.entries
2661
+ end
2662
+
2663
+ it "returns the cached value for all calls" do
2664
+ expect(context.view).to receive(:count_documents).once.and_return(1)
2665
+ expect(context.send(method)).to eq(1)
2666
+ end
2667
+
2668
+ context "when the results have been iterated over multiple times" do
2669
+
2670
+ before do
2671
+ context.entries
2672
+ end
2673
+
2674
+ it "resets the length on each full iteration" do
2675
+ expect(context.size).to eq(1)
2676
+ end
2677
+ end
2678
+ end
2679
+ end
2680
+ end
2681
+ end
2682
+
2683
+ describe "#limit" do
2684
+
2685
+ let!(:depeche_mode) do
2686
+ Band.create!(name: "Depeche Mode")
2687
+ end
2688
+
2689
+ let!(:new_order) do
2690
+ Band.create!(name: "New Order")
2691
+ end
2692
+
2693
+ let(:criteria) do
2694
+ Band.all
2695
+ end
2696
+
2697
+ let(:context) do
2698
+ described_class.new(criteria)
2699
+ end
2700
+
2701
+ it "limits the results" do
2702
+ expect(context.limit(1).entries).to eq([ depeche_mode ])
2703
+ end
2704
+ end
2705
+
2706
+ describe "#take" do
2707
+
2708
+ let!(:depeche_mode) do
2709
+ Band.create!(name: "Depeche Mode")
2710
+ end
2711
+
2712
+ let!(:new_order) do
2713
+ Band.create!(name: "New Order")
2714
+ end
2715
+
2716
+ let!(:rolling_stones) do
2717
+ Band.create!(name: "The Rolling Stones")
2718
+ end
2719
+
2720
+ let(:criteria) do
2721
+ Band.all
2722
+ end
2723
+
2724
+ let(:context) do
2725
+ described_class.new(criteria)
2726
+ end
2727
+
2728
+ it "takes the correct number results" do
2729
+ expect(context.take(2)).to eq([ depeche_mode, new_order ])
2730
+ end
2731
+
2732
+ it "returns an array when passing 1" do
2733
+ expect(context.take(1)).to eq([ depeche_mode ])
2734
+ end
2735
+
2736
+ it "does not return an array when not passing an argument" do
2737
+ expect(context.take).to eq(depeche_mode)
2738
+ end
2739
+
2740
+ it "returns all the documents taking more than whats in the db" do
2741
+ expect(context.take(5)).to eq([ depeche_mode, new_order, rolling_stones ])
2742
+ end
2743
+ end
2744
+
2745
+ describe "#take!" do
2746
+
2747
+ let!(:depeche_mode) do
2748
+ Band.create!(name: "Depeche Mode")
2749
+ end
2750
+
2751
+ let!(:new_order) do
2752
+ Band.create!(name: "New Order")
2753
+ end
2754
+
2755
+ let!(:rolling_stones) do
2756
+ Band.create!(name: "The Rolling Stones")
2757
+ end
2758
+
2759
+ let(:criteria) do
2760
+ Band.all
2761
+ end
2762
+
2763
+ let(:context) do
2764
+ described_class.new(criteria)
2765
+ end
2766
+
2767
+ it "takes the first document" do
2768
+ expect(context.take!).to eq(depeche_mode)
2769
+ end
2770
+
2771
+ context "when there are no documents" do
2772
+ it "raises an error" do
2773
+ expect do
2774
+ Person.take!
2775
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Person./)
2776
+ end
2777
+ end
2778
+ end
2779
+
2780
+ describe "#map" do
2781
+
2782
+ before do
2783
+ Band.create!(name: "Depeche Mode")
2784
+ Band.create!(name: "New Order")
2785
+ end
2786
+
2787
+ let(:criteria) do
2788
+ Band.all
2789
+ end
2790
+
2791
+ let(:context) do
2792
+ described_class.new(criteria)
2793
+ end
2794
+
2795
+ context "when passed the symbol field name" do
2796
+
2797
+ it "raises an error" do
2798
+ expect do
2799
+ context.map(:name)
2800
+ end.to raise_error(ArgumentError)
2801
+ end
2802
+ end
2803
+
2804
+ context "when passed a block" do
2805
+
2806
+ it "performs mapping" do
2807
+ expect(context.map(&:name)).to eq ["Depeche Mode", "New Order"]
2808
+ end
2809
+ end
2810
+ end
2811
+
2812
+ describe "#map_reduce" do
2813
+
2814
+ let!(:depeche_mode) do
2815
+ Band.create!(name: "Depeche Mode", likes: 200)
2816
+ end
2817
+
2818
+ let!(:tool) do
2819
+ Band.create!(name: "Tool", likes: 100)
2820
+ end
2821
+
2822
+ let(:map) do
2823
+ %Q{
2824
+ function() {
2825
+ emit(this.name, { likes: this.likes });
2826
+ }}
2827
+ end
2828
+
2829
+ let(:reduce) do
2830
+ %Q{
2831
+ function(key, values) {
2832
+ var result = { likes: 0 };
2833
+ values.forEach(function(value) {
2834
+ result.likes += value.likes;
2835
+ });
2836
+ return result;
2837
+ }}
2838
+ end
2839
+
2840
+ let(:ordered_results) do
2841
+ results['results'].sort_by { |doc| doc['_id'] }
2842
+ end
2843
+
2844
+ context "when no selection is provided" do
2845
+
2846
+ let(:criteria) do
2847
+ Band.all
2848
+ end
2849
+
2850
+ let(:context) do
2851
+ described_class.new(criteria)
2852
+ end
2853
+
2854
+ let(:results) do
2855
+ context.map_reduce(map, reduce).out(inline: 1)
2856
+ end
2857
+
2858
+ it "returns the first aggregate result" do
2859
+ expect(results).to include(
2860
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
2861
+ )
2862
+ end
2863
+
2864
+ it "returns the second aggregate result" do
2865
+ expect(results).to include(
2866
+ { "_id" => "Tool", "value" => { "likes" => 100 }}
2867
+ )
2868
+ end
2869
+
2870
+ it "returns the correct number of documents" do
2871
+ expect(results.count).to eq(2)
2872
+ end
2873
+
2874
+ it "contains the entire raw results" do
2875
+ expect(ordered_results).to eq([
2876
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
2877
+ { "_id" => "Tool", "value" => { "likes" => 100 }}
2878
+ ])
2879
+ end
2880
+
2881
+ context 'when statistics are available' do
2882
+ max_server_version '4.2'
2883
+
2884
+ it "contains the execution time" do
2885
+ expect(results.time).to_not be_nil
2886
+ end
2887
+
2888
+ it "contains the count statistics" do
2889
+ expect(results["counts"]).to eq({
2890
+ "input" => 2, "emit" => 2, "reduce" => 0, "output" => 2
2891
+ })
2892
+ end
2893
+
2894
+ it "contains the input count" do
2895
+ expect(results.input).to eq(2)
2896
+ end
2897
+
2898
+ it "contains the emitted count" do
2899
+ expect(results.emitted).to eq(2)
2900
+ end
2901
+
2902
+ it "contains the reduced count" do
2903
+ expect(results.reduced).to eq(0)
2904
+ end
2905
+
2906
+ it "contains the output count" do
2907
+ expect(results.output).to eq(2)
2908
+ end
2909
+ end
2910
+ end
2911
+
2912
+ context "when selection is provided" do
2913
+
2914
+ let(:criteria) do
2915
+ Band.where(name: "Depeche Mode")
2916
+ end
2917
+
2918
+ let(:context) do
2919
+ described_class.new(criteria)
2920
+ end
2921
+
2922
+ let(:results) do
2923
+ context.map_reduce(map, reduce).out(inline: 1)
2924
+ end
2925
+
2926
+ it "includes the aggregate result" do
2927
+ expect(results).to include(
2928
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
2929
+ )
2930
+ end
2931
+
2932
+ it "returns the correct number of documents" do
2933
+ expect(results.count).to eq(1)
2934
+ end
2935
+
2936
+ it "contains the entire raw results" do
2937
+ expect(ordered_results).to eq([
2938
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
2939
+ ])
2940
+ end
2941
+
2942
+ context 'when statistics are available' do
2943
+ max_server_version '4.2'
2944
+
2945
+ it "contains the execution time" do
2946
+ expect(results.time).to_not be_nil
2947
+ end
2948
+
2949
+ it "contains the count statistics" do
2950
+ expect(results["counts"]).to eq({
2951
+ "input" => 1, "emit" => 1, "reduce" => 0, "output" => 1
2952
+ })
2953
+ end
2954
+
2955
+ it "contains the input count" do
2956
+ expect(results.input).to eq(1)
2957
+ end
2958
+
2959
+ it "contains the emitted count" do
2960
+ expect(results.emitted).to eq(1)
2961
+ end
2962
+
2963
+ it "contains the reduced count" do
2964
+ expect(results.reduced).to eq(0)
2965
+ end
2966
+
2967
+ it "contains the output count" do
2968
+ expect(results.output).to eq(1)
2969
+ end
2970
+ end
2971
+ end
2972
+
2973
+ context "when sorting is provided" do
2974
+
2975
+ before do
2976
+ Band.index(name: -1)
2977
+ Band.create_indexes
2978
+ end
2979
+
2980
+ let(:criteria) do
2981
+ Band.desc(:name)
2982
+ end
2983
+
2984
+ let(:context) do
2985
+ described_class.new(criteria)
2986
+ end
2987
+
2988
+ let(:results) do
2989
+ context.map_reduce(map, reduce).out(inline: 1)
2990
+ end
2991
+
2992
+ it "returns the first aggregate result" do
2993
+ expect(results).to include(
2994
+ { "_id" => "Tool", "value" => { "likes" => 100 }}
2995
+ )
2996
+ end
2997
+
2998
+ it "returns the second aggregate result" do
2999
+ expect(results).to include(
3000
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3001
+ )
3002
+ end
3003
+
3004
+ it "returns the correct number of documents" do
3005
+ expect(results.count).to eq(2)
3006
+ end
3007
+
3008
+ it "contains the entire raw results" do
3009
+ expect(ordered_results).to eq([
3010
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
3011
+ { "_id" => "Tool", "value" => { "likes" => 100 }}
3012
+ ])
3013
+ end
3014
+ end
3015
+
3016
+ context "when limiting is provided" do
3017
+ # map/reduce with limit is not supported on sharded clusters:
3018
+ # https://jira.mongodb.org/browse/SERVER-2099
3019
+ require_topology :single, :replica_set
3020
+
3021
+ let(:criteria) do
3022
+ Band.limit(1)
3023
+ end
3024
+
3025
+ let(:context) do
3026
+ described_class.new(criteria)
3027
+ end
3028
+
3029
+ let(:results) do
3030
+ context.map_reduce(map, reduce).out(inline: 1)
3031
+ end
3032
+
3033
+ it "returns the first aggregate result" do
3034
+ expect(results).to include(
3035
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3036
+ )
3037
+ end
3038
+
3039
+ it "returns the correct number of documents" do
3040
+ expect(results.count).to eq(1)
3041
+ end
3042
+
3043
+ it "contains the entire raw results" do
3044
+ expect(results["results"]).to eq([
3045
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3046
+ ])
3047
+ end
3048
+ end
3049
+
3050
+ context "when the output is replace" do
3051
+
3052
+ let(:criteria) do
3053
+ Band.limit(1)
3054
+ end
3055
+
3056
+ let(:context) do
3057
+ described_class.new(criteria)
3058
+ end
3059
+
3060
+ let(:results) do
3061
+ context.map_reduce(map, reduce).out(replace: "mr-output")
3062
+ end
3063
+
3064
+ it "returns the correct number of documents" do
3065
+ expect(results.count).to eq(1)
3066
+ end
3067
+
3068
+ it "contains the entire results" do
3069
+ expect(results).to eq([
3070
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3071
+ ])
3072
+ end
3073
+ end
3074
+
3075
+ context "when the output is reduce" do
3076
+
3077
+ let(:criteria) do
3078
+ Band.limit(1)
3079
+ end
3080
+
3081
+ let(:context) do
3082
+ described_class.new(criteria)
3083
+ end
3084
+
3085
+ let(:results) do
3086
+ context.map_reduce(map, reduce).out(reduce: :mr_output)
3087
+ end
3088
+
3089
+ it "returns the correct number of documents" do
3090
+ expect(results.count).to eq(1)
3091
+ end
3092
+
3093
+ it "contains the entire results" do
3094
+ expect(results).to eq([
3095
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3096
+ ])
3097
+ end
3098
+ end
3099
+
3100
+ context "when the output is merge" do
3101
+
3102
+ let(:criteria) do
3103
+ Band.limit(1)
3104
+ end
3105
+
3106
+ let(:context) do
3107
+ described_class.new(criteria)
3108
+ end
3109
+
3110
+ let(:results) do
3111
+ context.map_reduce(map, reduce).out(merge: :mr_output)
3112
+ end
3113
+
3114
+ it "returns the correct number of documents" do
3115
+ expect(results.count).to eq(1)
3116
+ end
3117
+
3118
+ it "contains the entire results" do
3119
+ expect(results).to eq([
3120
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3121
+ ])
3122
+ end
3123
+ end
3124
+
3125
+ context "when the output specifies a different db" do
3126
+ # Limit is not supported in sharded clusters
3127
+ require_topology :single, :replica_set
3128
+
3129
+ let(:criteria) do
3130
+ Band.limit(1)
3131
+ end
3132
+
3133
+ let(:context) do
3134
+ described_class.new(criteria)
3135
+ end
3136
+
3137
+ after do
3138
+ Band.with(database: 'another-db') do |b|
3139
+ b.all.delete
3140
+ end
3141
+ end
3142
+
3143
+ context 'when db is a string' do
3144
+
3145
+ let(:results) do
3146
+ context.map_reduce(map, reduce).out(merge: :mr_output, db: 'another-db')
3147
+ end
3148
+
3149
+ it "returns the correct number of documents" do
3150
+ expect(results.count).to eq(1)
3151
+ end
3152
+
3153
+ it "contains the entire results" do
3154
+ expect(results).to eq([
3155
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3156
+ ])
3157
+ end
3158
+
3159
+ it 'writes to the specified db' do
3160
+ expect(Band.mongo_client.with(database: 'another-db')[:mr_output].find.count).to eq(1)
3161
+ end
3162
+ end
3163
+
3164
+ context 'when db is a symbol' do
3165
+
3166
+ let(:results) do
3167
+ context.map_reduce(map, reduce).out(merge: :mr_output, 'db' => 'another-db')
3168
+ end
3169
+
3170
+ it "returns the correct number of documents" do
3171
+ expect(results.count).to eq(1)
3172
+ end
3173
+
3174
+ it "contains the entire results" do
3175
+ expect(results).to eq([
3176
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
3177
+ ])
3178
+ end
3179
+
3180
+ it 'writes to the specified db' do
3181
+ expect(Band.mongo_client.with(database: 'another-db')[:mr_output].find.count).to eq(1)
3182
+ end
3183
+ end
3184
+ end
3185
+
3186
+ context "when providing no output" do
3187
+
3188
+ let(:criteria) do
3189
+ Band.limit(1)
3190
+ end
3191
+
3192
+ let(:context) do
3193
+ described_class.new(criteria)
3194
+ end
3195
+
3196
+ let(:results) do
3197
+ context.map_reduce(map, reduce)
3198
+ end
3199
+
3200
+ it "raises an error" do
3201
+ expect {
3202
+ results.entries
3203
+ }.to raise_error(Mongoid::Errors::NoMapReduceOutput)
3204
+ end
3205
+ end
3206
+
3207
+ context "when providing a finalize" do
3208
+
3209
+ let(:criteria) do
3210
+ Band.limit(1)
3211
+ end
3212
+
3213
+ let(:context) do
3214
+ described_class.new(criteria)
3215
+ end
3216
+
3217
+ let(:finalize) do
3218
+ %Q{
3219
+ function(key, value) {
3220
+ value.extra = true;
3221
+ return value;
3222
+ }}
3223
+ end
3224
+
3225
+ let(:results) do
3226
+ context.map_reduce(map, reduce).out(inline: 1).finalize(finalize)
3227
+ end
3228
+
3229
+ it "returns the correct number of documents" do
3230
+ expect(results.count).to eq(1)
3231
+ end
3232
+
3233
+ it "contains the entire results" do
3234
+ expect(results).to eq([
3235
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200, "extra" => true }}
3236
+ ])
3237
+ end
3238
+ end
3239
+ end
3240
+
3241
+ describe "#skip" do
3242
+
3243
+ let!(:depeche_mode) do
3244
+ Band.create!(name: "Depeche Mode")
3245
+ end
3246
+
3247
+ let!(:new_order) do
3248
+ Band.create!(name: "New Order")
3249
+ end
3250
+
3251
+ let(:criteria) do
3252
+ Band.all
3253
+ end
3254
+
3255
+ let(:context) do
3256
+ described_class.new(criteria)
3257
+ end
3258
+
3259
+ it "limits the results" do
3260
+ expect(context.skip(1).entries).to eq([ new_order ])
3261
+ end
3262
+ end
3263
+
3264
+ describe "#sort" do
3265
+
3266
+ let!(:depeche_mode) do
3267
+ Band.create!(name: "Depeche Mode")
3268
+ end
3269
+
3270
+ let!(:new_order) do
3271
+ Band.create!(name: "New Order")
3272
+ end
3273
+
3274
+ let(:criteria) do
3275
+ Band.all
3276
+ end
3277
+
3278
+ let(:context) do
3279
+ described_class.new(criteria)
3280
+ end
3281
+
3282
+ context "when providing a spec" do
3283
+
3284
+ it "sorts the results" do
3285
+ expect(context.sort(name: -1).entries).to eq([ new_order, depeche_mode ])
3286
+ end
3287
+
3288
+ it "returns the context" do
3289
+ expect(context.sort(name: 1)).to eq(context)
3290
+ end
3291
+ end
3292
+
3293
+ context "when providing a block" do
3294
+
3295
+ let(:sorted) do
3296
+ context.sort do |a, b|
3297
+ b.name <=> a.name
3298
+ end
3299
+ end
3300
+
3301
+ it "sorts the results in memory" do
3302
+ expect(sorted).to eq([ new_order, depeche_mode ])
3303
+ end
3304
+ end
3305
+ end
3306
+
3307
+ describe "#update" do
3308
+
3309
+ let!(:depeche_mode) do
3310
+ Band.create!(name: "Depeche Mode")
3311
+ end
3312
+
3313
+ let!(:new_order) do
3314
+ Band.create!(name: "New Order")
3315
+ end
3316
+
3317
+ let(:criteria) do
3318
+ Band.all
3319
+ end
3320
+
3321
+ let(:context) do
3322
+ described_class.new(criteria)
3323
+ end
3324
+
3325
+ context "when adding an element to a HABTM set" do
3326
+
3327
+ let(:person) do
3328
+ Person.create!
3329
+ end
3330
+
3331
+ let(:preference) do
3332
+ Preference.create!
3333
+ end
3334
+
3335
+ before do
3336
+ Person.where(id: person.id).
3337
+ update("$addToSet" => { preference_ids: preference.id })
3338
+ end
3339
+
3340
+ it "adds a single element to the array" do
3341
+ expect(person.reload.preference_ids).to eq([ preference.id ])
3342
+ end
3343
+ end
3344
+
3345
+ context "when providing attributes" do
3346
+
3347
+ context "when the attributes are of the correct type" do
3348
+
3349
+ before do
3350
+ context.update(name: "Smiths")
3351
+ end
3352
+
3353
+ it "updates only the first matching document" do
3354
+ expect(depeche_mode.reload.name).to eq("Smiths")
3355
+ end
3356
+
3357
+ it "does not update the last matching document" do
3358
+ expect(new_order.reload.name).to eq("New Order")
3359
+ end
3360
+ end
3361
+
3362
+ context "when the attributes must be mongoized" do
3363
+
3364
+ context "when coercing a string to integer" do
3365
+
3366
+ before do
3367
+ context.update(member_count: "1")
3368
+ end
3369
+
3370
+ it "updates the first matching document" do
3371
+ expect(depeche_mode.reload.member_count).to eq(1)
3372
+ end
3373
+
3374
+ it "does not update the last matching document" do
3375
+ expect(new_order.reload.member_count).to be_nil
3376
+ end
3377
+ end
3378
+
3379
+ context "when coercing a string to date" do
3380
+
3381
+ before do
3382
+ context.update(founded: "1979/1/1")
3383
+ end
3384
+
3385
+ it "updates the first matching document" do
3386
+ expect(depeche_mode.reload.founded).to eq(Date.new(1979, 1, 1))
3387
+ end
3388
+
3389
+ it "does not update the last matching document" do
3390
+ expect(new_order.reload.founded).to be_nil
3391
+ end
3392
+ end
3393
+ end
3394
+ end
3395
+
3396
+ context "when providing atomic operations" do
3397
+
3398
+ context "when only atomic operations are provided" do
3399
+
3400
+ context "when the attributes are in the correct type" do
3401
+
3402
+ before do
3403
+ context.update("$set" => { name: "Smiths" })
3404
+ end
3405
+
3406
+ it "updates the first matching document" do
3407
+ expect(depeche_mode.reload.name).to eq("Smiths")
3408
+ end
3409
+
3410
+ it "does not update the last matching document" do
3411
+ expect(new_order.reload.name).to eq("New Order")
3412
+ end
3413
+ end
3414
+
3415
+ context "when the attributes must be mongoized" do
3416
+
3417
+ before do
3418
+ context.update("$set" => { member_count: "1" })
3419
+ end
3420
+
3421
+ it "updates the first matching document" do
3422
+ expect(depeche_mode.reload.member_count).to eq(1)
3423
+ end
3424
+
3425
+ it "does not update the last matching document" do
3426
+ expect(new_order.reload.member_count).to be_nil
3427
+ end
3428
+ end
3429
+ end
3430
+
3431
+ context "when a mix are provided" do
3432
+
3433
+ before do
3434
+ context.update("$set" => { name: "Smiths" }, likes: 100)
3435
+ end
3436
+
3437
+ it "updates the first matching document's set" do
3438
+ expect(depeche_mode.reload.name).to eq("Smiths")
3439
+ end
3440
+
3441
+ it "updates the first matching document's updates" do
3442
+ expect(depeche_mode.reload.likes).to eq(100)
3443
+ end
3444
+
3445
+ it "does not update the last matching document's set" do
3446
+ expect(new_order.reload.name).to eq("New Order")
3447
+ end
3448
+
3449
+ it "does not update the last matching document's updates" do
3450
+ expect(new_order.reload.likes).to be_nil
3451
+ end
3452
+ end
3453
+ end
3454
+
3455
+ context "when providing no attributes" do
3456
+
3457
+ it "returns false" do
3458
+ expect(context.update).to be false
3459
+ end
3460
+ end
3461
+
3462
+ context 'when provided array filters' do
3463
+ min_server_version '3.6'
3464
+
3465
+ before do
3466
+ Band.delete_all
3467
+ b = Band.new(name: 'Depeche Mode')
3468
+ b.labels << Label.new(name: 'Warner')
3469
+ b.labels << Label.new(name: 'Sony')
3470
+ b.labels << Label.new(name: 'Cbs')
3471
+ b.save!
3472
+
3473
+ b = Band.new(name: 'FKA Twigs')
3474
+ b.labels << Label.new(name: 'Warner')
3475
+ b.labels << Label.new(name: 'Cbs')
3476
+ b.save!
3477
+ end
3478
+
3479
+
3480
+ let(:criteria) do
3481
+ Band.where(name: 'Depeche Mode')
3482
+ end
3483
+
3484
+ let!(:update) do
3485
+ context.update({ '$set' => { 'labels.$[i].name' => 'Sony' } },
3486
+ array_filters: [{ 'i.name' => 'Cbs' }])
3487
+ end
3488
+
3489
+ it 'applies the array filters' do
3490
+ expect(Band.where(name: 'Depeche Mode').first.labels.collect(&:name)).to match_array(['Warner', 'Sony', 'Sony'])
3491
+ end
3492
+
3493
+ it 'does not affect other documents' do
3494
+ expect(Band.where(name: 'FKA Twigs').first.labels.collect(&:name)).to match_array(['Warner', 'Cbs'])
3495
+ end
3496
+ end
3497
+ end
3498
+
3499
+ describe "#update_all" do
3500
+
3501
+ let!(:depeche_mode) do
3502
+ Band.create!(name: "Depeche Mode", origin: "Essex")
3503
+ end
3504
+
3505
+ let!(:new_order) do
3506
+ Band.create!(name: "New Order")
3507
+ end
3508
+
3509
+ let(:criteria) do
3510
+ Band.all
3511
+ end
3512
+
3513
+ let(:context) do
3514
+ described_class.new(criteria)
3515
+ end
3516
+
3517
+ context "when providing attributes" do
3518
+
3519
+ context "when the attributes are of the correct type" do
3520
+
3521
+ before do
3522
+ context.update_all(name: "Smiths")
3523
+ end
3524
+
3525
+ it "updates the first matching document" do
3526
+ expect(depeche_mode.reload.name).to eq("Smiths")
3527
+ end
3528
+
3529
+ it "does not clear out other attributes" do
3530
+ expect(depeche_mode.reload.origin).to eq("Essex")
3531
+ end
3532
+
3533
+ it "updates the last matching document" do
3534
+ expect(new_order.reload.name).to eq("Smiths")
3535
+ end
3536
+ end
3537
+
3538
+ context "when the attributes must be mongoized" do
3539
+
3540
+ before do
3541
+ context.update_all(member_count: "1")
3542
+ end
3543
+
3544
+ it "updates the first matching document" do
3545
+ expect(depeche_mode.reload.member_count).to eq(1)
3546
+ end
3547
+
3548
+ it "updates the last matching document" do
3549
+ expect(new_order.reload.member_count).to eq(1)
3550
+ end
3551
+ end
3552
+
3553
+ context "when using aliased field names" do
3554
+
3555
+ before do
3556
+ context.update_all(years: 100)
3557
+ end
3558
+
3559
+ it "updates the first matching document" do
3560
+ expect(depeche_mode.reload.years).to eq(100)
3561
+ end
3562
+
3563
+ it "updates the last matching document" do
3564
+ expect(new_order.reload.years).to eq(100)
3565
+ end
3566
+ end
3567
+ end
3568
+
3569
+ context "when providing atomic operations" do
3570
+
3571
+ context "when only atomic operations are provided" do
3572
+
3573
+ context "when the attributes are in the correct type" do
3574
+
3575
+ before do
3576
+ context.update_all("$set" => { name: "Smiths" })
3577
+ end
3578
+
3579
+ it "updates the first matching document" do
3580
+ expect(depeche_mode.reload.name).to eq("Smiths")
3581
+ end
3582
+
3583
+ it "updates the last matching document" do
3584
+ expect(new_order.reload.name).to eq("Smiths")
3585
+ end
3586
+ end
3587
+
3588
+ context "when the attributes must be mongoized" do
3589
+
3590
+ before do
3591
+ context.update_all("$set" => { member_count: "1" })
3592
+ end
3593
+
3594
+ it "updates the first matching document" do
3595
+ expect(depeche_mode.reload.member_count).to eq(1)
3596
+ end
3597
+
3598
+ it "updates the last matching document" do
3599
+ expect(new_order.reload.member_count).to eq(1)
3600
+ end
3601
+ end
3602
+ end
3603
+
3604
+ context "when a mix are provided" do
3605
+
3606
+ before do
3607
+ context.update_all("$set" => { name: "Smiths" }, likes: 100)
3608
+ end
3609
+
3610
+ it "updates the first matching document's set" do
3611
+ expect(depeche_mode.reload.name).to eq("Smiths")
3612
+ end
3613
+
3614
+ it "updates the first matching document's updates" do
3615
+ expect(depeche_mode.reload.likes).to eq(100)
3616
+ end
3617
+
3618
+ it "updates the last matching document's set" do
3619
+ expect(new_order.reload.name).to eq("Smiths")
3620
+ end
3621
+
3622
+ it "updates the last matching document's updates" do
3623
+ expect(new_order.reload.likes).to eq(100)
3624
+ end
3625
+ end
3626
+ end
3627
+
3628
+ context "when providing no attributes" do
3629
+
3630
+ it "returns false" do
3631
+ expect(context.update_all).to be false
3632
+ end
3633
+ end
3634
+
3635
+ context 'when provided array filters' do
3636
+ min_server_version '3.6'
3637
+
3638
+ before do
3639
+ Band.delete_all
3640
+ b = Band.new(name: 'Depeche Mode')
3641
+ b.labels << Label.new(name: 'Warner')
3642
+ b.labels << Label.new(name: 'Sony')
3643
+ b.labels << Label.new(name: 'Cbs')
3644
+ b.save!
3645
+
3646
+ b = Band.new(name: 'FKA Twigs')
3647
+ b.labels << Label.new(name: 'Warner')
3648
+ b.labels << Label.new(name: 'Cbs')
3649
+ b.save!
3650
+ end
3651
+
3652
+
3653
+ let(:criteria) do
3654
+ Band.all
3655
+ end
3656
+
3657
+ let!(:update) do
3658
+ context.update_all({ '$set' => { 'labels.$[i].name' => 'Sony' } },
3659
+ array_filters: [{ 'i.name' => 'Cbs' }])
3660
+ end
3661
+
3662
+ it 'applies the array filters' do
3663
+ expect(Band.where(name: 'Depeche Mode').first.labels.collect(&:name)).to match_array(['Warner', 'Sony', 'Sony'])
3664
+ end
3665
+
3666
+ it 'updates all documents' do
3667
+ expect(Band.where(name: 'FKA Twigs').first.labels.collect(&:name)).to match_array(['Warner', 'Sony'])
3668
+ end
3669
+ end
3670
+ end
3671
+
3672
+ describe '#pipeline' do
3673
+
3674
+ context 'when the criteria has a selector' do
3675
+
3676
+ before do
3677
+ Artist.index(name: "text")
3678
+ Artist.create_indexes
3679
+ end
3680
+
3681
+ let(:criteria) do
3682
+ Artist.text_search("New Order")
3683
+ end
3684
+
3685
+ let(:context) do
3686
+ described_class.new(criteria)
3687
+ end
3688
+
3689
+ let(:pipeline_match) do
3690
+ context.send(:pipeline, :some_field).first['$match']
3691
+ end
3692
+
3693
+ it 'creates a pipeline with the selector as one of the $match criteria' do
3694
+ expect(pipeline_match).to include({ '$text' => { '$search' => "New Order" } })
3695
+ end
3696
+
3697
+ it 'creates a pipeline with the $exists operator as one of the $match criteria' do
3698
+ expect(pipeline_match).to include({ 'some_field' => { '$exists' => true } })
3699
+ end
3700
+ end
3701
+ end
3702
+
3703
+ describe "#first!" do
3704
+
3705
+ let!(:depeche_mode) do
3706
+ Band.create!(name: "Depeche Mode")
3707
+ end
3708
+
3709
+ let!(:new_order) do
3710
+ Band.create!(name: "New Order")
3711
+ end
3712
+
3713
+ let!(:rolling_stones) do
3714
+ Band.create!(name: "The Rolling Stones")
3715
+ end
3716
+
3717
+ let!(:death_cab) do
3718
+ Band.create!(name: "Death Cab For Cutie")
3719
+ end
3720
+
3721
+ let(:context) do
3722
+ described_class.new(criteria)
3723
+ end
3724
+
3725
+ context "when there's no sort" do
3726
+ let(:criteria) do
3727
+ Band.all
3728
+ end
3729
+
3730
+ it "gets the first document" do
3731
+ expect(context.first!).to eq(depeche_mode)
3732
+ end
3733
+ end
3734
+
3735
+ context "when there's a custom sort" do
3736
+ let(:criteria) do
3737
+ Band.all
3738
+ end
3739
+
3740
+ it "gets the first document" do
3741
+ expect(context.sort(name: 1).first!).to eq(death_cab)
3742
+ end
3743
+ end
3744
+
3745
+ context "when there are no documents" do
3746
+ let(:criteria) do
3747
+ Band.where(name: "bogus")
3748
+ end
3749
+
3750
+ it "raises an error" do
3751
+ expect do
3752
+ context.first!
3753
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
3754
+ end
3755
+ end
3756
+ end
3757
+
3758
+ describe "#last!" do
3759
+
3760
+ let!(:depeche_mode) do
3761
+ Band.create!(name: "Depeche Mode")
3762
+ end
3763
+
3764
+ let!(:new_order) do
3765
+ Band.create!(name: "New Order")
3766
+ end
3767
+
3768
+ let!(:rolling_stones) do
3769
+ Band.create!(name: "The Rolling Stones")
3770
+ end
3771
+
3772
+ let!(:death_cab) do
3773
+ Band.create!(name: "Death Cab For Cutie")
3774
+ end
3775
+
3776
+ let(:context) do
3777
+ described_class.new(criteria)
3778
+ end
3779
+
3780
+ context "when there's no sort" do
3781
+ let(:criteria) do
3782
+ Band.all
3783
+ end
3784
+
3785
+ it "gets the last document" do
3786
+ expect(context.last!).to eq(death_cab)
3787
+ end
3788
+ end
3789
+
3790
+ context "when there's a custom sort" do
3791
+ let(:criteria) do
3792
+ Band.all
3793
+ end
3794
+
3795
+ it "gets the last document" do
3796
+ expect(context.sort(name: 1).last!).to eq(rolling_stones)
3797
+ end
3798
+ end
3799
+
3800
+ context "when there are no documents" do
3801
+ let(:criteria) do
3802
+ Band.where(name: "bogus")
3803
+ end
3804
+
3805
+ it "raises an error" do
3806
+ expect do
3807
+ context.last!
3808
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
3809
+ end
3810
+ end
3811
+ end
3812
+
3813
+ describe "#second" do
3814
+
3815
+ let!(:depeche_mode) do
3816
+ Band.create!(name: "Depeche Mode")
3817
+ end
3818
+
3819
+ let!(:new_order) do
3820
+ Band.create!(name: "New Order")
3821
+ end
3822
+
3823
+ let!(:rolling_stones) do
3824
+ Band.create!(name: "The Rolling Stones")
3825
+ end
3826
+
3827
+ let!(:death_cab) do
3828
+ Band.create!(name: "Death Cab For Cutie")
3829
+ end
3830
+
3831
+ let(:context) do
3832
+ described_class.new(criteria)
3833
+ end
3834
+
3835
+ context "when there's no sort" do
3836
+ let(:criteria) do
3837
+ Band.all
3838
+ end
3839
+
3840
+ it "gets the second document" do
3841
+ expect(context.second).to eq(new_order)
3842
+ end
3843
+ end
3844
+
3845
+ context "when there's a custom sort" do
3846
+ let(:criteria) do
3847
+ Band.all
3848
+ end
3849
+
3850
+ it "gets the second document" do
3851
+ expect(context.sort(name: 1).second).to eq(depeche_mode)
3852
+ end
3853
+ end
3854
+
3855
+ context "when there are no documents" do
3856
+ let(:criteria) do
3857
+ Band.where(name: "bogus")
3858
+ end
3859
+
3860
+ it "returns nil" do
3861
+ expect(context.second).to be_nil
3862
+ end
3863
+ end
3864
+ end
3865
+
3866
+ describe "#second!" do
3867
+
3868
+ let!(:depeche_mode) do
3869
+ Band.create!(name: "Depeche Mode")
3870
+ end
3871
+
3872
+ let!(:new_order) do
3873
+ Band.create!(name: "New Order")
3874
+ end
3875
+
3876
+ let!(:rolling_stones) do
3877
+ Band.create!(name: "The Rolling Stones")
3878
+ end
3879
+
3880
+ let!(:death_cab) do
3881
+ Band.create!(name: "Death Cab For Cutie")
3882
+ end
3883
+
3884
+ let(:context) do
3885
+ described_class.new(criteria)
3886
+ end
3887
+
3888
+ context "when there's no sort" do
3889
+ let(:criteria) do
3890
+ Band.all
3891
+ end
3892
+
3893
+ it "gets the second document" do
3894
+ expect(context.second!).to eq(new_order)
3895
+ end
3896
+ end
3897
+
3898
+ context "when there's a custom sort" do
3899
+ let(:criteria) do
3900
+ Band.all
3901
+ end
3902
+
3903
+ it "gets the second document" do
3904
+ expect(context.sort(name: 1).second!).to eq(depeche_mode)
3905
+ end
3906
+ end
3907
+
3908
+ context "when there are no documents" do
3909
+ let(:criteria) do
3910
+ Band.where(name: "bogus")
3911
+ end
3912
+
3913
+ it "raises an error" do
3914
+ expect do
3915
+ context.second!
3916
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
3917
+ end
3918
+ end
3919
+ end
3920
+
3921
+ describe "#third" do
3922
+
3923
+ let!(:depeche_mode) do
3924
+ Band.create!(name: "Depeche Mode")
3925
+ end
3926
+
3927
+ let!(:new_order) do
3928
+ Band.create!(name: "New Order")
3929
+ end
3930
+
3931
+ let!(:rolling_stones) do
3932
+ Band.create!(name: "The Rolling Stones")
3933
+ end
3934
+
3935
+ let!(:death_cab) do
3936
+ Band.create!(name: "Death Cab For Cutie")
3937
+ end
3938
+
3939
+ let(:context) do
3940
+ described_class.new(criteria)
3941
+ end
3942
+
3943
+ context "when there's no sort" do
3944
+ let(:criteria) do
3945
+ Band.all
3946
+ end
3947
+
3948
+ it "gets the third document" do
3949
+ expect(context.third).to eq(rolling_stones)
3950
+ end
3951
+ end
3952
+
3953
+ context "when there's a custom sort" do
3954
+ let(:criteria) do
3955
+ Band.all
3956
+ end
3957
+
3958
+ it "gets the third document" do
3959
+ expect(context.sort(name: 1).third).to eq(new_order)
3960
+ end
3961
+ end
3962
+
3963
+ context "when there are no documents" do
3964
+ let(:criteria) do
3965
+ Band.where(name: "bogus")
3966
+ end
3967
+
3968
+ it "returns nil" do
3969
+ expect(context.third).to be_nil
3970
+ end
3971
+ end
3972
+ end
3973
+
3974
+ describe "#third!" do
3975
+
3976
+ let!(:depeche_mode) do
3977
+ Band.create!(name: "Depeche Mode")
3978
+ end
3979
+
3980
+ let!(:new_order) do
3981
+ Band.create!(name: "New Order")
3982
+ end
3983
+
3984
+ let!(:rolling_stones) do
3985
+ Band.create!(name: "The Rolling Stones")
3986
+ end
3987
+
3988
+ let!(:death_cab) do
3989
+ Band.create!(name: "Death Cab For Cutie")
3990
+ end
3991
+
3992
+ let(:context) do
3993
+ described_class.new(criteria)
3994
+ end
3995
+
3996
+ context "when there's no sort" do
3997
+ let(:criteria) do
3998
+ Band.all
3999
+ end
4000
+
4001
+ it "gets the third document" do
4002
+ expect(context.third!).to eq(rolling_stones)
4003
+ end
4004
+ end
4005
+
4006
+ context "when there's a custom sort" do
4007
+ let(:criteria) do
4008
+ Band.all
4009
+ end
4010
+
4011
+ it "gets the third document" do
4012
+ expect(context.sort(name: 1).third!).to eq(new_order)
4013
+ end
4014
+ end
4015
+
4016
+ context "when there are no documents" do
4017
+ let(:criteria) do
4018
+ Band.where(name: "bogus")
4019
+ end
4020
+
4021
+ it "raises an error" do
4022
+ expect do
4023
+ context.third!
4024
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
4025
+ end
4026
+ end
4027
+ end
4028
+
4029
+ describe "#fourth" do
4030
+
4031
+ let!(:depeche_mode) do
4032
+ Band.create!(name: "Depeche Mode")
4033
+ end
4034
+
4035
+ let!(:new_order) do
4036
+ Band.create!(name: "New Order")
4037
+ end
4038
+
4039
+ let!(:rolling_stones) do
4040
+ Band.create!(name: "The Rolling Stones")
4041
+ end
4042
+
4043
+ let!(:death_cab) do
4044
+ Band.create!(name: "Death Cab For Cutie")
4045
+ end
4046
+
4047
+ let(:context) do
4048
+ described_class.new(criteria)
4049
+ end
4050
+
4051
+ context "when there's no sort" do
4052
+ let(:criteria) do
4053
+ Band.all
4054
+ end
4055
+
4056
+ it "gets the fourth document" do
4057
+ expect(context.fourth).to eq(death_cab)
4058
+ end
4059
+ end
4060
+
4061
+ context "when there's a custom sort" do
4062
+ let(:criteria) do
4063
+ Band.all
4064
+ end
4065
+
4066
+ it "gets the fourth document" do
4067
+ expect(context.sort(name: 1).fourth).to eq(rolling_stones)
4068
+ end
4069
+ end
4070
+
4071
+ context "when there are no documents" do
4072
+ let(:criteria) do
4073
+ Band.where(name: "bogus")
4074
+ end
4075
+
4076
+ it "returns nil" do
4077
+ expect(context.fourth).to be_nil
4078
+ end
4079
+ end
4080
+ end
4081
+
4082
+ describe "#fourth!" do
4083
+
4084
+ let!(:depeche_mode) do
4085
+ Band.create!(name: "Depeche Mode")
4086
+ end
4087
+
4088
+ let!(:new_order) do
4089
+ Band.create!(name: "New Order")
4090
+ end
4091
+
4092
+ let!(:rolling_stones) do
4093
+ Band.create!(name: "The Rolling Stones")
4094
+ end
4095
+
4096
+ let!(:death_cab) do
4097
+ Band.create!(name: "Death Cab For Cutie")
4098
+ end
4099
+
4100
+ let(:context) do
4101
+ described_class.new(criteria)
4102
+ end
4103
+
4104
+ context "when there's no sort" do
4105
+ let(:criteria) do
4106
+ Band.all
4107
+ end
4108
+
4109
+ it "gets the fourth document" do
4110
+ expect(context.fourth!).to eq(death_cab)
4111
+ end
4112
+ end
4113
+
4114
+ context "when there's a custom sort" do
4115
+ let(:criteria) do
4116
+ Band.all
4117
+ end
4118
+
4119
+ it "gets the fourth document" do
4120
+ expect(context.sort(name: 1).fourth!).to eq(rolling_stones)
4121
+ end
4122
+ end
4123
+
4124
+ context "when there are no documents" do
4125
+ let(:criteria) do
4126
+ Band.where(name: "bogus")
4127
+ end
4128
+
4129
+ it "raises an error" do
4130
+ expect do
4131
+ context.fourth!
4132
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
4133
+ end
4134
+ end
4135
+ end
4136
+
4137
+ describe "#fifth" do
4138
+
4139
+ let!(:depeche_mode) do
4140
+ Band.create!(name: "Depeche Mode")
4141
+ end
4142
+
4143
+ let!(:new_order) do
4144
+ Band.create!(name: "New Order")
4145
+ end
4146
+
4147
+ let!(:rolling_stones) do
4148
+ Band.create!(name: "The Rolling Stones")
4149
+ end
4150
+
4151
+ let!(:death_cab) do
4152
+ Band.create!(name: "Death Cab For Cutie")
4153
+ end
4154
+
4155
+ let!(:guns_and_roses) do
4156
+ Band.create!(name: "Guns and Roses")
4157
+ end
4158
+
4159
+ let(:context) do
4160
+ described_class.new(criteria)
4161
+ end
4162
+
4163
+ context "when there's no sort" do
4164
+ let(:criteria) do
4165
+ Band.all
4166
+ end
4167
+
4168
+ it "gets the fifth document" do
4169
+ expect(context.fifth).to eq(guns_and_roses)
4170
+ end
4171
+ end
4172
+
4173
+ context "when there's a custom sort" do
4174
+ let(:criteria) do
4175
+ Band.all
4176
+ end
4177
+
4178
+ it "gets the fifth document" do
4179
+ expect(context.sort(name: 1).fifth).to eq(rolling_stones)
4180
+ end
4181
+ end
4182
+
4183
+ context "when there are no documents" do
4184
+ let(:criteria) do
4185
+ Band.where(name: "bogus")
4186
+ end
4187
+
4188
+ it "returns nil" do
4189
+ expect(context.fifth).to be_nil
4190
+ end
4191
+ end
4192
+ end
4193
+
4194
+ describe "#fifth!" do
4195
+
4196
+ let!(:depeche_mode) do
4197
+ Band.create!(name: "Depeche Mode")
4198
+ end
4199
+
4200
+ let!(:new_order) do
4201
+ Band.create!(name: "New Order")
4202
+ end
4203
+
4204
+ let!(:rolling_stones) do
4205
+ Band.create!(name: "The Rolling Stones")
4206
+ end
4207
+
4208
+ let!(:death_cab) do
4209
+ Band.create!(name: "Death Cab For Cutie")
4210
+ end
4211
+
4212
+ let!(:guns_and_roses) do
4213
+ Band.create!(name: "Guns and Roses")
4214
+ end
4215
+
4216
+ let(:context) do
4217
+ described_class.new(criteria)
4218
+ end
4219
+
4220
+ context "when there's no sort" do
4221
+ let(:criteria) do
4222
+ Band.all
4223
+ end
4224
+
4225
+ it "gets the fifth document" do
4226
+ expect(context.fifth!).to eq(guns_and_roses)
4227
+ end
4228
+ end
4229
+
4230
+ context "when there's a custom sort" do
4231
+ let(:criteria) do
4232
+ Band.all
4233
+ end
4234
+
4235
+ it "gets the fifth document" do
4236
+ expect(context.sort(name: 1).fifth!).to eq(rolling_stones)
4237
+ end
4238
+ end
4239
+
4240
+ context "when there are no documents" do
4241
+ let(:criteria) do
4242
+ Band.where(name: "bogus")
4243
+ end
4244
+
4245
+ it "raises an error" do
4246
+ expect do
4247
+ context.fifth!
4248
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
4249
+ end
4250
+ end
4251
+ end
4252
+
4253
+ describe "#second_to_last" do
4254
+
4255
+ let!(:depeche_mode) do
4256
+ Band.create!(name: "Depeche Mode")
4257
+ end
4258
+
4259
+ let!(:new_order) do
4260
+ Band.create!(name: "New Order")
4261
+ end
4262
+
4263
+ let!(:rolling_stones) do
4264
+ Band.create!(name: "The Rolling Stones")
4265
+ end
4266
+
4267
+ let!(:death_cab) do
4268
+ Band.create!(name: "Death Cab For Cutie")
4269
+ end
4270
+
4271
+ let(:context) do
4272
+ described_class.new(criteria)
4273
+ end
4274
+
4275
+ context "when there's no sort" do
4276
+ let(:criteria) do
4277
+ Band.all
4278
+ end
4279
+
4280
+ it "gets the second_to_last document" do
4281
+ expect(context.second_to_last).to eq(rolling_stones)
4282
+ end
4283
+ end
4284
+
4285
+ context "when there's a custom sort" do
4286
+ let(:criteria) do
4287
+ Band.all
4288
+ end
4289
+
4290
+ it "gets the second_to_last document" do
4291
+ expect(context.sort(name: 1).second_to_last).to eq(new_order)
4292
+ end
4293
+ end
4294
+
4295
+ context "when there are no documents" do
4296
+ let(:criteria) do
4297
+ Band.where(name: "bogus")
4298
+ end
4299
+
4300
+ it "returns nil" do
4301
+ expect(context.second_to_last).to be_nil
4302
+ end
4303
+ end
4304
+ end
4305
+
4306
+ describe "#second_to_last!" do
4307
+
4308
+ let!(:depeche_mode) do
4309
+ Band.create!(name: "Depeche Mode")
4310
+ end
4311
+
4312
+ let!(:new_order) do
4313
+ Band.create!(name: "New Order")
4314
+ end
4315
+
4316
+ let!(:rolling_stones) do
4317
+ Band.create!(name: "The Rolling Stones")
4318
+ end
4319
+
4320
+ let!(:death_cab) do
4321
+ Band.create!(name: "Death Cab For Cutie")
4322
+ end
4323
+
4324
+ let(:context) do
4325
+ described_class.new(criteria)
4326
+ end
4327
+
4328
+ context "when there's no sort" do
4329
+ let(:criteria) do
4330
+ Band.all
4331
+ end
4332
+
4333
+ it "gets the second_to_last document" do
4334
+ expect(context.second_to_last!).to eq(rolling_stones)
4335
+ end
4336
+ end
4337
+
4338
+ context "when there's a custom sort" do
4339
+ let(:criteria) do
4340
+ Band.all
4341
+ end
4342
+
4343
+ it "gets the second_to_last document" do
4344
+ expect(context.sort(name: 1).second_to_last!).to eq(new_order)
4345
+ end
4346
+ end
4347
+
4348
+ context "when there are no documents" do
4349
+ let(:criteria) do
4350
+ Band.where(name: "bogus")
4351
+ end
4352
+
4353
+ it "raises an error" do
4354
+ expect do
4355
+ context.second_to_last!
4356
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
4357
+ end
4358
+ end
4359
+ end
4360
+
4361
+ describe "#third_to_last" do
4362
+
4363
+ let!(:depeche_mode) do
4364
+ Band.create!(name: "Depeche Mode")
4365
+ end
4366
+
4367
+ let!(:new_order) do
4368
+ Band.create!(name: "New Order")
4369
+ end
4370
+
4371
+ let!(:rolling_stones) do
4372
+ Band.create!(name: "The Rolling Stones")
4373
+ end
4374
+
4375
+ let!(:death_cab) do
4376
+ Band.create!(name: "Death Cab For Cutie")
4377
+ end
4378
+
4379
+ let(:context) do
4380
+ described_class.new(criteria)
4381
+ end
4382
+
4383
+ context "when there's no sort" do
4384
+ let(:criteria) do
4385
+ Band.all
4386
+ end
4387
+
4388
+ it "gets the third_to_last document" do
4389
+ expect(context.third_to_last).to eq(new_order)
4390
+ end
4391
+ end
4392
+
4393
+ context "when there's a custom sort" do
4394
+ let(:criteria) do
4395
+ Band.all
4396
+ end
4397
+
4398
+ it "gets the third_to_last document" do
4399
+ expect(context.sort(name: 1).third_to_last).to eq(depeche_mode)
4400
+ end
4401
+ end
4402
+
4403
+ context "when there are no documents" do
4404
+ let(:criteria) do
4405
+ Band.where(name: "bogus")
4406
+ end
4407
+
4408
+ it "returns nil" do
4409
+ expect(context.third_to_last).to be_nil
4410
+ end
4411
+ end
4412
+ end
4413
+
4414
+ describe "#third_to_last!" do
4415
+
4416
+ let!(:depeche_mode) do
4417
+ Band.create!(name: "Depeche Mode")
4418
+ end
4419
+
4420
+ let!(:new_order) do
4421
+ Band.create!(name: "New Order")
4422
+ end
4423
+
4424
+ let!(:rolling_stones) do
4425
+ Band.create!(name: "The Rolling Stones")
4426
+ end
4427
+
4428
+ let!(:death_cab) do
4429
+ Band.create!(name: "Death Cab For Cutie")
4430
+ end
4431
+
4432
+ let(:context) do
4433
+ described_class.new(criteria)
4434
+ end
4435
+
4436
+ context "when there's no sort" do
4437
+ let(:criteria) do
4438
+ Band.all
4439
+ end
4440
+
4441
+ it "gets the third_to_last document" do
4442
+ expect(context.third_to_last!).to eq(new_order)
4443
+ end
4444
+ end
4445
+
4446
+ context "when there's a custom sort" do
4447
+ let(:criteria) do
4448
+ Band.all
4449
+ end
4450
+
4451
+ it "gets the third_to_last document" do
4452
+ expect(context.sort(name: 1).third_to_last!).to eq(depeche_mode)
4453
+ end
4454
+ end
4455
+
4456
+ context "when there are no documents" do
4457
+ let(:criteria) do
4458
+ Band.where(name: "bogus")
4459
+ end
4460
+
4461
+ it "raises an error" do
4462
+ expect do
4463
+ context.third_to_last!
4464
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Band./)
4465
+ end
4466
+ end
4467
+ end
4468
+
4469
+ describe '#load_async' do
4470
+ let!(:band) do
4471
+ Band.create!(name: "Depeche Mode")
4472
+ end
4473
+
4474
+ let(:criteria) do
4475
+ Band.where(name: "Depeche Mode")
4476
+ end
4477
+
4478
+ let(:context) do
4479
+ described_class.new(criteria)
4480
+ end
4481
+
4482
+ context 'with global thread pool async query executor' do
4483
+ config_override :async_query_executor, :global_thread_pool
4484
+
4485
+ it 'preloads the documents' do
4486
+ context.load_async
4487
+ context.documents_loader.wait
4488
+
4489
+ expect(context.view).not_to receive(:map)
4490
+ expect(context.to_a).to eq([band])
4491
+ end
4492
+
4493
+ it 're-raises exception during preload' do
4494
+ expect_any_instance_of(Mongoid::Contextual::Mongo::DocumentsLoader)
4495
+ .to receive(:execute)
4496
+ .at_least(:once)
4497
+ .and_raise(Mongo::Error::OperationFailure)
4498
+
4499
+ context.load_async
4500
+ context.documents_loader.wait
4501
+
4502
+ expect do
4503
+ context.to_a
4504
+ end.to raise_error(Mongo::Error::OperationFailure)
4505
+ end
4506
+ end
4507
+
4508
+ context 'with immediate thread pool async query executor' do
4509
+ config_override :async_query_executor, :immediate
4510
+
4511
+ it 'preloads the documents' do
4512
+ context.load_async
4513
+ context.documents_loader.wait
4514
+
4515
+ expect(context.view).not_to receive(:map)
4516
+ expect(context.to_a).to eq([band])
4517
+ end
4518
+
4519
+ it 're-raises exception during preload' do
4520
+ expect_any_instance_of(Mongoid::Contextual::Mongo::DocumentsLoader)
4521
+ .to receive(:execute)
4522
+ .at_least(:once)
4523
+ .and_raise(Mongo::Error::OperationFailure)
4524
+
4525
+ context.load_async
4526
+ context.documents_loader.wait
4527
+
4528
+ expect do
4529
+ context.to_a
4530
+ end.to raise_error(Mongo::Error::OperationFailure)
4531
+ end
4532
+ end
4533
+ end
4534
+ end