mongo_doc 0.6.26 → 0.6.27

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.
Files changed (101) hide show
  1. data/README.textile +14 -203
  2. data/Rakefile +0 -134
  3. data/lib/mongo_doc/collection.rb +4 -0
  4. data/lib/mongo_doc/finders.rb +1 -0
  5. data/lib/mongo_doc/timestamps.rb +4 -3
  6. data/lib/mongo_doc/version.rb +3 -0
  7. data/lib/mongo_doc.rb +0 -5
  8. metadata +25 -212
  9. data/.document +0 -5
  10. data/.gitignore +0 -8
  11. data/.rspec +0 -2
  12. data/.rvmrc +0 -1
  13. data/.watchr +0 -32
  14. data/Gemfile +0 -3
  15. data/Gemfile.lock +0 -72
  16. data/HISTORY.md +0 -11
  17. data/VERSION +0 -1
  18. data/examples/simple_document.rb +0 -46
  19. data/examples/simple_object.rb +0 -34
  20. data/features/collections.feature +0 -9
  21. data/features/connection.feature +0 -13
  22. data/features/db_references.feature +0 -21
  23. data/features/embed_hash.feature +0 -16
  24. data/features/finders.feature +0 -76
  25. data/features/indexes.feature +0 -28
  26. data/features/mongodb.yml +0 -7
  27. data/features/mongodoc_base.feature +0 -128
  28. data/features/new_record.feature +0 -36
  29. data/features/partial_updates.feature +0 -95
  30. data/features/polymorphic_collections.feature +0 -16
  31. data/features/references.feature +0 -28
  32. data/features/removing_documents.feature +0 -68
  33. data/features/saving_an_object.feature +0 -15
  34. data/features/scopes.feature +0 -66
  35. data/features/step_definitions/collection_steps.rb +0 -17
  36. data/features/step_definitions/connection_steps.rb +0 -13
  37. data/features/step_definitions/document_steps.rb +0 -155
  38. data/features/step_definitions/documents.rb +0 -61
  39. data/features/step_definitions/embed_hash_steps.rb +0 -6
  40. data/features/step_definitions/field_steps.rb +0 -28
  41. data/features/step_definitions/finder_steps.rb +0 -15
  42. data/features/step_definitions/index_steps.rb +0 -10
  43. data/features/step_definitions/json_steps.rb +0 -9
  44. data/features/step_definitions/object_steps.rb +0 -50
  45. data/features/step_definitions/objects.rb +0 -24
  46. data/features/step_definitions/partial_update_steps.rb +0 -31
  47. data/features/step_definitions/query_steps.rb +0 -69
  48. data/features/step_definitions/removing_documents_steps.rb +0 -14
  49. data/features/step_definitions/scope_steps.rb +0 -18
  50. data/features/step_definitions/string_casting_steps.rb +0 -29
  51. data/features/step_definitions/util_steps.rb +0 -7
  52. data/features/string_casting.feature +0 -10
  53. data/features/support/support.rb +0 -12
  54. data/features/timestamps.feature +0 -14
  55. data/features/using_criteria.feature +0 -148
  56. data/mongo_doc.gemspec +0 -272
  57. data/mongodb.example.yml +0 -14
  58. data/perf/mongo_doc_object.rb +0 -83
  59. data/perf/mongo_document.rb +0 -84
  60. data/perf/ruby_driver.rb +0 -49
  61. data/script/console +0 -8
  62. data/spec/active_model_behavior.rb +0 -64
  63. data/spec/array_including_argument_matcher.rb +0 -62
  64. data/spec/associations/collection_proxy_spec.rb +0 -233
  65. data/spec/associations/document_proxy_spec.rb +0 -45
  66. data/spec/associations/hash_proxy_spec.rb +0 -181
  67. data/spec/associations/proxy_base_spec.rb +0 -92
  68. data/spec/associations_spec.rb +0 -218
  69. data/spec/attributes_accessor_spec.rb +0 -39
  70. data/spec/attributes_spec.rb +0 -184
  71. data/spec/bson_matchers.rb +0 -54
  72. data/spec/bson_spec.rb +0 -196
  73. data/spec/collection_spec.rb +0 -169
  74. data/spec/connection_spec.rb +0 -169
  75. data/spec/contexts/ids_spec.rb +0 -49
  76. data/spec/contexts/mongo_spec.rb +0 -241
  77. data/spec/contexts_spec.rb +0 -56
  78. data/spec/criteria/optional_spec.rb +0 -26
  79. data/spec/criteria_spec.rb +0 -73
  80. data/spec/cursor_spec.rb +0 -91
  81. data/spec/database_cleaner_spec.rb +0 -24
  82. data/spec/document_ext.rb +0 -9
  83. data/spec/document_spec.rb +0 -569
  84. data/spec/embedded_save_spec.rb +0 -73
  85. data/spec/ext_spec.rb +0 -100
  86. data/spec/finders_spec.rb +0 -61
  87. data/spec/hash_matchers.rb +0 -29
  88. data/spec/index_spec.rb +0 -79
  89. data/spec/matchers_spec.rb +0 -342
  90. data/spec/mongodb.yml +0 -6
  91. data/spec/mongodb_pairs.yml +0 -8
  92. data/spec/new_record_spec.rb +0 -128
  93. data/spec/polymorphic_collection_spec.rb +0 -47
  94. data/spec/references_many_spec.rb +0 -214
  95. data/spec/references_spec.rb +0 -121
  96. data/spec/root_spec.rb +0 -41
  97. data/spec/scope_spec.rb +0 -88
  98. data/spec/spec_helper.rb +0 -16
  99. data/spec/timestamps_spec.rb +0 -154
  100. data/spec/update_spec.rb +0 -167
  101. data/spec/validations_spec.rb +0 -30
