mongoid-locomotive 2.0.0.beta9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT_LICENSE +20 -0
- data/README.rdoc +47 -0
- data/lib/mongoid.rb +141 -0
- data/lib/mongoid/associations.rb +306 -0
- data/lib/mongoid/associations/embedded_in.rb +74 -0
- data/lib/mongoid/associations/embeds_many.rb +280 -0
- data/lib/mongoid/associations/embeds_one.rb +97 -0
- data/lib/mongoid/associations/foreign_key.rb +35 -0
- data/lib/mongoid/associations/meta_data.rb +38 -0
- data/lib/mongoid/associations/options.rb +62 -0
- data/lib/mongoid/associations/proxy.rb +33 -0
- data/lib/mongoid/associations/referenced_in.rb +59 -0
- data/lib/mongoid/associations/references_many.rb +245 -0
- data/lib/mongoid/associations/references_many_as_array.rb +78 -0
- data/lib/mongoid/associations/references_one.rb +99 -0
- data/lib/mongoid/atomicity.rb +55 -0
- data/lib/mongoid/attributes.rb +242 -0
- data/lib/mongoid/callbacks.rb +21 -0
- data/lib/mongoid/collection.rb +120 -0
- data/lib/mongoid/collections.rb +71 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +41 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/components.rb +34 -0
- data/lib/mongoid/config.rb +263 -0
- data/lib/mongoid/contexts.rb +24 -0
- data/lib/mongoid/contexts/enumerable.rb +156 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +285 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/criteria.rb +248 -0
- data/lib/mongoid/criterion/complex.rb +21 -0
- data/lib/mongoid/criterion/exclusion.rb +65 -0
- data/lib/mongoid/criterion/inclusion.rb +110 -0
- data/lib/mongoid/criterion/optional.rb +189 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/deprecation.rb +21 -0
- data/lib/mongoid/dirty.rb +252 -0
- data/lib/mongoid/document.rb +210 -0
- data/lib/mongoid/errors.rb +131 -0
- data/lib/mongoid/extensions.rb +115 -0
- data/lib/mongoid/extensions/array/accessors.rb +17 -0
- data/lib/mongoid/extensions/array/assimilation.rb +26 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +24 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/accessors.rb +42 -0
- data/lib/mongoid/extensions/hash/assimilation.rb +40 -0
- data/lib/mongoid/extensions/hash/conversions.rb +42 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
- data/lib/mongoid/extensions/object/conversions.rb +21 -0
- data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +15 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +35 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +83 -0
- data/lib/mongoid/fields.rb +62 -0
- data/lib/mongoid/finders.rb +145 -0
- data/lib/mongoid/hierarchy.rb +74 -0
- data/lib/mongoid/identity.rb +47 -0
- data/lib/mongoid/indexes.rb +27 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/logger.rb +19 -0
- data/lib/mongoid/matchers.rb +35 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +26 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/memoization.rb +33 -0
- data/lib/mongoid/named_scope.rb +37 -0
- data/lib/mongoid/paranoia.rb +106 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence.rb +216 -0
- data/lib/mongoid/persistence/command.rb +39 -0
- data/lib/mongoid/persistence/insert.rb +48 -0
- data/lib/mongoid/persistence/insert_embedded.rb +44 -0
- data/lib/mongoid/persistence/remove.rb +39 -0
- data/lib/mongoid/persistence/remove_all.rb +38 -0
- data/lib/mongoid/persistence/remove_embedded.rb +50 -0
- data/lib/mongoid/persistence/update.rb +71 -0
- data/lib/mongoid/railtie.rb +67 -0
- data/lib/mongoid/railties/database.rake +60 -0
- data/lib/mongoid/scope.rb +75 -0
- data/lib/mongoid/state.rb +32 -0
- data/lib/mongoid/timestamps.rb +27 -0
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +5 -0
- data/lib/mongoid/validations/uniqueness.rb +56 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +26 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +24 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +15 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- data/spec/integration/mongoid/association_attributes_spec.rb +71 -0
- data/spec/integration/mongoid/associations_spec.rb +768 -0
- data/spec/integration/mongoid/attributes_spec.rb +59 -0
- data/spec/integration/mongoid/callback_spec.rb +33 -0
- data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
- data/spec/integration/mongoid/criteria_spec.rb +281 -0
- data/spec/integration/mongoid/dirty_spec.rb +85 -0
- data/spec/integration/mongoid/document_spec.rb +741 -0
- data/spec/integration/mongoid/extensions_spec.rb +22 -0
- data/spec/integration/mongoid/finders_spec.rb +119 -0
- data/spec/integration/mongoid/inheritance_spec.rb +171 -0
- data/spec/integration/mongoid/named_scope_spec.rb +58 -0
- data/spec/integration/mongoid/paranoia_spec.rb +44 -0
- data/spec/integration/mongoid/persistence/update_spec.rb +46 -0
- data/spec/integration/mongoid/persistence_spec.rb +311 -0
- data/spec/integration/mongoid/validations/uniqueness_spec.rb +206 -0
- data/spec/models/account.rb +5 -0
- data/spec/models/address.rb +40 -0
- data/spec/models/agent.rb +7 -0
- data/spec/models/animal.rb +15 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/callbacks.rb +47 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/comment.rb +10 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/favorite.rb +8 -0
- data/spec/models/game.rb +9 -0
- data/spec/models/inheritance.rb +72 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +106 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/post.rb +25 -0
- data/spec/models/preference.rb +7 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/survey.rb +6 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/user.rb +6 -0
- data/spec/models/user_accout.rb +5 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +5 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
- data/spec/unit/mongoid/associations/embeds_many_spec.rb +626 -0
- data/spec/unit/mongoid/associations/embeds_one_spec.rb +287 -0
- data/spec/unit/mongoid/associations/foreign_key_spec.rb +90 -0
- data/spec/unit/mongoid/associations/meta_data_spec.rb +110 -0
- data/spec/unit/mongoid/associations/options_spec.rb +215 -0
- data/spec/unit/mongoid/associations/referenced_in_spec.rb +145 -0
- data/spec/unit/mongoid/associations/references_many_as_array_spec.rb +424 -0
- data/spec/unit/mongoid/associations/references_many_spec.rb +502 -0
- data/spec/unit/mongoid/associations/references_one_spec.rb +204 -0
- data/spec/unit/mongoid/associations_spec.rb +688 -0
- data/spec/unit/mongoid/atomicity_spec.rb +164 -0
- data/spec/unit/mongoid/attributes_spec.rb +646 -0
- data/spec/unit/mongoid/callbacks_spec.rb +85 -0
- data/spec/unit/mongoid/collection_spec.rb +187 -0
- data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
- data/spec/unit/mongoid/collections/master_spec.rb +41 -0
- data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
- data/spec/unit/mongoid/collections_spec.rb +98 -0
- data/spec/unit/mongoid/config_spec.rb +298 -0
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +447 -0
- data/spec/unit/mongoid/contexts/mongo_spec.rb +703 -0
- data/spec/unit/mongoid/contexts_spec.rb +25 -0
- data/spec/unit/mongoid/criteria_spec.rb +873 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +17 -0
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +121 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +274 -0
- data/spec/unit/mongoid/criterion/optional_spec.rb +483 -0
- data/spec/unit/mongoid/cursor_spec.rb +80 -0
- data/spec/unit/mongoid/deprecation_spec.rb +24 -0
- data/spec/unit/mongoid/dirty_spec.rb +430 -0
- data/spec/unit/mongoid/document_spec.rb +623 -0
- data/spec/unit/mongoid/errors_spec.rb +154 -0
- data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
- data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +52 -0
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
- data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +36 -0
- data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +145 -0
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/false_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
- data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +59 -0
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +29 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +44 -0
- data/spec/unit/mongoid/extensions/objectid/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
- data/spec/unit/mongoid/extensions/set/conversions_spec.rb +21 -0
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +28 -0
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +107 -0
- data/spec/unit/mongoid/extensions/time_conversions_spec.rb +186 -0
- data/spec/unit/mongoid/extensions/true_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extras_spec.rb +102 -0
- data/spec/unit/mongoid/factory_spec.rb +31 -0
- data/spec/unit/mongoid/field_spec.rb +169 -0
- data/spec/unit/mongoid/fields_spec.rb +181 -0
- data/spec/unit/mongoid/finders_spec.rb +439 -0
- data/spec/unit/mongoid/hierarchy_spec.rb +68 -0
- data/spec/unit/mongoid/identity_spec.rb +109 -0
- data/spec/unit/mongoid/indexes_spec.rb +99 -0
- data/spec/unit/mongoid/javascript_spec.rb +48 -0
- data/spec/unit/mongoid/logger_spec.rb +38 -0
- data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
- data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
- data/spec/unit/mongoid/matchers_spec.rb +329 -0
- data/spec/unit/mongoid/memoization_spec.rb +75 -0
- data/spec/unit/mongoid/named_scope_spec.rb +123 -0
- data/spec/unit/mongoid/paranoia_spec.rb +108 -0
- data/spec/unit/mongoid/paths_spec.rb +272 -0
- data/spec/unit/mongoid/persistence/insert_embedded_spec.rb +154 -0
- data/spec/unit/mongoid/persistence/insert_spec.rb +144 -0
- data/spec/unit/mongoid/persistence/remove_all_spec.rb +82 -0
- data/spec/unit/mongoid/persistence/remove_embedded_spec.rb +152 -0
- data/spec/unit/mongoid/persistence/remove_spec.rb +89 -0
- data/spec/unit/mongoid/persistence/update_spec.rb +177 -0
- data/spec/unit/mongoid/persistence_spec.rb +452 -0
- data/spec/unit/mongoid/scope_spec.rb +240 -0
- data/spec/unit/mongoid/serialization_spec.rb +43 -0
- data/spec/unit/mongoid/state_spec.rb +94 -0
- data/spec/unit/mongoid/timestamps_spec.rb +30 -0
- data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
- data/spec/unit/mongoid/validations/uniqueness_spec.rb +201 -0
- data/spec/unit/mongoid/validations_spec.rb +43 -0
- data/spec/unit/mongoid/versioning_spec.rb +41 -0
- data/spec/unit/mongoid_spec.rb +46 -0
- metadata +433 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Fields do
|
|
4
|
+
|
|
5
|
+
describe ".defaults" do
|
|
6
|
+
|
|
7
|
+
it "returns a hash of all the default values" do
|
|
8
|
+
Game.defaults.should == { "high_score" => 500, "score" => 0 }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "#defaults" do
|
|
14
|
+
|
|
15
|
+
context "with defaults specified as a non-primitive" do
|
|
16
|
+
|
|
17
|
+
before do
|
|
18
|
+
Person.field(:hash_testing, :type => Hash, :default => {})
|
|
19
|
+
Person.field(:array_testing, :type => Array, :default => [])
|
|
20
|
+
@first_person = Person.new
|
|
21
|
+
@second_person = Person.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
after do
|
|
25
|
+
Person.fields.delete("hash_testing")
|
|
26
|
+
Person.fields.delete("array_testing")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "should not return the same object when calling defaults with a default hash" do
|
|
30
|
+
@first_person.hash_testing.object_id.should_not == @second_person.hash_testing.object_id
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "should not return the same object when calling defaults with a default array" do
|
|
34
|
+
@first_person.array_testing.object_id.should_not == @second_person.array_testing.object_id
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context "on parent classes" do
|
|
39
|
+
|
|
40
|
+
before do
|
|
41
|
+
@shape = Shape.new
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "does not return subclass defaults" do
|
|
45
|
+
@shape.defaults.should == { "x" => 0, "y" => 0 }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context "on subclasses" do
|
|
51
|
+
|
|
52
|
+
before do
|
|
53
|
+
@circle = Circle.new
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "has the parent and child defaults" do
|
|
57
|
+
@circle.defaults.should == { "x" => 0, "y" => 0, "radius" => 0 }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe ".field" do
|
|
65
|
+
|
|
66
|
+
context "with no options" do
|
|
67
|
+
|
|
68
|
+
before do
|
|
69
|
+
Person.field(:testing)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "adds a reader for the fields defined" do
|
|
73
|
+
@person = Person.new(:testing => "Test")
|
|
74
|
+
@person.testing.should == "Test"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "adds a writer for the fields defined" do
|
|
78
|
+
@person = Person.new(:testing => "Test")
|
|
79
|
+
@person.testing = "Testy"
|
|
80
|
+
@person.testing.should == "Testy"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "adds an reader method with a question mark" do
|
|
84
|
+
@person = Person.new(:testing => "Test")
|
|
85
|
+
@person.testing?.should be_true
|
|
86
|
+
Person.new.testing?.should be_false
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
context "when type is an object" do
|
|
92
|
+
|
|
93
|
+
before do
|
|
94
|
+
@person = Person.new
|
|
95
|
+
@drink = MixedDrink.new(:name => "Jack and Coke")
|
|
96
|
+
@person.mixed_drink = @drink
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "allows proper access to the object" do
|
|
100
|
+
@person.mixed_drink.should == @drink
|
|
101
|
+
@person.attributes[:mixed_drink].except(:_id).except(:_type).should ==
|
|
102
|
+
{ "name" => "Jack and Coke" }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "adds an reader method with a question mark" do
|
|
106
|
+
@person.mixed_drink?.should be_true
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
context "when type is a boolean" do
|
|
112
|
+
|
|
113
|
+
before do
|
|
114
|
+
@person = Person.new(:terms => true)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it "adds an accessor method with a question mark" do
|
|
118
|
+
@person.terms?.should be_true
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
context "when as is specified" do
|
|
124
|
+
|
|
125
|
+
before do
|
|
126
|
+
Person.field :aliased, :as => :alias, :type => Boolean
|
|
127
|
+
@person = Person.new(:alias => true)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it "uses the alias to write the attribute" do
|
|
131
|
+
@person.expects(:write_attribute).with("aliased", true)
|
|
132
|
+
@person.alias = true
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it "uses the alias to read the attribute" do
|
|
136
|
+
@person.expects(:read_attribute).with("aliased")
|
|
137
|
+
@person.alias
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
it "uses the alias for the query method" do
|
|
141
|
+
@person.expects(:read_attribute).with("aliased")
|
|
142
|
+
@person.alias?
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
describe "#fields" do
|
|
150
|
+
|
|
151
|
+
context "on parent classes" do
|
|
152
|
+
|
|
153
|
+
before do
|
|
154
|
+
@shape = Shape.new
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it "does not return subclass fields" do
|
|
158
|
+
@shape.fields.keys.should include("x")
|
|
159
|
+
@shape.fields.keys.should include("y")
|
|
160
|
+
@shape.fields.keys.should_not include("radius")
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
context "on subclasses" do
|
|
166
|
+
|
|
167
|
+
before do
|
|
168
|
+
@circle = Circle.new
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "has the parent and child fields" do
|
|
172
|
+
@circle.fields.keys.should include("x")
|
|
173
|
+
@circle.fields.keys.should include("y")
|
|
174
|
+
@circle.fields.keys.should include("radius")
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
end
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Finders do
|
|
4
|
+
|
|
5
|
+
before do
|
|
6
|
+
@collection = stub(:name => "people")
|
|
7
|
+
@database = stub(:collection => @collection)
|
|
8
|
+
Mongoid.stubs(:database).returns(@database)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe ".all" do
|
|
12
|
+
|
|
13
|
+
before do
|
|
14
|
+
@conditions = { :conditions => { :test => "Test" } }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "when a selector is provided" do
|
|
18
|
+
|
|
19
|
+
it "finds from the collection and instantiate objects for each returned" do
|
|
20
|
+
Mongoid::Criteria.expects(:translate).with(Person, @conditions)
|
|
21
|
+
Person.all(@conditions)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context "when a selector is not provided" do
|
|
27
|
+
|
|
28
|
+
it "finds from the collection and instantiate objects for each returned" do
|
|
29
|
+
Mongoid::Criteria.expects(:translate).with(Person, nil)
|
|
30
|
+
Person.all
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe ".all_in" do
|
|
38
|
+
|
|
39
|
+
it "returns a new criteria with select conditions added" do
|
|
40
|
+
criteria = Person.all_in(:aliases => [ "Bond", "007" ])
|
|
41
|
+
criteria.selector.should == { :aliases => { "$all" => [ "Bond", "007" ] } }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe ".any_in" do
|
|
47
|
+
|
|
48
|
+
it "returns a new criteria with select conditions added" do
|
|
49
|
+
criteria = Person.any_in(:aliases => [ "Bond", "007" ])
|
|
50
|
+
criteria.selector.should == { :aliases => { "$in" => [ "Bond", "007" ] } }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
describe ".count" do
|
|
56
|
+
|
|
57
|
+
before do
|
|
58
|
+
@conditions = { :conditions => { :title => "Sir" } }
|
|
59
|
+
@criteria = mock
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "delegates to the criteria api" do
|
|
63
|
+
Mongoid::Criteria.expects(:translate).with(Person, @conditions).returns(@criteria)
|
|
64
|
+
@criteria.expects(:count).returns(10)
|
|
65
|
+
Person.count(@conditions).should == 10
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
context "when no options provided" do
|
|
69
|
+
|
|
70
|
+
it "adds in the default parameters" do
|
|
71
|
+
Mongoid::Criteria.expects(:translate).with(Person, nil).returns(@criteria)
|
|
72
|
+
@criteria.expects(:count).returns(10)
|
|
73
|
+
Person.count.should == 10
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
describe ".exists?" do
|
|
81
|
+
|
|
82
|
+
before do
|
|
83
|
+
@conditions = { :conditions => { :title => "Sir" } }
|
|
84
|
+
@criteria = mock
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "should be true if there are document in collection with options" do
|
|
88
|
+
Mongoid::Criteria.expects(:translate).with(Person, @conditions).returns(@criteria)
|
|
89
|
+
@criteria.expects(:limit).with(1).returns(@criteria)
|
|
90
|
+
@criteria.expects(:count).returns(1)
|
|
91
|
+
Person.exists?(@conditions).should be_true
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it 'should false if there are no document in collection with options' do
|
|
95
|
+
Mongoid::Criteria.expects(:translate).with(Person, @conditions).returns(@criteria)
|
|
96
|
+
@criteria.expects(:limit).with(1).returns(@criteria)
|
|
97
|
+
@criteria.expects(:count).returns(0)
|
|
98
|
+
Person.exists?(@conditions).should be_false
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
context "when no options provided" do
|
|
102
|
+
|
|
103
|
+
it "should be true if there are document in collection" do
|
|
104
|
+
Mongoid::Criteria.expects(:translate).with(Person, nil).returns(@criteria)
|
|
105
|
+
@criteria.expects(:limit).with(1).returns(@criteria)
|
|
106
|
+
@criteria.expects(:count).returns(1)
|
|
107
|
+
Person.exists?.should be_true
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it "should be false if there are no document in collection" do
|
|
111
|
+
Mongoid::Criteria.expects(:translate).with(Person, nil).returns(@criteria)
|
|
112
|
+
@criteria.expects(:limit).with(1).returns(@criteria)
|
|
113
|
+
@criteria.expects(:count).returns(1)
|
|
114
|
+
Person.exists?.should be_true
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
describe ".excludes" do
|
|
122
|
+
|
|
123
|
+
it "returns a new criteria with select conditions added" do
|
|
124
|
+
criteria = Person.excludes(:title => "Sir")
|
|
125
|
+
criteria.selector.should == { :title => { "$ne" => "Sir" } }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
describe ".find" do
|
|
131
|
+
|
|
132
|
+
before do
|
|
133
|
+
@attributes = {}
|
|
134
|
+
@criteria = mock
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
context "when an id is passed in" do
|
|
138
|
+
|
|
139
|
+
before do
|
|
140
|
+
@id = BSON::ObjectID.new.to_s
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it "delegates to criteria" do
|
|
144
|
+
Mongoid::Criteria.expects(:translate).with(Person, @id.to_s).returns(Person.new)
|
|
145
|
+
Person.find(@id.to_s)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
context "when no document is found" do
|
|
149
|
+
|
|
150
|
+
it "raises an error" do
|
|
151
|
+
@error = Mongoid::Errors::DocumentNotFound.new(Person, @id.to_s)
|
|
152
|
+
Mongoid::Criteria.expects(:translate).with(Person, @id.to_s).raises(@error)
|
|
153
|
+
lambda { Person.find(@id.to_s) }.should raise_error
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
context "when an array of ids is passed in" do
|
|
161
|
+
|
|
162
|
+
before do
|
|
163
|
+
@ids = []
|
|
164
|
+
3.times { @ids << BSON::ObjectID.new.to_s }
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
it "delegates to the criteria" do
|
|
168
|
+
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
context "when nil passed in" do
|
|
174
|
+
|
|
175
|
+
it "raises an error" do
|
|
176
|
+
lambda { Person.find(nil) }.should raise_error
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
context "when finding first" do
|
|
182
|
+
|
|
183
|
+
it "delegates to criteria" do
|
|
184
|
+
Mongoid::Criteria.expects(:translate).with(Person, :conditions => { :test => "Test" }).returns(@criteria)
|
|
185
|
+
@criteria.expects(:one).returns(@attributes)
|
|
186
|
+
Person.find(:first, :conditions => { :test => "Test" })
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
context "when finding all" do
|
|
192
|
+
|
|
193
|
+
before do
|
|
194
|
+
@conditions = { :conditions => { :test => "Test" } }
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
it "delegates to find_all" do
|
|
198
|
+
Mongoid::Criteria.expects(:translate).with(Person, @conditions).returns(@criteria)
|
|
199
|
+
Person.find(:all, @conditions)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
context "when sorting" do
|
|
205
|
+
|
|
206
|
+
before do
|
|
207
|
+
@conditions = { :conditions => { :test => "Test" }, :sort => { :test => -1 } }
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
it "adds the sort parameters for the collection call" do
|
|
211
|
+
Mongoid::Criteria.expects(:translate).with(Person, @conditions).returns(@criteria)
|
|
212
|
+
Person.find(:all, @conditions)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
describe ".find_or_create_by" do
|
|
219
|
+
|
|
220
|
+
before do
|
|
221
|
+
@person = Person.new(:age => 30)
|
|
222
|
+
@criteria = mock
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
context "when the document is found" do
|
|
226
|
+
|
|
227
|
+
it "returns the document" do
|
|
228
|
+
Mongoid::Criteria.expects(:translate).with(
|
|
229
|
+
Person, :conditions => { :age => 30 }
|
|
230
|
+
).returns(@criteria)
|
|
231
|
+
@criteria.expects(:one).returns(@person)
|
|
232
|
+
Person.find_or_create_by(:age => 30).should == @person
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
context "when the document is not found" do
|
|
238
|
+
|
|
239
|
+
it "creates a new document" do
|
|
240
|
+
Mongoid::Criteria.expects(:translate).with(
|
|
241
|
+
Person, :conditions => { :age => 30 }
|
|
242
|
+
).returns(@criteria)
|
|
243
|
+
@criteria.expects(:one).returns(nil)
|
|
244
|
+
Person.expects(:create).returns(@person)
|
|
245
|
+
person = Person.find_or_create_by(:age => 30)
|
|
246
|
+
person.should be_a_kind_of(Person)
|
|
247
|
+
person.age.should == 30
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
describe ".find_or_initialize_by" do
|
|
255
|
+
|
|
256
|
+
before do
|
|
257
|
+
@person = Person.new(:age => 30)
|
|
258
|
+
@criteria = mock
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
context "when the document is found" do
|
|
262
|
+
|
|
263
|
+
it "returns the document" do
|
|
264
|
+
Mongoid::Criteria.expects(:translate).with(
|
|
265
|
+
Person, :conditions => { :age => 30 }
|
|
266
|
+
).returns(@criteria)
|
|
267
|
+
@criteria.expects(:one).returns(@person)
|
|
268
|
+
Person.find_or_initialize_by(:age => 30).should == @person
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
context "when the document is not found" do
|
|
274
|
+
|
|
275
|
+
it "returns a new document with the conditions" do
|
|
276
|
+
Mongoid::Criteria.expects(:translate).with(
|
|
277
|
+
Person, :conditions => { :age => 30 }
|
|
278
|
+
).returns(@criteria)
|
|
279
|
+
@criteria.expects(:one).returns(nil)
|
|
280
|
+
person = Person.find_or_initialize_by(:age => 30)
|
|
281
|
+
person.should be_a_kind_of(Person)
|
|
282
|
+
person.should be_a_new_record
|
|
283
|
+
person.age.should == 30
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
describe ".first" do
|
|
291
|
+
|
|
292
|
+
before do
|
|
293
|
+
@criteria = mock
|
|
294
|
+
@conditions = { :conditions => { :test => "Test" } }
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
context "when a selector is provided" do
|
|
298
|
+
|
|
299
|
+
it "finds the first document from the collection and instantiates it" do
|
|
300
|
+
Mongoid::Criteria.expects(:translate).with(Person, @conditions).returns(@criteria)
|
|
301
|
+
@criteria.expects(:one)
|
|
302
|
+
Person.first(@conditions)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
context "when a selector is not provided" do
|
|
308
|
+
|
|
309
|
+
it "finds the first document from the collection and instantiates it" do
|
|
310
|
+
Mongoid::Criteria.expects(:translate).with(Person, nil).returns(@criteria)
|
|
311
|
+
@criteria.expects(:one)
|
|
312
|
+
Person.first
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
describe ".last" do
|
|
320
|
+
|
|
321
|
+
before do
|
|
322
|
+
@criteria = mock
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
it "finds the last document by the id" do
|
|
326
|
+
Mongoid::Criteria.expects(:translate).with(Person, nil).returns(@criteria)
|
|
327
|
+
@criteria.expects(:last)
|
|
328
|
+
Person.last
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
describe ".max" do
|
|
334
|
+
|
|
335
|
+
before do
|
|
336
|
+
@criteria = mock
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
it "returns the sum of a new criteria" do
|
|
340
|
+
Person.expects(:criteria).returns(@criteria)
|
|
341
|
+
@criteria.expects(:max).with(:age).returns(50.0)
|
|
342
|
+
Person.max(:age).should == 50.0
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
describe ".min" do
|
|
348
|
+
|
|
349
|
+
before do
|
|
350
|
+
@criteria = mock
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
it "returns the sum of a new criteria" do
|
|
354
|
+
Person.expects(:criteria).returns(@criteria)
|
|
355
|
+
@criteria.expects(:min).with(:age).returns(50.0)
|
|
356
|
+
Person.min(:age).should == 50.0
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
describe ".not_in" do
|
|
362
|
+
|
|
363
|
+
it "returns a new criteria with select conditions added" do
|
|
364
|
+
criteria = Person.not_in(:aliases => [ "Bond", "007" ])
|
|
365
|
+
criteria.selector.should == { :aliases => { "$nin" => [ "Bond", "007" ] } }
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
describe ".paginate" do
|
|
371
|
+
|
|
372
|
+
before do
|
|
373
|
+
@criteria = stub(:page => 1, :per_page => "20", :count => 100)
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
context "when pagination parameters are passed" do
|
|
377
|
+
|
|
378
|
+
before do
|
|
379
|
+
@params = { :conditions => { :test => "Test" }, :page => 2, :per_page => 20 }
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
it "delegates to will paginate with the results" do
|
|
383
|
+
Mongoid::Criteria.expects(:translate).with(Person, @params).returns(@criteria)
|
|
384
|
+
@criteria.expects(:paginate).returns([])
|
|
385
|
+
Person.paginate(@params)
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
context "when pagination parameters are not passed" do
|
|
391
|
+
|
|
392
|
+
before do
|
|
393
|
+
@params = { :conditions => { :test => "Test" }}
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
it "delegates to will paginate with default values" do
|
|
397
|
+
Mongoid::Criteria.expects(:translate).with(Person, @params).returns(@criteria)
|
|
398
|
+
@criteria.expects(:paginate).returns([])
|
|
399
|
+
Person.paginate(:conditions => { :test => "Test" })
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
describe ".only" do
|
|
407
|
+
|
|
408
|
+
it "returns a new criteria with select conditions added" do
|
|
409
|
+
criteria = Person.only(:title, :age)
|
|
410
|
+
criteria.options.should == { :fields => [ :title, :age ] }
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
describe ".sum" do
|
|
416
|
+
|
|
417
|
+
before do
|
|
418
|
+
@criteria = mock
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
it "returns the sum of a new criteria" do
|
|
422
|
+
Person.expects(:criteria).returns(@criteria)
|
|
423
|
+
@criteria.expects(:sum).with(:age).returns(50.0)
|
|
424
|
+
sum = Person.sum(:age)
|
|
425
|
+
sum.should == 50.0
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
describe ".where" do
|
|
431
|
+
|
|
432
|
+
it "returns a new criteria with select conditions added" do
|
|
433
|
+
criteria = Person.where(:title => "Sir")
|
|
434
|
+
criteria.selector.should == { :title => "Sir" }
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
end
|