mongoid 6.4.8 → 7.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (1372) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +30 -30
  4. data/LICENSE +1 -0
  5. data/README.md +34 -20
  6. data/Rakefile +75 -6
  7. data/lib/config/locales/en.yml +149 -66
  8. data/lib/mongoid/association/accessors.rb +402 -0
  9. data/lib/mongoid/association/bindable.rb +179 -0
  10. data/lib/mongoid/association/builders.rb +84 -0
  11. data/lib/mongoid/association/constrainable.rb +43 -0
  12. data/lib/mongoid/association/depending.rb +128 -0
  13. data/lib/mongoid/association/eager_loadable.rb +35 -0
  14. data/lib/mongoid/association/embedded/batchable.rb +375 -0
  15. data/lib/mongoid/association/embedded/cyclic.rb +102 -0
  16. data/lib/mongoid/association/embedded/embedded_in/binding.rb +52 -0
  17. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +38 -0
  18. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +111 -0
  19. data/lib/mongoid/association/embedded/embedded_in.rb +134 -0
  20. data/lib/mongoid/association/embedded/embeds_many/binding.rb +41 -0
  21. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +45 -0
  22. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +549 -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 +39 -0
  26. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +145 -0
  27. data/lib/mongoid/association/embedded/embeds_one.rb +150 -0
  28. data/lib/mongoid/association/embedded.rb +6 -0
  29. data/lib/mongoid/association/macros.rb +227 -0
  30. data/lib/mongoid/association/many.rb +198 -0
  31. data/lib/mongoid/association/marshalable.rb +31 -0
  32. data/lib/mongoid/association/nested/many.rb +191 -0
  33. data/lib/mongoid/association/nested/nested_buildable.rb +66 -0
  34. data/lib/mongoid/association/nested/one.rb +116 -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 +122 -0
  38. data/lib/mongoid/association/proxy.rb +189 -0
  39. data/lib/mongoid/association/referenced/auto_save.rb +73 -0
  40. data/lib/mongoid/association/referenced/belongs_to/binding.rb +82 -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 +125 -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/eager.rb +163 -0
  47. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +72 -0
  48. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
  49. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +53 -0
  50. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +292 -0
  51. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +270 -0
  52. data/lib/mongoid/association/referenced/has_many/binding.rb +36 -0
  53. data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
  54. data/lib/mongoid/association/referenced/has_many/eager.rb +44 -0
  55. data/lib/mongoid/association/referenced/has_many/enumerable.rb +502 -0
  56. data/lib/mongoid/association/referenced/has_many/proxy.rb +545 -0
  57. data/lib/mongoid/association/referenced/has_many.rb +251 -0
  58. data/lib/mongoid/association/referenced/has_one/binding.rb +42 -0
  59. data/lib/mongoid/association/referenced/has_one/buildable.rb +74 -0
  60. data/lib/mongoid/association/referenced/has_one/eager.rb +36 -0
  61. data/lib/mongoid/association/referenced/has_one/nested_builder.rb +113 -0
  62. data/lib/mongoid/association/referenced/has_one/proxy.rb +111 -0
  63. data/lib/mongoid/association/referenced/has_one.rb +184 -0
  64. data/lib/mongoid/association/referenced/syncable.rb +155 -0
  65. data/lib/mongoid/association/referenced.rb +9 -0
  66. data/lib/mongoid/association/reflections.rb +69 -0
  67. data/lib/mongoid/association/relatable.rb +455 -0
  68. data/lib/mongoid/association.rb +135 -0
  69. data/lib/mongoid/atomic/modifiers.rb +27 -51
  70. data/lib/mongoid/atomic/paths/embedded/many.rb +24 -8
  71. data/lib/mongoid/atomic/paths/embedded/one.rb +5 -8
  72. data/lib/mongoid/atomic/paths/embedded.rb +3 -4
  73. data/lib/mongoid/atomic/paths/root.rb +4 -7
  74. data/lib/mongoid/atomic/paths.rb +2 -1
  75. data/lib/mongoid/atomic.rb +43 -48
  76. data/lib/mongoid/attributes/dynamic.rb +19 -33
  77. data/lib/mongoid/attributes/nested.rb +29 -21
  78. data/lib/mongoid/attributes/processing.rb +11 -26
  79. data/lib/mongoid/attributes/projector.rb +119 -0
  80. data/lib/mongoid/attributes/readonly.rb +5 -10
  81. data/lib/mongoid/attributes.rb +94 -91
  82. data/lib/mongoid/cacheable.rb +7 -10
  83. data/lib/mongoid/changeable.rb +7 -61
  84. data/lib/mongoid/clients/factory.rb +42 -17
  85. data/lib/mongoid/clients/options.rb +17 -18
  86. data/lib/mongoid/clients/sessions.rb +22 -10
  87. data/lib/mongoid/clients/storage_options.rb +7 -12
  88. data/lib/mongoid/clients/validators/storage.rb +2 -9
  89. data/lib/mongoid/clients/validators.rb +2 -1
  90. data/lib/mongoid/clients.rb +3 -10
  91. data/lib/mongoid/composable.rb +9 -13
  92. data/lib/mongoid/config/environment.rb +51 -13
  93. data/lib/mongoid/config/options.rb +20 -23
  94. data/lib/mongoid/config/validators/client.rb +2 -17
  95. data/lib/mongoid/config/validators/option.rb +2 -3
  96. data/lib/mongoid/config/validators.rb +2 -1
  97. data/lib/mongoid/config.rb +123 -40
  98. data/lib/mongoid/contextual/aggregable/memory.rb +17 -12
  99. data/lib/mongoid/contextual/aggregable/mongo.rb +25 -26
  100. data/lib/mongoid/contextual/aggregable/none.rb +65 -0
  101. data/lib/mongoid/contextual/aggregable.rb +17 -0
  102. data/lib/mongoid/contextual/atomic.rb +29 -31
  103. data/lib/mongoid/contextual/command.rb +2 -5
  104. data/lib/mongoid/contextual/geo_near.rb +7 -38
  105. data/lib/mongoid/contextual/map_reduce.rb +5 -33
  106. data/lib/mongoid/contextual/memory.rb +167 -70
  107. data/lib/mongoid/contextual/mongo.rb +295 -106
  108. data/lib/mongoid/contextual/none.rb +88 -28
  109. data/lib/mongoid/contextual/queryable.rb +2 -3
  110. data/lib/mongoid/contextual.rb +7 -9
  111. data/lib/mongoid/copyable.rb +20 -14
  112. data/lib/mongoid/criteria/findable.rb +12 -15
  113. data/lib/mongoid/criteria/includable.rb +21 -28
  114. data/lib/mongoid/criteria/inspectable.rb +2 -3
  115. data/lib/mongoid/criteria/marshalable.rb +2 -5
  116. data/lib/mongoid/criteria/modifiable.rb +10 -22
  117. data/lib/mongoid/criteria/options.rb +4 -5
  118. data/lib/mongoid/criteria/permission.rb +2 -1
  119. data/lib/mongoid/criteria/queryable/aggregable.rb +2 -13
  120. data/lib/mongoid/criteria/queryable/expandable.rb +68 -0
  121. data/lib/mongoid/criteria/queryable/extensions/array.rb +5 -26
  122. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +3 -4
  123. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +3 -4
  124. data/lib/mongoid/criteria/queryable/extensions/date.rb +3 -8
  125. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +5 -11
  126. data/lib/mongoid/criteria/queryable/extensions/hash.rb +4 -25
  127. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +3 -14
  128. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +4 -13
  129. data/lib/mongoid/criteria/queryable/extensions/object.rb +4 -29
  130. data/lib/mongoid/criteria/queryable/extensions/range.rb +41 -17
  131. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +9 -16
  132. data/lib/mongoid/criteria/queryable/extensions/set.rb +3 -4
  133. data/lib/mongoid/criteria/queryable/extensions/string.rb +4 -19
  134. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -10
  135. data/lib/mongoid/criteria/queryable/extensions/time.rb +4 -9
  136. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +13 -6
  137. data/lib/mongoid/criteria/queryable/extensions.rb +1 -4
  138. data/lib/mongoid/criteria/queryable/key.rb +101 -27
  139. data/lib/mongoid/criteria/queryable/macroable.rb +3 -4
  140. data/lib/mongoid/criteria/queryable/mergeable.rb +205 -44
  141. data/lib/mongoid/criteria/queryable/optional.rb +5 -48
  142. data/lib/mongoid/criteria/queryable/options.rb +2 -19
  143. data/lib/mongoid/criteria/queryable/pipeline.rb +14 -19
  144. data/lib/mongoid/criteria/queryable/selectable.rb +436 -166
  145. data/lib/mongoid/criteria/queryable/selector.rb +45 -55
  146. data/lib/mongoid/criteria/queryable/smash.rb +2 -11
  147. data/lib/mongoid/criteria/queryable/storable.rb +237 -0
  148. data/lib/mongoid/criteria/queryable.rb +7 -9
  149. data/lib/mongoid/criteria/scopable.rb +27 -19
  150. data/lib/mongoid/criteria.rb +90 -75
  151. data/lib/mongoid/document.rb +43 -74
  152. data/lib/mongoid/equality.rb +29 -12
  153. data/lib/mongoid/errors/ambiguous_relationship.rb +4 -5
  154. data/lib/mongoid/errors/callback.rb +2 -3
  155. data/lib/mongoid/errors/criteria_argument_required.rb +18 -0
  156. data/lib/mongoid/errors/delete_restriction.rb +10 -13
  157. data/lib/mongoid/errors/document_not_destroyed.rb +4 -7
  158. data/lib/mongoid/errors/document_not_found.rb +25 -15
  159. data/lib/mongoid/errors/eager_load.rb +6 -5
  160. data/lib/mongoid/errors/empty_config_file.rb +25 -0
  161. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +2 -3
  162. data/lib/mongoid/errors/invalid_collection.rb +2 -1
  163. data/lib/mongoid/errors/invalid_config_file.rb +25 -0
  164. data/lib/mongoid/errors/invalid_config_option.rb +2 -3
  165. data/lib/mongoid/errors/invalid_dependent_strategy.rb +31 -0
  166. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +24 -0
  167. data/lib/mongoid/errors/invalid_elem_match_operator.rb +32 -0
  168. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +25 -0
  169. data/lib/mongoid/errors/invalid_expression_operator.rb +27 -0
  170. data/lib/mongoid/errors/invalid_field.rb +2 -5
  171. data/lib/mongoid/errors/invalid_field_operator.rb +32 -0
  172. data/lib/mongoid/errors/invalid_field_option.rb +2 -3
  173. data/lib/mongoid/errors/invalid_find.rb +2 -3
  174. data/lib/mongoid/errors/invalid_includes.rb +2 -3
  175. data/lib/mongoid/errors/invalid_index.rb +2 -3
  176. data/lib/mongoid/errors/invalid_options.rb +4 -5
  177. data/lib/mongoid/errors/invalid_path.rb +2 -3
  178. data/lib/mongoid/errors/invalid_persistence_option.rb +2 -5
  179. data/lib/mongoid/errors/invalid_query.rb +40 -0
  180. data/lib/mongoid/errors/invalid_relation.rb +4 -9
  181. data/lib/mongoid/errors/invalid_relation_option.rb +28 -0
  182. data/lib/mongoid/errors/invalid_scope.rb +2 -3
  183. data/lib/mongoid/errors/invalid_session_use.rb +3 -6
  184. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +6 -5
  185. data/lib/mongoid/errors/invalid_storage_options.rb +2 -3
  186. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -3
  187. data/lib/mongoid/errors/invalid_time.rb +2 -3
  188. data/lib/mongoid/errors/invalid_value.rb +2 -1
  189. data/lib/mongoid/errors/inverse_not_found.rb +3 -4
  190. data/lib/mongoid/errors/mixed_client_configuration.rb +2 -3
  191. data/lib/mongoid/errors/mixed_relations.rb +2 -3
  192. data/lib/mongoid/errors/mongoid_error.rb +3 -10
  193. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +3 -4
  194. data/lib/mongoid/errors/no_client_config.rb +4 -5
  195. data/lib/mongoid/errors/no_client_database.rb +2 -3
  196. data/lib/mongoid/errors/no_client_hosts.rb +2 -3
  197. data/lib/mongoid/errors/no_clients_config.rb +2 -3
  198. data/lib/mongoid/errors/no_default_client.rb +3 -4
  199. data/lib/mongoid/errors/no_environment.rb +2 -3
  200. data/lib/mongoid/errors/no_map_reduce_output.rb +2 -3
  201. data/lib/mongoid/errors/no_metadata.rb +2 -3
  202. data/lib/mongoid/errors/no_parent.rb +2 -3
  203. data/lib/mongoid/errors/readonly_attribute.rb +2 -3
  204. data/lib/mongoid/errors/readonly_document.rb +3 -6
  205. data/lib/mongoid/errors/scope_overwrite.rb +2 -1
  206. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +2 -0
  207. data/lib/mongoid/errors/unknown_attribute.rb +2 -3
  208. data/lib/mongoid/errors/unknown_model.rb +24 -0
  209. data/lib/mongoid/errors/unsaved_document.rb +3 -2
  210. data/lib/mongoid/errors/unsupported_javascript.rb +2 -3
  211. data/lib/mongoid/errors/validations.rb +2 -1
  212. data/lib/mongoid/errors.rb +15 -1
  213. data/lib/mongoid/evolvable.rb +4 -5
  214. data/lib/mongoid/extensions/array.rb +33 -42
  215. data/lib/mongoid/extensions/big_decimal.rb +2 -11
  216. data/lib/mongoid/extensions/boolean.rb +3 -5
  217. data/lib/mongoid/extensions/date.rb +13 -12
  218. data/lib/mongoid/extensions/date_time.rb +4 -12
  219. data/lib/mongoid/extensions/decimal128.rb +2 -5
  220. data/lib/mongoid/extensions/false_class.rb +4 -7
  221. data/lib/mongoid/extensions/float.rb +5 -10
  222. data/lib/mongoid/extensions/hash.rb +56 -38
  223. data/lib/mongoid/extensions/integer.rb +5 -12
  224. data/lib/mongoid/extensions/module.rb +2 -3
  225. data/lib/mongoid/extensions/nil_class.rb +2 -5
  226. data/lib/mongoid/extensions/object.rb +21 -48
  227. data/lib/mongoid/extensions/object_id.rb +2 -7
  228. data/lib/mongoid/extensions/range.rb +3 -11
  229. data/lib/mongoid/extensions/regexp.rb +3 -4
  230. data/lib/mongoid/extensions/set.rb +2 -7
  231. data/lib/mongoid/extensions/string.rb +21 -41
  232. data/lib/mongoid/extensions/symbol.rb +2 -7
  233. data/lib/mongoid/extensions/time.rb +12 -9
  234. data/lib/mongoid/extensions/time_with_zone.rb +25 -7
  235. data/lib/mongoid/extensions/true_class.rb +4 -7
  236. data/lib/mongoid/extensions.rb +18 -7
  237. data/lib/mongoid/factory.rb +59 -10
  238. data/lib/mongoid/fields/foreign_key.rb +8 -27
  239. data/lib/mongoid/fields/localized.rb +2 -11
  240. data/lib/mongoid/fields/standard.rb +8 -50
  241. data/lib/mongoid/fields/validators/macro.rb +37 -19
  242. data/lib/mongoid/fields/validators.rb +2 -1
  243. data/lib/mongoid/fields.rb +245 -70
  244. data/lib/mongoid/findable.rb +78 -27
  245. data/lib/mongoid/indexable/specification.rb +3 -16
  246. data/lib/mongoid/indexable/validators/options.rb +2 -7
  247. data/lib/mongoid/indexable.rb +10 -22
  248. data/lib/mongoid/inspectable.rb +4 -11
  249. data/lib/mongoid/interceptable.rb +18 -35
  250. data/lib/mongoid/loggable.rb +13 -16
  251. data/lib/mongoid/matchable.rb +3 -153
  252. data/lib/mongoid/matcher/all.rb +22 -0
  253. data/lib/mongoid/matcher/and.rb +21 -0
  254. data/lib/mongoid/matcher/bits.rb +41 -0
  255. data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
  256. data/lib/mongoid/matcher/bits_all_set.rb +20 -0
  257. data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
  258. data/lib/mongoid/matcher/bits_any_set.rb +20 -0
  259. data/lib/mongoid/matcher/elem_match.rb +36 -0
  260. data/lib/mongoid/matcher/elem_match_expression.rb +20 -0
  261. data/lib/mongoid/matcher/eq.rb +11 -0
  262. data/lib/mongoid/matcher/eq_impl.rb +67 -0
  263. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +26 -0
  264. data/lib/mongoid/matcher/exists.rb +15 -0
  265. data/lib/mongoid/matcher/expression.rb +35 -0
  266. data/lib/mongoid/matcher/expression_operator.rb +19 -0
  267. data/lib/mongoid/matcher/field_expression.rb +62 -0
  268. data/lib/mongoid/matcher/field_operator.rb +54 -0
  269. data/lib/mongoid/matcher/gt.rb +17 -0
  270. data/lib/mongoid/matcher/gte.rb +17 -0
  271. data/lib/mongoid/matcher/in.rb +25 -0
  272. data/lib/mongoid/matcher/lt.rb +17 -0
  273. data/lib/mongoid/matcher/lte.rb +17 -0
  274. data/lib/mongoid/matcher/mod.rb +17 -0
  275. data/lib/mongoid/matcher/ne.rb +16 -0
  276. data/lib/mongoid/matcher/nin.rb +11 -0
  277. data/lib/mongoid/matcher/nor.rb +25 -0
  278. data/lib/mongoid/matcher/not.rb +29 -0
  279. data/lib/mongoid/matcher/or.rb +21 -0
  280. data/lib/mongoid/matcher/regex.rb +41 -0
  281. data/lib/mongoid/matcher/size.rb +26 -0
  282. data/lib/mongoid/matcher/type.rb +99 -0
  283. data/lib/mongoid/matcher.rb +110 -0
  284. data/lib/mongoid/persistable/creatable.rb +4 -21
  285. data/lib/mongoid/persistable/deletable.rb +11 -26
  286. data/lib/mongoid/persistable/destroyable.rb +11 -10
  287. data/lib/mongoid/persistable/incrementable.rb +7 -8
  288. data/lib/mongoid/persistable/logical.rb +5 -7
  289. data/lib/mongoid/persistable/poppable.rb +3 -6
  290. data/lib/mongoid/persistable/pullable.rb +3 -8
  291. data/lib/mongoid/persistable/pushable.rb +14 -10
  292. data/lib/mongoid/persistable/renamable.rb +10 -8
  293. data/lib/mongoid/persistable/savable.rb +3 -8
  294. data/lib/mongoid/persistable/settable.rb +60 -18
  295. data/lib/mongoid/persistable/unsettable.rb +8 -8
  296. data/lib/mongoid/persistable/updatable.rb +32 -20
  297. data/lib/mongoid/persistable/upsertable.rb +4 -9
  298. data/lib/mongoid/persistable.rb +136 -42
  299. data/lib/mongoid/persistence_context.rb +101 -48
  300. data/lib/mongoid/positional.rb +3 -6
  301. data/lib/mongoid/query_cache.rb +117 -130
  302. data/lib/mongoid/railtie.rb +3 -16
  303. data/lib/mongoid/railties/controller_runtime.rb +3 -1
  304. data/lib/mongoid/railties/database.rake +9 -0
  305. data/lib/mongoid/reloadable.rb +14 -19
  306. data/lib/mongoid/scopable.rb +19 -44
  307. data/lib/mongoid/selectable.rb +8 -17
  308. data/lib/mongoid/serializable.rb +28 -33
  309. data/lib/mongoid/shardable.rb +108 -21
  310. data/lib/mongoid/stateful.rb +3 -10
  311. data/lib/mongoid/stringified_symbol.rb +52 -0
  312. data/lib/mongoid/tasks/database.rake +12 -5
  313. data/lib/mongoid/tasks/database.rb +85 -5
  314. data/lib/mongoid/threaded/lifecycle.rb +2 -21
  315. data/lib/mongoid/threaded.rb +2 -55
  316. data/lib/mongoid/timestamps/created/short.rb +2 -1
  317. data/lib/mongoid/timestamps/created.rb +3 -2
  318. data/lib/mongoid/timestamps/short.rb +2 -1
  319. data/lib/mongoid/timestamps/timeless.rb +6 -9
  320. data/lib/mongoid/timestamps/updated/short.rb +2 -1
  321. data/lib/mongoid/timestamps/updated.rb +3 -4
  322. data/lib/mongoid/timestamps.rb +3 -2
  323. data/lib/mongoid/touchable.rb +132 -0
  324. data/lib/mongoid/traversable.rb +183 -60
  325. data/lib/mongoid/validatable/associated.rb +5 -6
  326. data/lib/mongoid/validatable/format.rb +2 -1
  327. data/lib/mongoid/validatable/length.rb +2 -1
  328. data/lib/mongoid/validatable/localizable.rb +2 -3
  329. data/lib/mongoid/validatable/macros.rb +3 -8
  330. data/lib/mongoid/validatable/presence.rb +10 -15
  331. data/lib/mongoid/validatable/queryable.rb +2 -3
  332. data/lib/mongoid/validatable/uniqueness.rb +25 -38
  333. data/lib/mongoid/validatable.rb +16 -35
  334. data/lib/mongoid/version.rb +3 -2
  335. data/lib/mongoid/warnings.rb +29 -0
  336. data/lib/mongoid.rb +34 -15
  337. data/lib/rails/generators/mongoid/config/config_generator.rb +10 -2
  338. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +53 -29
  339. data/lib/rails/generators/mongoid/model/model_generator.rb +3 -2
  340. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
  341. data/lib/rails/generators/mongoid_generator.rb +2 -1
  342. data/lib/rails/mongoid.rb +4 -5
  343. data/spec/README.md +33 -0
  344. data/spec/config/mongoid.yml +14 -3
  345. data/spec/config/mongoid_with_schema_map_uuid.yml +27 -0
  346. data/spec/integration/app_spec.rb +349 -0
  347. data/spec/integration/associations/belongs_to_spec.rb +15 -0
  348. data/spec/integration/associations/embedded_dirty_spec.rb +57 -0
  349. data/spec/integration/associations/embedded_spec.rb +268 -0
  350. data/spec/integration/associations/embeds_many_spec.rb +206 -0
  351. data/spec/integration/associations/embeds_one_spec.rb +23 -0
  352. data/spec/integration/associations/foreign_key_spec.rb +98 -0
  353. data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
  354. data/spec/integration/associations/has_many_spec.rb +128 -0
  355. data/spec/integration/associations/has_one_spec.rb +176 -0
  356. data/spec/integration/associations/nested_attributes_assignment_spec.rb +115 -0
  357. data/spec/integration/associations/reverse_population_spec.rb +34 -0
  358. data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
  359. data/spec/integration/associations/scope_option_spec.rb +101 -0
  360. data/spec/integration/atomic/modifiers_spec.rb +116 -0
  361. data/spec/integration/bson_regexp_raw_spec.rb +19 -0
  362. data/spec/integration/callbacks_models.rb +61 -0
  363. data/spec/integration/callbacks_spec.rb +228 -0
  364. data/spec/integration/contextual/empty_spec.rb +141 -0
  365. data/spec/integration/criteria/alias_query_spec.rb +161 -0
  366. data/spec/integration/criteria/date_field_spec.rb +40 -0
  367. data/spec/integration/criteria/default_scope_spec.rb +72 -0
  368. data/spec/integration/criteria/logical_spec.rb +124 -0
  369. data/spec/integration/criteria/range_spec.rb +265 -0
  370. data/spec/integration/criteria/time_with_zone_spec.rb +142 -0
  371. data/spec/integration/discriminator_key_spec.rb +353 -0
  372. data/spec/integration/discriminator_value_spec.rb +206 -0
  373. data/spec/integration/document_spec.rb +30 -1
  374. data/spec/integration/i18n_fallbacks_spec.rb +103 -0
  375. data/spec/integration/matcher_examples_spec.rb +758 -0
  376. data/spec/integration/matcher_operator_data/all.yml +140 -0
  377. data/spec/integration/matcher_operator_data/and.yml +93 -0
  378. data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
  379. data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
  380. data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
  381. data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
  382. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  383. data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
  384. data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
  385. data/spec/integration/matcher_operator_data/eq.yml +191 -0
  386. data/spec/integration/matcher_operator_data/exists.yml +213 -0
  387. data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
  388. data/spec/integration/matcher_operator_data/gt.yml +132 -0
  389. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  390. data/spec/integration/matcher_operator_data/gte.yml +132 -0
  391. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  392. data/spec/integration/matcher_operator_data/implicit.yml +331 -0
  393. data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
  394. data/spec/integration/matcher_operator_data/in.yml +210 -0
  395. data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
  396. data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
  397. data/spec/integration/matcher_operator_data/lt.yml +132 -0
  398. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  399. data/spec/integration/matcher_operator_data/lte.yml +132 -0
  400. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  401. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  402. data/spec/integration/matcher_operator_data/multiple.yml +29 -0
  403. data/spec/integration/matcher_operator_data/ne.yml +150 -0
  404. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  405. data/spec/integration/matcher_operator_data/nin.yml +114 -0
  406. data/spec/integration/matcher_operator_data/nor.yml +126 -0
  407. data/spec/integration/matcher_operator_data/not.yml +196 -0
  408. data/spec/integration/matcher_operator_data/or.yml +137 -0
  409. data/spec/integration/matcher_operator_data/regex.yml +174 -0
  410. data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
  411. data/spec/integration/matcher_operator_data/size.yml +174 -0
  412. data/spec/integration/matcher_operator_data/type.yml +70 -0
  413. data/spec/integration/matcher_operator_data/type_array.yml +16 -0
  414. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  415. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  416. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  417. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  418. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  419. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  420. data/spec/integration/matcher_operator_data/type_decimal.yml +40 -0
  421. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  422. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  423. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  424. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  425. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  426. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  427. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  428. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  429. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  430. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  431. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  432. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  433. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  434. data/spec/integration/matcher_operator_spec.rb +124 -0
  435. data/spec/integration/matcher_spec.rb +283 -0
  436. data/spec/integration/server_query_spec.rb +141 -0
  437. data/spec/integration/shardable_spec.rb +148 -0
  438. data/spec/integration/stringified_symbol_field_spec.rb +203 -0
  439. data/spec/lite_spec_helper.rb +83 -0
  440. data/spec/mongoid/association/accessors_spec.rb +1067 -0
  441. data/spec/mongoid/association/auto_save_spec.rb +403 -0
  442. data/spec/mongoid/association/builders_spec.rb +255 -0
  443. data/spec/mongoid/association/constrainable_spec.rb +117 -0
  444. data/spec/mongoid/association/counter_cache_spec.rb +452 -0
  445. data/spec/mongoid/association/depending_spec.rb +959 -0
  446. data/spec/mongoid/association/eager_spec.rb +281 -0
  447. data/spec/mongoid/association/embedded/cyclic_spec.rb +180 -0
  448. data/spec/mongoid/association/embedded/dirty_spec.rb +67 -0
  449. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +173 -0
  450. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +36 -0
  451. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +559 -0
  452. data/spec/mongoid/association/embedded/embedded_in_spec.rb +903 -0
  453. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +56 -0
  454. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +106 -0
  455. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +4673 -0
  456. data/spec/mongoid/association/embedded/embeds_many_models.rb +190 -0
  457. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +51 -0
  458. data/spec/mongoid/association/embedded/embeds_many_spec.rb +864 -0
  459. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +79 -0
  460. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +81 -0
  461. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +1025 -0
  462. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +8 -0
  463. data/spec/mongoid/association/embedded/embeds_one_models.rb +54 -0
  464. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
  465. data/spec/mongoid/association/embedded/embeds_one_spec.rb +956 -0
  466. data/spec/mongoid/association/macros_spec.rb +1116 -0
  467. data/spec/mongoid/association/nested/many_spec.rb +232 -0
  468. data/spec/mongoid/association/nested/one_spec.rb +253 -0
  469. data/spec/mongoid/association/options_spec.rb +1323 -0
  470. data/spec/mongoid/association/polymorphic_spec.rb +162 -0
  471. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +243 -0
  472. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +238 -0
  473. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +412 -0
  474. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +1373 -0
  475. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
  476. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2046 -0
  477. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +180 -0
  478. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +148 -0
  479. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +196 -0
  480. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +76 -0
  481. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +3803 -0
  482. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +67 -0
  483. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
  484. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1071 -0
  485. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +155 -0
  486. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +142 -0
  487. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +290 -0
  488. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +2286 -0
  489. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
  490. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +4112 -0
  491. data/spec/mongoid/association/referenced/has_many_models.rb +95 -0
  492. data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
  493. data/spec/mongoid/association/referenced/has_many_spec.rb +1248 -0
  494. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +133 -0
  495. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +159 -0
  496. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +193 -0
  497. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +1142 -0
  498. data/spec/mongoid/association/referenced/has_one_models.rb +97 -0
  499. data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
  500. data/spec/mongoid/association/referenced/has_one_spec.rb +1383 -0
  501. data/spec/mongoid/association/reflections_spec.rb +92 -0
  502. data/spec/mongoid/association/syncable_spec.rb +499 -0
  503. data/spec/mongoid/association_spec.rb +191 -0
  504. data/spec/mongoid/atomic/modifiers_spec.rb +51 -2
  505. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +2 -0
  506. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +2 -0
  507. data/spec/mongoid/atomic/paths/root_spec.rb +2 -0
  508. data/spec/mongoid/atomic/paths_spec.rb +107 -12
  509. data/spec/mongoid/atomic_spec.rb +57 -10
  510. data/spec/mongoid/attributes/dynamic_spec.rb +152 -0
  511. data/spec/mongoid/attributes/nested_spec.rb +98 -78
  512. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  513. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  514. data/spec/mongoid/attributes/projector_spec.rb +44 -0
  515. data/spec/mongoid/attributes/readonly_spec.rb +84 -127
  516. data/spec/mongoid/attributes_spec.rb +461 -34
  517. data/spec/mongoid/cacheable_spec.rb +6 -4
  518. data/spec/mongoid/changeable_spec.rb +60 -35
  519. data/spec/mongoid/clients/factory_spec.rb +98 -9
  520. data/spec/mongoid/clients/options_spec.rb +93 -60
  521. data/spec/mongoid/clients/sessions_spec.rb +34 -33
  522. data/spec/mongoid/clients/transactions_spec.rb +415 -0
  523. data/spec/mongoid/clients_spec.rb +108 -16
  524. data/spec/mongoid/composable_spec.rb +2 -0
  525. data/spec/mongoid/config/environment_spec.rb +126 -8
  526. data/spec/mongoid/config/options_spec.rb +22 -3
  527. data/spec/mongoid/config_spec.rb +357 -2
  528. data/spec/mongoid/contextual/aggregable/memory_spec.rb +53 -11
  529. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +122 -20
  530. data/spec/mongoid/contextual/aggregable/none_spec.rb +60 -0
  531. data/spec/mongoid/contextual/atomic_spec.rb +242 -81
  532. data/spec/mongoid/contextual/geo_near_spec.rb +30 -19
  533. data/spec/mongoid/contextual/map_reduce_spec.rb +24 -7
  534. data/spec/mongoid/contextual/memory_spec.rb +870 -88
  535. data/spec/mongoid/contextual/mongo_spec.rb +1339 -184
  536. data/spec/mongoid/contextual/none_spec.rb +45 -71
  537. data/spec/mongoid/copyable_spec.rb +294 -29
  538. data/spec/mongoid/copyable_spec_models.rb +47 -0
  539. data/spec/mongoid/criteria/findable_spec.rb +77 -20
  540. data/spec/mongoid/criteria/inspectable_spec.rb +2 -0
  541. data/spec/mongoid/criteria/marshalable_spec.rb +2 -0
  542. data/spec/mongoid/criteria/modifiable_spec.rb +215 -40
  543. data/spec/mongoid/criteria/options_spec.rb +2 -0
  544. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +3 -1
  545. data/spec/mongoid/criteria/queryable/expandable_spec.rb +61 -0
  546. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +2 -0
  547. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +2 -0
  548. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +2 -0
  549. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +3 -1
  550. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +2 -0
  551. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +35 -17
  552. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +2 -0
  553. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +2 -0
  554. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +2 -0
  555. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +2 -0
  556. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +2 -0
  557. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +53 -0
  558. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +2 -0
  559. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +239 -178
  560. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +3 -1
  561. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +9 -7
  562. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +2 -0
  563. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +46 -1
  564. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +2 -0
  565. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +42 -23
  566. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +30 -1
  567. data/spec/mongoid/criteria/queryable/key_spec.rb +50 -6
  568. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +145 -12
  569. data/spec/mongoid/criteria/queryable/optional_spec.rb +3 -1
  570. data/spec/mongoid/criteria/queryable/options_spec.rb +2 -0
  571. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +14 -0
  572. data/spec/mongoid/criteria/queryable/queryable_spec.rb +3 -1
  573. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2069 -0
  574. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +38 -0
  575. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1155 -2927
  576. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +589 -0
  577. data/spec/mongoid/criteria/queryable/selector_spec.rb +114 -2
  578. data/spec/mongoid/criteria/queryable/smash_spec.rb +2 -0
  579. data/spec/mongoid/criteria/queryable/storable_spec.rb +298 -0
  580. data/spec/mongoid/criteria/scopable_spec.rb +129 -0
  581. data/spec/mongoid/criteria_projection_spec.rb +410 -0
  582. data/spec/mongoid/criteria_spec.rb +826 -478
  583. data/spec/mongoid/document_fields_spec.rb +113 -0
  584. data/spec/mongoid/document_persistence_context_spec.rb +32 -0
  585. data/spec/mongoid/document_query_spec.rb +89 -0
  586. data/spec/mongoid/document_spec.rb +131 -31
  587. data/spec/mongoid/equality_spec.rb +144 -41
  588. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +5 -3
  589. data/spec/mongoid/errors/callback_spec.rb +2 -0
  590. data/spec/mongoid/errors/delete_restriction_spec.rb +4 -2
  591. data/spec/mongoid/errors/document_not_destroyed_spec.rb +2 -0
  592. data/spec/mongoid/errors/document_not_found_spec.rb +51 -0
  593. data/spec/mongoid/errors/eager_load_spec.rb +2 -0
  594. data/spec/mongoid/errors/invalid_collection_spec.rb +2 -0
  595. data/spec/mongoid/errors/invalid_config_file_spec.rb +31 -0
  596. data/spec/mongoid/errors/invalid_config_option_spec.rb +2 -0
  597. data/spec/mongoid/errors/invalid_field_option_spec.rb +2 -0
  598. data/spec/mongoid/errors/invalid_field_spec.rb +2 -0
  599. data/spec/mongoid/errors/invalid_find_spec.rb +2 -0
  600. data/spec/mongoid/errors/invalid_includes_spec.rb +2 -0
  601. data/spec/mongoid/errors/invalid_index_spec.rb +2 -0
  602. data/spec/mongoid/errors/invalid_options_spec.rb +4 -2
  603. data/spec/mongoid/errors/invalid_path_spec.rb +2 -0
  604. data/spec/mongoid/errors/invalid_relation_spec.rb +4 -2
  605. data/spec/mongoid/errors/invalid_scope_spec.rb +3 -1
  606. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +2 -0
  607. data/spec/mongoid/errors/invalid_storage_options_spec.rb +2 -0
  608. data/spec/mongoid/errors/invalid_time_spec.rb +2 -0
  609. data/spec/mongoid/errors/inverse_not_found_spec.rb +3 -1
  610. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +2 -0
  611. data/spec/mongoid/errors/mixed_relations_spec.rb +2 -0
  612. data/spec/mongoid/errors/mongoid_error_spec.rb +22 -8
  613. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +5 -3
  614. data/spec/mongoid/errors/no_client_config_spec.rb +4 -2
  615. data/spec/mongoid/errors/no_client_database_spec.rb +5 -3
  616. data/spec/mongoid/errors/no_client_hosts_spec.rb +5 -3
  617. data/spec/mongoid/errors/no_clients_config_spec.rb +2 -0
  618. data/spec/mongoid/errors/no_environment_spec.rb +2 -0
  619. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +2 -0
  620. data/spec/mongoid/errors/no_metadata_spec.rb +4 -2
  621. data/spec/mongoid/errors/no_parent_spec.rb +3 -1
  622. data/spec/mongoid/errors/readonly_attribute_spec.rb +2 -0
  623. data/spec/mongoid/errors/readonly_document_spec.rb +2 -0
  624. data/spec/mongoid/errors/scope_overwrite_spec.rb +2 -0
  625. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +2 -0
  626. data/spec/mongoid/errors/unknown_attribute_spec.rb +4 -2
  627. data/spec/mongoid/errors/unsaved_document_spec.rb +3 -1
  628. data/spec/mongoid/errors/unsupported_javascript_spec.rb +2 -0
  629. data/spec/mongoid/errors/validations_spec.rb +2 -0
  630. data/spec/mongoid/extensions/array_spec.rb +36 -58
  631. data/spec/mongoid/extensions/big_decimal_spec.rb +2 -0
  632. data/spec/mongoid/extensions/binary_spec.rb +2 -0
  633. data/spec/mongoid/extensions/boolean_spec.rb +2 -0
  634. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +335 -0
  635. data/spec/mongoid/extensions/date_spec.rb +8 -160
  636. data/spec/mongoid/extensions/date_time_spec.rb +17 -60
  637. data/spec/mongoid/extensions/decimal128_spec.rb +2 -0
  638. data/spec/mongoid/extensions/false_class_spec.rb +3 -1
  639. data/spec/mongoid/extensions/float_spec.rb +17 -3
  640. data/spec/mongoid/extensions/hash_spec.rb +123 -1
  641. data/spec/mongoid/extensions/integer_spec.rb +14 -2
  642. data/spec/mongoid/extensions/module_spec.rb +2 -0
  643. data/spec/mongoid/extensions/nil_class_spec.rb +2 -0
  644. data/spec/mongoid/extensions/object_id_spec.rb +2 -0
  645. data/spec/mongoid/extensions/object_spec.rb +20 -35
  646. data/spec/mongoid/extensions/range_spec.rb +9 -0
  647. data/spec/mongoid/extensions/regexp_spec.rb +2 -0
  648. data/spec/mongoid/extensions/set_spec.rb +2 -0
  649. data/spec/mongoid/extensions/string_spec.rb +60 -27
  650. data/spec/mongoid/extensions/stringified_symbol_spec.rb +84 -0
  651. data/spec/mongoid/extensions/symbol_spec.rb +2 -0
  652. data/spec/mongoid/extensions/time_spec.rb +29 -0
  653. data/spec/mongoid/extensions/time_with_zone_spec.rb +33 -0
  654. data/spec/mongoid/extensions/true_class_spec.rb +3 -1
  655. data/spec/mongoid/extensions_spec.rb +16 -2
  656. data/spec/mongoid/factory_spec.rb +301 -23
  657. data/spec/mongoid/fields/foreign_key_spec.rb +26 -32
  658. data/spec/mongoid/fields/localized_spec.rb +6 -4
  659. data/spec/mongoid/fields/standard_spec.rb +2 -0
  660. data/spec/mongoid/fields_spec.rb +419 -37
  661. data/spec/mongoid/findable_spec.rb +83 -19
  662. data/spec/mongoid/indexable/specification_spec.rb +2 -0
  663. data/spec/mongoid/indexable_spec.rb +54 -14
  664. data/spec/mongoid/inspectable_spec.rb +39 -2
  665. data/spec/mongoid/interceptable_spec.rb +189 -96
  666. data/spec/mongoid/interceptable_spec_models.rb +76 -0
  667. data/spec/mongoid/loggable_spec.rb +2 -0
  668. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  669. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
  670. data/spec/mongoid/matcher/extract_attribute_spec.rb +40 -0
  671. data/spec/mongoid/persistable/creatable_spec.rb +112 -27
  672. data/spec/mongoid/persistable/deletable_spec.rb +273 -11
  673. data/spec/mongoid/persistable/destroyable_spec.rb +201 -11
  674. data/spec/mongoid/persistable/incrementable_spec.rb +23 -7
  675. data/spec/mongoid/persistable/logical_spec.rb +19 -3
  676. data/spec/mongoid/persistable/poppable_spec.rb +19 -3
  677. data/spec/mongoid/persistable/pullable_spec.rb +36 -6
  678. data/spec/mongoid/persistable/pushable_spec.rb +91 -7
  679. data/spec/mongoid/persistable/renamable_spec.rb +18 -2
  680. data/spec/mongoid/persistable/savable_spec.rb +189 -26
  681. data/spec/mongoid/persistable/settable_spec.rb +149 -18
  682. data/spec/mongoid/persistable/unsettable_spec.rb +19 -3
  683. data/spec/mongoid/persistable/updatable_spec.rb +45 -23
  684. data/spec/mongoid/persistable/upsertable_spec.rb +4 -2
  685. data/spec/mongoid/persistable_spec.rb +106 -15
  686. data/spec/mongoid/persistence_context_spec.rb +29 -2
  687. data/spec/mongoid/positional_spec.rb +2 -0
  688. data/spec/mongoid/query_cache_middleware_spec.rb +68 -0
  689. data/spec/mongoid/query_cache_spec.rb +529 -85
  690. data/spec/mongoid/relations/proxy_spec.rb +126 -124
  691. data/spec/mongoid/reloadable_spec.rb +298 -20
  692. data/spec/mongoid/scopable_spec.rb +102 -37
  693. data/spec/mongoid/selectable_spec.rb +8 -6
  694. data/spec/mongoid/serializable_spec.rb +160 -29
  695. data/spec/mongoid/shardable_models.rb +75 -0
  696. data/spec/mongoid/shardable_spec.rb +251 -26
  697. data/spec/mongoid/stateful_spec.rb +3 -1
  698. data/spec/mongoid/tasks/database_rake_spec.rb +15 -13
  699. data/spec/mongoid/tasks/database_spec.rb +3 -1
  700. data/spec/mongoid/threaded_spec.rb +4 -2
  701. data/spec/mongoid/timestamps/created/short_spec.rb +3 -1
  702. data/spec/mongoid/timestamps/created_spec.rb +3 -1
  703. data/spec/mongoid/timestamps/timeless_spec.rb +4 -2
  704. data/spec/mongoid/timestamps/updated/short_spec.rb +5 -3
  705. data/spec/mongoid/timestamps/updated_spec.rb +5 -3
  706. data/spec/mongoid/timestamps_spec.rb +8 -6
  707. data/spec/mongoid/touchable_spec.rb +588 -0
  708. data/spec/mongoid/touchable_spec_models.rb +53 -0
  709. data/spec/mongoid/traversable_spec.rb +1167 -1
  710. data/spec/mongoid/validatable/associated_spec.rb +3 -1
  711. data/spec/mongoid/validatable/format_spec.rb +2 -0
  712. data/spec/mongoid/validatable/length_spec.rb +2 -0
  713. data/spec/mongoid/validatable/numericality_spec.rb +2 -0
  714. data/spec/mongoid/validatable/presence_spec.rb +33 -26
  715. data/spec/mongoid/validatable/uniqueness_spec.rb +102 -73
  716. data/spec/mongoid/validatable_spec.rb +5 -3
  717. data/spec/mongoid_spec.rb +9 -1
  718. data/spec/rails/controller_extension/controller_runtime_spec.rb +3 -1
  719. data/spec/rails/mongoid_spec.rb +4 -2
  720. data/spec/shared/LICENSE +20 -0
  721. data/spec/shared/bin/get-mongodb-download-url +17 -0
  722. data/spec/shared/bin/s3-copy +45 -0
  723. data/spec/shared/bin/s3-upload +69 -0
  724. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  725. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  726. data/spec/shared/lib/mrss/constraints.rb +378 -0
  727. data/spec/shared/lib/mrss/docker_runner.rb +291 -0
  728. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  729. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  730. data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
  731. data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
  732. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  733. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  734. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  735. data/spec/shared/lib/mrss/utils.rb +15 -0
  736. data/spec/shared/share/Dockerfile.erb +325 -0
  737. data/spec/shared/share/haproxy-1.conf +16 -0
  738. data/spec/shared/share/haproxy-2.conf +17 -0
  739. data/spec/shared/shlib/config.sh +27 -0
  740. data/spec/shared/shlib/distro.sh +74 -0
  741. data/spec/shared/shlib/server.sh +392 -0
  742. data/spec/shared/shlib/set_env.sh +169 -0
  743. data/spec/spec_helper.rb +70 -83
  744. data/spec/support/authorization.rb +2 -0
  745. data/spec/support/client_registry.rb +9 -0
  746. data/spec/support/constraints.rb +27 -75
  747. data/spec/support/expectations.rb +21 -3
  748. data/spec/support/helpers.rb +11 -0
  749. data/spec/support/macros.rb +31 -0
  750. data/spec/support/models/account.rb +38 -0
  751. data/spec/support/models/acolyte.rb +19 -0
  752. data/spec/support/models/actor.rb +20 -0
  753. data/spec/support/models/actress.rb +4 -0
  754. data/spec/support/models/address.rb +85 -0
  755. data/spec/support/models/address_component.rb +7 -0
  756. data/spec/support/models/address_number.rb +8 -0
  757. data/spec/support/models/agency.rb +7 -0
  758. data/spec/support/models/agent.rb +18 -0
  759. data/spec/support/models/album.rb +20 -0
  760. data/spec/support/models/alert.rb +9 -0
  761. data/spec/support/models/animal.rb +28 -0
  762. data/spec/support/models/answer.rb +8 -0
  763. data/spec/support/models/appointment.rb +9 -0
  764. data/spec/support/models/armrest.rb +9 -0
  765. data/spec/support/models/article.rb +14 -0
  766. data/spec/support/models/artist.rb +91 -0
  767. data/spec/support/models/artwork.rb +6 -0
  768. data/spec/support/models/audible_sound.rb +3 -0
  769. data/spec/support/models/audio.rb +7 -0
  770. data/spec/support/models/augmentation.rb +13 -0
  771. data/spec/support/models/author.rb +8 -0
  772. data/spec/support/models/baby.rb +6 -0
  773. data/spec/support/models/band.rb +37 -0
  774. data/spec/support/models/bar.rb +12 -0
  775. data/spec/support/models/basic.rb +8 -0
  776. data/spec/support/models/bed.rb +3 -0
  777. data/spec/support/models/big_palette.rb +4 -0
  778. data/spec/support/models/birthday.rb +15 -0
  779. data/spec/support/models/bolt.rb +7 -0
  780. data/spec/support/models/bomb.rb +6 -0
  781. data/spec/support/models/book.rb +18 -0
  782. data/spec/support/models/breed.rb +6 -0
  783. data/spec/support/models/browser.rb +8 -0
  784. data/spec/support/models/building.rb +8 -0
  785. data/spec/support/models/building_address.rb +9 -0
  786. data/spec/support/models/bus.rb +9 -0
  787. data/spec/support/models/business.rb +7 -0
  788. data/spec/support/models/callback_test.rb +11 -0
  789. data/spec/support/models/canvas.rb +27 -0
  790. data/spec/support/models/car.rb +3 -0
  791. data/spec/support/models/cat.rb +10 -0
  792. data/spec/support/models/category.rb +10 -0
  793. data/spec/support/models/child.rb +6 -0
  794. data/spec/support/models/child_doc.rb +24 -0
  795. data/spec/support/models/church.rb +8 -0
  796. data/spec/support/models/circle.rb +5 -0
  797. data/spec/support/models/circuit.rb +6 -0
  798. data/spec/support/models/circus.rb +9 -0
  799. data/spec/support/models/code.rb +15 -0
  800. data/spec/support/models/coding/pull_request.rb +11 -0
  801. data/spec/support/models/coding.rb +3 -0
  802. data/spec/support/models/comment.rb +18 -0
  803. data/spec/support/models/company.rb +7 -0
  804. data/spec/support/models/consumption_period.rb +9 -0
  805. data/spec/support/models/contextable_item.rb +7 -0
  806. data/spec/support/models/contractor.rb +7 -0
  807. data/spec/support/models/cookie.rb +8 -0
  808. data/spec/support/models/country_code.rb +12 -0
  809. data/spec/support/models/courier_job.rb +6 -0
  810. data/spec/support/models/crate.rb +12 -0
  811. data/spec/support/models/customer.rb +10 -0
  812. data/spec/support/models/customer_address.rb +11 -0
  813. data/spec/support/models/deed.rb +7 -0
  814. data/spec/support/models/definition.rb +11 -0
  815. data/spec/support/models/delegating_patient.rb +15 -0
  816. data/spec/support/models/description.rb +13 -0
  817. data/spec/support/models/dictionary.rb +18 -0
  818. data/spec/support/models/division.rb +12 -0
  819. data/spec/support/models/doctor.rb +14 -0
  820. data/spec/support/models/dog.rb +9 -0
  821. data/spec/support/models/dokument.rb +8 -0
  822. data/spec/support/models/draft.rb +11 -0
  823. data/spec/support/models/dragon.rb +6 -0
  824. data/spec/support/models/driver.rb +9 -0
  825. data/spec/support/models/drug.rb +8 -0
  826. data/spec/support/models/dungeon.rb +6 -0
  827. data/spec/support/models/edit.rb +7 -0
  828. data/spec/support/models/email.rb +8 -0
  829. data/spec/support/models/employer.rb +7 -0
  830. data/spec/support/models/entry.rb +8 -0
  831. data/spec/support/models/eraser.rb +3 -0
  832. data/spec/support/models/even.rb +9 -0
  833. data/spec/support/models/event.rb +24 -0
  834. data/spec/support/models/exhibition.rb +6 -0
  835. data/spec/support/models/exhibitor.rb +8 -0
  836. data/spec/support/models/explosion.rb +6 -0
  837. data/spec/support/models/eye.rb +11 -0
  838. data/spec/support/models/eye_bowl.rb +11 -0
  839. data/spec/support/models/face.rb +10 -0
  840. data/spec/support/models/favorite.rb +8 -0
  841. data/spec/support/models/filesystem.rb +7 -0
  842. data/spec/support/models/fire_hydrant.rb +8 -0
  843. data/spec/support/models/firefox.rb +6 -0
  844. data/spec/support/models/fish.rb +9 -0
  845. data/spec/support/models/folder.rb +9 -0
  846. data/spec/support/models/folder_item.rb +11 -0
  847. data/spec/support/models/fruits.rb +36 -0
  848. data/spec/support/models/game.rb +21 -0
  849. data/spec/support/models/ghost.rb +9 -0
  850. data/spec/support/models/guitar.rb +4 -0
  851. data/spec/support/models/hole.rb +12 -0
  852. data/spec/support/models/home.rb +6 -0
  853. data/spec/support/models/house.rb +8 -0
  854. data/spec/support/models/html_writer.rb +5 -0
  855. data/spec/support/models/id_key.rb +8 -0
  856. data/spec/support/models/idnodef.rb +7 -0
  857. data/spec/support/models/image.rb +24 -0
  858. data/spec/support/models/implant.rb +18 -0
  859. data/spec/support/models/instrument.rb +8 -0
  860. data/spec/support/models/item.rb +10 -0
  861. data/spec/support/models/jar.rb +9 -0
  862. data/spec/support/models/kaleidoscope.rb +8 -0
  863. data/spec/support/models/kangaroo.rb +6 -0
  864. data/spec/support/models/label.rb +50 -0
  865. data/spec/support/models/language.rb +7 -0
  866. data/spec/support/models/lat_lng.rb +17 -0
  867. data/spec/support/models/league.rb +13 -0
  868. data/spec/support/models/learner.rb +4 -0
  869. data/spec/support/models/line_item.rb +8 -0
  870. data/spec/support/models/location.rb +10 -0
  871. data/spec/support/models/login.rb +10 -0
  872. data/spec/support/models/manufacturer.rb +9 -0
  873. data/spec/support/models/meat.rb +6 -0
  874. data/spec/support/models/membership.rb +7 -0
  875. data/spec/support/models/message.rb +13 -0
  876. data/spec/support/models/minim.rb +6 -0
  877. data/spec/support/models/mixed_drink.rb +6 -0
  878. data/spec/support/models/mop.rb +24 -0
  879. data/spec/support/models/movie.rb +15 -0
  880. data/spec/support/models/my_hash.rb +4 -0
  881. data/spec/support/models/name.rb +26 -0
  882. data/spec/support/models/name_only.rb +8 -0
  883. data/spec/support/models/node.rb +7 -0
  884. data/spec/support/models/note.rb +19 -0
  885. data/spec/support/models/nut.rb +7 -0
  886. data/spec/support/models/odd.rb +9 -0
  887. data/spec/support/models/order.rb +12 -0
  888. data/spec/support/models/ordered_post.rb +13 -0
  889. data/spec/support/models/ordered_preference.rb +8 -0
  890. data/spec/support/models/oscar.rb +16 -0
  891. data/spec/support/models/other_owner_object.rb +4 -0
  892. data/spec/support/models/override.rb +18 -0
  893. data/spec/support/models/ownable.rb +8 -0
  894. data/spec/support/models/owner.rb +10 -0
  895. data/spec/support/models/pack.rb +5 -0
  896. data/spec/support/models/page.rb +18 -0
  897. data/spec/support/models/page_question.rb +6 -0
  898. data/spec/support/models/palette.rb +9 -0
  899. data/spec/support/models/parent.rb +7 -0
  900. data/spec/support/models/parent_doc.rb +8 -0
  901. data/spec/support/models/passport.rb +13 -0
  902. data/spec/support/models/patient.rb +11 -0
  903. data/spec/support/models/pdf_writer.rb +5 -0
  904. data/spec/support/models/pencil.rb +3 -0
  905. data/spec/support/models/person.rb +230 -0
  906. data/spec/support/models/pet.rb +25 -0
  907. data/spec/support/models/pet_owner.rb +8 -0
  908. data/spec/support/models/phone.rb +13 -0
  909. data/spec/support/models/piano.rb +4 -0
  910. data/spec/support/models/pizza.rb +9 -0
  911. data/spec/support/models/player.rb +37 -0
  912. data/spec/support/models/post.rb +52 -0
  913. data/spec/support/models/post_genre.rb +8 -0
  914. data/spec/support/models/powerup.rb +13 -0
  915. data/spec/support/models/preference.rb +11 -0
  916. data/spec/support/models/princess.rb +12 -0
  917. data/spec/support/models/product.rb +19 -0
  918. data/spec/support/models/profile.rb +17 -0
  919. data/spec/support/models/pronunciation.rb +7 -0
  920. data/spec/support/models/pub.rb +8 -0
  921. data/spec/support/models/publication/encyclopedia.rb +11 -0
  922. data/spec/support/models/publication/review.rb +13 -0
  923. data/spec/support/models/publication.rb +4 -0
  924. data/spec/support/models/purchase.rb +6 -0
  925. data/spec/support/models/purchased_item.rb +10 -0
  926. data/spec/support/models/question.rb +10 -0
  927. data/spec/support/models/quiz.rb +9 -0
  928. data/spec/support/models/rating.rb +10 -0
  929. data/spec/support/models/record.rb +54 -0
  930. data/spec/support/models/registry.rb +6 -0
  931. data/spec/support/models/role.rb +9 -0
  932. data/spec/support/models/root_category.rb +6 -0
  933. data/spec/support/models/sandwich.rb +11 -0
  934. data/spec/support/models/scheduler.rb +9 -0
  935. data/spec/support/models/scribe.rb +7 -0
  936. data/spec/support/models/sealer.rb +7 -0
  937. data/spec/support/models/seat.rb +24 -0
  938. data/spec/support/models/seo.rb +9 -0
  939. data/spec/support/models/series.rb +7 -0
  940. data/spec/support/models/server.rb +15 -0
  941. data/spec/support/models/service.rb +24 -0
  942. data/spec/support/models/shape.rb +14 -0
  943. data/spec/support/models/shelf.rb +7 -0
  944. data/spec/support/models/shipment_address.rb +5 -0
  945. data/spec/support/models/shipping_container.rb +7 -0
  946. data/spec/support/models/shipping_pack.rb +5 -0
  947. data/spec/support/models/shirt.rb +11 -0
  948. data/spec/support/models/shop.rb +8 -0
  949. data/spec/support/models/short_agent.rb +6 -0
  950. data/spec/support/models/short_quiz.rb +7 -0
  951. data/spec/support/models/simple.rb +7 -0
  952. data/spec/support/models/slave.rb +8 -0
  953. data/spec/support/models/song.rb +10 -0
  954. data/spec/support/models/sound.rb +7 -0
  955. data/spec/support/models/spacer.rb +7 -0
  956. data/spec/support/models/square.rb +6 -0
  957. data/spec/support/models/staff.rb +9 -0
  958. data/spec/support/models/store_as_dup_test1.rb +7 -0
  959. data/spec/support/models/store_as_dup_test2.rb +7 -0
  960. data/spec/support/models/store_as_dup_test3.rb +7 -0
  961. data/spec/support/models/store_as_dup_test4.rb +7 -0
  962. data/spec/support/models/strategy.rb +5 -0
  963. data/spec/support/models/sub_item.rb +5 -0
  964. data/spec/support/models/subscription.rb +7 -0
  965. data/spec/support/models/survey.rb +7 -0
  966. data/spec/support/models/symptom.rb +8 -0
  967. data/spec/support/models/system_role.rb +7 -0
  968. data/spec/support/models/tag.rb +10 -0
  969. data/spec/support/models/target.rb +7 -0
  970. data/spec/support/models/template.rb +7 -0
  971. data/spec/support/models/thing.rb +11 -0
  972. data/spec/support/models/threadlocker.rb +7 -0
  973. data/spec/support/models/title.rb +5 -0
  974. data/spec/support/models/tool.rb +10 -0
  975. data/spec/support/models/topping.rb +7 -0
  976. data/spec/support/models/toy.rb +9 -0
  977. data/spec/support/models/track.rb +40 -0
  978. data/spec/support/models/translation.rb +7 -0
  979. data/spec/support/models/tree.rb +11 -0
  980. data/spec/support/models/truck.rb +7 -0
  981. data/spec/support/models/updatable.rb +7 -0
  982. data/spec/support/models/user.rb +23 -0
  983. data/spec/support/models/user_account.rb +12 -0
  984. data/spec/support/models/validation_callback.rb +12 -0
  985. data/spec/support/models/vehicle.rb +18 -0
  986. data/spec/support/models/version.rb +7 -0
  987. data/spec/support/models/vertex.rb +8 -0
  988. data/spec/support/models/vet_visit.rb +7 -0
  989. data/spec/support/models/video.rb +15 -0
  990. data/spec/support/models/video_game.rb +3 -0
  991. data/spec/support/models/washer.rb +7 -0
  992. data/spec/support/models/weapon.rb +13 -0
  993. data/spec/support/models/wiki_page.rb +17 -0
  994. data/spec/support/models/word.rb +17 -0
  995. data/spec/support/models/word_origin.rb +13 -0
  996. data/spec/support/models/writer.rb +13 -0
  997. data/spec/support/schema_maps/schema_map_aws.json +17 -0
  998. data/spec/support/schema_maps/schema_map_aws_key_alt_names.json +12 -0
  999. data/spec/support/schema_maps/schema_map_azure.json +17 -0
  1000. data/spec/support/schema_maps/schema_map_azure_key_alt_names.json +12 -0
  1001. data/spec/support/schema_maps/schema_map_gcp.json +17 -0
  1002. data/spec/support/schema_maps/schema_map_gcp_key_alt_names.json +12 -0
  1003. data/spec/support/schema_maps/schema_map_kmip.json +17 -0
  1004. data/spec/support/schema_maps/schema_map_kmip_key_alt_names.json +12 -0
  1005. data/spec/support/schema_maps/schema_map_local.json +18 -0
  1006. data/spec/support/schema_maps/schema_map_local_key_alt_names.json +12 -0
  1007. data/spec/support/shared/time.rb +53 -0
  1008. data/spec/support/spec_config.rb +28 -6
  1009. data.tar.gz.sig +0 -0
  1010. metadata +1304 -843
  1011. metadata.gz.sig +3 -5
  1012. data/lib/mongoid/criteria/queryable/forwardable.rb +0 -65
  1013. data/lib/mongoid/matchable/all.rb +0 -27
  1014. data/lib/mongoid/matchable/and.rb +0 -30
  1015. data/lib/mongoid/matchable/default.rb +0 -72
  1016. data/lib/mongoid/matchable/elem_match.rb +0 -28
  1017. data/lib/mongoid/matchable/exists.rb +0 -23
  1018. data/lib/mongoid/matchable/gt.rb +0 -23
  1019. data/lib/mongoid/matchable/gte.rb +0 -23
  1020. data/lib/mongoid/matchable/in.rb +0 -24
  1021. data/lib/mongoid/matchable/lt.rb +0 -23
  1022. data/lib/mongoid/matchable/lte.rb +0 -23
  1023. data/lib/mongoid/matchable/ne.rb +0 -21
  1024. data/lib/mongoid/matchable/nin.rb +0 -22
  1025. data/lib/mongoid/matchable/nor.rb +0 -37
  1026. data/lib/mongoid/matchable/or.rb +0 -33
  1027. data/lib/mongoid/matchable/regexp.rb +0 -27
  1028. data/lib/mongoid/matchable/size.rb +0 -21
  1029. data/lib/mongoid/relations/accessors.rb +0 -267
  1030. data/lib/mongoid/relations/auto_save.rb +0 -94
  1031. data/lib/mongoid/relations/binding.rb +0 -218
  1032. data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
  1033. data/lib/mongoid/relations/bindings/embedded/many.rb +0 -43
  1034. data/lib/mongoid/relations/bindings/embedded/one.rb +0 -45
  1035. data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
  1036. data/lib/mongoid/relations/bindings/referenced/many.rb +0 -38
  1037. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
  1038. data/lib/mongoid/relations/bindings/referenced/one.rb +0 -44
  1039. data/lib/mongoid/relations/bindings.rb +0 -9
  1040. data/lib/mongoid/relations/builder.rb +0 -57
  1041. data/lib/mongoid/relations/builders/embedded/in.rb +0 -29
  1042. data/lib/mongoid/relations/builders/embedded/many.rb +0 -36
  1043. data/lib/mongoid/relations/builders/embedded/one.rb +0 -30
  1044. data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
  1045. data/lib/mongoid/relations/builders/nested_attributes/one.rb +0 -126
  1046. data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
  1047. data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
  1048. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
  1049. data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
  1050. data/lib/mongoid/relations/builders.rb +0 -106
  1051. data/lib/mongoid/relations/cascading/delete.rb +0 -44
  1052. data/lib/mongoid/relations/cascading/destroy.rb +0 -43
  1053. data/lib/mongoid/relations/cascading/nullify.rb +0 -35
  1054. data/lib/mongoid/relations/cascading/restrict.rb +0 -39
  1055. data/lib/mongoid/relations/cascading.rb +0 -56
  1056. data/lib/mongoid/relations/constraint.rb +0 -55
  1057. data/lib/mongoid/relations/conversions.rb +0 -34
  1058. data/lib/mongoid/relations/counter_cache.rb +0 -160
  1059. data/lib/mongoid/relations/cyclic.rb +0 -107
  1060. data/lib/mongoid/relations/eager/base.rb +0 -153
  1061. data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
  1062. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
  1063. data/lib/mongoid/relations/eager/has_many.rb +0 -38
  1064. data/lib/mongoid/relations/eager/has_one.rb +0 -30
  1065. data/lib/mongoid/relations/eager.rb +0 -36
  1066. data/lib/mongoid/relations/embedded/batchable.rb +0 -384
  1067. data/lib/mongoid/relations/embedded/in.rb +0 -241
  1068. data/lib/mongoid/relations/embedded/many.rb +0 -683
  1069. data/lib/mongoid/relations/embedded/one.rb +0 -235
  1070. data/lib/mongoid/relations/macros.rb +0 -367
  1071. data/lib/mongoid/relations/many.rb +0 -242
  1072. data/lib/mongoid/relations/marshalable.rb +0 -32
  1073. data/lib/mongoid/relations/metadata.rb +0 -1179
  1074. data/lib/mongoid/relations/nested_builder.rb +0 -74
  1075. data/lib/mongoid/relations/one.rb +0 -60
  1076. data/lib/mongoid/relations/options.rb +0 -49
  1077. data/lib/mongoid/relations/polymorphic.rb +0 -39
  1078. data/lib/mongoid/relations/proxy.rb +0 -215
  1079. data/lib/mongoid/relations/referenced/in.rb +0 -304
  1080. data/lib/mongoid/relations/referenced/many.rb +0 -812
  1081. data/lib/mongoid/relations/referenced/many_to_many.rb +0 -479
  1082. data/lib/mongoid/relations/referenced/one.rb +0 -290
  1083. data/lib/mongoid/relations/reflections.rb +0 -64
  1084. data/lib/mongoid/relations/synchronization.rb +0 -169
  1085. data/lib/mongoid/relations/targets/enumerable.rb +0 -493
  1086. data/lib/mongoid/relations/targets.rb +0 -2
  1087. data/lib/mongoid/relations/touchable.rb +0 -97
  1088. data/lib/mongoid/relations.rb +0 -148
  1089. data/lib/support/ruby_version.rb +0 -26
  1090. data/spec/app/models/account.rb +0 -36
  1091. data/spec/app/models/acolyte.rb +0 -17
  1092. data/spec/app/models/actor.rb +0 -18
  1093. data/spec/app/models/actress.rb +0 -2
  1094. data/spec/app/models/address.rb +0 -79
  1095. data/spec/app/models/address_component.rb +0 -5
  1096. data/spec/app/models/address_number.rb +0 -6
  1097. data/spec/app/models/agency.rb +0 -5
  1098. data/spec/app/models/agent.rb +0 -16
  1099. data/spec/app/models/album.rb +0 -18
  1100. data/spec/app/models/alert.rb +0 -7
  1101. data/spec/app/models/animal.rb +0 -25
  1102. data/spec/app/models/answer.rb +0 -6
  1103. data/spec/app/models/appointment.rb +0 -7
  1104. data/spec/app/models/article.rb +0 -12
  1105. data/spec/app/models/artist.rb +0 -87
  1106. data/spec/app/models/artwork.rb +0 -4
  1107. data/spec/app/models/audio.rb +0 -5
  1108. data/spec/app/models/augmentation.rb +0 -11
  1109. data/spec/app/models/author.rb +0 -6
  1110. data/spec/app/models/baby.rb +0 -4
  1111. data/spec/app/models/band.rb +0 -31
  1112. data/spec/app/models/bar.rb +0 -10
  1113. data/spec/app/models/basic.rb +0 -6
  1114. data/spec/app/models/bed.rb +0 -1
  1115. data/spec/app/models/big_palette.rb +0 -2
  1116. data/spec/app/models/birthday.rb +0 -13
  1117. data/spec/app/models/bomb.rb +0 -4
  1118. data/spec/app/models/book.rb +0 -16
  1119. data/spec/app/models/breed.rb +0 -4
  1120. data/spec/app/models/browser.rb +0 -6
  1121. data/spec/app/models/building.rb +0 -6
  1122. data/spec/app/models/building_address.rb +0 -7
  1123. data/spec/app/models/bus.rb +0 -7
  1124. data/spec/app/models/business.rb +0 -5
  1125. data/spec/app/models/callback_test.rb +0 -9
  1126. data/spec/app/models/canvas.rb +0 -25
  1127. data/spec/app/models/car.rb +0 -1
  1128. data/spec/app/models/cat.rb +0 -8
  1129. data/spec/app/models/category.rb +0 -8
  1130. data/spec/app/models/child.rb +0 -4
  1131. data/spec/app/models/child_doc.rb +0 -22
  1132. data/spec/app/models/church.rb +0 -6
  1133. data/spec/app/models/circle.rb +0 -3
  1134. data/spec/app/models/circuit.rb +0 -4
  1135. data/spec/app/models/circus.rb +0 -7
  1136. data/spec/app/models/code.rb +0 -11
  1137. data/spec/app/models/comment.rb +0 -16
  1138. data/spec/app/models/company.rb +0 -5
  1139. data/spec/app/models/consumption_period.rb +0 -7
  1140. data/spec/app/models/contextable_item.rb +0 -5
  1141. data/spec/app/models/contractor.rb +0 -5
  1142. data/spec/app/models/cookie.rb +0 -6
  1143. data/spec/app/models/country_code.rb +0 -8
  1144. data/spec/app/models/courier_job.rb +0 -4
  1145. data/spec/app/models/definition.rb +0 -9
  1146. data/spec/app/models/delegating_patient.rb +0 -16
  1147. data/spec/app/models/description.rb +0 -11
  1148. data/spec/app/models/dictionary.rb +0 -10
  1149. data/spec/app/models/division.rb +0 -10
  1150. data/spec/app/models/doctor.rb +0 -12
  1151. data/spec/app/models/dog.rb +0 -7
  1152. data/spec/app/models/dokument.rb +0 -6
  1153. data/spec/app/models/draft.rb +0 -9
  1154. data/spec/app/models/dragon.rb +0 -4
  1155. data/spec/app/models/driver.rb +0 -7
  1156. data/spec/app/models/drug.rb +0 -6
  1157. data/spec/app/models/dungeon.rb +0 -4
  1158. data/spec/app/models/edit.rb +0 -5
  1159. data/spec/app/models/email.rb +0 -6
  1160. data/spec/app/models/employer.rb +0 -5
  1161. data/spec/app/models/entry.rb +0 -6
  1162. data/spec/app/models/eraser.rb +0 -1
  1163. data/spec/app/models/even.rb +0 -7
  1164. data/spec/app/models/event.rb +0 -22
  1165. data/spec/app/models/exhibition.rb +0 -4
  1166. data/spec/app/models/exhibitor.rb +0 -6
  1167. data/spec/app/models/explosion.rb +0 -4
  1168. data/spec/app/models/eye.rb +0 -9
  1169. data/spec/app/models/eye_bowl.rb +0 -9
  1170. data/spec/app/models/face.rb +0 -8
  1171. data/spec/app/models/favorite.rb +0 -6
  1172. data/spec/app/models/filesystem.rb +0 -5
  1173. data/spec/app/models/fire_hydrant.rb +0 -6
  1174. data/spec/app/models/firefox.rb +0 -4
  1175. data/spec/app/models/fish.rb +0 -7
  1176. data/spec/app/models/folder.rb +0 -7
  1177. data/spec/app/models/folder_item.rb +0 -9
  1178. data/spec/app/models/fruits.rb +0 -34
  1179. data/spec/app/models/game.rb +0 -19
  1180. data/spec/app/models/ghost.rb +0 -7
  1181. data/spec/app/models/home.rb +0 -4
  1182. data/spec/app/models/house.rb +0 -6
  1183. data/spec/app/models/html_writer.rb +0 -3
  1184. data/spec/app/models/id_key.rb +0 -6
  1185. data/spec/app/models/image.rb +0 -22
  1186. data/spec/app/models/implant.rb +0 -16
  1187. data/spec/app/models/item.rb +0 -8
  1188. data/spec/app/models/jar.rb +0 -7
  1189. data/spec/app/models/kaleidoscope.rb +0 -6
  1190. data/spec/app/models/kangaroo.rb +0 -4
  1191. data/spec/app/models/label.rb +0 -47
  1192. data/spec/app/models/language.rb +0 -5
  1193. data/spec/app/models/lat_lng.rb +0 -15
  1194. data/spec/app/models/league.rb +0 -11
  1195. data/spec/app/models/learner.rb +0 -2
  1196. data/spec/app/models/line_item.rb +0 -6
  1197. data/spec/app/models/location.rb +0 -8
  1198. data/spec/app/models/login.rb +0 -8
  1199. data/spec/app/models/manufacturer.rb +0 -7
  1200. data/spec/app/models/meat.rb +0 -4
  1201. data/spec/app/models/membership.rb +0 -4
  1202. data/spec/app/models/message.rb +0 -11
  1203. data/spec/app/models/mixed_drink.rb +0 -4
  1204. data/spec/app/models/movie.rb +0 -13
  1205. data/spec/app/models/my_hash.rb +0 -2
  1206. data/spec/app/models/name.rb +0 -24
  1207. data/spec/app/models/node.rb +0 -5
  1208. data/spec/app/models/note.rb +0 -17
  1209. data/spec/app/models/odd.rb +0 -7
  1210. data/spec/app/models/ordered_post.rb +0 -11
  1211. data/spec/app/models/ordered_preference.rb +0 -6
  1212. data/spec/app/models/oscar.rb +0 -14
  1213. data/spec/app/models/override.rb +0 -16
  1214. data/spec/app/models/ownable.rb +0 -6
  1215. data/spec/app/models/owner.rb +0 -6
  1216. data/spec/app/models/pack.rb +0 -3
  1217. data/spec/app/models/page.rb +0 -16
  1218. data/spec/app/models/page_question.rb +0 -4
  1219. data/spec/app/models/palette.rb +0 -7
  1220. data/spec/app/models/parent.rb +0 -5
  1221. data/spec/app/models/parent_doc.rb +0 -6
  1222. data/spec/app/models/passport.rb +0 -5
  1223. data/spec/app/models/patient.rb +0 -9
  1224. data/spec/app/models/pdf_writer.rb +0 -3
  1225. data/spec/app/models/pencil.rb +0 -1
  1226. data/spec/app/models/person.rb +0 -207
  1227. data/spec/app/models/pet.rb +0 -23
  1228. data/spec/app/models/pet_owner.rb +0 -6
  1229. data/spec/app/models/phone.rb +0 -9
  1230. data/spec/app/models/pizza.rb +0 -7
  1231. data/spec/app/models/player.rb +0 -35
  1232. data/spec/app/models/post.rb +0 -50
  1233. data/spec/app/models/post_genre.rb +0 -6
  1234. data/spec/app/models/powerup.rb +0 -11
  1235. data/spec/app/models/preference.rb +0 -9
  1236. data/spec/app/models/princess.rb +0 -10
  1237. data/spec/app/models/product.rb +0 -17
  1238. data/spec/app/models/profile.rb +0 -5
  1239. data/spec/app/models/pronunciation.rb +0 -5
  1240. data/spec/app/models/pub.rb +0 -6
  1241. data/spec/app/models/purchase.rb +0 -4
  1242. data/spec/app/models/question.rb +0 -8
  1243. data/spec/app/models/quiz.rb +0 -7
  1244. data/spec/app/models/rating.rb +0 -8
  1245. data/spec/app/models/record.rb +0 -52
  1246. data/spec/app/models/registry.rb +0 -4
  1247. data/spec/app/models/role.rb +0 -7
  1248. data/spec/app/models/root_category.rb +0 -4
  1249. data/spec/app/models/sandwich.rb +0 -9
  1250. data/spec/app/models/scheduler.rb +0 -7
  1251. data/spec/app/models/seo.rb +0 -7
  1252. data/spec/app/models/series.rb +0 -4
  1253. data/spec/app/models/server.rb +0 -13
  1254. data/spec/app/models/service.rb +0 -22
  1255. data/spec/app/models/shape.rb +0 -12
  1256. data/spec/app/models/shelf.rb +0 -5
  1257. data/spec/app/models/shipment_address.rb +0 -2
  1258. data/spec/app/models/shipping_container.rb +0 -5
  1259. data/spec/app/models/shipping_pack.rb +0 -3
  1260. data/spec/app/models/shop.rb +0 -6
  1261. data/spec/app/models/short_agent.rb +0 -4
  1262. data/spec/app/models/short_quiz.rb +0 -5
  1263. data/spec/app/models/simple.rb +0 -5
  1264. data/spec/app/models/slave.rb +0 -6
  1265. data/spec/app/models/song.rb +0 -8
  1266. data/spec/app/models/sound.rb +0 -5
  1267. data/spec/app/models/square.rb +0 -4
  1268. data/spec/app/models/staff.rb +0 -7
  1269. data/spec/app/models/store_as_dup_test1.rb +0 -5
  1270. data/spec/app/models/store_as_dup_test2.rb +0 -5
  1271. data/spec/app/models/strategy.rb +0 -3
  1272. data/spec/app/models/sub_item.rb +0 -3
  1273. data/spec/app/models/subscription.rb +0 -5
  1274. data/spec/app/models/survey.rb +0 -5
  1275. data/spec/app/models/symptom.rb +0 -6
  1276. data/spec/app/models/tag.rb +0 -8
  1277. data/spec/app/models/target.rb +0 -5
  1278. data/spec/app/models/template.rb +0 -5
  1279. data/spec/app/models/thing.rb +0 -9
  1280. data/spec/app/models/title.rb +0 -3
  1281. data/spec/app/models/tool.rb +0 -8
  1282. data/spec/app/models/topping.rb +0 -5
  1283. data/spec/app/models/track.rb +0 -38
  1284. data/spec/app/models/translation.rb +0 -5
  1285. data/spec/app/models/tree.rb +0 -9
  1286. data/spec/app/models/truck.rb +0 -3
  1287. data/spec/app/models/user.rb +0 -21
  1288. data/spec/app/models/user_account.rb +0 -10
  1289. data/spec/app/models/validation_callback.rb +0 -10
  1290. data/spec/app/models/vehicle.rb +0 -11
  1291. data/spec/app/models/version.rb +0 -5
  1292. data/spec/app/models/vet_visit.rb +0 -5
  1293. data/spec/app/models/video.rb +0 -13
  1294. data/spec/app/models/video_game.rb +0 -1
  1295. data/spec/app/models/weapon.rb +0 -11
  1296. data/spec/app/models/wiki_page.rb +0 -14
  1297. data/spec/app/models/word.rb +0 -15
  1298. data/spec/app/models/word_origin.rb +0 -11
  1299. data/spec/app/models/writer.rb +0 -11
  1300. data/spec/mongoid/criteria/queryable/forwardable_spec.rb +0 -87
  1301. data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
  1302. data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
  1303. data/spec/mongoid/matchable/all_spec.rb +0 -31
  1304. data/spec/mongoid/matchable/and_spec.rb +0 -187
  1305. data/spec/mongoid/matchable/default_spec.rb +0 -130
  1306. data/spec/mongoid/matchable/elem_match_spec.rb +0 -86
  1307. data/spec/mongoid/matchable/exists_spec.rb +0 -57
  1308. data/spec/mongoid/matchable/gt_spec.rb +0 -86
  1309. data/spec/mongoid/matchable/gte_spec.rb +0 -84
  1310. data/spec/mongoid/matchable/in_spec.rb +0 -49
  1311. data/spec/mongoid/matchable/lt_spec.rb +0 -85
  1312. data/spec/mongoid/matchable/lte_spec.rb +0 -85
  1313. data/spec/mongoid/matchable/ne_spec.rb +0 -46
  1314. data/spec/mongoid/matchable/nin_spec.rb +0 -48
  1315. data/spec/mongoid/matchable/nor_spec.rb +0 -209
  1316. data/spec/mongoid/matchable/or_spec.rb +0 -131
  1317. data/spec/mongoid/matchable/regexp_spec.rb +0 -59
  1318. data/spec/mongoid/matchable/size_spec.rb +0 -25
  1319. data/spec/mongoid/matchable_spec.rb +0 -853
  1320. data/spec/mongoid/relations/accessors_spec.rb +0 -821
  1321. data/spec/mongoid/relations/auto_save_spec.rb +0 -314
  1322. data/spec/mongoid/relations/bindings/embedded/in_spec.rb +0 -171
  1323. data/spec/mongoid/relations/bindings/embedded/many_spec.rb +0 -54
  1324. data/spec/mongoid/relations/bindings/embedded/one_spec.rb +0 -77
  1325. data/spec/mongoid/relations/bindings/referenced/in_spec.rb +0 -241
  1326. data/spec/mongoid/relations/bindings/referenced/many_spec.rb +0 -153
  1327. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +0 -178
  1328. data/spec/mongoid/relations/bindings/referenced/one_spec.rb +0 -131
  1329. data/spec/mongoid/relations/builders/embedded/in_spec.rb +0 -34
  1330. data/spec/mongoid/relations/builders/embedded/many_spec.rb +0 -132
  1331. data/spec/mongoid/relations/builders/embedded/one_spec.rb +0 -99
  1332. data/spec/mongoid/relations/builders/nested_attributes/many_spec.rb +0 -233
  1333. data/spec/mongoid/relations/builders/nested_attributes/one_spec.rb +0 -250
  1334. data/spec/mongoid/relations/builders/referenced/in_spec.rb +0 -230
  1335. data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
  1336. data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
  1337. data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
  1338. data/spec/mongoid/relations/builders_spec.rb +0 -253
  1339. data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
  1340. data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
  1341. data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
  1342. data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
  1343. data/spec/mongoid/relations/cascading_spec.rb +0 -355
  1344. data/spec/mongoid/relations/constraint_spec.rb +0 -75
  1345. data/spec/mongoid/relations/conversions_spec.rb +0 -128
  1346. data/spec/mongoid/relations/counter_cache_spec.rb +0 -450
  1347. data/spec/mongoid/relations/cyclic_spec.rb +0 -178
  1348. data/spec/mongoid/relations/eager/belongs_to_spec.rb +0 -163
  1349. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +0 -133
  1350. data/spec/mongoid/relations/eager/has_many_spec.rb +0 -244
  1351. data/spec/mongoid/relations/eager/has_one_spec.rb +0 -163
  1352. data/spec/mongoid/relations/eager_spec.rb +0 -279
  1353. data/spec/mongoid/relations/embedded/dirty_spec.rb +0 -65
  1354. data/spec/mongoid/relations/embedded/in_spec.rb +0 -579
  1355. data/spec/mongoid/relations/embedded/many_spec.rb +0 -4412
  1356. data/spec/mongoid/relations/embedded/one_spec.rb +0 -1068
  1357. data/spec/mongoid/relations/macros_spec.rb +0 -1039
  1358. data/spec/mongoid/relations/metadata_spec.rb +0 -1985
  1359. data/spec/mongoid/relations/options_spec.rb +0 -35
  1360. data/spec/mongoid/relations/polymorphic_spec.rb +0 -128
  1361. data/spec/mongoid/relations/referenced/in_spec.rb +0 -1366
  1362. data/spec/mongoid/relations/referenced/many_spec.rb +0 -3816
  1363. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +0 -3577
  1364. data/spec/mongoid/relations/referenced/one_spec.rb +0 -1240
  1365. data/spec/mongoid/relations/reflections_spec.rb +0 -101
  1366. data/spec/mongoid/relations/synchronization_spec.rb +0 -495
  1367. data/spec/mongoid/relations/targets/enumerable_spec.rb +0 -1825
  1368. data/spec/mongoid/relations/touchable_spec.rb +0 -402
  1369. data/spec/mongoid/relations_spec.rb +0 -189
  1370. data/spec/support/cluster_config.rb +0 -158
  1371. data/spec/support/session_registry.rb +0 -50
  1372. /data/spec/{app → support}/models/array_field.rb +0 -0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Mongoid::Contextual::Mongo do
