mongodoc 0.0.0 → 0.1.0

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.
@@ -0,0 +1,180 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "MongoDoc::Collection" do
4
+ it ".mongo_collection creates a Mongo::Collection of the appropriate name" do
5
+ name = 'collection_name'
6
+ db = stub('db')
7
+ db.should_receive(:collection).with(name)
8
+ MongoDoc.should_receive(:database).and_return(db)
9
+ MongoDoc::Collection.mongo_collection(name)
10
+ end
11
+
12
+ it ".new generates a Mongo::Collection by calling .mongo_collection" do
13
+ name = 'collection_name'
14
+ MongoDoc::Collection.should_receive(:mongo_collection).with(name)
15
+ MongoDoc::Collection.new(name)
16
+ end
17
+
18
+ context "with the underlying Mongo::Collection" do
19
+ before do
20
+ @mongo_collection = stub('collection')
21
+ MongoDoc::Collection.stub(:mongo_collection).and_return(@mongo_collection)
22
+ end
23
+
24
+ it "#_collection is the underlying Mongo::Collection" do
25
+ MongoDoc::Collection.new('collection_name')._collection.should == @mongo_collection
26
+ end
27
+
28
+ %w([] clear count create_index db drop drop_index drop_indexes group hint index_information name options 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
+ MongoDoc::Collection.new('collection_name').send(delegated_method)
32
+ end
33
+ end
34
+
35
+ context "#find" do
36
+ before do
37
+ @query = { 'sample' => 'data' }
38
+ @options = {:limit => 1}
39
+ @block = lambda {}
40
+ @cursor = stub('cursor', :close => nil)
41
+ @collection = MongoDoc::Collection.new('collection_name')
42
+ @mongo_collection.stub(:find).and_return(@cursor)
43
+ MongoDoc::Cursor.stub(:new).and_return(@cursor)
44
+ end
45
+
46
+ it "delegates to the Mongo::Collection" do
47
+ @mongo_collection.should_receive(:find).with(@query, @options, &@block).and_return(@cursor)
48
+ @collection.find(@query, @options, &@block)
49
+ end
50
+
51
+ it "wraps the cursor" do
52
+ MongoDoc::Cursor.should_receive(:new).with(@cursor).and_return(@cursor)
53
+ @collection.find(@query, @options, &@block)
54
+ end
55
+
56
+ it "calls the block with a wrapped cursor" do
57
+ @collection.find(@query, @options) {|cursor| @result = cursor}
58
+ @result.should == @cursor
59
+ end
60
+ end
61
+
62
+ context "#find_one" do
63
+ before do
64
+ @spec_or_object_id = { 'sample' => 'data' }
65
+ @options = {:limit => 1}
66
+ @collection = MongoDoc::Collection.new('collection_name')
67
+ @bson = stub('bson')
68
+ @mongo_collection.stub(:find_one).and_return(@bson)
69
+ end
70
+
71
+ it "delegates to the Mongo::Collection" do
72
+ @mongo_collection.should_receive(:find_one).with(@spec_or_object_id, @options)
73
+ @collection.find_one(@spec_or_object_id, @options)
74
+ end
75
+
76
+ it "converts the result back from bson" do
77
+ MongoDoc::BSON.should_receive(:decode).with(@bson)
78
+ @collection.find_one(@spec_or_object_id, @options)
79
+ end
80
+
81
+ it "returns the converted result" do
82
+ obj = stub('obj')
83
+ MongoDoc::BSON.stub(:decode).and_return(obj)
84
+ @collection.find_one(@spec_or_object_id, @options).should == obj
85
+ end
86
+
87
+ it "returns nil if the delegate returns nil" do
88
+ @mongo_collection.stub(:find_one)
89
+ @collection.find_one(@spec_or_object_id, @options).should be_nil
90
+ end
91
+ end
92
+
93
+ context "#insert" do
94
+ before do
95
+ @doc = { 'sample' => 'data' }
96
+ @options = {:safe => false}
97
+ end
98
+
99
+ it "delegates to the Mongo::Collection" do
100
+ @mongo_collection.should_receive(:insert).with(@doc, @options)
101
+ MongoDoc::Collection.new('collection_name').insert(@doc, @options)
102
+ end
103
+
104
+ it "converts the doc_or_docs to bson" do
105
+ @doc.should_receive(:to_bson)
106
+ @mongo_collection.stub(:insert)
107
+ MongoDoc::Collection.new('collection_name').insert(@doc, @options)
108
+ end
109
+
110
+ it "returns the delegates result" do
111
+ result = 'result'
112
+ @mongo_collection.stub(:insert).and_return(result)
113
+ MongoDoc::Collection.new('collection_name').insert(@doc, @options).should == result
114
+ end
115
+ end
116
+
117
+ context "#save" do
118
+ before do
119
+ @doc = { 'sample' => 'data' }
120
+ @options = {:safe => false}
121
+ end
122
+
123
+ it "delegates to the Mongo::Collection" do
124
+ @mongo_collection.should_receive(:save).with(@doc, @options)
125
+ MongoDoc::Collection.new('collection_name').save(@doc, @options)
126
+ end
127
+
128
+ it "converts the doc to bson" do
129
+ @doc.should_receive(:to_bson)
130
+ @mongo_collection.stub(:save)
131
+ MongoDoc::Collection.new('collection_name').save(@doc, @options)
132
+ end
133
+
134
+ it "returns the delegates result" do
135
+ result = 'result'
136
+ @mongo_collection.stub(:save).and_return(result)
137
+ MongoDoc::Collection.new('collection_name').save(@doc, @options).should == result
138
+ end
139
+ end
140
+
141
+ context "#update" do
142
+ before do
143
+ @spec = { 'sample' => 'old' }
144
+ @doc = { 'sample' => 'data' }
145
+ @options = {:safe => false}
146
+ @database = stub('database', :db_command => nil)
147
+ MongoDoc.stub(:database).and_return(@database)
148
+ end
149
+
150
+ it "delegates to the Mongo::Collection" do
151
+ @mongo_collection.should_receive(:update).with(@spec, @doc, @options)
152
+ MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options)
153
+ end
154
+
155
+ it "converts the doc to bson" do
156
+ @doc.should_receive(:to_bson)
157
+ @mongo_collection.stub(:update)
158
+ MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options)
159
+ end
160
+
161
+ it "gets the last error from the database" do
162
+ @mongo_collection.stub(:update)
163
+ @database.should_receive(:db_command).with({'getlasterror' => 1})
164
+ MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options)
165
+ end
166
+
167
+ it "returns the updateExisting value of get last error" do
168
+ result = 'check'
169
+ @mongo_collection.stub(:update)
170
+ @database.stub(:db_command).and_return({'updatedExisting' => result})
171
+ MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options).should == result
172
+ end
173
+
174
+ it "returns false otherwise" do
175
+ @mongo_collection.stub(:update)
176
+ MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options).should be_false
177
+ end
178
+ end
179
+ end
180
+ end
@@ -5,7 +5,12 @@ describe "MongoDoc Connections" do
5
5
  it ".connection raises a no connection error when connect has not been called" do
6
6
  lambda {MongoDoc.connection}.should raise_error(MongoDoc::NoConnectionError)
7
7
  end
8
-
8
+
9
+ it ".connection raises a no connection error when the connection has failed" do
10
+ MongoDoc.send(:class_variable_set, :@@connection, nil)
11
+ lambda {MongoDoc.connection}.should raise_error(MongoDoc::NoConnectionError)
12
+ end
13
+
9
14
  describe ".connect" do
10
15
  it "when called with no params just connects" do
11
16
  Mongo::Connection.should_receive(:new)
@@ -54,6 +59,11 @@ describe "MongoDoc Connections" do
54
59
  lambda {MongoDoc.database}.should raise_error(MongoDoc::NoDatabaseError)
55
60
  end
56
61
 
62
+ it "raises a no database error when the database connect failed" do
63
+ MongoDoc.send(:class_variable_set, :@@database, nil)
64
+ lambda {MongoDoc.database}.should raise_error(MongoDoc::NoDatabaseError)
65
+ end
66
+
57
67
  it "returns the current database when not given any arguments" do
58
68
  db = 'db'
59
69
  MongoDoc.send(:class_variable_set, :@@database, db)