mongoid-pre 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. data/.gitignore +6 -0
  2. data/.watchr +24 -0
  3. data/MIT_LICENSE +20 -0
  4. data/README.rdoc +49 -0
  5. data/Rakefile +52 -0
  6. data/VERSION +1 -0
  7. data/caliper.yml +4 -0
  8. data/lib/mongoid.rb +135 -0
  9. data/lib/mongoid/associations.rb +263 -0
  10. data/lib/mongoid/associations/belongs_to_related.rb +59 -0
  11. data/lib/mongoid/associations/embedded_in.rb +64 -0
  12. data/lib/mongoid/associations/embeds_many.rb +193 -0
  13. data/lib/mongoid/associations/embeds_one.rb +95 -0
  14. data/lib/mongoid/associations/has_many_related.rb +133 -0
  15. data/lib/mongoid/associations/has_one_related.rb +81 -0
  16. data/lib/mongoid/associations/meta_data.rb +28 -0
  17. data/lib/mongoid/associations/options.rb +52 -0
  18. data/lib/mongoid/associations/proxy.rb +31 -0
  19. data/lib/mongoid/attributes.rb +185 -0
  20. data/lib/mongoid/callbacks.rb +18 -0
  21. data/lib/mongoid/collection.rb +119 -0
  22. data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
  23. data/lib/mongoid/collections/master.rb +28 -0
  24. data/lib/mongoid/collections/mimic.rb +46 -0
  25. data/lib/mongoid/collections/operations.rb +41 -0
  26. data/lib/mongoid/collections/slaves.rb +44 -0
  27. data/lib/mongoid/commands.rb +161 -0
  28. data/lib/mongoid/commands/create.rb +19 -0
  29. data/lib/mongoid/commands/delete.rb +16 -0
  30. data/lib/mongoid/commands/delete_all.rb +25 -0
  31. data/lib/mongoid/commands/deletion.rb +18 -0
  32. data/lib/mongoid/commands/destroy.rb +17 -0
  33. data/lib/mongoid/commands/destroy_all.rb +25 -0
  34. data/lib/mongoid/commands/save.rb +30 -0
  35. data/lib/mongoid/components.rb +31 -0
  36. data/lib/mongoid/config.rb +86 -0
  37. data/lib/mongoid/contexts.rb +25 -0
  38. data/lib/mongoid/contexts/enumerable.rb +151 -0
  39. data/lib/mongoid/contexts/ids.rb +25 -0
  40. data/lib/mongoid/contexts/mongo.rb +285 -0
  41. data/lib/mongoid/contexts/paging.rb +42 -0
  42. data/lib/mongoid/criteria.rb +239 -0
  43. data/lib/mongoid/criterion/complex.rb +21 -0
  44. data/lib/mongoid/criterion/exclusion.rb +65 -0
  45. data/lib/mongoid/criterion/inclusion.rb +93 -0
  46. data/lib/mongoid/criterion/optional.rb +136 -0
  47. data/lib/mongoid/cursor.rb +82 -0
  48. data/lib/mongoid/deprecation.rb +22 -0
  49. data/lib/mongoid/dirty.rb +203 -0
  50. data/lib/mongoid/document.rb +306 -0
  51. data/lib/mongoid/errors.rb +77 -0
  52. data/lib/mongoid/extensions.rb +99 -0
  53. data/lib/mongoid/extensions/array/accessors.rb +17 -0
  54. data/lib/mongoid/extensions/array/aliasing.rb +4 -0
  55. data/lib/mongoid/extensions/array/assimilation.rb +26 -0
  56. data/lib/mongoid/extensions/array/conversions.rb +27 -0
  57. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  58. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  59. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  60. data/lib/mongoid/extensions/boolean/conversions.rb +16 -0
  61. data/lib/mongoid/extensions/date/conversions.rb +15 -0
  62. data/lib/mongoid/extensions/datetime/conversions.rb +17 -0
  63. data/lib/mongoid/extensions/float/conversions.rb +16 -0
  64. data/lib/mongoid/extensions/hash/accessors.rb +38 -0
  65. data/lib/mongoid/extensions/hash/assimilation.rb +30 -0
  66. data/lib/mongoid/extensions/hash/conversions.rb +15 -0
  67. data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
  68. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  69. data/lib/mongoid/extensions/integer/conversions.rb +16 -0
  70. data/lib/mongoid/extensions/nil/assimilation.rb +13 -0
  71. data/lib/mongoid/extensions/object/conversions.rb +27 -0
  72. data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
  73. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  74. data/lib/mongoid/extensions/string/conversions.rb +15 -0
  75. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  76. data/lib/mongoid/extensions/symbol/inflections.rb +36 -0
  77. data/lib/mongoid/extensions/time/conversions.rb +18 -0
  78. data/lib/mongoid/extras.rb +61 -0
  79. data/lib/mongoid/factory.rb +19 -0
  80. data/lib/mongoid/field.rb +52 -0
  81. data/lib/mongoid/fields.rb +62 -0
  82. data/lib/mongoid/finders.rb +136 -0
  83. data/lib/mongoid/identity.rb +39 -0
  84. data/lib/mongoid/indexes.rb +27 -0
  85. data/lib/mongoid/javascript.rb +21 -0
  86. data/lib/mongoid/javascript/functions.yml +37 -0
  87. data/lib/mongoid/matchers.rb +36 -0
  88. data/lib/mongoid/matchers/all.rb +11 -0
  89. data/lib/mongoid/matchers/default.rb +26 -0
  90. data/lib/mongoid/matchers/exists.rb +13 -0
  91. data/lib/mongoid/matchers/gt.rb +11 -0
  92. data/lib/mongoid/matchers/gte.rb +11 -0
  93. data/lib/mongoid/matchers/in.rb +11 -0
  94. data/lib/mongoid/matchers/lt.rb +11 -0
  95. data/lib/mongoid/matchers/lte.rb +11 -0
  96. data/lib/mongoid/matchers/ne.rb +11 -0
  97. data/lib/mongoid/matchers/nin.rb +11 -0
  98. data/lib/mongoid/matchers/size.rb +11 -0
  99. data/lib/mongoid/memoization.rb +27 -0
  100. data/lib/mongoid/named_scope.rb +42 -0
  101. data/lib/mongoid/observable.rb +30 -0
  102. data/lib/mongoid/paths.rb +54 -0
  103. data/lib/mongoid/persistence.rb +27 -0
  104. data/lib/mongoid/persistence/command.rb +20 -0
  105. data/lib/mongoid/persistence/insert.rb +71 -0
  106. data/lib/mongoid/persistence/update.rb +78 -0
  107. data/lib/mongoid/scope.rb +75 -0
  108. data/lib/mongoid/state.rb +32 -0
  109. data/lib/mongoid/timestamps.rb +27 -0
  110. data/lib/mongoid/validations.rb +51 -0
  111. data/lib/mongoid/validations/associated.rb +32 -0
  112. data/lib/mongoid/validations/locale/en.yml +4 -0
  113. data/lib/mongoid/validations/uniqueness.rb +22 -0
  114. data/lib/mongoid/versioning.rb +26 -0
  115. data/mongoid.gemspec +413 -0
  116. data/perf/benchmark.rb +77 -0
  117. data/spec/integration/mongoid/associations_spec.rb +340 -0
  118. data/spec/integration/mongoid/attributes_spec.rb +22 -0
  119. data/spec/integration/mongoid/commands_spec.rb +230 -0
  120. data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
  121. data/spec/integration/mongoid/criteria_spec.rb +272 -0
  122. data/spec/integration/mongoid/dirty_spec.rb +70 -0
  123. data/spec/integration/mongoid/document_spec.rb +650 -0
  124. data/spec/integration/mongoid/extensions_spec.rb +22 -0
  125. data/spec/integration/mongoid/finders_spec.rb +119 -0
  126. data/spec/integration/mongoid/inheritance_spec.rb +137 -0
  127. data/spec/integration/mongoid/named_scope_spec.rb +46 -0
  128. data/spec/integration/mongoid/persistence/update_spec.rb +46 -0
  129. data/spec/models/address.rb +39 -0
  130. data/spec/models/animal.rb +6 -0
  131. data/spec/models/callbacks.rb +18 -0
  132. data/spec/models/comment.rb +8 -0
  133. data/spec/models/country_code.rb +6 -0
  134. data/spec/models/employer.rb +5 -0
  135. data/spec/models/game.rb +7 -0
  136. data/spec/models/inheritance.rb +56 -0
  137. data/spec/models/location.rb +5 -0
  138. data/spec/models/mixed_drink.rb +4 -0
  139. data/spec/models/name.rb +13 -0
  140. data/spec/models/namespacing.rb +11 -0
  141. data/spec/models/patient.rb +6 -0
  142. data/spec/models/person.rb +99 -0
  143. data/spec/models/pet.rb +7 -0
  144. data/spec/models/pet_owner.rb +6 -0
  145. data/spec/models/phone.rb +7 -0
  146. data/spec/models/post.rb +15 -0
  147. data/spec/models/translation.rb +5 -0
  148. data/spec/models/vet_visit.rb +5 -0
  149. data/spec/spec.opts +3 -0
  150. data/spec/spec_helper.rb +31 -0
  151. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +145 -0
  152. data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
  153. data/spec/unit/mongoid/associations/embeds_many_spec.rb +516 -0
  154. data/spec/unit/mongoid/associations/embeds_one_spec.rb +282 -0
  155. data/spec/unit/mongoid/associations/has_many_related_spec.rb +418 -0
  156. data/spec/unit/mongoid/associations/has_one_related_spec.rb +179 -0
  157. data/spec/unit/mongoid/associations/meta_data_spec.rb +88 -0
  158. data/spec/unit/mongoid/associations/options_spec.rb +192 -0
  159. data/spec/unit/mongoid/associations_spec.rb +595 -0
  160. data/spec/unit/mongoid/attributes_spec.rb +507 -0
  161. data/spec/unit/mongoid/callbacks_spec.rb +55 -0
  162. data/spec/unit/mongoid/collection_spec.rb +187 -0
  163. data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
  164. data/spec/unit/mongoid/collections/master_spec.rb +41 -0
  165. data/spec/unit/mongoid/collections/mimic_spec.rb +43 -0
  166. data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
  167. data/spec/unit/mongoid/commands/create_spec.rb +31 -0
  168. data/spec/unit/mongoid/commands/delete_all_spec.rb +59 -0
  169. data/spec/unit/mongoid/commands/delete_spec.rb +38 -0
  170. data/spec/unit/mongoid/commands/destroy_all_spec.rb +21 -0
  171. data/spec/unit/mongoid/commands/destroy_spec.rb +51 -0
  172. data/spec/unit/mongoid/commands/save_spec.rb +107 -0
  173. data/spec/unit/mongoid/commands_spec.rb +270 -0
  174. data/spec/unit/mongoid/config_spec.rb +176 -0
  175. data/spec/unit/mongoid/contexts/enumerable_spec.rb +421 -0
  176. data/spec/unit/mongoid/contexts/mongo_spec.rb +682 -0
  177. data/spec/unit/mongoid/contexts_spec.rb +25 -0
  178. data/spec/unit/mongoid/criteria_spec.rb +824 -0
  179. data/spec/unit/mongoid/criterion/complex_spec.rb +19 -0
  180. data/spec/unit/mongoid/criterion/exclusion_spec.rb +91 -0
  181. data/spec/unit/mongoid/criterion/inclusion_spec.rb +219 -0
  182. data/spec/unit/mongoid/criterion/optional_spec.rb +319 -0
  183. data/spec/unit/mongoid/cursor_spec.rb +74 -0
  184. data/spec/unit/mongoid/deprecation_spec.rb +24 -0
  185. data/spec/unit/mongoid/dirty_spec.rb +286 -0
  186. data/spec/unit/mongoid/document_spec.rb +818 -0
  187. data/spec/unit/mongoid/errors_spec.rb +103 -0
  188. data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
  189. data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
  190. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +35 -0
  191. data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
  192. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +22 -0
  193. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
  194. data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
  195. data/spec/unit/mongoid/extensions/date/conversions_spec.rb +102 -0
  196. data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +67 -0
  197. data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
  198. data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
  199. data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +46 -0
  200. data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +21 -0
  201. data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
  202. data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
  203. data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
  204. data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +24 -0
  205. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +57 -0
  206. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
  207. data/spec/unit/mongoid/extensions/string/conversions_spec.rb +17 -0
  208. data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
  209. data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +91 -0
  210. data/spec/unit/mongoid/extensions/time/conversions_spec.rb +70 -0
  211. data/spec/unit/mongoid/extras_spec.rb +102 -0
  212. data/spec/unit/mongoid/factory_spec.rb +31 -0
  213. data/spec/unit/mongoid/field_spec.rb +143 -0
  214. data/spec/unit/mongoid/fields_spec.rb +181 -0
  215. data/spec/unit/mongoid/finders_spec.rb +404 -0
  216. data/spec/unit/mongoid/identity_spec.rb +109 -0
  217. data/spec/unit/mongoid/indexes_spec.rb +93 -0
  218. data/spec/unit/mongoid/javascript_spec.rb +48 -0
  219. data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
  220. data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
  221. data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
  222. data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
  223. data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
  224. data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
  225. data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
  226. data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
  227. data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
  228. data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
  229. data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
  230. data/spec/unit/mongoid/matchers_spec.rb +329 -0
  231. data/spec/unit/mongoid/memoization_spec.rb +75 -0
  232. data/spec/unit/mongoid/named_scope_spec.rb +123 -0
  233. data/spec/unit/mongoid/observable_spec.rb +46 -0
  234. data/spec/unit/mongoid/paths_spec.rb +124 -0
  235. data/spec/unit/mongoid/persistence/insert_spec.rb +175 -0
  236. data/spec/unit/mongoid/persistence/update_spec.rb +148 -0
  237. data/spec/unit/mongoid/persistence_spec.rb +40 -0
  238. data/spec/unit/mongoid/scope_spec.rb +240 -0
  239. data/spec/unit/mongoid/state_spec.rb +83 -0
  240. data/spec/unit/mongoid/timestamps_spec.rb +25 -0
  241. data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
  242. data/spec/unit/mongoid/validations/uniqueness_spec.rb +47 -0
  243. data/spec/unit/mongoid/validations_spec.rb +190 -0
  244. data/spec/unit/mongoid/versioning_spec.rb +41 -0
  245. data/spec/unit/mongoid_spec.rb +46 -0
  246. metadata +476 -0