@@ -7,7 +9,7 @@ describe Mongoid::Contextual::Mongo do
7
9
  describe "##{method}" do
8
10
 
9
11
  before do
10
- Band.create(name: "Depeche Mode")
12
+ Band.create!(name: "Depeche Mode")
11
13
  end
12
14
 
13
15
  context "when the count is zero" do
@@ -78,11 +80,11 @@ describe Mongoid::Contextual::Mongo do
78
80
  describe "#count" do
79
81
 
80
82
  let!(:depeche) do
81
- Band.create(name: "Depeche Mode")
83
+ Band.create!(name: "Depeche Mode")
82
84
  end
83
85
 
84
86
  let!(:new_order) do
85
- Band.create(name: "New Order")
87
+ Band.create!(name: "New Order")
86
88
  end
87
89
 
88
90
  let(:criteria) do
@@ -106,11 +108,8 @@ describe Mongoid::Contextual::Mongo do
106
108
  described_class.new(criteria.cache)
107
109
  end
108
110
 
109
- before do
110
- expect(context.view).to receive(:count).once.and_return(1)
111
- end
112
-
113
111
  it "returns the count cached value after first call" do
112
+ expect(context.view).to receive(:count_documents).once.and_return(1)
114
113
  2.times { expect(context.count).to eq(1) }
