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,98 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Collections do
|
|
4
|
+
|
|
5
|
+
describe ".collection" do
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
@person = Person.new
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "sets the collection name to the class pluralized" do
|
|
12
|
+
Person.collection.name.should == "people"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
context "when document is embedded" do
|
|
16
|
+
|
|
17
|
+
before do
|
|
18
|
+
@address = Address.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "raises an error" do
|
|
22
|
+
lambda { Address.collection }.should raise_error
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe ".collection_name=" do
|
|
28
|
+
|
|
29
|
+
context "on a parent class" do
|
|
30
|
+
|
|
31
|
+
it "sets the collection name on the document class" do
|
|
32
|
+
Patient.collection_name = "pats"
|
|
33
|
+
Patient.collection_name.should == "pats"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
context "on a subclass" do
|
|
38
|
+
|
|
39
|
+
after do
|
|
40
|
+
Canvas.collection_name = "canvases"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "sets the collection name for the entire hierarchy" do
|
|
44
|
+
Firefox.collection_name = "browsers"
|
|
45
|
+
Canvas.collection_name.should == "browsers"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe ".index_information" do
|
|
51
|
+
|
|
52
|
+
it "returns index information from the collection" do
|
|
53
|
+
Person.index_information["title_1"].should_not be_nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe ".logger=" do
|
|
58
|
+
|
|
59
|
+
before do
|
|
60
|
+
@logger = stub.quacks_like(Logger.allocate)
|
|
61
|
+
Person.logger = @logger
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
after do
|
|
65
|
+
Person.logger = nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "sets the logger on the connection" do
|
|
69
|
+
Person.db.connection.logger.should == @logger
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
describe ".store_in" do
|
|
74
|
+
|
|
75
|
+
context "on a parent class" do
|
|
76
|
+
|
|
77
|
+
it "sets the collection name and collection for the document" do
|
|
78
|
+
Mongoid::Collection.expects(:new).with(Patient, "population").returns(@collection)
|
|
79
|
+
Patient.store_in :population
|
|
80
|
+
Patient.collection_name.should == "population"
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
context "on a subclass" do
|
|
85
|
+
|
|
86
|
+
after do
|
|
87
|
+
Mongoid::Collection.expects(:new).with(Firefox, "canvases")
|
|
88
|
+
Firefox.store_in :canvases
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "changes the collection name for the entire hierarchy" do
|
|
92
|
+
Mongoid::Collection.expects(:new).with(Firefox, "browsers").returns(@collection)
|
|
93
|
+
Firefox.store_in :browsers
|
|
94
|
+
Canvas.collection_name.should == "browsers"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Config do
|
|
4
|
+
let(:config) { Mongoid::Config.instance }
|
|
5
|
+
|
|
6
|
+
after do
|
|
7
|
+
config.reset
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
describe "#database=" do
|
|
11
|
+
|
|
12
|
+
context "when object provided is not a Mongo::DB" do
|
|
13
|
+
|
|
14
|
+
it "raises an error" do
|
|
15
|
+
lambda { config.database = "Test" }.should
|
|
16
|
+
raise_error(Mongoid::Errors::InvalidDatabase)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe "#destructive_fields" do
|
|
22
|
+
|
|
23
|
+
it "returns an array of bad field names" do
|
|
24
|
+
config.destructive_fields.should include("collection")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe "#from_hash" do
|
|
29
|
+
context "regular mongoid.yml" do
|
|
30
|
+
before do
|
|
31
|
+
file_name = File.join(File.dirname(__FILE__), "..", "..", "config", "mongoid.yml")
|
|
32
|
+
@settings = YAML.load(ERB.new(File.new(file_name).read).result)
|
|
33
|
+
config.from_hash(@settings["test"])
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
after { config.reset }
|
|
37
|
+
|
|
38
|
+
it "sets the master db" do
|
|
39
|
+
config.master.name.should == "mongoid_config_test"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "sets allow_dynamic_fields" do
|
|
43
|
+
config.allow_dynamic_fields.should == false
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "sets reconnect_time" do
|
|
47
|
+
config.reconnect_time.should == 5
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "sets parameterize keys" do
|
|
51
|
+
config.parameterize_keys.should == false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "sets persist_in_safe_mode" do
|
|
55
|
+
config.persist_in_safe_mode.should == false
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "sets raise_not_found_error" do
|
|
59
|
+
config.raise_not_found_error.should == false
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "sets use_object_ids" do
|
|
63
|
+
config.use_object_ids.should == true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "returns nil, which is interpreted as the local time_zone" do
|
|
67
|
+
config.use_utc.should be_false
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
context "mongoid_with_utc.yml" do
|
|
72
|
+
before do
|
|
73
|
+
file_name = File.join(File.dirname(__FILE__), "..", "..", "config", "mongoid_with_utc.yml")
|
|
74
|
+
file = File.new(file_name)
|
|
75
|
+
@settings = YAML.load(file.read)["test"]
|
|
76
|
+
config.from_hash(@settings)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
after { config.reset }
|
|
80
|
+
|
|
81
|
+
it "sets time_zone" do
|
|
82
|
+
config.use_utc.should be_true
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe "#master=" do
|
|
89
|
+
|
|
90
|
+
context "when object provided is not a Mongo::DB" do
|
|
91
|
+
|
|
92
|
+
it "raises an error" do
|
|
93
|
+
lambda { config.master = "Test" }.should
|
|
94
|
+
raise_error(Mongoid::Errors::InvalidDatabase)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
context "when the database version is not supported" do
|
|
99
|
+
|
|
100
|
+
let(:database) do
|
|
101
|
+
stub.quacks_like(Mongo::DB.allocate)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
let(:connection) do
|
|
105
|
+
stub.quacks_like(Mongo::Connection.allocate)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
let(:version) do
|
|
109
|
+
Mongo::ServerVersion.new("1.3.0")
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
before do
|
|
113
|
+
database.stubs(:kind_of?).returns(true)
|
|
114
|
+
database.stubs(:connection).returns(connection)
|
|
115
|
+
connection.stubs(:server_version).returns(version)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it "raises an error" do
|
|
119
|
+
lambda { config.master = database }.should
|
|
120
|
+
raise_error(Mongoid::Errors::UnsupportedVersion)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
describe "#parameterize_keys" do
|
|
126
|
+
|
|
127
|
+
it "defaults to true" do
|
|
128
|
+
config.parameterize_keys.should == true
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
describe "#persist_in_safe_mode=" do
|
|
133
|
+
|
|
134
|
+
context "when setting to true" do
|
|
135
|
+
|
|
136
|
+
before do
|
|
137
|
+
config.persist_in_safe_mode = true
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
it "sets the value" do
|
|
141
|
+
config.persist_in_safe_mode.should == true
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
context "when setting to false" do
|
|
147
|
+
|
|
148
|
+
before do
|
|
149
|
+
config.persist_in_safe_mode = false
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
after do
|
|
153
|
+
config.persist_in_safe_mode = true
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it "sets the value" do
|
|
157
|
+
config.persist_in_safe_mode.should == false
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
describe "#raise_not_found_error=" do
|
|
165
|
+
|
|
166
|
+
context "when setting to true" do
|
|
167
|
+
|
|
168
|
+
before do
|
|
169
|
+
config.raise_not_found_error = true
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it "sets the value" do
|
|
173
|
+
config.raise_not_found_error.should == true
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
context "when setting to false" do
|
|
179
|
+
|
|
180
|
+
before do
|
|
181
|
+
config.raise_not_found_error = false
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
it "sets the value" do
|
|
185
|
+
config.raise_not_found_error.should == false
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
describe "#reconnect!" do
|
|
193
|
+
|
|
194
|
+
before do
|
|
195
|
+
@connection = mock
|
|
196
|
+
@master = mock
|
|
197
|
+
config.expects(:master).returns(@master)
|
|
198
|
+
@master.expects(:connection).returns(@connection)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
it "reconnects on the master connection" do
|
|
202
|
+
@connection.expects(:connect_to_master).returns(true)
|
|
203
|
+
config.reconnect!
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
describe "#reconnect_time" do
|
|
208
|
+
|
|
209
|
+
it "defaults to 3" do
|
|
210
|
+
config.reconnect_time.should == 3
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
describe "#reconnect_time=" do
|
|
216
|
+
|
|
217
|
+
after do
|
|
218
|
+
config.reconnect_time = 3
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
it "sets the time" do
|
|
222
|
+
config.reconnect_time = 5
|
|
223
|
+
config.reconnect_time.should == 5
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
describe "#slaves=" do
|
|
228
|
+
|
|
229
|
+
context "when object provided is not a Mongo::DB" do
|
|
230
|
+
|
|
231
|
+
it "raises an error" do
|
|
232
|
+
lambda { config.slaves = ["Test"] }.should raise_error
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
context "when the database version is not supported" do
|
|
237
|
+
|
|
238
|
+
let(:database) do
|
|
239
|
+
stub.quacks_like(Mongo::DB.allocate)
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
let(:connection) do
|
|
243
|
+
stub.quacks_like(Mongo::Connection.allocate)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
let(:version) do
|
|
247
|
+
Mongo::ServerVersion.new("1.3.0")
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
before do
|
|
251
|
+
database.stubs(:kind_of?).returns(true)
|
|
252
|
+
database.stubs(:connection).returns(connection)
|
|
253
|
+
connection.stubs(:server_version).returns(version)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
it "raises an error" do
|
|
257
|
+
lambda { config.slaves = [ database ] }.should
|
|
258
|
+
raise_error(Mongoid::Errors::UnsupportedVersion)
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
describe "#allow_dynamic_fields=" do
|
|
264
|
+
|
|
265
|
+
context "when setting to true" do
|
|
266
|
+
|
|
267
|
+
before do
|
|
268
|
+
config.allow_dynamic_fields = true
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
it "sets the value" do
|
|
272
|
+
config.allow_dynamic_fields.should == true
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
context "when setting to false" do
|
|
278
|
+
|
|
279
|
+
before do
|
|
280
|
+
config.allow_dynamic_fields = false
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
it "sets the value" do
|
|
284
|
+
config.allow_dynamic_fields.should == false
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
describe "#use_object_ids" do
|
|
292
|
+
|
|
293
|
+
it "defaults to false" do
|
|
294
|
+
config.use_object_ids.should == false
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
end
|
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Mongoid::Contexts::Enumerable do
|
|
4
|
+
|
|
5
|
+
before do
|
|
6
|
+
@london = Address.new(:number => 1, :street => "Bond Street")
|
|
7
|
+
@shanghai = Address.new(:number => 10, :street => "Nan Jing Dong Lu")
|
|
8
|
+
@melbourne = Address.new(:number => 20, :street => "Bourke Street")
|
|
9
|
+
@new_york = Address.new(:number => 20, :street => "Broadway")
|
|
10
|
+
@docs = [ @london, @shanghai, @melbourne, @new_york ]
|
|
11
|
+
@criteria = Mongoid::Criteria.new(Address)
|
|
12
|
+
@criteria.documents = @docs
|
|
13
|
+
@criteria.only(:number)
|
|
14
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "#aggregate" do
|
|
18
|
+
|
|
19
|
+
before do
|
|
20
|
+
@counts = @context.aggregate
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "groups by the fields provided in the options" do
|
|
24
|
+
@counts.size.should == 3
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "stores the counts in proper groups" do
|
|
28
|
+
@counts[1].should == 1
|
|
29
|
+
@counts[10].should == 1
|
|
30
|
+
@counts[20].should == 2
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "#avg" do
|
|
35
|
+
|
|
36
|
+
it "returns the avg value for the supplied field" do
|
|
37
|
+
@context.avg(:number).should == 12.75
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
describe "#count" do
|
|
42
|
+
|
|
43
|
+
it "returns the size of the enumerable" do
|
|
44
|
+
@context.count.should == 4
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe "#distinct" do
|
|
50
|
+
|
|
51
|
+
context "when the criteria is limited" do
|
|
52
|
+
|
|
53
|
+
before do
|
|
54
|
+
@criteria.where(:street => "Bourke Street")
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "returns an array of distinct values for the field" do
|
|
58
|
+
@context.distinct(:street).should == [ "Bourke Street" ]
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context "when the criteria is not limited" do
|
|
63
|
+
|
|
64
|
+
before do
|
|
65
|
+
@criteria = Mongoid::Criteria.new(Address)
|
|
66
|
+
@criteria.documents = @docs
|
|
67
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "returns an array of distinct values for the field" do
|
|
71
|
+
@context.distinct(:street).should ==
|
|
72
|
+
[ "Bond Street", "Nan Jing Dong Lu", "Bourke Street", "Broadway" ]
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
describe "#execute" do
|
|
78
|
+
|
|
79
|
+
before do
|
|
80
|
+
@criteria = Mongoid::Criteria.new(Address)
|
|
81
|
+
@criteria.documents = @docs
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
context "when the selector is present" do
|
|
85
|
+
before do
|
|
86
|
+
@criteria.where(:street => "Bourke Street")
|
|
87
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
88
|
+
end
|
|
89
|
+
it "returns the matching documents from the array" do
|
|
90
|
+
@context.execute.should == [ @melbourne ]
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
context "when selector is empty" do
|
|
95
|
+
|
|
96
|
+
before do
|
|
97
|
+
@criteria.only(:number)
|
|
98
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it "returns all the documents" do
|
|
102
|
+
@context.execute.should == @docs
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
context "when skip and limit are in the options" do
|
|
107
|
+
|
|
108
|
+
before do
|
|
109
|
+
@criteria.skip(2).limit(2)
|
|
110
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "properly narrows down the matching results" do
|
|
114
|
+
@context.execute.should == [ @melbourne, @new_york ]
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
context "when limit is set without skip in the options" do
|
|
119
|
+
|
|
120
|
+
before do
|
|
121
|
+
@criteria.limit(2)
|
|
122
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "properly narrows down the matching results" do
|
|
126
|
+
@context.execute.size.should == 2
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
describe "#first" do
|
|
134
|
+
|
|
135
|
+
context "when a selector is present" do
|
|
136
|
+
before do
|
|
137
|
+
@criteria.where(:street => "Bourke Street")
|
|
138
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it "returns the first that matches the selector" do
|
|
142
|
+
@context.first.should == @melbourne
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
describe "#group" do
|
|
149
|
+
|
|
150
|
+
before do
|
|
151
|
+
@group = @context.group
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
it "groups by the fields provided in the options" do
|
|
155
|
+
@group.size.should == 3
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it "stores the documents in proper groups" do
|
|
159
|
+
@group[1].should == [ @london ]
|
|
160
|
+
@group[10].should == [ @shanghai ]
|
|
161
|
+
@group[20].should == [ @melbourne, @new_york ]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
describe ".initialize" do
|
|
167
|
+
|
|
168
|
+
let(:selector) { { :field => "value" } }
|
|
169
|
+
let(:options) { { :skip => 20 } }
|
|
170
|
+
let(:documents) { [stub] }
|
|
171
|
+
|
|
172
|
+
before do
|
|
173
|
+
@criteria = Mongoid::Criteria.new(Address)
|
|
174
|
+
@criteria.documents = documents
|
|
175
|
+
@criteria.where(selector).skip(20)
|
|
176
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it "sets the selector" do
|
|
180
|
+
@context.selector.should == selector
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
it "sets the options" do
|
|
184
|
+
@context.options.should == options
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
it "sets the documents" do
|
|
188
|
+
@context.documents.should == documents
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
describe "#iterate" do
|
|
194
|
+
before do
|
|
195
|
+
@criteria.where(:street => "Bourke Street")
|
|
196
|
+
@criteria.documents = @docs
|
|
197
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
it "executes the criteria" do
|
|
201
|
+
acc = []
|
|
202
|
+
@context.iterate do |doc|
|
|
203
|
+
acc << doc
|
|
204
|
+
end
|
|
205
|
+
acc.should == [@melbourne]
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
describe "#last" do
|
|
210
|
+
|
|
211
|
+
context "when the selector is present" do
|
|
212
|
+
before do
|
|
213
|
+
@criteria.where(:street => "Bourke Street")
|
|
214
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
215
|
+
end
|
|
216
|
+
it "returns the last matching in the enumerable" do
|
|
217
|
+
@context.last.should == @melbourne
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
describe "#max" do
|
|
224
|
+
|
|
225
|
+
it "returns the max value for the supplied field" do
|
|
226
|
+
@context.max(:number).should == 20
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
describe "#min" do
|
|
232
|
+
|
|
233
|
+
it "returns the min value for the supplied field" do
|
|
234
|
+
@context.min(:number).should == 1
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
describe "#one" do
|
|
240
|
+
|
|
241
|
+
context "when the selector is present" do
|
|
242
|
+
before do
|
|
243
|
+
@criteria.where(:street => "Bourke Street")
|
|
244
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
245
|
+
end
|
|
246
|
+
it "returns the first matching in the enumerable" do
|
|
247
|
+
@context.one.should == @melbourne
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
describe "#page" do
|
|
254
|
+
|
|
255
|
+
context "when the page option exists" do
|
|
256
|
+
|
|
257
|
+
before do
|
|
258
|
+
@criteria = Mongoid::Criteria.new(Person).extras({ :page => 5 })
|
|
259
|
+
@criteria.documents = []
|
|
260
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
it "returns the page option" do
|
|
264
|
+
@context.page.should == 5
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
context "when the page option does not exist" do
|
|
270
|
+
|
|
271
|
+
before do
|
|
272
|
+
@criteria = Mongoid::Criteria.new(Person)
|
|
273
|
+
@criteria.documents = []
|
|
274
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
it "returns 1" do
|
|
278
|
+
@context.page.should == 1
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
describe "#paginate" do
|
|
286
|
+
|
|
287
|
+
before do
|
|
288
|
+
@criteria = Person.criteria.skip(2).limit(2)
|
|
289
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
290
|
+
@results = @context.paginate
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
it "executes and paginates the results" do
|
|
294
|
+
@results.current_page.should == 2
|
|
295
|
+
@results.per_page.should == 2
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
describe "#per_page" do
|
|
301
|
+
|
|
302
|
+
context "when a limit option exists" do
|
|
303
|
+
|
|
304
|
+
it "returns 20" do
|
|
305
|
+
@context.per_page.should == 20
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
context "when a limit option does not exist" do
|
|
311
|
+
|
|
312
|
+
before do
|
|
313
|
+
@criteria = Person.criteria.limit(50)
|
|
314
|
+
@criteria.documents = []
|
|
315
|
+
@context = Mongoid::Contexts::Enumerable.new(@criteria)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
it "returns the limit" do
|
|
319
|
+
@context.per_page.should == 50
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
describe "#sum" do
|
|
327
|
+
|
|
328
|
+
it "returns the sum of all the field values" do
|
|
329
|
+
@context.sum(:number).should == 51
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
context "#id_criteria" do
|
|
335
|
+
|
|
336
|
+
let(:criteria) do
|
|
337
|
+
criteria = Mongoid::Criteria.new(Address)
|
|
338
|
+
criteria.documents = []
|
|
339
|
+
criteria
|
|
340
|
+
end
|
|
341
|
+
let(:context) { criteria.context }
|
|
342
|
+
|
|
343
|
+
context "with a single argument" do
|
|
344
|
+
|
|
345
|
+
let(:id) { BSON::ObjectID.new.to_s }
|
|
346
|
+
|
|
347
|
+
before do
|
|
348
|
+
criteria.expects(:id).with(id).returns(criteria)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
context "when the document is found" do
|
|
352
|
+
|
|
353
|
+
let(:document) { stub }
|
|
354
|
+
|
|
355
|
+
it "returns a matching document" do
|
|
356
|
+
context.expects(:one).returns(document)
|
|
357
|
+
document.expects(:blank? => false)
|
|
358
|
+
context.id_criteria(id).should == document
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
context "when the document is not found" do
|
|
364
|
+
|
|
365
|
+
it "raises an error" do
|
|
366
|
+
context.expects(:one).returns(nil)
|
|
367
|
+
lambda { context.id_criteria(id) }.should raise_error
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
context "multiple arguments" do
|
|
375
|
+
|
|
376
|
+
context "when an array of ids" do
|
|
377
|
+
|
|
378
|
+
let(:ids) do
|
|
379
|
+
(0..2).inject([]) { |ary, i| ary << BSON::ObjectID.new.to_s }
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
context "when documents are found" do
|
|
383
|
+
|
|
384
|
+
let(:docs) do
|
|
385
|
+
(0..2).inject([]) { |ary, i| ary << stub }
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
before do
|
|
389
|
+
criteria.expects(:id).with(ids).returns(criteria)
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
it "returns matching documents" do
|
|
393
|
+
context.expects(:execute).returns(docs)
|
|
394
|
+
context.id_criteria(ids).should == docs
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
context "when documents are not found" do
|
|
400
|
+
|
|
401
|
+
it "raises an error" do
|
|
402
|
+
context.expects(:execute).returns([])
|
|
403
|
+
lambda { context.id_criteria(ids) }.should raise_error
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
context "when an array of object ids" do
|
|
411
|
+
|
|
412
|
+
let(:ids) do
|
|
413
|
+
(0..2).inject([]) { |ary, i| ary << BSON::ObjectID.new }
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
context "when documents are found" do
|
|
417
|
+
|
|
418
|
+
let(:docs) do
|
|
419
|
+
(0..2).inject([]) { |ary, i| ary << stub }
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
before do
|
|
423
|
+
criteria.expects(:id).with(ids).returns(criteria)
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
it "returns matching documents" do
|
|
427
|
+
context.expects(:execute).returns(docs)
|
|
428
|
+
context.id_criteria(ids).should == docs
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
context "when documents are not found" do
|
|
434
|
+
|
|
435
|
+
it "raises an error" do
|
|
436
|
+
context.expects(:execute).returns([])
|
|
437
|
+
lambda { context.id_criteria(ids) }.should raise_error
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
end
|