File: database_spec.rb

package info (click to toggle)
ruby-em-mongo 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: ruby: 2,728; makefile: 2
file content (183 lines) | stat: -rw-r--r-- 5,234 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require File.expand_path('spec_helper', File.dirname(__FILE__) + '/../')

describe EMMongo::Database do
  include EM::Spec

  it 'should add a user' do
    @conn = EM::Mongo::Connection.new
    @db = @conn.db
    @db.collection(EM::Mongo::Database::SYSTEM_USER_COLLECTION).remove({})
    @db.add_user('test', 'test').callback do |res|
      res.should_not == nil
      res.should be_a_kind_of(BSON::ObjectId)
      done
    end
  end

  it 'should authenticate a user' do
    @conn = EM::Mongo::Connection.new
    @db = @conn.db
    @db.add_user('test', 'test')
    @db.authenticate('test', 'test').callback do |res|
      res.should == true
      done
    end
  end

  it "should create a collection" do
    @conn = EM::Mongo::Connection.new
    @db = @conn.db
    @db.create_collection("a").callback do |col|
      col.should be_kind_of EM::Mongo::Collection
      col.name.should == "a"
      done
    end
  end

  it "should drop a collection" do
    @conn = EM::Mongo::Connection.new
    @db = @conn.db
    @db.create_collection("a").callback do |col|
      @db.drop_collection("a").callback do
        @db.collection_names.callback do |names|
          names.should_not include "a"
          done
        end
      end
    end
  end

  it "should provide a list of collection names in the database" do
    @conn = EM::Mongo::Connection.new
    @db = @conn.db
    @db.create_collection "a"
    @db.create_collection("b").callback do
      @db.collection_names.callback do |names|
        names.should include "a"
        names.should include "b"
        done
      end
    end
  end

  it "should provide a list of collections in the database" do
    @conn = EM::Mongo::Connection.new
    @db = @conn.db
    @db.create_collection "a"
    @db.create_collection("b").callback do
      @db.collection_names.callback do |names|
        @db.collections do |collections|
          collections.length.should == names.length
          collections.each do |col|
            col.should be_kind_of EM::Mongo::Collection
          end
        end
        done
      end
    end
  end

  it 'should cache collections correctly' do
    @conn = EM::Mongo::Connection.new
    @db = @conn.db
    a = @db.collection('first_collection')
    b = @db.collection('second_collection')
    a.should_not == b
    @db.collection('first_collection').should == a
    @db.collection('second_collection').should == b
    done
  end

  describe "Errors" do
    describe "when there are no errors" do
      it "should return a nil 'err' from get_last_error" do
        @conn = EM::Mongo::Connection.new
        @db = @conn.db
        @db.reset_error_history.callback do
          @db.get_last_error.callback do |doc|
            doc['err'].should be_nil
            done
          end
        end
      end
      it "should have a false error?" do
        @conn = EM::Mongo::Connection.new
        @db = @conn.db
        @db.reset_error_history.callback do
          @db.error?.callback do |result|
            result.should == false
            done
          end
        end
      end
    end
    describe "when there are errors" do
      it "should return a value for 'err' from get_last_error" do
        @conn = EM::Mongo::Connection.new
        @db = @conn.db
        @db.command({:forceerror=>1}, :check_response => false).callback do
          @db.get_last_error.callback do |doc|
            doc['err'].should_not be_nil
            done
          end
        end
      end
      it "should have a true error?" do
        @conn = EM::Mongo::Connection.new
        @db = @conn.db
        @db.command({:forceerror=>1}, :check_response => false).callback do
          @db.error?.callback do |result|
            result.should == true
            done
          end
        end
      end
    end
    it "should be able to reset the error history" do
      @conn = EM::Mongo::Connection.new
      @db = @conn.db
      @db.command({:forceerror=>1}, :check_response => false).callback do
        @db.reset_error_history.callback do
          @db.error?.callback do |result|
            result.should == false
            done
          end
        end
      end
    end
  end

  describe "Command" do
    it "should fail when the database returns an error" do
      @conn = EM::Mongo::Connection.new
      @db = @conn.db
      @db.command({:non_command => 1}, :check_response => true).errback do
        done
      end
    end
    it "should not fail when checkresponse is false" do
      @conn = EM::Mongo::Connection.new
      @db = @conn.db
      @db.command({:non_command => 1}, :check_response => false).callback do
        done
      end
    end
    it "should succesfully execute a valid command" do
      @conn, @coll = connection_and_collection
      @db = @conn.db
      @coll.insert( {:col => {:easy => "andy" } } )
      @db.command({:collstats => @coll.name}).callback do |doc|
        doc.should_not be_nil
        doc["count"].should == 1
        done
      end
    end
  end

  describe "Indexes" do
    #Index functions are integration tested via the collection specs. Maybe the wrong order,
    #but the collection index functions all call down to the database index functions, and the
    #tests would simply duplicate eachother
  end

end