115
114
  end
116
115
  end
@@ -134,7 +133,7 @@ describe Mongoid::Contextual::Mongo do
134
133
  context "and a limit true" do
135
134
 
136
135
  before do
137
- 2.times { Band.create(name: "Depeche Mode", likes: 1) }
136
+ 2.times { Band.create!(name: "Depeche Mode", likes: 1) }
138
137
  end
139
138
 
140
139
  let(:count) do
@@ -152,7 +151,7 @@ describe Mongoid::Contextual::Mongo do
152
151
  context "when provided limit" do
153
152
 
154
153
  before do
155
- 2.times { Band.create(name: "Depeche Mode") }
154
+ 2.times { Band.create!(name: "Depeche Mode") }
156
155
  end
157
156
 
158
157
  let(:context) do
@@ -168,7 +167,8 @@ describe Mongoid::Contextual::Mongo do
168
167
  end
169
168
  end
170
169
 
171
- context 'when a collation is specified', if: collation_supported? do
170
+ context 'when a collation is specified' do
171
+ min_server_version '3.4'
172
172
 
173
173
  let(:context) do
174
174
  described_class.new(criteria)
@@ -191,16 +191,81 @@ describe Mongoid::Contextual::Mongo do
191
191
  end
192
192
  end
193
193
 