@@ -0,0 +1,74 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Cursor do
4
+
5
+ let(:collection) do
6
+ stub.quacks_like(Mongoid::Collection.allocate)
7
+ end
8
+
9
+ let(:proxy) do
10
+ stub.quacks_like(Mongo::Cursor.allocate)
11
+ end
12
+
13
+ let(:cursor) do
14
+ Mongoid::Cursor.new(Person, collection, proxy)
15
+ end
16
+
17
+ (Mongoid::Cursor::OPERATIONS - [ :timeout ]).each do |name|
18
+
19
+ describe "##{name}" do
20
+
21
+ before do
22
+ proxy.expects(name)
23
+ end
24
+
25
+ it "delegates to the proxy" do
26
+ cursor.send(name)
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "#collection" do
32
+
33
+ it "returns the mongoid collection" do
34
+ cursor.collection.should == collection
35
+ end
36
+ end
37
+
38
+ describe "#each" do
39
+
40
+ before do
41
+ proxy.expects(:each).yields({ "_type" => "Person" })
42
+ end
43
+
44
+ it "yields to the next document" do
45
+ cursor.each do |doc|
46
+ doc.attributes.except(:_id).should == Person.new.attributes.except(:_id)
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "#next_document" do
52
+
53
+ before do
54
+ proxy.expects(:next_document).returns({ "_type" => "Person" })
55
+ end
56
+
57
+ it "returns the next document from the proxied cursor" do
58
+ doc = cursor.next_document
59
+ doc.attributes.except(:_id).should == Person.new.attributes.except(:_id)
60
+ end
61
+ end
62
+
63
+ describe "#to_a" do
64
+
65
+ before do
66
+ proxy.expects(:to_a).returns([{ "_type" => "Person" }])
67
+ end
68
+
69
+ it "converts the proxy cursor to an array of documents" do
70
+ docs = cursor.to_a
71
+ docs[0].attributes.except(:_id).should == Person.new.attributes.except(:_id)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Deprecation do
4
+
5
+ let(:logger) do
6
+ stub.quacks_like(Logger.allocate)
7
+ end
8
+
9
+ before do
10
+ Logger.expects(:new).with($stdout).returns(logger)
11
+ end
12
+
13
+ describe "#alert" do
14
+
15
+ let(:deprecation) do
16
+ Mongoid::Deprecation.instance
17
+ end
18
+
19
+ it "calls warn on the memoized logger" do
20
+ logger.expects(:warn).with("Deprecation: testing")
21
+ deprecation.alert("testing")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,286 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Dirty do
4
+
5
+ describe "#attribute_change" do
6
+
7
+ context "when the attribute has changed" do
8
+
9
+ before do
10
+ @person = Person.new(:title => "Grand Poobah")
11
+ @person.title = "Captain Obvious"
12
+ end
13
+
14
+ it "returns an array of the old value and new value" do
15
+ @person.attribute_change("title").should ==
16
+ [ "Grand Poobah", "Captain Obvious" ]
17
+ end
18
+
19
+ it "allows access via (attribute)_change" do
20
+ @person.title_change.should ==
21
+ [ "Grand Poobah", "Captain Obvious" ]
22
+ end
23
+ end
24
+
25
+ context "when the attribute has not changed" do
26
+
27
+ before do
28
+ @person = Person.new(:title => "Grand Poobah")
29
+ end
30
+
31
+ it "returns an empty array" do
32
+ @person.attribute_change("title").should be_nil
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "#attribute_changed?" do
38
+
39
+ context "when the attribute has changed" do
40
+
41
+ before do
42
+ @person = Person.new(:title => "Grand Poobah")
43
+ @person.title = "Captain Obvious"
44
+ end
45
+
46
+ it "returns true" do
47
+ @person.attribute_changed?("title").should == true
48
+ end
49
+
50
+ it "allows access via (attribute)_changed?" do
51
+ @person.title_changed?.should == true
52
+ end
53
+ end
54
+
55
+ context "when the attribute has not changed" do
56
+
57
+ before do
58
+ @person = Person.new(:title => "Grand Poobah")
59
+ end
60
+
61
+ it "returns false" do
62
+ @person.attribute_changed?("title").should == false
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#attribute_was" do
68
+
69
+ context "when the attribute has changed" do
70
+
71
+ before do
72
+ @person = Person.new(:title => "Grand Poobah")
73
+ @person.title = "Captain Obvious"
74
+ end
75
+
76
+ it "returns the old value" do
77
+ @person.attribute_was("title").should == "Grand Poobah"
78
+ end
79
+
80
+ it "allows access via (attribute)_was" do
81
+ @person.title_was.should == "Grand Poobah"
82
+ end
83
+ end
84
+
85
+ context "when the attribute has not changed" do
86
+
87
+ before do
88
+ @person = Person.new(:title => "Grand Poobah")
89
+ end
90
+
91
+ it "returns nil" do
92
+ @person.attribute_was("title").should be_nil
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "#changed" do
98
+
99
+ context "when the document has changed" do
100
+
101
+ before do
102
+ @person = Person.new(:title => "Grand Poobah")
103
+ @person.title = "Captain Obvious"
104
+ end
105
+
106
+ it "returns an array of changed field names" do
107
+ @person.changed.should == [ "title" ]
108
+ end
109
+ end
110
+
111
+ context "when the document has not changed" do
112
+
113
+ before do
114
+ @person = Person.new(:title => "Grand Poobah")
115
+ end
116
+
117
+ it "returns an empty array" do
118
+ @person.changed.should == []
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "#changed?" do
124
+
125
+ context "when the document has changed" do
126
+
127
+ before do
128
+ @person = Person.new(:title => "Grand Poobah")
129
+ @person.title = "Captain Obvious"
130
+ end
131
+
132
+ it "returns true" do
133
+ @person.should be_changed
134
+ end
135
+ end
136
+
137
+ context "when the document has not changed" do
138
+
139
+ before do
140
+ @person = Person.new(:title => "Grand Poobah")
141
+ end
142
+
143
+ it "returns false" do
144
+ @person.should_not be_changed
145
+ end
146
+ end
147
+ end
148
+
149
+ describe "#changes" do
150
+
151
+ context "when the document has changed" do
152
+
153
+ before do
154
+ @person = Person.new(:title => "Grand Poobah")
155
+ @person.title = "Captain Obvious"
156
+ end
157
+
158
+ it "returns a hash of changes" do
159
+ @person.changes.should ==
160
+ { "title" => [ "Grand Poobah", "Captain Obvious" ] }
161
+ end
162
+ end
163
+
164
+ context "when the document has not changed" do
165
+
166
+ before do
167
+ @person = Person.new(:title => "Grand Poobah")
168
+ end
169
+
170
+ it "returns an empty hash" do
171
+ @person.changes.should == {}
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "#setters" do
177
+
178
+ context "when the document has changed" do
179
+
180
+ context "when the document is a root document" do
181
+
182
+ before do
183
+ @person = Person.new(:title => "Grand Poobah")
184
+ @person.title = "Captain Obvious"
185
+ end
186
+
187
+ it "returns a hash of field names and new values" do
188
+ @person.setters.should ==
189
+ { "title" => "Captain Obvious" }
190
+ end
191
+ end
192
+
193
+ context "when the document is embedded" do
194
+
195
+ before do
196
+ @person = Person.new(:title => "Grand Poobah")
197
+ @address = Address.new(:street => "Oxford St")
198
+ @person.addresses << @address
199
+ @address.street = "Bond St"
200
+ end
201
+
202
+ it "returns a hash of field names and new values" do
203
+ @address.setters.should ==
204
+ { "addresses.$.street" => "Bond St" }
205
+ end
206
+ end
207
+ end
208
+
209
+ context "when the document has not changed" do
210
+
211
+ before do
212
+ @person = Person.new(:title => "Grand Poobah")
213
+ end
214
+
215
+ it "returns an empty hash" do
216
+ @person.setters.should == {}
217
+ end
218
+ end
219
+ end
220
+
221
+ describe "#previous_changes" do
222
+
223
+ before do
224
+ @person = Person.new(:title => "Grand Poobah")
225
+ @person.title = "Captain Obvious"
226
+ end
227
+
228
+ context "when the document has been saved" do
229
+
230
+ before do
231
+ @person.collection.expects(:save).returns(true)
232
+ @person.save!
233
+ end
234
+
235
+ it "returns the changes before the save" do
236
+ @person.previous_changes["title"].should ==
237
+ [ "Grand Poobah", "Captain Obvious" ]
238
+ end
239
+ end
240
+
241
+ context "when the document has not been saved" do
242
+
243
+ it "returns an empty hash" do
244
+ @person.previous_changes.should == {}
245
+ end
246
+ end
247
+ end
248
+
249
+ describe "#reset_attribute!" do
250
+
251
+ context "when the attribute has changed" do
252
+
253
+ before do
254
+ @person = Person.new(:title => "Grand Poobah")
255
+ @person.title = "Captain Obvious"
256
+ end
257
+
258
+ it "resets the value to the original" do
259
+ @person.reset_attribute!("title")
260
+ @person.title.should == "Grand Poobah"
261
+ end
262
+
263
+ it "allows access via reset_(attribute)!" do
264
+ @person.reset_title!
265
+ @person.title.should == "Grand Poobah"
266
+ end
267
+
268
+ it "removes the field from the changes" do
269
+ @person.reset_title!
270
+ @person.changed.should == []
271
+ end
272
+ end
273
+
274
+ context "when the attribute has not changed" do
275
+
276
+ before do
277
+ @person = Person.new(:title => "Grand Poobah")
278
+ end
279
+
280
+ it "does nothing" do
281
+ @person.reset_attribute!("title")
282
+ @person.title.should == "Grand Poobah"
283
+ end
284
+ end
285
+ end
286
+ end
@@ -0,0 +1,818 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Document do
4
+
5
+ before do
6
+ @database = mock
7
+ @collection = stub(:name => "people")
8
+ @canvas_collection = stub(:name => "canvases")
9
+ Mongoid::Collection.stubs(:new).with(Person, "people").returns(@collection)
10
+ Mongoid::Collection.stubs(:new).with(Canvas, "canvases").returns(@canvas_collection)
11
+ @collection.stubs(:create_index).with(:_type, false)
12
+ @canvas_collection.stubs(:create_index).with(:_type, false)
13
+ end
14
+
15
+ after do
16
+ Person._collection = nil
17
+ Canvas._collection = nil
18
+ end
19
+
20
+ describe "#==" do
21
+
22
+ context "when other object is a Document" do
23
+
24
+ context "when attributes are equal" do
25
+
26
+ before do
27
+ @document = Person.new(:_id => 1, :title => "Sir")
28
+ @other = Person.new(:_id => 1, :title => "Sir")
29
+ end
30
+
31
+ it "returns true" do
32
+ @document.should == @other
33
+ end
34
+
35
+ end
36
+
37
+ context "when attributes are not equal" do
38
+
39
+ before do
40
+ @document = Person.new(:title => "Sir")
41
+ @other = Person.new(:title => "Madam")
42
+ end
43
+
44
+ it "returns false" do
45
+ @document.should_not == @other
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ context "when other object is not a Document" do
53
+
54
+ it "returns false" do
55
+ Person.new.==("Test").should be_false
56
+ end
57
+
58
+ end
59
+
60
+ context "when comapring parent to its subclass" do
61
+
62
+ it "returns false" do
63
+ Canvas.new.should_not == Firefox.new
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ describe "#eql?" do
71
+
72
+ context "when other object is a Document" do
73
+
74
+ context "when attributes are equal" do
75
+
76
+ before do
77
+ @document = Person.new(:_id => 1, :title => "Sir")
78
+ @other = Person.new(:_id => 1, :title => "Sir")
79
+ end
80
+
81
+ it "returns true" do
82
+ @document.eql?(@other).should be_true
83
+ end
84
+
85
+ end
86
+
87
+ context "when attributes are not equal" do
88
+
89
+ before do
90
+ @document = Person.new(:title => "Sir")
91
+ @other = Person.new(:title => "Madam")
92
+ end
93
+
94
+ it "returns false" do
95
+ @document.eql?(@other).should_not be_true
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
102
+ context "when other object is not a Document" do
103
+
104
+ it "returns false" do
105
+ Person.new.eql?("Test").should be_false
106
+ end
107
+
108
+ end
109
+
110
+ context "when comapring parent to its subclass" do
111
+
112
+ it "returns false" do
113
+ Canvas.new.eql?(Firefox.new).should_not be_true
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ describe "#hash" do
121
+
122
+ before do
123
+ @document = Person.new(:_id => 1, :title => "Sir")
124
+ @other = Person.new(:_id => 2, :title => "Sir")
125
+ end
126
+
127
+ it "deligates to id" do
128
+ @document.hash.should == @document.id.hash
129
+ end
130
+
131
+ it "has unique hash per id" do
132
+ @document.hash.should_not == @other.hash
133
+ end
134
+
135
+ end
136
+
137
+ describe "#alias_method_chain" do
138
+
139
+ context "on a field setter" do
140
+
141
+ before do
142
+ @person = Person.new
143
+ end
144
+
145
+ it "chains the method properly" do
146
+ @person.score = 10
147
+ @person.rescored.should == 30
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+
154
+ describe "#assimilate" do
155
+
156
+ before do
157
+ @child = Name.new(:first_name => "Hank", :last_name => "Moody")
158
+ @parent = Person.new(:title => "Mr.")
159
+ @options = Mongoid::Associations::Options.new(:name => :name)
160
+ end
161
+
162
+ it "sets up all associations in the object graph" do
163
+ @child.assimilate(@parent, @options)
164
+ @parent.name.should == @child
165
+ end
166
+
167
+ end
168
+
169
+ describe ".db" do
170
+
171
+ before do
172
+ @db = stub
173
+ @collection.expects(:db).returns(@db)
174
+ end
175
+
176
+ it "returns the database from the collection" do
177
+ Person.db.should == @db
178
+ end
179
+ end
180
+
181
+ describe "#clone" do
182
+
183
+ before do
184
+ @comment = Comment.new(:text => "Woooooo")
185
+ @clone = @comment.clone
186
+ end
187
+
188
+ it "returns a new document sans id and versions" do
189
+ @clone.id.should_not == @comment.id
190
+ @clone.versions.should be_empty
191
+ end
192
+
193
+ end
194
+
195
+ describe ".collection" do
196
+
197
+ before do
198
+ @person = Person.new
199
+ end
200
+
201
+ it "sets the collection name to the class pluralized" do
202
+ Person.collection.name.should == "people"
203
+ end
204
+
205
+ context "when document is embedded" do
206
+
207
+ before do
208
+ @address = Address.new
209
+ end
210
+
211
+ it "raises an error" do
212
+ lambda { Address.collection }.should raise_error
213
+ end
214
+
215
+ end
216
+
217
+ end
218
+
219
+ describe ".collection_name=" do
220
+
221
+ context "on a parent class" do
222
+
223
+ it "sets the collection name on the document class" do
224
+ Patient.collection_name = "pats"
225
+ Patient.collection_name.should == "pats"
226
+ end
227
+
228
+ end
229
+
230
+ context "on a subclass" do
231
+
232
+ after do
233
+ Canvas.collection_name = "canvases"
234
+ end
235
+
236
+ it "sets the collection name for the entire hierarchy" do
237
+ Firefox.collection_name = "browsers"
238
+ Canvas.collection_name.should == "browsers"
239
+ end
240
+
241
+ end
242
+
243
+ end
244
+
245
+ describe ".embedded" do
246
+
247
+ context "when the document is embedded" do
248
+
249
+ it "returns true" do
250
+ address = Address.new
251
+ address.embedded.should be_true
252
+ end
253
+
254
+ end
255
+
256
+ context "when the document is not embedded" do
257
+
258
+ it "returns false" do
259
+ person = Person.new
260
+ person.embedded.should be_false
261
+ end
262
+
263
+ end
264
+
265
+ context "when a subclass is embedded" do
266
+
267
+ it "returns true" do
268
+ circle = Circle.new
269
+ circle.embedded.should be_true
270
+ end
271
+
272
+ end
273
+
274
+ end
275
+
276
+ describe ".hereditary" do
277
+
278
+ context "when the class is part of a hierarchy" do
279
+
280
+ it "returns true" do
281
+ Canvas.hereditary.should be_true
282
+ end
283
+
284
+ end
285
+
286
+ context "when the class is not part of a hierarchy" do
287
+
288
+ it "returns false" do
289
+ Game.hereditary.should be_false
290
+ end
291
+
292
+ end
293
+
294
+ end
295
+
296
+ describe ".human_name" do
297
+
298
+ it "returns the class name underscored and humanized" do
299
+ MixedDrink.model_name.human.should == "Mixed drink"
300
+ end
301
+
302
+ end
303
+
304
+ describe ".initialize" do
305
+
306
+ context "when passed a block" do
307
+
308
+ it "yields self to the block" do
309
+ person = Person.new do |p|
310
+ p.title = "Sir"
311
+ p.age = 60
312
+ end
313
+ person.title.should == "Sir"
314
+ person.age.should == 60
315
+ end
316
+
317
+ end
318
+
319
+ context "with no attributes" do
320
+
321
+ it "sets default attributes" do
322
+ person = Person.new
323
+ person.attributes.empty?.should be_false
324
+ person.age.should == 100
325
+ person.blood_alcohol_content.should == 0.0
326
+ end
327
+
328
+ end
329
+
330
+ context "with nil attributes" do
331
+
332
+ before do
333
+ @person = Person.new(nil)
334
+ end
335
+
336
+ it "sets default attributes" do
337
+ @person.attributes.empty?.should be_false
338
+ @person.age.should == 100
339
+ @person.blood_alcohol_content.should == 0.0
340
+ end
341
+
342
+ end
343
+
344
+ context "with attributes" do
345
+
346
+ before do
347
+ @attributes = {
348
+ :_id => "1",
349
+ :title => "value",
350
+ :age => "30",
351
+ :terms => "true",
352
+ :name => {
353
+ :_id => "2", :first_name => "Test", :last_name => "User"
354
+ },
355
+ :addresses => [
356
+ { :_id => "3", :street => "First Street" },
357
+ { :_id => "4", :street => "Second Street" }
358
+ ]
359
+ }
360
+ end
361
+
362
+ it "sets the attributes hash on the object properly casted" do
363
+ person = Person.new(@attributes)
364
+ person.attributes[:age].should == 30
365
+ person.attributes[:terms].should be_true
366
+ end
367
+
368
+ end
369
+
370
+ context "with a primary key" do
371
+
372
+ context "when the value for the key exists" do
373
+
374
+ before do
375
+ Address.key :street
376
+ @address = Address.new(:street => "Test")
377
+ end
378
+
379
+ it "sets the primary key" do
380
+ @address.id.should == "test"
381
+ end
382
+
383
+ end
384
+
385
+ end
386
+
387
+ context "without a type specified" do
388
+
389
+ it "sets the type" do
390
+ Person.new._type.should == "Person"
391
+ end
392
+
393
+ end
394
+
395
+ end
396
+
397
+ describe ".instantiate" do
398
+
399
+ context "when attributes have an id" do
400
+
401
+ before do
402
+ @attributes = { "_id" => "1", "_type" => "Person", "title" => "Sir", "age" => 30 }
403
+ end
404
+
405
+ it "sets the attributes directly" do
406
+ person = Person.instantiate(@attributes)
407
+ person._id.should == "1"
408
+ person._type.should == "Person"
409
+ person.title.should == "Sir"
410
+ person.age.should == 30
411
+ end
412
+
413
+ end
414
+
415
+ context "with nil attributes" do
416
+
417
+ it "sets the attributes directly" do
418
+ person = Person.instantiate(nil)
419
+ person.id.should_not be_nil
420
+ end
421
+
422
+ end
423
+
424
+ end
425
+
426
+ describe ".key" do
427
+
428
+ context "when key is single field" do
429
+
430
+ before do
431
+ Address.key :street
432
+ @address = Address.new(:street => "Testing Street Name")
433
+ @address.expects(:collection).returns(@collection)
434
+ @collection.expects(:save)
435
+ end
436
+
437
+ it "adds the callback for primary key generation" do
438
+ @address.save
439
+ @address.id.should == "testing-street-name"
440
+ end
441
+
442
+ end
443
+
444
+ context "when key is composite" do
445
+
446
+ before do
447
+ Address.key :street, :post_code
448
+ @address = Address.new(:street => "Testing Street Name", :post_code => "94123")
449
+ @address.expects(:collection).returns(@collection)
450
+ @collection.expects(:save)
451
+ end
452
+
453
+ it "combines all fields" do
454
+ @address.save
455
+ @address.id.should == "testing-street-name-94123"
456
+ end
457
+
458
+ end
459
+
460
+ context "when key is on a subclass" do
461
+
462
+ before do
463
+ Firefox.key :name
464
+ end
465
+
466
+ it "sets the key for the entire hierarchy" do
467
+ Canvas.primary_key.should == [:name]
468
+ end
469
+
470
+ end
471
+
472
+ end
473
+
474
+ describe "#_parent" do
475
+
476
+ before do
477
+ @attributes = { :title => "Sir",
478
+ :addresses => [
479
+ { :street => "Street 1" },
480
+ { :street => "Street 2" } ] }
481
+ @person = Person.new(@attributes)
482
+ end
483
+
484
+ context "when document is embedded" do
485
+
486
+ it "returns the parent document" do
487
+ @person.addresses.first._parent.should == @person
488
+ end
489
+
490
+ end
491
+
492
+ context "when document is root" do
493
+
494
+ it "returns nil" do
495
+ @person._parent.should be_nil
496
+ end
497
+
498
+ end
499
+
500
+ end
501
+
502
+ describe "#parentize" do
503
+
504
+ before do
505
+ @parent = Person.new
506
+ @child = Name.new
507
+ end
508
+
509
+ it "sets the parent on each element" do
510
+ @child.parentize(@parent, :child)
511
+ @child._parent.should == @parent
512
+ end
513
+
514
+ end
515
+
516
+ describe "#reload" do
517
+
518
+ before do
519
+ @attributes = { "title" => "Herr" }
520
+ @person = Person.new(:_id => Mongo::ObjectID.new.to_s)
521
+ @collection.expects(:find_one).with(:_id => @person.id).returns(@attributes)
522
+ end
523
+
524
+ it "reloads the object attribtues from the database" do
525
+ @person.reload
526
+ @person.attributes.should == @attributes
527
+ end
528
+
529
+ it 'should return a person object' do
530
+ @person.reload.should be_kind_of(Person)
531
+ end
532
+
533
+ end
534
+
535
+ describe "#remove" do
536
+
537
+ context "when removing an element from a has many" do
538
+
539
+ before do
540
+ @person = Person.new
541
+ @address = Address.new(:street => "Testing")
542
+ @person.addresses << @address
543
+ end
544
+
545
+ it "removes the child document attributes" do
546
+ @person.remove(@address)
547
+ @person.addresses.size.should == 0
548
+ end
549
+
550
+ end
551
+
552
+ context "when removing a has one" do
553
+
554
+ before do
555
+ @person = Person.new
556
+ @name = Name.new(:first_name => "Neytiri")
557
+ @person.name = @name
558
+ end
559
+
560
+ it "removes the child document attributes" do
561
+ @person.remove(@name)
562
+ @person.name.should be_nil
563
+ end
564
+ end
565
+ end
566
+
567
+ describe "#_root" do
568
+
569
+ before do
570
+ @person = Person.new(:title => "Mr")
571
+ @phone_number = Phone.new(:number => "415-555-1212")
572
+ @country_code = CountryCode.new(:code => 1)
573
+ @phone_number.country_code = @country_code
574
+ @person.phone_numbers << @phone_number
575
+ end
576
+
577
+ context "when document is the root" do
578
+
579
+ it "returns self" do
580
+ @person._root.should == @person
581
+ end
582
+ end
583
+
584
+ context "when document is embedded one level" do
585
+
586
+ it "returns the parent" do
587
+ @phone_number._root.should == @person
588
+ end
589
+ end
590
+
591
+ context "when document is embedded multiple levels" do
592
+
593
+ it "returns the top level parent" do
594
+ @country_code._root.should == @person
595
+ end
596
+ end
597
+ end
598
+
599
+ describe ".store_in" do
600
+
601
+ context "on a parent class" do
602
+
603
+ it "sets the collection name and collection for the document" do
604
+ Mongoid::Collection.expects(:new).with(Patient, "population").returns(@collection)
605
+ Patient.store_in :population
606
+ Patient.collection_name.should == "population"
607
+ end
608
+
609
+ end
610
+
611
+ context "on a subclass" do
612
+
613
+ after do
614
+ Mongoid::Collection.expects(:new).with(Firefox, "canvases")
615
+ Firefox.store_in :canvases
616
+ end
617
+
618
+ it "changes the collection name for the entire hierarchy" do
619
+ Mongoid::Collection.expects(:new).with(Firefox, "browsers").returns(@collection)
620
+ Firefox.store_in :browsers
621
+ Canvas.collection_name.should == "browsers"
622
+ end
623
+
624
+ end
625
+
626
+ end
627
+
628
+ describe "._types" do
629
+
630
+ it "returns all subclasses for the class plus the class" do
631
+ types = Canvas._types
632
+ types.size.should == 3
633
+ types.should include("Firefox")
634
+ types.should include("Browser")
635
+ types.should include("Canvas")
636
+ end
637
+
638
+ it "does not return parent classes" do
639
+ types = Browser._types
640
+ types.size.should == 2
641
+ types.should include("Firefox")
642
+ types.should include("Browser")
643
+ end
644
+
645
+ end
646
+
647
+ describe "#to_a" do
648
+
649
+ it "returns an array with the document in it" do
650
+ person = Person.new
651
+ person.to_a.should == [ person ]
652
+ end
653
+
654
+ end
655
+
656
+ describe "#to_param" do
657
+
658
+ it "returns the id" do
659
+ id = Mongo::ObjectID.new.to_s
660
+ Person.new(:_id => id).to_param.should == id.to_s
661
+ end
662
+
663
+ end
664
+
665
+ context "validations" do
666
+
667
+ context "when running validations" do
668
+
669
+ before do
670
+ @person = Person.new
671
+ @canvas = Canvas.new
672
+ @firefox = Firefox.new
673
+ end
674
+
675
+ describe "#validates_acceptance_of" do
676
+
677
+ it "fails if field not accepted" do
678
+ Person.class_eval do
679
+ validates_acceptance_of :terms, :allow_nil => false
680
+ end
681
+ @person.valid?.should be_false
682
+ @person.errors[:terms].should_not be_nil
683
+ end
684
+
685
+ end
686
+
687
+ describe "#validates_associated" do
688
+
689
+ context "when association is a has_many" do
690
+
691
+ it "fails when any association fails validation" do
692
+ Person.class_eval do
693
+ validates_associated :addresses
694
+ end
695
+ Address.class_eval do
696
+ validates_presence_of :street
697
+ end
698
+ @person.addresses << Address.new
699
+ @person.valid?.should be_false
700
+ @person.errors[:addresses].should_not be_nil
701
+ end
702
+
703
+ end
704
+
705
+ context "when association is a has_one" do
706
+
707
+ context "when the associated is not nil" do
708
+
709
+ it "fails when the association fails validation" do
710
+ Person.class_eval do
711
+ validates_associated :name
712
+ end
713
+ Name.class_eval do
714
+ validates_presence_of :first_name
715
+ end
716
+ @person.name = Name.new
717
+ @person.valid?.should be_false
718
+ @person.errors[:name].should_not be_nil
719
+ end
720
+
721
+ end
722
+
723
+ context "when the associated is nil" do
724
+
725
+ it "returns true" do
726
+ Person.class_eval do
727
+ validates_associated :name
728
+ end
729
+ @person.valid?
730
+ @person.errors[:name].should be_empty
731
+ end
732
+
733
+ end
734
+
735
+ end
736
+
737
+ end
738
+
739
+ describe "#validates_format_of" do
740
+
741
+ it "fails if the field is in the wrong format" do
742
+ Person.class_eval do
743
+ validates_format_of :title, :with => /[A-Za-z]/
744
+ end
745
+ @person.title = 10
746
+ @person.valid?.should be_false
747
+ @person.errors[:title].should_not be_nil
748
+ end
749
+
750
+ end
751
+
752
+ describe "#validates_length_of" do
753
+
754
+ it "fails if the field is the wrong length" do
755
+ Person.class_eval do
756
+ validates_length_of :title, :minimum => 10
757
+ end
758
+ @person.title = "Testing"
759
+ @person.valid?.should be_false
760
+ @person.errors[:title].should_not be_nil
761
+ end
762
+
763
+ end
764
+
765
+ describe "#validates_numericality_of" do
766
+
767
+ it "fails if the field is not a number" do
768
+ Person.class_eval do
769
+ validates_numericality_of :age
770
+ end
771
+ @person.age = "foo"
772
+ @person.valid?.should be_false
773
+ @person.errors[:age].should_not be_nil
774
+ end
775
+
776
+ end
777
+
778
+ describe "#validates_presence_of" do
779
+
780
+ context "on a parent class" do
781
+
782
+ it "fails if the field is nil on the parent" do
783
+ Person.class_eval do
784
+ validates_presence_of :title
785
+ end
786
+ @person.valid?.should be_false
787
+ @person.errors[:title].should_not be_nil
788
+ end
789
+
790
+ it "fails if the field is nil on a subclass" do
791
+ Canvas.class_eval do
792
+ validates_presence_of :name
793
+ end
794
+ @firefox.valid?.should be_false
795
+ @firefox.errors[:name].should_not be_nil
796
+ end
797
+
798
+ end
799
+
800
+ context "on a subclass" do
801
+
802
+ it "parent class does not get subclass validations" do
803
+ Firefox.class_eval do
804
+ validates_presence_of :version
805
+ end
806
+ @canvas.name = "Testing"
807
+ @canvas.valid?.should be_true
808
+ end
809
+
810
+ end
811
+
812
+ end
813
+
814
+ end
815
+
816
+ end
817
+
818
+ end