main
1require "spec_helper"
2
3describe "mongo" do
4 let(:connection) { Mongo::MongoClient.new("localhost", 27017) }
5 let(:db) { connection.db("mydb") }
6
7 after :each do
8 db['test'].remove()
9 end
10
11 after :all do
12 connection.drop_database("mydb")
13 end
14
15 it "lists all the databases" do
16 connection.database_names.should_not be_empty
17 connection.database_info.each { |x| puts x.inspect }
18 end
19
20 it "connects to a database" do
21 db.should_not be_nil
22 end
23
24 it "connects to a collection" do
25 test = db.collection("test")
26 test.should_not be_nil
27 end
28
29 it "can insert into a collection" do
30 document = { "name" => "MongoDB", "type" => "database", "count" => 1, "info" => { "x" => 203, "y" => "102" } }
31 collection = db['test']
32 id = collection.insert(document)
33 id.should_not be_nil
34 collection.find_one.should_not be_nil
35 collection.find("_id" => id).first['name'].should == 'MongoDB'
36 end
37
38 it "can list all collections" do
39 db.collection_names.should_not be_empty
40 end
41
42 it "can insert multiple documents" do
43 collection = db['test']
44 100.times { |x| collection.insert("i" => x) }
45 collection.find.count.should == 100
46 end
47
48 it "can sort" do
49 collection = db['test']
50 100.times { |x| collection.insert("i" => x) }
51 results = collection.find.sort(:i).to_a
52 100.times { |x| results[x]['i'].should == x }
53 results = collection.find.sort(:i => :desc).to_a.reverse
54 100.times { |x| results[x]['i'].should == x }
55 end
56
57 it "can count" do
58 collection = db['test']
59 100.times { |x| collection.insert("i" => x) }
60 collection.count.should == 100
61 end
62
63 it "can find items greater than 50" do
64 collection = db['test']
65 100.times { |x| collection.insert("i" => x) }
66 results = collection.find("i" => { "$gt" => 50 }).map {|x| x['i'] }.to_a
67 results.count.should == 49
68 (51...100).each { |x| results.should include(x) }
69 end
70
71 it "can find items between 20 and 30" do
72 collection = db['test']
73 100.times { |x| collection.insert("i" => x) }
74 results = collection.find("i" => { "$gte" => 20, "$lte" => 30}).map {|x| x['i'] }.to_a
75 results.count.should == 11
76 (20...30).each { |x| results.should include(x) }
77 end
78
79 it "can return specific fields" do
80 collection = db['test']
81 id = collection.insert({'name' => 'blah', 'type' => "MongoDb"})
82 result = collection.find({"_id" => id}, :fields => ["name"]).first
83 result['name'].should == 'blah'
84 end
85
86 it "can also find using a regex" do
87 collection = db['test']
88 collection.insert({'name' => 'blah', 'type' => "MongoDb"})
89 result = collection.find({"name" => /b/}).first
90 result['name'].should == 'blah'
91 end
92
93 it "can update a document" do
94 collection = db['test']
95 id = collection.insert({"name" => 'mo'})
96 collection.update({"_id" => id}, {"name" => 'om'})
97 result = collection.find("_id" => id).first
98 result['name'].should == 'om'
99 end
100
101 it "can update a document using an atomic operator" do
102 collection = db['test']
103 id = collection.insert({"name" => 'mo'})
104 collection.update({"_id" => id}, { "$set" => { "name" => 'om'}})
105 result = collection.find("_id" => id).first
106 result['name'].should == 'om'
107 end
108
109 it "can remove documents" do
110 collection = db['test']
111 100.times { |x| collection.insert("i" => x) }
112 collection.remove("i" => 71)
113 collection.count.should == 99
114 collection.find("i" => 71).to_a.should be_empty
115 end
116
117 it "can remove all documents" do
118 collection = db['test']
119 100.times { |x| collection.insert("i" => x) }
120 collection.remove
121 collection.count.should == 0
122 end
123
124 it "can explain the query" do
125 collection = db['test']
126 100.times { |x| collection.insert("i" => x) }
127 collection.find("i" => 71).explain.should_not be_nil
128 end
129
130 it "can provide index information" do
131 collection = db['test']
132 collection.index_information.should_not be_nil
133 end
134
135 it "can drop a collection" do
136 collection = db['test']
137 db.collection_names.should include 'test'
138 collection.drop
139 db.collection_names.should_not include 'test'
140 end
141
142 it "can drop a database" do
143 connection.database_names.should include('mydb')
144 connection.drop_database("mydb")
145 connection.database_names.should_not include('mydb')
146 end
147end