194
+ describe "#estimated_count" do
195
+
196
+ let!(:depeche) do
197
+ Band.create!(name: "Depeche Mode")
198
+ end
199
+
200
+ let!(:new_order) do
201
+ Band.create!(name: "New Order")
202
+ end
203
+
204
+ let(:criteria) do
205
+ Band.where
206
+ end
207
+
208
+ context "when not providing options" do
209
+ it 'returns the correct count' do
210
+ expect(criteria.estimated_count).to eq(2)
211
+ end
212
+ end
213
+
214
+ context "when providing options" do
215
+ it 'returns the correct count' do
216
+ expect(criteria.estimated_count(maxTimeMS: 1000)).to eq(2)
217
+ end
218
+ end
219
+
220
+ context "when context is cached" do
221
+
222
+ let(:context) do
223
+ described_class.new(criteria.cache)
224
+ end
225
+
226
+ it "returns the count cached value after first call" do
227
+ expect(context.view).to receive(:estimated_document_count).once.and_return(1)
228
+ 2.times do
229
+ context.estimated_count
230
+ end
231
+ end
232
+ end
233
+
234
+ context "when the criteria contains a selector", :focus do
235
+ let(:criteria) do
236
+ Band.where(name: "New Order")
237
+ end
238
+
239
+ context "when not providing options" do
240
+ it 'raises an error' do
241
+ expect do
242
+ criteria.estimated_count
243
+ end.to raise_error(Mongoid::Errors::InvalidEstimatedCountCriteria)
244
+ end
245
+ end
246
+
247
+ context "when providing options" do
248
+ it 'raises an error' do
249
+ expect do
250
+ criteria.estimated_count(maxTimeMS: 1000)
251
+ end.to raise_error(Mongoid::Errors::InvalidEstimatedCountCriteria)
252
+ end
253
+ end
254
+ end
255
+ end
256
+
257
+
258
+
194
259
  [ :delete, :delete_all ].each do |method|