@@ -1,169 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "MongoDoc::Collection" do
4
-
5
- let(:mongo_collection) { stub('collection') }
6
-
7
- it ".new delegates to .mongo_collection" do
8
- name = 'collection_name'
9
- MongoDoc::Collection.should_receive(:mongo_collection).with(name).and_return(mongo_collection)
10
- MongoDoc::Collection.new(name)
11
- end
12
-
13
- context "with the underlying Mongo::Collection" do
14
-
15
- let(:collection) do
16
- MongoDoc::Collection.stub(:mongo_collection).and_return(mongo_collection)
17
- MongoDoc::Collection.new('collection_name')
18
- end
19
-
20
- it "has Criteria mixed in" do
21
- MongoDoc::Criteria.should === collection
22
- end
23
-
24
- it "#collection references self for Mongo context" do
25
- collection.send(:collection).should == collection
26
- end
27
-
28
- %w([] clear count create_index db distinct drop drop_index drop_indexes group hint index_information map_reduce mapreduce name options pk_factory remove rename size).each do |delegated_method|
29
- it "delegates #{delegated_method} to the Mongo::Collection" do
30
- mongo_collection.should_receive(delegated_method)
31
- collection.send(delegated_method)
32
- end
33
- end
34
-
35
- context "#find" do
36
- let(:cursor) { MongoDoc::Cursor.new(collection, stub('cursor', :close => nil)) }
37
-
38
- before do
39
- collection.stub(:wrapped_cursor).and_return(cursor)
40
- end
41
-
42
- it "wraps the cursor" do
43
- query = {'sample' => 'data'}
44
- options = { :limit => 1}
45
- collection.should_receive(:wrapped_cursor).with(query, options).and_return(cursor)
46
- collection.find(query, options)
47
- end
48
-
49
- it "calls the block with a wrapped cursor" do
50
- collection.find {|c| @result = c}
51
- @result.should == cursor
52
- end
53
- end
54
-
55
- context "#find_one" do
56
- let(:bson) { stub('bson') }
57
-
58
- before do
59
- mongo_collection.stub(:find_one).and_return(bson)
60
- end
61
-
62
- it "delegates to the Mongo::Collection" do
63
- spec = { 'sample' => 'data' }
64
- options = {:limit => 1}
65
- mongo_collection.should_receive(:find_one).with(spec, options)
66
- collection.find_one(spec, options)
67
- end
68
-
69
- it "converts the result back from bson" do
70
- MongoDoc::BSON.should_receive(:decode).with(bson)
71
- collection.find_one({ 'sample' => 'data' })
72
- end
73
-
74
- it "returns the converted result" do
75
- obj = stub('obj')
76
- MongoDoc::BSON.stub(:decode).and_return(obj)
77
- collection.find_one({ 'sample' => 'data' }).should == obj
78
- end
79
-
80
- it "returns nil if the delegate returns nil" do
81
- mongo_collection.stub(:find_one)
82
- collection.find_one({ 'sample' => 'data' }).should be_nil
83
- end
84
- end
85
-
86
- context "#insert" do
87
- let(:doc) { {'sample' => 'data'} }
88
- let(:options) { {:safe => false} }
89
-
90
- it "delegates to the Mongo::Collection" do
91
- mongo_collection.should_receive(:insert).with(doc, options)
92
- collection.insert(doc, options)
93
- end
94
-
95
- it "converts the doc_or_docs to bson" do
96
- doc.should_receive(:to_bson)
97
- mongo_collection.stub(:insert)
98
- collection.insert(doc, options)
99
- end
100
-
101
- it "returns the delegates result" do
102
- result = 'result'
103
- mongo_collection.stub(:insert).and_return(result)
104
- collection.insert(doc).should == result
105
- end
106
- end
107
-
108
- context "#save" do
109
- let(:doc) { {'sample' => 'data'} }
110
-
111
- it "delegates to the Mongo::Collection" do
112
- options = {:safe => false}
113
- mongo_collection.should_receive(:save).with(doc, options)
114
- collection.save(doc, options)
115
- end
116
-
117
- it "converts the doc to bson" do
118
- doc.should_receive(:to_bson)
119
- mongo_collection.stub(:save)
120
- collection.save(doc)
121
- end
122
-
123
- it "returns the delegates result" do
124
- result = 'result'
125
- mongo_collection.stub(:save).and_return(result)
126
- collection.save(doc).should == result
127
- end
128
- end
129
-
130
- context "#update" do
131
- let(:spec) { {'sample' => 'old'} }
132
-
133
- let(:doc) { {'sample' => 'data'} }
134
-
135
- let(:options) { {:safe => false} }
136
-
137
- before do
138
- collection.stub(:last_error).and_return('updatedExisting' => true)
139
- mongo_collection.stub(:update)
140
- end
141
-
142
- it "delegates to the Mongo::Collection" do
143
- mongo_collection.should_receive(:update).with(spec, doc, options)
144
- collection.update(spec, doc, options)
145
- end
146
-
147
- it "converts the doc to bson" do
148
- doc.should_receive(:to_bson)
149
- collection.update(spec, doc, options)
150
- end
151
-
152
- it "gets the last error from the database" do
153
- collection.should_receive(:last_error)
154
- collection.update(spec, doc, options)
155
- end
156
-
157
- it "returns the updateExisting value of get last error" do
158
- result = 'check'
159
- collection.stub(:last_error).and_return({'updatedExisting' => result})
160
- collection.update(spec, doc, options).should == result
161
- end
162
-
163
- it "returns false otherwise" do
164
- collection.stub(:last_error)
165
- collection.update(spec, doc, options).should be_false
166
- end
167
- end
168
- end
169
- end
@@ -1,169 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # Resets for testing
4
- module MongoDoc
5
- module Connection
6
-
7
- def reset
8
- @config_path = nil
9
- @configuration = nil
10
- @connection = nil
11
- @database = nil
12
- @host = nil
13
- @name = nil
14
- @options = nil
15
- @port = nil
16
- @strict = nil
17
- end
18
-
19
- end
20
- end
21
-
22
- describe "MongoDoc::Connection.Connections" do
23
-
24
- before do
25
- MongoDoc::Connection.reset
26
- end
27
-
28
- describe "#connection" do
29
- let(:conn) { mock }
30
-
31
- it "allows the connection to be set" do
32
- MongoDoc::Connection.connection = conn
33
- MongoDoc::Connection.connection.should == conn
34
- end
35
- end
36
-
37
- context "Non-rails environment" do
38
-
39
- it "does not see Rails" do
40
- Object.const_defined?('Rails').should be_false
41
- end
42
-
43
- it "default the configuration location to './mongodb.yml'" do
44
- MongoDoc::Connection.config_path.should == './mongodb.yml'
45
- end
46
-
47
- context "without a configuration" do
48
- let(:connection) { stub('connection') }
49
-
50
- before do
51
- MongoDoc::Connection.stub(:connect).and_return(connection)
52
- end
53
-
54
- it "creates a default connection" do
55
- MongoDoc::Connection.should_receive(:connect).and_return(connection)
56
- MongoDoc::Connection.connection
57
- end
58
-
59
- it "creates a default database with strict false" do
60
- connection.should_receive(:db).with("mongo_doc", :strict => false)
61
- MongoDoc::Connection.database
62
- end
63
- end
64
- end
65
-
66
- context "Rails environment" do
67
-
68
- class FauxApp
69
- def root
70
- Pathname.new('faux_app')
71
- end
72
- end
73
-
74
- module FauxRails
75
- extend self
76
-
77
- def env
78
- 'staging'
79
- end
80
- end
81
-
82
- before(:all) do
83
- Object.const_set('Rails', FauxRails)
84
- require 'mongo_doc/railties/config'
85
- end
86
-
87
- before do
88
- MongoDoc::Railties::Config.config(FauxApp.new)
89
- end
90
-
91
- after(:all) do
92
- Object.send(:remove_const, 'Rails')
93
- end
94
-
95
- it "sees Rails" do
96
- Object.const_defined?('Rails').should be_true
97
- end
98
-
99
- it "default the configuration location to Rails.root + '/config/mongodb.yml'" do
100
- MongoDoc::Connection.config_path.to_s.should == 'faux_app/config/mongodb.yml'
101
- end
102
-
103
- context "without a configuration" do
104
- let(:connection) { stub('connection') }
105
-
106
- before do
107
- MongoDoc::Connection.stub(:connect).and_return(connection)
108
- end
109
-
110
- it "creates a default connection" do
111
- MongoDoc::Connection.should_receive(:connect).and_return(connection)
112
- MongoDoc::Connection.connection
113
- end
114
-
115
- it "creates a default database with strict false" do
116
- connection.should_receive(:db).with("faux_app_staging", :strict => false)
117
- MongoDoc::Connection.database
118
- end
119
- end
120
- end
121
-
122
- context ".verify_server_version" do
123
- let(:connection) { stub('connection') }
124
-
125
- before do
126
- MongoDoc::Connection.connection = connection
127
- end
128
-
129
- it "raises when the server version is unsupported" do
130
- connection.stub(:server_version).and_return(Mongo::ServerVersion.new('1.3.4'))
131
- lambda { MongoDoc::Connection.send(:verify_server_version) }.should raise_error(MongoDoc::UnsupportedServerVersionError)
132
- end
133
-
134
- it "returns when the server version is supported" do
135
- connection.stub(:server_version).and_return(Mongo::ServerVersion.new('1.4.0'))
136
- lambda { MongoDoc::Connection.send(:verify_server_version) }.should_not raise_error(MongoDoc::UnsupportedServerVersionError)
137
- end
138
- end
139
-
140
- describe ".connect" do
141
- let(:connection) { stub('connection') }
142
-
143
- before do
144
- MongoDoc::Connection.stub(:verify_server_version)
145
- end
146
-
147
- it "creates a Mongo::Connection" do
148
- host = 'host'
149
- port = 'port'
150
- options = 'options'
151
- MongoDoc::Connection.host = host
152
- MongoDoc::Connection.port = port
153
- MongoDoc::Connection.options = options
154
- Mongo::Connection.should_receive(:new).with(host, port, options).and_return(connection)
155
- MongoDoc::Connection.send(:connect)
156
- end
157
-
158
- it "raises NoConnectionError if the connection fails" do
159
- Mongo::Connection.stub(:new).and_return(nil)
160
- lambda { MongoDoc::Connection.send(:connect) }.should raise_error(MongoDoc::NoConnectionError)
161
- end
162
-
163
- it "verifies the connection version" do
164
- Mongo::Connection.stub(:new).and_return(connection)
165
- MongoDoc::Connection.should_receive(:verify_server_version)
166
- MongoDoc::Connection.send(:connect)
167
- end
168
- end
169
- end
@@ -1,49 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "MongoDoc::Contexts::Ids" do
4
-
5
- class Address
6
- include MongoDoc::Document
7
- include MongoDoc::Matchers
8
-
9
- attr_accessor :number
10
- attr_accessor :street
11
- end
12
-
13
- let(:criteria) { Mongoid::Criteria.new(Address) }
14
- let(:context) { criteria.context }
15
-
16
- context "#id_criteria" do
17
- context "single id" do
18
- let(:id) { 'a' * 24 }
19
- let(:obj_id) { BSON::ObjectID.from_string(id) }
20
-
21
- it "converts strings to an object id" do
22
- criteria.should_receive(:id).with(obj_id)
23
- context.stub(:one)
24
- context.id_criteria(id)
25
- end
26
-
27
- it "delegates to one if passed a string or ObjectID" do
28
- context.should_receive(:one)
29
- context.id_criteria(id)
30
- end
31
- end
32
-
33
- context "mutliple ids" do
34
- let(:ids) { ['a' * 24, 'b' * 24] }
35
- let(:obj_ids) { [BSON::ObjectID.from_string(ids.first), BSON::ObjectID.from_string(ids.last)] }
36
-
37
- it "converts strings to an object id" do
38
- criteria.should_receive(:id).with(obj_ids)
39
- criteria.stub(:entries)
40
- context.id_criteria(ids)
41
- end
42
-
43
- it "delegates to entries if passed an array" do
44
- criteria.should_receive(:entries)
45
- context.id_criteria(ids)
46
- end
47
- end
48
- end
49
- end
@@ -1,241 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "MongoDoc::Contexts::Mongo" do
4
-
5
- class Address
6
- include MongoDoc::Document
7
- include MongoDoc::Matchers
8
-
9
- attr_accessor :number
10
- attr_accessor :street
11
- end
12
-
13
- let(:criteria) { Mongoid::Criteria.new(Address) }
14
- let(:context) { criteria.context }
15
-
16
- context "#initialize" do
17
- it "sets the criteria" do
18
- context.criteria.should == criteria
19
- end
20
- end
21
-
22
- context "#collection" do
23
- it "delegates to klass" do
24
- klass = mock('klass', :collection => true)
25
- context.should_receive(:klass).and_return(klass)
26
- context.collection
27
- end
28
- end
29
-
30
- context "querying" do
31
- let(:collection) { stub('collection') }
32
-
33
- before { context.stub(:collection).and_return(collection) }
34
-
35
- context "#aggregate" do
36
- it "uses group with the appropriate JS" do
37
- collection.should_receive(:group).with(nil, {}, {:count=>0}, MongoDoc::Contexts::Mongo::AGGREGATE_REDUCE, true)
38
- context.aggregate
39
- end
40
- end
41
-
42
- context "#avg" do
43
- it "is the sum/count" do
44
- context.should_receive(:count).and_return(1)
45
- context.should_receive(:sum).with(:field_name).and_return(1)
46
- context.avg(:field_name)
47
- end
48
-
49
- it "returns nil if there is no sum" do
50
- context.stub(:sum).and_return(nil)
51
- context.avg(:field_name).should be_nil
52
- end
53
- end
54
-
55
- context "#count" do
56
- it "uses find and count" do
57
- result = stub('result')
58
- result.should_receive(:count)
59
- collection.should_receive(:find).and_return(result)
60
- context.count
61
- end
62
- end
63
-
64
- context "#distinct" do
65
- it "uses distinct" do
66
- collection.should_receive(:distinct).with(:field_name, {})
67
- context.distinct(:field_name)
68
- end
69
- end
70
-
71
- context "#execute" do
72
- it "uses filtered options" do
73
- collection.should_receive(:find).with({},{})
74
- criteria.cache
75
- context.execute
76
- end
77
-
78
- it "uses find" do
79
- collection.should_receive(:find)
80
- context.execute
81
- end
82
-
83
- it "returns [] if nothing returned from find" do
84
- collection.stub(:find => nil)
85
- context.execute.should == []
86
- end
87
-
88
- it "returns the cursor if one is returned from find" do
89
- cursor = stub('cursor')
90
- collection.stub(:find => cursor)
91
- context.execute.should == cursor
92
- end
93
-
94
- it "memoizes the count if paginating" do
95
- count = 20
96
- cursor = stub('cursor', :count => count)
97
- collection.stub(:find => cursor)
98
- context.execute
99
- context.count.should == count
100
- end
101
- end
102
-
103
- context "#group" do
104
- it "uses group with the appropriate JS" do
105
- collection.should_receive(:group).with(nil, {}, {:group=>[]}, MongoDoc::Contexts::Mongo::GROUP_REDUCE, true).and_return([])
106
- context.group
107
- end
108
-
109
- it "decodes returned documents" do
110
- doc = stub('doc')
111
- collection.stub(:group).and_return([{:group => [doc]}])
112
- MongoDoc::BSON.should_receive(:decode).and_return(doc)
113
- context.group
114
- end
115
- end
116
-
117
- context "#iterate" do
118
-
119
- it "delegates to caching if cached" do
120
- context.should_receive(:caching)
121
- criteria.cache
122
- context.iterate
123
- end
124
-
125
- it "iterates over the results of execute" do
126
- item = stub('item')
127
- context.stub(:execute).and_return([item])
128
- context.iterate do |doc|
129
- doc.should == item
130
- end
131
- end
132
-
133
- context "#caching" do
134
- let(:item) { stub('item') }
135
-
136
- before do
137
- criteria.cache
138
- end
139
-
140
- context "when not previously cached" do
141
- it "iterates over the results of execute" do
142
- context.stub(:execute).and_return([item])
143
- context.iterate do |doc|
144
- doc.should == item
145
- end
146
- end
147
-
148
- it "memoizes the result" do
149
- context.stub(:execute).and_return([item])
150
- context.iterate
151
- context.cache.should == [item]
152
- end
153
- end
154
-
155
- context "when previously cached" do
156
- before do
157
- context.instance_variable_set(:@cache, [item])
158
- end
159
-
160
- it "does not execute" do
161
- context.should_not_receive(:execute)
162
- context.iterate do |doc|
163
- doc.should == item
164
- end
165
- end
166
-
167
- it "iterates over the results of execute" do
168
- context.should_not_receive(:execute)
169
- acc = []
170
- context.iterate do |doc|
171
- acc << doc
172
- end
173
- acc.should == [item]
174
- end
175
- end
176
- end
177
- end
178
-
179
- context "#last" do
180
- it "delegates to find_one" do
181
- collection.should_receive(:find_one).with({}, {:sort=>[[:_id, :desc]]})
182
- context.last
183
- end
184
- end
185
-
186
- context "#max" do
187
- it "delegates to grouped" do
188
- context.should_receive(:grouped).with(:max, "number", MongoDoc::Contexts::Mongo::MAX_REDUCE)
189
- context.max(:number)
190
- end
191
- end
192
-
193
- context "#min" do
194
- it "delegates to grouped" do
195
- context.should_receive(:grouped).with(:min, "number", MongoDoc::Contexts::Mongo::MIN_REDUCE)
196
- context.min(:number)
197
- end
198
- end
199
-
200
- context "#one" do
201
- it "delegates to find_one" do
202
- collection.should_receive(:find_one).with({}, {})
203
- context.one
204
- end
205
- end
206
-
207
- context "#sum" do
208
- it "delegates to grouped" do
209
- context.should_receive(:grouped).with(:sum, "number", MongoDoc::Contexts::Mongo::SUM_REDUCE)
210
- context.sum(:number)
211
- end
212
- end
213
-
214
- context "#grouped" do
215
- it "delegates to group" do
216
- op = 'op'
217
- field = 'name'
218
- reduce = '[field]'
219
- collection.should_receive(:group).with(nil, {}, {op =>"start"}, field, true).and_return([])
220
- context.send(:grouped, op, field, reduce)
221
- end
222
- end
223
-
224
- context "#empty? or #blank?" do
225
- it "delegates to find_one" do
226
- collection.should_receive(:find_one).with({}, {})
227
- context.empty?
228
- end
229
-
230
- it "returns true if find_one returns no result" do
231
- collection.stub(:find_one).and_return(nil)
232
- context.should be_blank
233
- end
234
-
235
- it "returns false if find_one returns any result" do
236
- collection.stub(:find_one).and_return(stub('result'))
237
- context.should_not be_empty
238
- end
239
- end
240
- end
241
- end
@@ -1,56 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe MongoDoc::Contexts do
4
-
5
- class ContextTest
6
- include MongoDoc::Document
7
-
8
- embed_many :children
9
- end
10
-
11
- context ".context_for" do
12
- let(:criteria) { stub('criteria', :klass => klass) }
13
-
14
- context "when criteria is for a top-level MongoDoc::Document" do
15
- let(:klass) { ContextTest }
16
-
17
- it "creates a Mongo context" do
18
- MongoDoc::Contexts::Mongo.should_receive(:new).with(criteria)
19
- Mongoid::Contexts.context_for(criteria)
20
- end
21
- end
22
-
23
- context "when criteria is for an association" do
24
- let(:klass) { ContextTest.new.children }
25
-
26
- it "creates an Enumerable context" do
27
- Mongoid::Contexts::Enumerable.should_receive(:new).with(criteria)
28
- Mongoid::Contexts.context_for(criteria)
29
- end
30
- end
31
-
32
- context "when criteria is for a MongoDoc::Collection" do
33
- let(:klass) { MongoDoc::Collection.new('collection') }
34
-
35
- before do
36
- MongoDoc::Collection.stub(:mongo_collection).and_return(stub('collection'))
37
- end
38
-
39
- it "creates a Mongo context" do
40
- MongoDoc::Contexts::Mongo.should_receive(:new).with(criteria)
41
- Mongoid::Contexts.context_for(criteria)
42
- end
43
- end
44
-
45
- context "when criteria is not recognized" do
46
- let(:klass) { Object }
47
-
48
- it "raises an exception" do
49
- expect do
50
- Mongoid::Contexts.context_for(criteria)
51
- end.should raise_error(Mongoid::Contexts::UnknownContext)
52
- end
53
- end
54
- end
55
- end
56
-
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::Criterion::Optional do
4
- class OptionalTest
5
- include MongoDoc::Document
6
- end
7
-
8
- let(:criteria) { Mongoid::Criteria.new(OptionalTest) }
9
- let(:id) { BSON::ObjectID.new }
10
- let(:string_id) { id.to_s }
11
-
12
- describe "#id" do
13
- it "converts a string id to bson ids" do
14
- criteria.id(string_id)
15
- BSON::ObjectID.should === criteria.selector[:_id]
16
- end
17
-
18
- it "converts many string ids to bson ids" do
19
- criteria.id(string_id, string_id)
20
- criteria.selector[:_id].should have_key('$in')
21
- criteria.selector[:_id]['$in'].each do |selector_id|
22
- BSON::ObjectID.should === selector_id
23
- end
24
- end
25
- end
26
- end