195
260
 
196
261
  describe "##{method}" do
197
262
 
198
263
  let!(:depeche_mode) do
199
- Band.create(name: "Depeche Mode")
264
+ Band.create!(name: "Depeche Mode")
200
265
  end
201
266
 
202
267
  let!(:new_order) do
203
- Band.create(name: "New Order")
268
+ Band.create!(name: "New Order")
204
269
  end
205
270
 
206
271
  context "when the selector is contraining" do
@@ -229,7 +294,8 @@ describe Mongoid::Contextual::Mongo do
229
294
  expect(deleted).to eq(1)
230
295
  end
231
296
 
232
- context 'when the criteria has a collation', if: collation_supported? do
297
+ context 'when the criteria has a collation' do
298
+ min_server_version '3.4'
233
299
 
234
300
  let(:criteria) do
235
301
  Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
@@ -300,11 +366,11 @@ describe Mongoid::Contextual::Mongo do
300
366
  describe "##{method}" do
301
367
 
302
368
  let!(:depeche_mode) do
303
- Band.create(name: "Depeche Mode")
369
+ Band.create!(name: "Depeche Mode")
304
370
  end
305
371
 
306
372
  let!(:new_order) do
307
- Band.create(name: "New Order")
373
+ Band.create!(name: "New Order")
308
374
  end
309
375
 
310
376
  context "when the selector is contraining" do
@@ -333,7 +399,8 @@ describe Mongoid::Contextual::Mongo do
333
399
  expect(destroyed).to eq(1)
334
400
  end
335
401
 
336
- context 'when the criteria has a collation', if: collation_supported? do
402
+ context 'when the criteria has a collation' do
403
+ min_server_version '3.4'
337
404
 
338
405
  let(:criteria) do
339
406
  Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
@@ -384,7 +451,7 @@ describe Mongoid::Contextual::Mongo do
384
451
  context 'when the write concern is unacknowledged' do
385
452
 
386
453
  before do
387
- 2.times { Band.create }
454
+ 2.times { Band.create! }
388
455
  end
389
456
 
390
457
  let(:criteria) do
@@ -406,27 +473,83 @@ describe Mongoid::Contextual::Mongo do
406
473
  describe "#distinct" do
407
474
 
408
475
  before do
409
- Band.create(name: "Depeche Mode", years: 30)
410
- Band.create(name: "New Order", years: 25)
476
+ Band.create!(name: "Depeche Mode", years: 30, sales: "1E2")
477
+ Band.create!(name: "New Order", years: 25, sales: "2E3")
478
+ Band.create!(name: "10,000 Maniacs", years: 20, sales: "1E2")
411
479
  end
412
480
 
413
- context "when limiting the result set" do
481
+ with_config_values :legacy_pluck_distinct, true, false do
482
+ context "when limiting the result set" do
414
483
 
415
- let(:criteria) do
416
- Band.where(name: "Depeche Mode")
484
+ let(:criteria) do
485
+ Band.where(name: "Depeche Mode")
486
+ end
487
+
488
+ let(:context) do
489
+ described_class.new(criteria)
490
+ end
491
+
492
+ it "returns the distinct matching fields" do
493
+ expect(context.distinct(:name)).to eq([ "Depeche Mode" ])
494
+ end
417
495
  end
418
496
 
419
- let(:context) do
420
- described_class.new(criteria)
497
+ context "when not limiting the result set" do
498
+
499
+ let(:criteria) do
500
+ Band.criteria
501
+ end
502
+
503
+ let(:context) do
504
+ described_class.new(criteria)
505
+ end
506
+
507
+ it "returns the distinct field values" do
508
+ expect(context.distinct(:name).sort).to eq([ "10,000 Maniacs", "Depeche Mode", "New Order" ].sort)
509
+ end
421
510
  end
422
511
 
423
- it "returns the distinct matching fields" do
424
- expect(context.distinct(:name)).to eq([ "Depeche Mode" ])
512
+ context "when providing an aliased field" do
513
+
514
+ let(:criteria) do
515
+ Band.criteria
516
+ end
517
+
518
+ let(:context) do
519
+ described_class.new(criteria)
520
+ end
521
+
522
+ it "returns the distinct field values" do
523
+ expect(context.distinct(:years).sort).to eq([ 20, 25, 30 ])
524
+ end
425
525
  end
426
- end
427
526
 
428
- context "when not limiting the result set" do
527
+ context 'when a collation is specified' do
528
+ min_server_version '3.4'
529
+
530
+ before do
531
+ Band.create!(name: 'DEPECHE MODE')
532
+ end
533
+
534
+ let(:context) do
535
+ described_class.new(criteria)
536
+ end
537
+
538
+ let(:expected_results) do
539
+ ["10,000 Maniacs", "Depeche Mode", "New Order"]
540
+ end
541
+
542
+ let(:criteria) do
543
+ Band.where({}).collation(locale: 'en_US', strength: 2)
544
+ end
545
+
546
+ it 'applies the collation' do
547
+ expect(context.distinct(:name).sort).to eq(expected_results.sort)
548
+ end
549
+ end
550
+ end
429
551
 
552
+ context "when providing a demongoizable field" do
430
553
  let(:criteria) do
431
554
  Band.criteria
432
555
  end
@@ -435,46 +558,253 @@ describe Mongoid::Contextual::Mongo do
435
558
  described_class.new(criteria)
436
559
  end
437
560
 
438
- it "returns the distinct field values" do
439
- expect(context.distinct(:name)).to eq([ "Depeche Mode", "New Order" ])
561
+ context "when legacy_pluck_distinct is set" do
562
+ config_override :legacy_pluck_distinct, true
563
+
564
+ it "returns the non-demongoized distinct field values" do
565
+ expect(context.distinct(:sales).sort).to eq([ "1E2", "2E3" ])
566
+ end
567
+ end
568
+
569
+ context "when legacy_pluck_distinct is not set" do
570
+ config_override :legacy_pluck_distinct, false
571
+
572
+ it "returns the non-demongoized distinct field values" do
573
+ expect(context.distinct(:sales).sort).to eq([ BigDecimal("1E2"), BigDecimal("2E3") ])
574
+ end
440
575
  end
441
576
  end
442
577
 
443
- context "when providing an aliased field" do
578
+ context "when getting a localized field" do
579
+ before do
580
+ I18n.locale = :en
581
+ d = Dictionary.create!(description: 'english-text')
582
+ I18n.locale = :de
583
+ d.description = 'deutsch-text'
584
+ d.save!
585
+ end
586
+
587
+ after do
588
+ I18n.locale = :en
589
+ end
444
590
 
445
591
  let(:criteria) do
446
- Band.criteria
592
+ Dictionary.criteria
447
593
  end
448
594
 
449
595
  let(:context) do
450
596
  described_class.new(criteria)
451
597
  end
452
598
 
453
- it "returns the distinct field values" do
454
- expect(context.distinct(:years).sort).to eq([ 25, 30 ])
599
+ context "when getting the field without _translations" do
600
+ context "when legacy_pluck_distinct is set" do
601
+ config_override :legacy_pluck_distinct, true
602
+
603
+ it "gets the full hash" do
604
+ expect(context.distinct(:description)).to eq([{ "de" => "deutsch-text", "en" => "english-text" }])
605
+ end
606
+ end
607
+
608
+ context "when legacy_pluck_distinct is not set" do
609
+ config_override :legacy_pluck_distinct, false
610
+
611
+ it "gets the demongoized localized field" do
612
+ expect(context.distinct(:description)).to eq([ 'deutsch-text' ])
613
+ end
614
+ end
455
615
  end
456
- end
457
616
 
458
- context 'when a collation is specified', if: collation_supported? do
617
+ context "when getting the field with _translations" do
618
+ context "when legacy_pluck_distinct is set" do
619
+ config_override :legacy_pluck_distinct, true
459
620
 
460
- before do
461
- Band.create(name: 'DEPECHE MODE')
621
+ it "gets an empty list" do
622
+ expect(context.distinct(:description_translations)).to eq([])
623
+ end
624
+ end
625
+
626
+ context "when legacy_pluck_distinct is not set" do
627
+ config_override :legacy_pluck_distinct, false
628
+
629
+ it "gets the full hash" do
630
+ expect(context.distinct(:description_translations)).to eq([ { "de" => "deutsch-text", "en" => "english-text" } ])
631
+ end
632
+ end
462
633
  end
463
634
 
464
- let(:context) do
465
- described_class.new(criteria)
635
+ context 'when plucking a specific locale' do
636
+
637
+ let(:distinct) do
638
+ context.distinct(:'description.de')
639
+ end
640
+
641
+ context "when legacy_pluck_distinct is set" do
642
+ config_override :legacy_pluck_distinct, true
643
+
644
+ it 'returns the specific translation' do
645
+ expect(distinct).to eq([ 'deutsch-text' ])
646
+ end
647
+ end
648
+
649
+ context "when legacy_pluck_distinct is not set" do
650
+ config_override :legacy_pluck_distinct, false
651
+
652
+ it 'returns the specific translation' do
653
+ expect(distinct).to eq([ "deutsch-text" ])
654
+ end
655
+ end
466
656
  end
467
657
 
468
- let(:expected_results) do
469
- ["Depeche Mode", "New Order"]
658
+ context 'when plucking a specific locale from _translations field' do
659
+
660
+ let(:distinct) do
661
+ context.distinct(:'description_translations.de')
662
+ end
663
+
664
+ context "when legacy_pluck_distinct is set" do
665
+ config_override :legacy_pluck_distinct, true
666
+
667
+ it 'returns the empty list' do
668
+ expect(distinct).to eq([])
669
+ end
670
+ end
671
+
672
+ context "when legacy_pluck_distinct is not set" do
673
+ config_override :legacy_pluck_distinct, false
674
+
675
+ it 'returns the specific translations' do
676
+ expect(distinct).to eq(['deutsch-text'])
677
+ end
678
+ end
470
679
  end
471
680
 
472
- let(:criteria) do
473
- Band.where({}).collation(locale: 'en_US', strength: 2)
681
+ context 'when fallbacks are enabled with a locale list' do
682
+ require_fallbacks
683
+
684
+ around(:all) do |example|
685
+ prev_fallbacks = I18n.fallbacks.dup
686
+ I18n.fallbacks[:he] = [ :en ]
687
+ example.run
688
+ I18n.fallbacks = prev_fallbacks
689
+ end
690
+
691
+ after do
692
+ I18n.locale = :en
693
+ end
694
+
695
+ let(:distinct) do
696
+ context.distinct(:description).first
697
+ end
698
+
699
+ context "when legacy_pluck_distinct is set" do
700
+ config_override :legacy_pluck_distinct, true
701
+
702
+ it "does not correctly use the fallback" do
703
+ distinct.should == {"de"=>"deutsch-text", "en"=>"english-text"}
704
+ end
705
+ end
706
+
707
+ context "when legacy_pluck_distinct is not set" do
708
+ config_override :legacy_pluck_distinct, false
709
+
710
+ it "correctly uses the fallback" do
711
+ I18n.locale = :en
712
+ d = Dictionary.create!(description: 'english-text')
713
+ I18n.locale = :he
714
+ distinct.should == "english-text"
715
+ end
716
+ end
717
+ end
718
+
719
+ context "when the localized field is embedded" do
720
+ before do
721
+ p = Passport.new
722
+ I18n.locale = :en
723
+ p.name = "Neil"
724
+ I18n.locale = :he
725
+ p.name = "Nissim"
726
+
727
+ Person.create!(passport: p, employer_id: 12345)
728
+ end
729
+
730
+ after do
731
+ I18n.locale = :en
732
+ end
733
+
734
+ let(:criteria) do
735
+ Person.where(employer_id: 12345)
736
+ end
737
+
738
+ let(:context) do
739
+ described_class.new(criteria)
740
+ end
741
+
742
+ let(:distinct) do
743
+ context.distinct("pass.name").first
744
+ end
745
+
746
+ let(:distinct_translations) do
747
+ context.distinct("pass.name_translations").first
748
+ end
749
+
750
+ let(:distinct_translations_field) do
751
+ context.distinct("pass.name_translations.en").first
752
+ end
753
+
754
+ context "when legacy_pluck_distinct is set" do
755
+ config_override :legacy_pluck_distinct, true
756
+
757
+ it "returns the full hash" do
758
+ expect(distinct).to eq({ "en" => "Neil", "he" => "Nissim" })
759
+ end
760
+
761
+ it "returns the empty hash" do
762
+ expect(distinct_translations).to eq(nil)
763
+ end
764
+
765
+ it "returns the empty hash" do
766
+ expect(distinct_translations_field).to eq(nil)
767
+ end
768
+ end
769
+
770
+ context "when legacy_pluck_distinct is not set" do
771
+ config_override :legacy_pluck_distinct, false
772
+
773
+ it "returns the translation for the current locale" do
774
+ expect(distinct).to eq("Nissim")
775
+ end
776
+
777
+ it "returns the full _translation hash" do
778
+ expect(distinct_translations).to eq({ "en" => "Neil", "he" => "Nissim" })
779
+ end
780
+
781
+ it "returns the translation for the requested locale" do
782
+ expect(distinct_translations_field).to eq("Neil")
783
+ end
784
+ end
785
+ end
786
+ end
787
+
788
+ context "when getting an embedded field" do
789
+
790
+ let(:label) { Label.new(sales: "1E2") }
791
+ let!(:band) { Band.create!(label: label) }
792
+ let(:criteria) { Band.where(_id: band.id) }
793
+ let(:context) { described_class.new(criteria) }
794
+
795
+ context "when legacy_pluck_distinct is set" do
796
+ config_override :legacy_pluck_distinct, true
797
+
798
+ it "returns the distinct matching fields" do
799
+ expect(context.distinct("label.sales")).to eq([ "1E2" ])
800
+ end
474
801
  end
475
802
 
476
- it 'applies the collation' do
477
- expect(context.distinct(:name)).to eq(expected_results)
803
+ context "when legacy_pluck_distinct is not set" do
804
+ config_override :legacy_pluck_distinct, false
805
+ it "returns the distinct matching fields" do
806
+ expect(context.distinct("label.sales")).to eq([ BigDecimal("1E2") ])
807
+ end
478
808
  end
479
809
  end
480
810
  end
@@ -482,7 +812,7 @@ describe Mongoid::Contextual::Mongo do
482
812
  describe "#each" do
483
813
 
484
814
  before do
485
- Band.create(name: "Depeche Mode")
815
+ Band.create!(name: "Depeche Mode")
486
816
  end
487
817
 
488
818
  let(:criteria) do
@@ -493,7 +823,8 @@ describe Mongoid::Contextual::Mongo do
493
823
  described_class.new(criteria)
494
824
  end
495
825
 
496
- context 'when the criteria has a collation', if: collation_supported? do
826
+ context 'when the criteria has a collation' do
827
+ min_server_version '3.4'
497
828
 
498
829
  let(:criteria) do
499
830
  Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
@@ -559,7 +890,7 @@ describe Mongoid::Contextual::Mongo do
559
890
  context "when iterating with next" do
560
891
 
561
892
  before do
562
- 10.times { |i| Band.create(name: "Test #{i}") }
893
+ 10.times { |i| Band.create!(name: "Test #{i}") }
563
894
  end
564
895
 
565
896
  let(:criteria) do
@@ -571,12 +902,50 @@ describe Mongoid::Contextual::Mongo do
571
902
  end
572
903
 
573
904
  it "does not load all documents" do
574
- expect(Mongo::Logger.logger).to receive(:debug?).exactly(2).times.and_call_original
905
+ subscriber = Mrss::EventSubscriber.new
906
+ context.view.client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
907
+
575
908
  enum.next
909
+
910
+ find_events = subscriber.all_events.select do |evt|
911
+ evt.command_name == 'find'
912
+ end
913
+ expect(find_events.length).to be(2)
914
+ get_more_events = subscriber.all_events.select do |evt|
915
+ evt.command_name == 'getMore'
916
+ end
917
+ expect(get_more_events.length).to be(0)
918
+ ensure
919
+ context.view.client.unsubscribe(Mongo::Monitoring::COMMAND, subscriber)
576
920
  end
577
921
  end
578
922
  end
579
923
  end
924
+
925
+ context 'when the criteria has a parent document' do
926
+
927
+ before do
928
+ Post.create!(person: person)
929
+ Post.create!(person: person)
930
+ Post.create!(person: person)
931
+ end
932
+
933
+ let(:person) do
934
+ Person.new
935
+ end
936
+
937
+ let(:criteria) do
938
+ person.posts.all
939
+ end
940
+
941
+ let(:persons) do
942
+ criteria.collect(&:person)
943
+ end
944
+
945
+ it 'sets the same parent object on each related object' do
946
+ expect(persons.uniq.size).to eq(1)
947
+ end
948
+ end
580
949
  end
581
950
 
582
951
  describe "#eager_load" do
@@ -591,12 +960,12 @@ describe Mongoid::Contextual::Mongo do
591
960
 
592
961
  context "when no documents are returned" do
593
962
 
594
- let(:game_metadata) do
963
+ let(:game_association) do
595
964
  Person.reflect_on_association(:game)
596
965
  end
597
966
 
598
967
  it "does not make any additional database queries" do
599
- expect(game_metadata).to receive(:eager_load).never
968
+ expect(game_association).to receive(:eager_load).never
600
969
  context.send(:eager_load, [])
601
970
  end
602
971
  end
@@ -605,7 +974,7 @@ describe Mongoid::Contextual::Mongo do
605
974
  describe "#exists?" do
606
975
 
607
976
  before do
608
- Band.create(name: "Depeche Mode")
977
+ Band.create!(name: "Depeche Mode")
609
978
  end
610
979
 
611
980
  context "when the count is zero" do
@@ -718,11 +1087,11 @@ describe Mongoid::Contextual::Mongo do
718
1087
  describe "#find_one_and_replace" do
719
1088
 
720
1089
  let!(:depeche) do
721
- Band.create(name: "Depeche Mode")
1090
+ Band.create!(name: "Depeche Mode")
722
1091
  end
723
1092
 
724
1093
  let!(:tool) do
725
- Band.create(name: "Tool")
1094
+ Band.create!(name: "Tool")
726
1095
  end
727
1096
 
728
1097
  context "when the selector matches" do
@@ -825,7 +1194,8 @@ describe Mongoid::Contextual::Mongo do
825
1194
  end
826
1195
  end
827
1196
 
828
- context 'when a collation is specified on the criteria', if: collation_supported? do
1197
+ context 'when a collation is specified on the criteria' do
1198
+ min_server_version '3.4'
829
1199
 
830
1200
  let(:criteria) do
831
1201
  Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
@@ -873,11 +1243,11 @@ describe Mongoid::Contextual::Mongo do
873
1243
  describe "#find_one_and_update" do
874
1244
 
875
1245
  let!(:depeche) do
876
- Band.create(name: "Depeche Mode")
1246
+ Band.create!(name: "Depeche Mode")
877
1247
  end
878
1248
 
879
1249
  let!(:tool) do
880
- Band.create(name: "Tool")
1250
+ Band.create!(name: "Tool")
881
1251
  end
882
1252
 
883
1253
  context "when the selector matches" do
@@ -978,7 +1348,8 @@ describe Mongoid::Contextual::Mongo do
978
1348
  end
979
1349
  end
980
1350
 
981
- context 'when a collation is specified on the criteria', if: collation_supported? do
1351
+ context 'when a collation is specified on the criteria' do
1352
+ min_server_version '3.4'
982
1353
 
983
1354
  let(:criteria) do
984
1355
  Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
@@ -1025,7 +1396,7 @@ describe Mongoid::Contextual::Mongo do
1025
1396
  describe "#find_one_and_delete" do
1026
1397
 
1027
1398
  let!(:depeche) do
1028
- Band.create(name: "Depeche Mode")
1399
+ Band.create!(name: "Depeche Mode")
1029
1400
  end
1030
1401
 
1031
1402
  let(:criteria) do
@@ -1052,7 +1423,8 @@ describe Mongoid::Contextual::Mongo do
1052
1423
  }.to raise_error(Mongoid::Errors::DocumentNotFound)
1053
1424
  end
1054
1425
 
1055
- context 'when a collation is specified on the criteria', if: collation_supported? do
1426
+ context 'when a collation is specified on the criteria' do
1427
+ min_server_version '3.4'
1056
1428
 
1057
1429
  let(:criteria) do
1058
1430
  Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
@@ -1103,11 +1475,15 @@ describe Mongoid::Contextual::Mongo do
1103
1475
  describe "##{method}" do
1104
1476
 
1105
1477
  let!(:depeche_mode) do
1106
- Band.create(name: "Depeche Mode")
1478
+ Band.create!(name: "Depeche Mode")
1107
1479
  end
1108
1480
 
1109
1481
  let!(:new_order) do
1110
- Band.create(name: "New Order")
1482
+ Band.create!(name: "New Order")
1483
+ end
1484
+
1485
+ let!(:rolling_stones) do
1486
+ Band.create!(name: "The Rolling Stones")
1111
1487
  end
1112
1488
 
1113
1489
  context "when the context is not cached" do
@@ -1124,7 +1500,8 @@ describe Mongoid::Contextual::Mongo do
1124
1500
  expect(context.send(method)).to eq(depeche_mode)
1125
1501
  end
1126
1502
 
1127
- context 'when the criteria has a collation', if: collation_supported? do
1503
+ context 'when the criteria has a collation' do
1504
+ min_server_version '3.4'
1128
1505
 
1129
1506
  let(:criteria) do
1130
1507
  Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
@@ -1149,14 +1526,14 @@ describe Mongoid::Contextual::Mongo do
1149
1526
  context "when there is sort on the context" do
1150
1527
 
1151
1528
  it "follows the main sort" do
1152
- expect(context.send(method)).to eq(new_order)
1529
+ expect(context.send(method)).to eq(rolling_stones)
1153
1530
  end
1154
1531
  end
1155
1532
 
1156
1533
  context "when subsequently calling #last" do
1157
1534
 
1158
1535
  it "returns the correct document" do
1159
- expect(context.send(method)).to eq(new_order)
1536
+ expect(context.send(method)).to eq(rolling_stones)
1160
1537
  expect(context.last).to eq(depeche_mode)
1161
1538
  end
1162
1539
  end
@@ -1177,27 +1554,26 @@ describe Mongoid::Contextual::Mongo do
1177
1554
  expect(context.send(method)).to eq(depeche_mode)
1178
1555
  end
1179
1556
 
1180
- context 'when calling #last' do
1557
+ context "when calling ##{method}" do
1181
1558
 
1182
- it 'returns the last document, sorted by _id' do
1559
+ it 'returns the requested document, sorted by _id' do
1183
1560
  expect(context.send(method)).to eq(depeche_mode)
1184
- expect(context.last).to eq(new_order)
1561
+ expect(context.last).to eq(rolling_stones)
1185
1562
  end
1186
1563
  end
1187
1564
 
1188
- context 'with option { sort: :none }' do
1189
-
1565
+ context 'with option { id_sort: :none }' do
1190
1566
  let(:opts) do
1191
1567
  { id_sort: :none }
1192
1568
  end
1193
1569
 
1194
- it 'does not apply the sort on _id' do
1570
+ it 'applies the sort on _id' do
1195
1571
  expect(context.send(method, opts)).to eq(depeche_mode)
1196
1572
  end
1197
1573
 
1198
- context 'when calling #last' do
1574
+ context "when calling ##{method}" do
1199
1575
 
1200
- it 'does not apply a sort on _id' do
1576
+ it 'doesn\'t apply a sort on _id' do
1201
1577
  expect(context.send(method, opts)).to eq(depeche_mode)
1202
1578
  expect(context.last(opts)).to eq(depeche_mode)
1203
1579
  end
@@ -1215,33 +1591,32 @@ describe Mongoid::Contextual::Mongo do
1215
1591
  described_class.new(criteria)
1216
1592
  end
1217
1593
 
1218
-
1219
1594
  it 'applies the criteria sort' do
1220
- expect(context.send(method)).to eq(new_order)
1595
+ expect(context.send(method)).to eq(rolling_stones)
1221
1596
  end
1222
1597
 
1223
1598
  context 'when calling #last' do
1224
1599
 
1225
1600
  it 'applies the criteria sort' do
1226
- expect(context.send(method)).to eq(new_order)
1601
+ expect(context.send(method)).to eq(rolling_stones)
1227
1602
  expect(context.last).to eq(depeche_mode)
1228
1603
  end
1229
1604
  end
1230
1605
 
1231
- context 'with option { sort: :none }' do
1606
+ context 'with option { id_sort: :none }' do
1232
1607
 
1233
1608
  let(:opts) do
1234
1609
  { id_sort: :none }
1235
1610
  end
1236
1611
 
1237
- it 'applies the criteria sort' do
1238
- expect(context.send(method, opts)).to eq(new_order)
1612
+ it 'uses the preexisting sort' do
1613
+ expect(context.send(method, opts)).to eq(rolling_stones)
1239
1614
  end
1240
1615
 
1241
1616
  context 'when calling #last' do
1242
1617
 
1243
- it 'applies the criteria sort' do
1244
- expect(context.send(method, opts)).to eq(new_order)
1618
+ it 'uses the preexisting sort' do
1619
+ expect(context.send(method, opts)).to eq(rolling_stones)
1245
1620
  expect(context.last(opts)).to eq(depeche_mode)
1246
1621
  end
1247
1622
  end
@@ -1261,14 +1636,14 @@ describe Mongoid::Contextual::Mongo do
1261
1636
  context "when there is sort on the context" do
1262
1637
 
1263
1638
  it "follows the main sort" do
1264
- expect(context.send(method)).to eq(new_order)
1639
+ expect(context.send(method)).to eq(rolling_stones)
1265
1640
  end
1266
1641
  end
1267
1642
 
1268
1643
  context "when subsequently calling #last" do
1269
1644
 
1270
1645
  it "returns the correct document" do
1271
- expect(context.send(method)).to eq(new_order)
1646
+ expect(context.send(method)).to eq(rolling_stones)
1272
1647
  expect(context.last).to eq(depeche_mode)
1273
1648
  end
1274
1649
  end
@@ -1308,33 +1683,727 @@ describe Mongoid::Contextual::Mongo do
1308
1683
  end
1309
1684
  end
1310
1685
  end
1311
- end
1312
- end
1313
1686
 
1314
- describe "#initialize" do
1687
+ context "when including a limit" do
1315
1688
 
1316
- let(:criteria) do
1317
- Band.where(name: "Depeche Mode").no_timeout
1318
- end
1689
+ context "when the context is not cached" do
1319
1690
 
1320
- let(:context) do
1321
- described_class.new(criteria)
1322
- end
1691
+ let(:context) do
1692
+ described_class.new(criteria)
1693
+ end
1323
1694
 
1324
- it "sets the criteria" do
1325
- expect(context.criteria).to eq(criteria)
1326
- end
1695
+ context "when the limit is 1" do
1696
+ let(:criteria) do
1697
+ Band.criteria
1698
+ end
1327
1699
 
1328
- it "sets the klass" do
1329
- expect(context.klass).to eq(Band)
1330
- end
1700
+ let(:docs) do
1701
+ context.send(method, 1)
1702
+ end
1331
1703
 
1332
- it "sets the view" do
1333
- expect(context.view).to be_a(Mongo::Collection::View)
1334
- end
1704
+ it "returns an array of documents" do
1705
+ expect(docs).to eq([ depeche_mode ])
1706
+ end
1707
+ end
1335
1708
 
1336
- it "sets the view selector" do
1337
- expect(context.view.selector).to eq({ "name" => "Depeche Mode" })
1709
+ context "when the limit is >1" do
1710
+ let(:criteria) do
1711
+ Band.criteria
1712
+ end
1713
+
1714
+ let(:docs) do
1715
+ context.send(method, 2)
1716
+ end
1717
+
1718
+ it "returns the number of documents in order" do
1719
+ expect(docs).to eq([ depeche_mode, new_order ])
1720
+ end
1721
+ end
1722
+
1723
+ context 'when the criteria has a collation' do
1724
+ min_server_version '3.4'
1725
+
1726
+ let(:criteria) do
1727
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
1728
+ end
1729
+
1730
+ it "returns the first matching document" do
1731
+ expect(context.send(method, 1)).to eq([ depeche_mode ])
1732
+ end
1733
+ end
1734
+ end
1735
+
1736
+ context "when the context is cached" do
1737
+
1738
+ let(:context) do
1739
+ described_class.new(criteria)
1740
+ end
1741
+
1742
+ context "when the whole context is loaded" do
1743
+
1744
+ before do
1745
+ context.to_a
1746
+ end
1747
+
1748
+ context "when all of the documents are cached" do
1749
+
1750
+ let(:criteria) do
1751
+ Band.all.cache
1752
+ end
1753
+
1754
+ context "when requesting all of the documents" do
1755
+
1756
+ let(:docs) do
1757
+ context.send(method, 3)
1758
+ end
1759
+
1760
+ it "returns all of the documents without touching the database" do
1761
+ expect(context).to receive(:view).never
1762
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
1763
+ end
1764
+ end
1765
+
1766
+ context "when requesting fewer than all of the documents" do
1767
+
1768
+ let(:docs) do
1769
+ context.send(method, 2)
1770
+ end
1771
+
1772
+ it "returns all of the documents without touching the database" do
1773
+ expect(context).to receive(:view).never
1774
+ expect(docs).to eq([ depeche_mode, new_order ])
1775
+ end
1776
+ end
1777
+ end
1778
+
1779
+ context "when only one document is cached" do
1780
+
1781
+ let(:criteria) do
1782
+ Band.where(name: "Depeche Mode").cache
1783
+ end
1784
+
1785
+ context "when requesting one document" do
1786
+
1787
+ let(:docs) do
1788
+ context.send(method, 1)
1789
+ end
1790
+
1791
+ it "returns one document without touching the database" do
1792
+ expect(context).to receive(:view).never
1793
+ expect(docs).to eq([ depeche_mode ])
1794
+ end
1795
+ end
1796
+ end
1797
+ end
1798
+
1799
+ context "when the first method was called before" do
1800
+
1801
+ let(:context) do
1802
+ described_class.new(criteria)
1803
+ end
1804
+
1805
+ let(:criteria) do
1806
+ Band.all.cache
1807
+ end
1808
+
1809
+ before do
1810
+ context.first(before_limit)
1811
+ end
1812
+
1813
+ let(:docs) do
1814
+ context.send(method, limit)
1815
+ end
1816
+
1817
+ context "when getting all of the documents before" do
1818
+ let(:before_limit) { 3 }
1819
+
1820
+ context "when getting all of the documents" do
1821
+ let(:limit) { 3 }
1822
+
1823
+ it "returns all documents without touching the database" do
1824
+ expect(context).to receive(:view).never
1825
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
1826
+ end
1827
+ end
1828
+
1829
+ context "when getting fewer documents" do
1830
+ let(:limit) { 2 }
1831
+
1832
+ it "returns the correct documents without touching the database" do
1833
+ expect(context).to receive(:view).never
1834
+ expect(docs).to eq([ depeche_mode, new_order ])
1835
+ end
1836
+ end
1837
+ end
1838
+
1839
+ context "when getting fewer documents before" do
1840
+ let(:before_limit) { 2 }
1841
+
1842
+ context "when getting the same number of documents" do
1843
+ let(:limit) { 2 }
1844
+
1845
+ it "returns the correct documents without touching the database" do
1846
+ expect(context).to receive(:view).never
1847
+ expect(docs).to eq([ depeche_mode, new_order ])
1848
+ end
1849
+ end
1850
+
1851
+ context "when getting more documents" do
1852
+ let(:limit) { 3 }
1853
+
1854
+ it "returns the correct documents and touches the database" do
1855
+ expect(context).to receive(:view).exactly(3).times.and_call_original
1856
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
1857
+ end
1858
+ end
1859
+ end
1860
+
1861
+ context "when getting one document before" do
1862
+ let(:before_limit) { 1 }
1863
+
1864
+ context "when getting one document" do
1865
+ let(:limit) { 1 }
1866
+
1867
+ it "returns the correct documents without touching the database" do
1868
+ expect(context).to receive(:view).never
1869
+ expect(docs).to eq([ depeche_mode ])
1870
+ end
1871
+ end
1872
+
1873
+ context "when getting more than one document" do
1874
+ let(:limit) { 3 }
1875
+
1876
+ it "returns the correct documents and touches the database" do
1877
+ expect(context).to receive(:view).exactly(3).times.and_call_original
1878
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
1879
+ end
1880
+ end
1881
+ end
1882
+ end
1883
+ end
1884
+ end
1885
+
1886
+ context "when given an empty hash" do
1887
+ let(:context) { described_class.new(criteria) }
1888
+ let(:criteria) { Band.criteria }
1889
+ let(:docs) { context.send(method, {}) }
1890
+
1891
+ it "behaves as if limit is nil" do
1892
+ expect(docs).to eq(depeche_mode)
1893
+ end
1894
+ end
1895
+
1896
+ context "when calling #first then #last" do
1897
+
1898
+ let(:context) do
1899
+ described_class.new(criteria)
1900
+ end
1901
+
1902
+ let(:criteria) do
1903
+ Band.all.cache
1904
+ end
1905
+
1906
+ before do
1907
+ context.first(before_limit)
1908
+ end
1909
+
1910
+ let(:docs) do
1911
+ context.last(limit)
1912
+ end
1913
+
1914
+ context "when getting one from the beginning and one from the end" do
1915
+ let(:before_limit) { 2 }
1916
+ let(:limit) { 1 }
1917
+
1918
+ it "gets the correct document" do
1919
+ expect(docs).to eq([rolling_stones])
1920
+ end
1921
+ end
1922
+ end
1923
+ end
1924
+ end
1925
+
1926
+ describe "#last" do
1927
+ let!(:depeche_mode) do
1928
+ Band.create!(name: "Depeche Mode")
1929
+ end
1930
+
1931
+ let!(:new_order) do
1932
+ Band.create!(name: "New Order")
1933
+ end
1934
+
1935
+ let!(:rolling_stones) do
1936
+ Band.create!(name: "The Rolling Stones")
1937
+ end
1938
+
1939
+ context "when the context is not cached" do
1940
+
1941
+ let(:criteria) do
1942
+ Band.where(name: "Depeche Mode")
1943
+ end
1944
+
1945
+ let(:context) do
1946
+ described_class.new(criteria)
1947
+ end
1948
+
1949
+ it "returns the last matching document" do
1950
+ expect(context.last).to eq(depeche_mode)
1951
+ end
1952
+
1953
+ context 'when the criteria has a collation' do
1954
+ min_server_version '3.4'
1955
+
1956
+ let(:criteria) do
1957
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
1958
+ end
1959
+
1960
+ it "returns the last matching document" do
1961
+ expect(context.last).to eq(depeche_mode)
1962
+ end
1963
+ end
1964
+ end
1965
+
1966
+ context "when using .desc" do
1967
+
1968
+ let(:criteria) do
1969
+ Band.desc(:name)
1970
+ end
1971
+
1972
+ let(:context) do
1973
+ described_class.new(criteria)
1974
+ end
1975
+
1976
+ context "when there is sort on the context" do
1977
+
1978
+ it "follows the main sort" do
1979
+ expect(context.last).to eq(depeche_mode)
1980
+ end
1981
+ end
1982
+
1983
+ context "when subsequently calling #first" do
1984
+
1985
+ it "returns the correct document" do
1986
+ expect(context.last).to eq(depeche_mode)
1987
+ expect(context.first).to eq(rolling_stones)
1988
+ end
1989
+ end
1990
+ end
1991
+
1992
+ context 'when the criteria has no sort' do
1993
+
1994
+ let(:criteria) do
1995
+ Band.all
1996
+ end
1997
+
1998
+ let(:context) do
1999
+ described_class.new(criteria)
2000
+ end
2001
+
2002
+ it 'applies a sort on _id' do
2003
+ expect(context.last).to eq(rolling_stones)
2004
+ end
2005
+
2006
+ context 'when calling #first' do
2007
+
2008
+ it 'returns the first document, sorted by _id' do
2009
+ pending "MONGOID-5416"
2010
+ expect(context.last).to eq(rolling_stones)
2011
+ expect(context.first).to eq(depeche_mode)
2012
+ end
2013
+ end
2014
+
2015
+ context 'with option { id_sort: :none }' do
2016
+ let(:opts) do
2017
+ { id_sort: :none }
2018
+ end
2019
+
2020
+ it 'doesn\'t apply the sort on _id' do
2021
+ expect(context.last(opts)).to eq(depeche_mode)
2022
+ end
2023
+
2024
+ context 'when calling #first' do
2025
+
2026
+ it 'doesn\'t apply the sort on _id' do
2027
+ pending "MONGOID-5416"
2028
+ expect(context.last(opts)).to eq(rolling_stones)
2029
+ expect(context.first(opts)).to eq(depeche_mode)
2030
+ end
2031
+ end
2032
+ end
2033
+ end
2034
+
2035
+ context 'when the criteria has a sort' do
2036
+
2037
+ let(:criteria) do
2038
+ Band.desc(:name)
2039
+ end
2040
+
2041
+ let(:context) do
2042
+ described_class.new(criteria)
2043
+ end
2044
+
2045
+
2046
+ it 'applies the criteria sort' do
2047
+ expect(context.last).to eq(depeche_mode)
2048
+ end
2049
+
2050
+ context 'when calling #first' do
2051
+
2052
+ it 'applies the criteria sort' do
2053
+ expect(context.last).to eq(depeche_mode)
2054
+ expect(context.first).to eq(rolling_stones)
2055
+ end
2056
+ end
2057
+
2058
+ context 'with option { id_sort: :none }' do
2059
+
2060
+ let(:opts) do
2061
+ { id_sort: :none }
2062
+ end
2063
+
2064
+ it 'uses the preexisting sort' do
2065
+ expect(context.last(opts)).to eq(depeche_mode)
2066
+ end
2067
+
2068
+ context 'when calling #first' do
2069
+
2070
+ it 'uses the preexisting sort' do
2071
+ expect(context.last(opts)).to eq(depeche_mode)
2072
+ expect(context.first(opts)).to eq(rolling_stones)
2073
+ end
2074
+ end
2075
+ end
2076
+ end
2077
+
2078
+ context "when using .sort" do
2079
+
2080
+ let(:criteria) do
2081
+ Band.all.sort(:name => -1).criteria
2082
+ end
2083
+
2084
+ let(:context) do
2085
+ described_class.new(criteria)
2086
+ end
2087
+
2088
+ context "when there is sort on the context" do
2089
+
2090
+ it "follows the main sort" do
2091
+ expect(context.last).to eq(depeche_mode)
2092
+ end
2093
+ end
2094
+
2095
+ context "when subsequently calling #first" do
2096
+
2097
+ it "returns the correct document" do
2098
+ expect(context.last).to eq(depeche_mode)
2099
+ expect(context.first).to eq(rolling_stones)
2100
+ end
2101
+ end
2102
+ end
2103
+
2104
+ context "when the context is cached" do
2105
+
2106
+ let(:criteria) do
2107
+ Band.where(name: "Depeche Mode").cache
2108
+ end
2109
+
2110
+ let(:context) do
2111
+ described_class.new(criteria)
2112
+ end
2113
+
2114
+ context "when the cache is loaded" do
2115
+
2116
+ before do
2117
+ context.to_a
2118
+ end
2119
+
2120
+ it "returns the last document without touching the database" do
2121
+ expect(context).to receive(:view).never
2122
+ expect(context.last).to eq(depeche_mode)
2123
+ end
2124
+ end
2125
+
2126
+ context "when last method was called before" do
2127
+
2128
+ before do
2129
+ context.last
2130
+ end
2131
+
2132
+ it "returns the last document without touching the database" do
2133
+ expect(context).to receive(:view).never
2134
+ expect(context.last).to eq(depeche_mode)
2135
+ end
2136
+ end
2137
+ end
2138
+
2139
+ context "when including a limit" do
2140
+
2141
+ context "when the context is not cached" do
2142
+
2143
+ let(:context) do
2144
+ described_class.new(criteria)
2145
+ end
2146
+
2147
+ context "when the limit is 1" do
2148
+ let(:criteria) do
2149
+ Band.criteria
2150
+ end
2151
+
2152
+ let(:docs) do
2153
+ context.last(1)
2154
+ end
2155
+
2156
+ it "returns an array of documents" do
2157
+ expect(docs).to eq([ rolling_stones ])
2158
+ end
2159
+ end
2160
+
2161
+ context "when the limit is >1" do
2162
+ let(:criteria) do
2163
+ Band.criteria
2164
+ end
2165
+
2166
+ let(:docs) do
2167
+ context.last(2)
2168
+ end
2169
+
2170
+ it "returns the number of documents in order" do
2171
+ expect(docs).to eq([ new_order, rolling_stones ])
2172
+ end
2173
+ end
2174
+
2175
+ context 'when the criteria has a collation' do
2176
+ min_server_version '3.4'
2177
+
2178
+ let(:criteria) do
2179
+ Band.where(name: "DEPECHE MODE").collation(locale: 'en_US', strength: 2)
2180
+ end
2181
+
2182
+ it "returns the first matching document" do
2183
+ expect(context.last(1)).to eq([ depeche_mode ])
2184
+ end
2185
+ end
2186
+ end
2187
+
2188
+ context "when the context is cached" do
2189
+
2190
+ let(:context) do
2191
+ described_class.new(criteria)
2192
+ end
2193
+
2194
+ context "when the whole context is loaded" do
2195
+
2196
+ before do
2197
+ context.to_a
2198
+ end
2199
+
2200
+ context "when all of the documents are cached" do
2201
+
2202
+ let(:criteria) do
2203
+ Band.all.cache
2204
+ end
2205
+
2206
+ context "when requesting all of the documents" do
2207
+
2208
+ let(:docs) do
2209
+ context.last(3)
2210
+ end
2211
+
2212
+ it "returns all of the documents without touching the database" do
2213
+ expect(context).to receive(:view).never
2214
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2215
+ end
2216
+ end
2217
+
2218
+ context "when requesting fewer than all of the documents" do
2219
+
2220
+ let(:docs) do
2221
+ context.last(2)
2222
+ end
2223
+
2224
+ it "returns all of the documents without touching the database" do
2225
+ expect(context).to receive(:view).never
2226
+ expect(docs).to eq([ new_order, rolling_stones ])
2227
+ end
2228
+ end
2229
+ end
2230
+
2231
+ context "when only one document is cached" do
2232
+
2233
+ let(:criteria) do
2234
+ Band.where(name: "Depeche Mode").cache
2235
+ end
2236
+
2237
+ context "when requesting one document" do
2238
+
2239
+ let(:docs) do
2240
+ context.last(1)
2241
+ end
2242
+
2243
+ it "returns one document without touching the database" do
2244
+ expect(context).to receive(:view).never
2245
+ expect(docs).to eq([ depeche_mode ])
2246
+ end
2247
+ end
2248
+ end
2249
+ end
2250
+
2251
+ context "when the last method was called before" do
2252
+
2253
+ let(:context) do
2254
+ described_class.new(criteria)
2255
+ end
2256
+
2257
+ let(:criteria) do
2258
+ Band.all.cache
2259
+ end
2260
+
2261
+ before do
2262
+ context.last(before_limit)
2263
+ end
2264
+
2265
+ let(:docs) do
2266
+ context.last(limit)
2267
+ end
2268
+
2269
+ context "when getting all of the documents before" do
2270
+ let(:before_limit) { 3 }
2271
+
2272
+ context "when getting all of the documents" do
2273
+ let(:limit) { 3 }
2274
+
2275
+ it "returns all documents without touching the database" do
2276
+ expect(context).to receive(:view).never
2277
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2278
+ end
2279
+ end
2280
+
2281
+ context "when getting fewer documents" do
2282
+ let(:limit) { 2 }
2283
+
2284
+ it "returns the correct documents without touching the database" do
2285
+ expect(context).to receive(:view).never
2286
+ expect(docs).to eq([ new_order, rolling_stones ])
2287
+ end
2288
+ end
2289
+ end
2290
+
2291
+ context "when getting fewer documents before" do
2292
+ let(:before_limit) { 2 }
2293
+
2294
+ context "when getting the same number of documents" do
2295
+ let(:limit) { 2 }
2296
+
2297
+ it "returns the correct documents without touching the database" do
2298
+ expect(context).to receive(:view).never
2299
+ expect(docs).to eq([ new_order, rolling_stones ])
2300
+ end
2301
+ end
2302
+
2303
+ context "when getting more documents" do
2304
+ let(:limit) { 3 }
2305
+
2306
+ it "returns the correct documents and touches the database" do
2307
+ expect(context).to receive(:view).twice.and_call_original
2308
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2309
+ end
2310
+ end
2311
+ end
2312
+
2313
+ context "when getting one document before" do
2314
+ let(:before_limit) { 1 }
2315
+
2316
+ context "when getting one document" do
2317
+ let(:limit) { 1 }
2318
+
2319
+ it "returns the correct documents without touching the database" do
2320
+ expect(context).to receive(:view).never
2321
+ expect(docs).to eq([ rolling_stones ])
2322
+ end
2323
+ end
2324
+
2325
+ context "when getting more than one document" do
2326
+ let(:limit) { 3 }
2327
+
2328
+ it "returns the correct documents and touches the database" do
2329
+ expect(context).to receive(:view).twice.and_call_original
2330
+ expect(docs).to eq([ depeche_mode, new_order, rolling_stones ])
2331
+ end
2332
+ end
2333
+ end
2334
+ end
2335
+ end
2336
+ end
2337
+
2338
+ context "when calling #last then #first" do
2339
+
2340
+ let(:context) do
2341
+ described_class.new(criteria)
2342
+ end
2343
+
2344
+ let(:criteria) do
2345
+ Band.all.cache
2346
+ end
2347
+
2348
+ before do
2349
+ context.last(before_limit)
2350
+ end
2351
+
2352
+ let(:docs) do
2353
+ context.first(limit)
2354
+ end
2355
+
2356
+ context "when getting one from the beginning and one from the end" do
2357
+ let(:before_limit) { 2 }
2358
+ let(:limit) { 1 }
2359
+
2360
+ it "hits the database" do
2361
+ expect(context).to receive(:view).exactly(3).times.and_call_original
2362
+ docs
2363
+ end
2364
+
2365
+ it "gets the correct document" do
2366
+ pending "MONGOID-5416"
2367
+ expect(docs).to eq([ depeche_mode ])
2368
+ end
2369
+ end
2370
+ end
2371
+
2372
+ context "when given an empty hash" do
2373
+ let(:context) { described_class.new(criteria) }
2374
+ let(:criteria) { Band.criteria }
2375
+ let(:docs) { context.last({}) }
2376
+
2377
+ it "behaves as if limit is nil" do
2378
+ expect(docs).to eq(rolling_stones)
2379
+ end
2380
+ end
2381
+ end
2382
+
2383
+ describe "#initialize" do
2384
+
2385
+ let(:criteria) do
2386
+ Band.where(name: "Depeche Mode").no_timeout
2387
+ end
2388
+
2389
+ let(:context) do
2390
+ described_class.new(criteria)
2391
+ end
2392
+
2393
+ it "sets the criteria" do
2394
+ expect(context.criteria).to eq(criteria)
2395
+ end
2396
+
2397
+ it "sets the klass" do
2398
+ expect(context.klass).to eq(Band)
2399
+ end
2400
+
2401
+ it "sets the view" do
2402
+ expect(context.view).to be_a(Mongo::Collection::View)
2403
+ end
2404
+
2405
+ it "sets the view selector" do
2406
+ expect(context.view.selector).to eq({ "name" => "Depeche Mode" })
1338
2407
  end
1339
2408
  end
1340
2409
 
@@ -1343,8 +2412,8 @@ describe Mongoid::Contextual::Mongo do
1343
2412
  describe "##{method}" do
1344
2413
 
1345
2414
  before do
1346
- Band.create(name: "Depeche Mode")
1347
- Band.create(name: "New Order")
2415
+ Band.create!(name: "Depeche Mode")
2416
+ Band.create!(name: "New Order")
1348
2417
  end
1349
2418
 
1350
2419
  context "when the criteria has a limit" do
@@ -1362,12 +2431,8 @@ describe Mongoid::Contextual::Mongo do
1362
2431
  end
1363
2432
 
1364
2433
  context "when calling more than once" do
1365
-
1366
- before do
1367
- expect(context.view).to receive(:count).once.and_return(2)
1368
- end
1369
-
1370
2434
  it "returns the cached value for subsequent calls" do
2435
+ expect(context.view).to receive(:count_documents).once.and_return(2)
1371
2436
  2.times { expect(context.send(method)).to eq(2) }
1372
2437
  end
1373
2438
  end
@@ -1376,10 +2441,10 @@ describe Mongoid::Contextual::Mongo do
1376
2441
 
1377
2442
  before do
1378
2443
  context.entries
1379
- expect(context.view).to receive(:count).once.and_return(2)
1380
2444
  end
1381
2445
 
1382
2446
  it "returns the cached value for all calls" do
2447
+ expect(context.view).to receive(:count_documents).once.and_return(2)
1383
2448
  expect(context.send(method)).to eq(2)
1384
2449
  end
1385
2450
 
@@ -1411,12 +2476,8 @@ describe Mongoid::Contextual::Mongo do
1411
2476
  end
1412
2477
 
1413
2478
  context "when calling more than once" do
1414
-
1415
- before do
1416
- expect(context.view).to receive(:count).once.and_return(1)
1417
- end
1418
-
1419
2479
  it "returns the cached value for subsequent calls" do
2480
+ expect(context.view).to receive(:count_documents).once.and_return(1)
1420
2481
  2.times { expect(context.send(method)).to eq(1) }
1421
2482
  end
1422
2483
  end
@@ -1425,10 +2486,10 @@ describe Mongoid::Contextual::Mongo do
1425
2486
 
1426
2487
  before do
1427
2488
  context.entries
1428
- expect(context.view).to receive(:count).once.and_return(1)
1429
2489
  end
1430
2490
 
1431
2491
  it "returns the cached value for all calls" do
2492
+ expect(context.view).to receive(:count_documents).once.and_return(1)
1432
2493
  expect(context.send(method)).to eq(1)
1433
2494
  end
1434
2495
 
@@ -1450,11 +2511,11 @@ describe Mongoid::Contextual::Mongo do
1450
2511
  describe "#limit" do
1451
2512
 
1452
2513
  let!(:depeche_mode) do
1453
- Band.create(name: "Depeche Mode")
2514
+ Band.create!(name: "Depeche Mode")
1454
2515
  end
1455
2516
 
1456
2517
  let!(:new_order) do
1457
- Band.create(name: "New Order")
2518
+ Band.create!(name: "New Order")
1458
2519
  end
1459
2520
 
1460
2521
  let(:criteria) do
@@ -1470,11 +2531,85 @@ describe Mongoid::Contextual::Mongo do
1470
2531
  end
1471
2532
  end
1472
2533
 
2534
+ describe "#take" do
2535
+
2536
+ let!(:depeche_mode) do
2537
+ Band.create!(name: "Depeche Mode")
2538
+ end
2539
+
2540
+ let!(:new_order) do
2541
+ Band.create!(name: "New Order")
2542
+ end
2543
+
2544
+ let!(:rolling_stones) do
2545
+ Band.create!(name: "The Rolling Stones")
2546
+ end
2547
+
2548
+ let(:criteria) do
2549
+ Band.all
2550
+ end
2551
+
2552
+ let(:context) do
2553
+ described_class.new(criteria)
2554
+ end
2555
+
2556
+ it "takes the correct number results" do
2557
+ expect(context.take(2)).to eq([ depeche_mode, new_order ])
2558
+ end
2559
+
2560
+ it "returns an array when passing 1" do
2561
+ expect(context.take(1)).to eq([ depeche_mode ])
2562
+ end
2563
+
2564
+ it "does not return an array when not passing an argument" do
2565
+ expect(context.take).to eq(depeche_mode)
2566
+ end
2567
+
2568
+ it "returns all the documents taking more than whats in the db" do
2569
+ expect(context.take(5)).to eq([ depeche_mode, new_order, rolling_stones ])
2570
+ end
2571
+ end
2572
+
2573
+ describe "#take!" do
2574
+
2575
+ let!(:depeche_mode) do
2576
+ Band.create!(name: "Depeche Mode")
2577
+ end
2578
+
2579
+ let!(:new_order) do
2580
+ Band.create!(name: "New Order")
2581
+ end
2582
+
2583
+ let!(:rolling_stones) do
2584
+ Band.create!(name: "The Rolling Stones")
2585
+ end
2586
+
2587
+ let(:criteria) do
2588
+ Band.all
2589
+ end
2590
+
2591
+ let(:context) do
2592
+ described_class.new(criteria)
2593
+ end
2594
+
2595
+ it "takes the first document" do
2596
+ expect(context.take!).to eq(depeche_mode)
2597
+ end
2598
+
2599
+ context "when there are no documents" do
2600
+ it "raises an error" do
2601
+ expect do
2602
+ Person.take!
2603
+ end.to raise_error(Mongoid::Errors::DocumentNotFound)
2604
+ end
2605
+ end
2606
+ end
2607
+
1473
2608
  describe "#map" do
1474
2609
 
1475
2610
  before do
1476
- Band.create(name: "Depeche Mode")
1477
- Band.create(name: "New Order")
2611
+ Band.create!(name: "Depeche Mode")
2612
+ Band.create!(name: "New Order")
1478
2613
  end
1479
2614
 
1480
2615
  let(:criteria) do
@@ -1487,7 +2622,8 @@ describe Mongoid::Contextual::Mongo do
1487
2622
 
1488
2623
  context "when passed the symbol field name" do
1489
2624
 
1490
- it "performs mapping" do
2625
+ it "performs mapping and warns" do
2626
+ expect(Mongoid::Warnings).to receive(:warn_map_field_deprecated)
1491
2627
  expect(context.map(:name)).to eq ["Depeche Mode", "New Order"]
1492
2628
  end
1493
2629
  end
@@ -1503,11 +2639,11 @@ describe Mongoid::Contextual::Mongo do
1503
2639
  describe "#map_reduce" do
1504
2640
 
1505
2641
  let!(:depeche_mode) do
1506
- Band.create(name: "Depeche Mode", likes: 200)
2642
+ Band.create!(name: "Depeche Mode", likes: 200)
1507
2643
  end
1508
2644
 
1509
2645
  let!(:tool) do
1510
- Band.create(name: "Tool", likes: 100)
2646
+ Band.create!(name: "Tool", likes: 100)
1511
2647
  end
1512
2648
 
1513
2649
  let(:map) do
@@ -1528,6 +2664,10 @@ describe Mongoid::Contextual::Mongo do
1528
2664
  }}
1529
2665
  end
1530
2666
 
2667
+ let(:ordered_results) do
2668
+ results['results'].sort_by { |doc| doc['_id'] }
2669
+ end
2670
+
1531
2671
  context "when no selection is provided" do
1532
2672
 
1533
2673
  let(:criteria) do
@@ -1559,36 +2699,40 @@ describe Mongoid::Contextual::Mongo do
1559
2699
  end
1560
2700
 
1561
2701
  it "contains the entire raw results" do
1562
- expect(results["results"]).to eq([
2702
+ expect(ordered_results).to eq([
1563
2703
  { "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
1564
2704
  { "_id" => "Tool", "value" => { "likes" => 100 }}
1565
2705
  ])
1566
2706
  end
1567
2707
 
1568
- it "contains the execution time" do
1569
- expect(results.time).to_not be_nil
1570
- end
2708
+ context 'when statistics are available' do
2709
+ max_server_version '4.2'
1571
2710
 
1572
- it "contains the count statistics" do
1573
- expect(results["counts"]).to eq({
1574
- "input" => 2, "emit" => 2, "reduce" => 0, "output" => 2
1575
- })
1576
- end
2711
+ it "contains the execution time" do
2712
+ expect(results.time).to_not be_nil
2713
+ end
1577
2714
 
1578
- it "contains the input count" do
1579
- expect(results.input).to eq(2)
1580
- end
2715
+ it "contains the count statistics" do
2716
+ expect(results["counts"]).to eq({
2717
+ "input" => 2, "emit" => 2, "reduce" => 0, "output" => 2
2718
+ })
2719
+ end
1581
2720
 
1582
- it "contains the emitted count" do
1583
- expect(results.emitted).to eq(2)
1584
- end
2721
+ it "contains the input count" do
2722
+ expect(results.input).to eq(2)
2723
+ end
1585
2724
 
1586
- it "contains the reduced count" do
1587
- expect(results.reduced).to eq(0)
1588
- end
2725
+ it "contains the emitted count" do
2726
+ expect(results.emitted).to eq(2)
2727
+ end
1589
2728
 
1590
- it "contains the output count" do
1591
- expect(results.output).to eq(2)
2729
+ it "contains the reduced count" do
2730
+ expect(results.reduced).to eq(0)
2731
+ end
2732
+
2733
+ it "contains the output count" do
2734
+ expect(results.output).to eq(2)
2735
+ end
1592
2736
  end
1593
2737
  end
1594
2738
 
@@ -1617,35 +2761,39 @@ describe Mongoid::Contextual::Mongo do
1617
2761
  end
1618
2762
 
1619
2763
  it "contains the entire raw results" do
1620
- expect(results["results"]).to eq([
2764
+ expect(ordered_results).to eq([
1621
2765
  { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
1622
2766
  ])
1623
2767
  end
1624
2768
 
1625
- it "contains the execution time" do
1626
- expect(results.time).to_not be_nil
1627
- end
2769
+ context 'when statistics are available' do
2770
+ max_server_version '4.2'
1628
2771
 
1629
- it "contains the count statistics" do
1630
- expect(results["counts"]).to eq({
1631
- "input" => 1, "emit" => 1, "reduce" => 0, "output" => 1
1632
- })
1633
- end
2772
+ it "contains the execution time" do
2773
+ expect(results.time).to_not be_nil
2774
+ end
1634
2775
 
1635
- it "contains the input count" do
1636
- expect(results.input).to eq(1)
1637
- end
2776
+ it "contains the count statistics" do
2777
+ expect(results["counts"]).to eq({
2778
+ "input" => 1, "emit" => 1, "reduce" => 0, "output" => 1
2779
+ })
2780
+ end
1638
2781
 
1639
- it "contains the emitted count" do
1640
- expect(results.emitted).to eq(1)
1641
- end
2782
+ it "contains the input count" do
2783
+ expect(results.input).to eq(1)
2784
+ end
1642
2785
 
1643
- it "contains the reduced count" do
1644
- expect(results.reduced).to eq(0)
1645
- end
2786
+ it "contains the emitted count" do
2787
+ expect(results.emitted).to eq(1)
2788
+ end
2789
+
2790
+ it "contains the reduced count" do
2791
+ expect(results.reduced).to eq(0)
2792
+ end
1646
2793
 
1647
- it "contains the output count" do
1648
- expect(results.output).to eq(1)
2794
+ it "contains the output count" do
2795
+ expect(results.output).to eq(1)
2796
+ end
1649
2797
  end
1650
2798
  end
1651
2799
 
@@ -1685,7 +2833,7 @@ describe Mongoid::Contextual::Mongo do
1685
2833
  end
1686
2834
 
1687
2835
  it "contains the entire raw results" do
1688
- expect(results["results"]).to eq([
2836
+ expect(ordered_results).to eq([
1689
2837
  { "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
1690
2838
  { "_id" => "Tool", "value" => { "likes" => 100 }}
1691
2839
  ])
@@ -1693,6 +2841,9 @@ describe Mongoid::Contextual::Mongo do
1693
2841
  end
1694
2842
 
1695
2843
  context "when limiting is provided" do
2844
+ # map/reduce with limit is not supported on sharded clusters:
2845
+ # https://jira.mongodb.org/browse/SERVER-2099
2846
+ require_topology :single, :replica_set
1696
2847
 
1697
2848
  let(:criteria) do
1698
2849
  Band.limit(1)
@@ -1799,6 +2950,8 @@ describe Mongoid::Contextual::Mongo do
1799
2950
  end
1800
2951
 
1801
2952
  context "when the output specifies a different db" do
2953
+ # Limit is not supported in sharded clusters
2954
+ require_topology :single, :replica_set
1802
2955
 
1803
2956
  let(:criteria) do
1804
2957
  Band.limit(1)
@@ -1915,11 +3068,11 @@ describe Mongoid::Contextual::Mongo do
1915
3068
  describe "#skip" do
1916
3069
 
1917
3070
  let!(:depeche_mode) do
1918
- Band.create(name: "Depeche Mode")
3071
+ Band.create!(name: "Depeche Mode")
1919
3072
  end
1920
3073
 
1921
3074
  let!(:new_order) do
1922
- Band.create(name: "New Order")
3075
+ Band.create!(name: "New Order")
1923
3076
  end
1924
3077
 
1925
3078
  let(:criteria) do
@@ -1938,11 +3091,11 @@ describe Mongoid::Contextual::Mongo do
1938
3091
  describe "#sort" do
1939
3092
 
1940
3093
  let!(:depeche_mode) do
1941
- Band.create(name: "Depeche Mode")
3094
+ Band.create!(name: "Depeche Mode")
1942
3095
  end
1943
3096
 
1944
3097
  let!(:new_order) do
1945
- Band.create(name: "New Order")
3098
+ Band.create!(name: "New Order")
1946
3099
  end
1947
3100
 
1948
3101
  let(:criteria) do
@@ -1981,11 +3134,11 @@ describe Mongoid::Contextual::Mongo do
1981
3134
  describe "#update" do
1982
3135
 
1983
3136
  let!(:depeche_mode) do
1984
- Band.create(name: "Depeche Mode")
3137
+ Band.create!(name: "Depeche Mode")
1985
3138
  end
1986
3139
 
1987
3140
  let!(:new_order) do
1988
- Band.create(name: "New Order")
3141
+ Band.create!(name: "New Order")
1989
3142
  end
1990
3143
 
1991
3144
  let(:criteria) do
@@ -1999,11 +3152,11 @@ describe Mongoid::Contextual::Mongo do
1999
3152
  context "when adding an element to a HABTM set" do
2000
3153
 
2001
3154
  let(:person) do
2002
- Person.create
3155
+ Person.create!
2003
3156
  end
2004
3157
 
2005
3158
  let(:preference) do
2006
- Preference.create
3159
+ Preference.create!
2007
3160
  end
2008
3161
 
2009
3162
  before do
@@ -2133,7 +3286,8 @@ describe Mongoid::Contextual::Mongo do
2133
3286
  end
2134
3287
  end
2135
3288
 
2136
- context 'when provided array filters', if: array_filters_supported? do
3289
+ context 'when provided array filters' do
3290
+ min_server_version '3.6'
2137
3291
 
2138
3292
  before do
2139
3293
  Band.delete_all
@@ -2141,12 +3295,12 @@ describe Mongoid::Contextual::Mongo do
2141
3295
  b.labels << Label.new(name: 'Warner')
2142
3296
  b.labels << Label.new(name: 'Sony')
2143
3297
  b.labels << Label.new(name: 'Cbs')
2144
- b.save
3298
+ b.save!
2145
3299
 
2146
3300
  b = Band.new(name: 'FKA Twigs')
2147
3301
  b.labels << Label.new(name: 'Warner')
2148
3302
  b.labels << Label.new(name: 'Cbs')
2149
- b.save
3303
+ b.save!
2150
3304
  end
2151
3305
 
2152
3306
 
@@ -2172,11 +3326,11 @@ describe Mongoid::Contextual::Mongo do
2172
3326
  describe "#update_all" do
2173
3327
 
2174
3328
  let!(:depeche_mode) do
2175
- Band.create(name: "Depeche Mode", origin: "Essex")
3329
+ Band.create!(name: "Depeche Mode", origin: "Essex")
2176
3330
  end
2177
3331
 
2178
3332
  let!(:new_order) do
2179
- Band.create(name: "New Order")
3333
+ Band.create!(name: "New Order")
2180
3334
  end
2181
3335
 
2182
3336
  let(:criteria) do
@@ -2305,7 +3459,8 @@ describe Mongoid::Contextual::Mongo do
2305
3459
  end
2306
3460
  end
2307
3461
 
2308
- context 'when provided array filters', if: array_filters_supported? do
3462
+ context 'when provided array filters' do
3463
+ min_server_version '3.6'
2309
3464
 
2310
3465
  before do
2311
3466
  Band.delete_all
@@ -2313,12 +3468,12 @@ describe Mongoid::Contextual::Mongo do
2313
3468
  b.labels << Label.new(name: 'Warner')
2314
3469
  b.labels << Label.new(name: 'Sony')
2315
3470
  b.labels << Label.new(name: 'Cbs')
2316
- b.save
3471
+ b.save!
2317
3472
 
2318
3473
  b = Band.new(name: 'FKA Twigs')
2319
3474
  b.labels << Label.new(name: 'Warner')
2320
3475
  b.labels << Label.new(name: 'Cbs')
2321
- b.save
3476
+ b.save!
2322
3477
  end
2323
3478
 
2324
3479
 
@@ -2328,7 +3483,7 @@ describe Mongoid::Contextual::Mongo do
2328
3483
 
2329
3484
  let!(:update) do
2330
3485
  context.update_all({ '$set' => { 'labels.$[i].name' => 'Sony' } },
2331
- array_filters: [{ 'i.name' => 'Cbs' }])
3486
+ array_filters: [{ 'i.name' => 'Cbs' }])
2332
3487
  end
2333
3488
 
2334
3489
  it 'applies the array filters' do
@@ -2343,7 +3498,7 @@ describe Mongoid::Contextual::Mongo do
2343
3498
 
2344
3499
  describe '#pipeline' do
2345
3500
 
2346
- context 'when the criteria has a selector', if: non_legacy_server? do
3501
+ context 'when the criteria has a selector' do
2347
3502
 
2348
3503
  before do
2349
3504
  Artist.index(name: "text")