mongodoc 0.2.1 → 0.2.2
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/README.textile +42 -12
 - data/Rakefile +4 -4
 - data/TODO +26 -0
 - data/VERSION +1 -1
 - data/examples/simple_document.rb +1 -1
 - data/examples/simple_object.rb +0 -2
 - data/features/mongodb.yml +6 -5
 - data/features/removing_documents.feature +68 -0
 - data/features/step_definitions/collection_steps.rb +3 -3
 - data/features/step_definitions/document_steps.rb +2 -2
 - data/features/step_definitions/removing_documents_steps.rb +14 -0
 - data/features/support/support.rb +2 -2
 - data/lib/mongodoc.rb +4 -7
 - data/lib/mongodoc/associations/collection_proxy.rb +103 -0
 - data/lib/mongodoc/associations/document_proxy.rb +53 -0
 - data/lib/mongodoc/associations/hash_proxy.rb +96 -0
 - data/lib/mongodoc/associations/proxy_base.rb +51 -0
 - data/lib/mongodoc/attributes.rb +49 -17
 - data/lib/mongodoc/collection.rb +15 -5
 - data/lib/mongodoc/connection.rb +83 -20
 - data/lib/mongodoc/criteria.rb +9 -4
 - data/lib/mongodoc/cursor.rb +9 -3
 - data/lib/mongodoc/document.rb +37 -24
 - data/lib/mongodoc/validations/macros.rb +11 -0
 - data/lib/mongodoc/validations/validates_embedded.rb +13 -0
 - data/mongodb.example.yml +13 -5
 - data/mongodoc.gemspec +33 -23
 - data/spec/associations/collection_proxy_spec.rb +200 -0
 - data/spec/associations/document_proxy_spec.rb +42 -0
 - data/spec/associations/hash_proxy_spec.rb +163 -0
 - data/spec/attributes_spec.rb +113 -47
 - data/spec/bson_spec.rb +24 -24
 - data/spec/collection_spec.rb +67 -86
 - data/spec/connection_spec.rb +98 -150
 - data/spec/criteria_spec.rb +4 -3
 - data/spec/cursor_spec.rb +33 -27
 - data/spec/document_spec.rb +173 -156
 - data/spec/embedded_save_spec.rb +8 -3
 - data/spec/new_record_spec.rb +33 -121
 - metadata +80 -39
 - data/lib/mongodoc/parent_proxy.rb +0 -44
 - data/lib/mongodoc/proxy.rb +0 -83
 - data/spec/parent_proxy_spec.rb +0 -44
 - data/spec/proxy_spec.rb +0 -80
 
    
        data/spec/collection_spec.rb
    CHANGED
    
    | 
         @@ -1,179 +1,160 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
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 
4 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
               
     | 
| 
      
 5 
     | 
    
         
            +
              let(:mongo_collection) { stub('collection') }
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              it ".new delegates to .mongo_collection" do
         
     | 
| 
       13 
8 
     | 
    
         
             
                name = 'collection_name'
         
     | 
| 
       14 
     | 
    
         
            -
                MongoDoc::Collection.should_receive(:mongo_collection).with(name)
         
     | 
| 
      
 9 
     | 
    
         
            +
                MongoDoc::Collection.should_receive(:mongo_collection).with(name).and_return(mongo_collection)
         
     | 
| 
       15 
10 
     | 
    
         
             
                MongoDoc::Collection.new(name)
         
     | 
| 
       16 
11 
     | 
    
         
             
              end
         
     | 
| 
       17 
12 
     | 
    
         | 
| 
       18 
13 
     | 
    
         
             
              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 
14 
     | 
    
         | 
| 
       24 
     | 
    
         
            -
                 
     | 
| 
       25 
     | 
    
         
            -
                  MongoDoc::Collection. 
     | 
| 
      
 15 
     | 
    
         
            +
                let(:collection) do
         
     | 
| 
      
 16 
     | 
    
         
            +
                  MongoDoc::Collection.stub(:mongo_collection).and_return(mongo_collection)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  MongoDoc::Collection.new('collection_name')
         
     | 
| 
       26 
18 
     | 
    
         
             
                end
         
     | 
| 
       27 
19 
     | 
    
         | 
| 
       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|
         
     | 
| 
      
 20 
     | 
    
         
            +
                %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 
21 
     | 
    
         
             
                  it "delegates #{delegated_method} to the Mongo::Collection" do
         
     | 
| 
       30 
     | 
    
         
            -
                     
     | 
| 
       31 
     | 
    
         
            -
                     
     | 
| 
      
 22 
     | 
    
         
            +
                    mongo_collection.should_receive(delegated_method)
         
     | 
| 
      
 23 
     | 
    
         
            +
                    collection.send(delegated_method)
         
     | 
| 
       32 
24 
     | 
    
         
             
                  end
         
     | 
| 
       33 
25 
     | 
    
         
             
                end
         
     | 
| 
       34 
26 
     | 
    
         | 
| 
       35 
27 
     | 
    
         
             
                context "#find" do
         
     | 
| 
       36 
     | 
    
         
            -
                   
     | 
| 
       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
         
     | 
| 
      
 28 
     | 
    
         
            +
                  let(:cursor) { MongoDoc::Cursor.new(collection, stub('cursor', :close => nil)) }
         
     | 
| 
       45 
29 
     | 
    
         | 
| 
       46 
     | 
    
         
            -
                   
     | 
| 
       47 
     | 
    
         
            -
                     
     | 
| 
       48 
     | 
    
         
            -
                    @collection.find(@query, @options, &@block)
         
     | 
| 
      
 30 
     | 
    
         
            +
                  before do
         
     | 
| 
      
 31 
     | 
    
         
            +
                    collection.stub(:wrapped_cursor).and_return(cursor)
         
     | 
| 
       49 
32 
     | 
    
         
             
                  end
         
     | 
| 
       50 
33 
     | 
    
         | 
| 
       51 
34 
     | 
    
         
             
                  it "wraps the cursor" do
         
     | 
| 
       52 
     | 
    
         
            -
                     
     | 
| 
       53 
     | 
    
         
            -
                     
     | 
| 
      
 35 
     | 
    
         
            +
                    query = {'sample' => 'data'}
         
     | 
| 
      
 36 
     | 
    
         
            +
                    options = { :limit => 1}
         
     | 
| 
      
 37 
     | 
    
         
            +
                    collection.should_receive(:wrapped_cursor).with(query, options).and_return(cursor)
         
     | 
| 
      
 38 
     | 
    
         
            +
                    collection.find(query, options)
         
     | 
| 
       54 
39 
     | 
    
         
             
                  end
         
     | 
| 
       55 
40 
     | 
    
         | 
| 
       56 
41 
     | 
    
         
             
                  it "calls the block with a wrapped cursor" do
         
     | 
| 
       57 
     | 
    
         
            -
                     
     | 
| 
       58 
     | 
    
         
            -
                    @result.should ==  
     | 
| 
      
 42 
     | 
    
         
            +
                    collection.find {|c| @result = c}
         
     | 
| 
      
 43 
     | 
    
         
            +
                    @result.should == cursor
         
     | 
| 
       59 
44 
     | 
    
         
             
                  end
         
     | 
| 
       60 
45 
     | 
    
         
             
                end
         
     | 
| 
       61 
46 
     | 
    
         | 
| 
       62 
47 
     | 
    
         
             
                context "#find_one" do
         
     | 
| 
      
 48 
     | 
    
         
            +
                  let(:bson) { stub('bson') }
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
       63 
50 
     | 
    
         
             
                  before do
         
     | 
| 
       64 
     | 
    
         
            -
                     
     | 
| 
       65 
     | 
    
         
            -
                    @options = {:limit => 1}
         
     | 
| 
       66 
     | 
    
         
            -
                    @collection = MongoDoc::Collection.new('collection_name')
         
     | 
| 
       67 
     | 
    
         
            -
                    @bson = stub('bson')
         
     | 
| 
       68 
     | 
    
         
            -
                    @mongo_collection.stub(:find_one).and_return(@bson)
         
     | 
| 
      
 51 
     | 
    
         
            +
                    mongo_collection.stub(:find_one).and_return(bson)
         
     | 
| 
       69 
52 
     | 
    
         
             
                  end
         
     | 
| 
       70 
53 
     | 
    
         | 
| 
       71 
54 
     | 
    
         
             
                  it "delegates to the Mongo::Collection" do
         
     | 
| 
       72 
     | 
    
         
            -
                     
     | 
| 
       73 
     | 
    
         
            -
                     
     | 
| 
      
 55 
     | 
    
         
            +
                    spec = { 'sample' => 'data' }
         
     | 
| 
      
 56 
     | 
    
         
            +
                    options = {:limit => 1}
         
     | 
| 
      
 57 
     | 
    
         
            +
                    mongo_collection.should_receive(:find_one).with(spec, options)
         
     | 
| 
      
 58 
     | 
    
         
            +
                    collection.find_one(spec, options)
         
     | 
| 
       74 
59 
     | 
    
         
             
                  end
         
     | 
| 
       75 
60 
     | 
    
         | 
| 
       76 
61 
     | 
    
         
             
                  it "converts the result back from bson" do
         
     | 
| 
       77 
     | 
    
         
            -
                    MongoDoc::BSON.should_receive(:decode).with( 
     | 
| 
       78 
     | 
    
         
            -
                     
     | 
| 
      
 62 
     | 
    
         
            +
                    MongoDoc::BSON.should_receive(:decode).with(bson)
         
     | 
| 
      
 63 
     | 
    
         
            +
                    collection.find_one({ 'sample' => 'data' })
         
     | 
| 
       79 
64 
     | 
    
         
             
                  end
         
     | 
| 
       80 
65 
     | 
    
         | 
| 
       81 
66 
     | 
    
         
             
                  it "returns the converted result" do
         
     | 
| 
       82 
67 
     | 
    
         
             
                    obj = stub('obj')
         
     | 
| 
       83 
68 
     | 
    
         
             
                    MongoDoc::BSON.stub(:decode).and_return(obj)
         
     | 
| 
       84 
     | 
    
         
            -
                     
     | 
| 
      
 69 
     | 
    
         
            +
                    collection.find_one({ 'sample' => 'data' }).should == obj
         
     | 
| 
       85 
70 
     | 
    
         
             
                  end
         
     | 
| 
       86 
71 
     | 
    
         | 
| 
       87 
72 
     | 
    
         
             
                  it "returns nil if the delegate returns nil" do
         
     | 
| 
       88 
     | 
    
         
            -
                     
     | 
| 
       89 
     | 
    
         
            -
                     
     | 
| 
      
 73 
     | 
    
         
            +
                    mongo_collection.stub(:find_one)
         
     | 
| 
      
 74 
     | 
    
         
            +
                    collection.find_one({ 'sample' => 'data' }).should be_nil
         
     | 
| 
       90 
75 
     | 
    
         
             
                  end
         
     | 
| 
       91 
76 
     | 
    
         
             
                end
         
     | 
| 
       92 
77 
     | 
    
         | 
| 
       93 
78 
     | 
    
         
             
                context "#insert" do
         
     | 
| 
       94 
     | 
    
         
            -
                   
     | 
| 
       95 
     | 
    
         
            -
                    @doc = { 'sample' => 'data' }
         
     | 
| 
       96 
     | 
    
         
            -
                    @options = {:safe => false}
         
     | 
| 
       97 
     | 
    
         
            -
                  end
         
     | 
| 
      
 79 
     | 
    
         
            +
                  let(:doc) { {'sample' => 'data'} }
         
     | 
| 
       98 
80 
     | 
    
         | 
| 
       99 
81 
     | 
    
         
             
                  it "delegates to the Mongo::Collection" do
         
     | 
| 
       100 
     | 
    
         
            -
                     
     | 
| 
       101 
     | 
    
         
            -
                     
     | 
| 
      
 82 
     | 
    
         
            +
                    options = {:safe => false}
         
     | 
| 
      
 83 
     | 
    
         
            +
                    mongo_collection.should_receive(:insert).with(doc, options)
         
     | 
| 
      
 84 
     | 
    
         
            +
                    collection.insert(doc, options)
         
     | 
| 
       102 
85 
     | 
    
         
             
                  end
         
     | 
| 
       103 
86 
     | 
    
         | 
| 
       104 
87 
     | 
    
         
             
                  it "converts the doc_or_docs to bson" do
         
     | 
| 
       105 
     | 
    
         
            -
                     
     | 
| 
       106 
     | 
    
         
            -
                     
     | 
| 
       107 
     | 
    
         
            -
                     
     | 
| 
      
 88 
     | 
    
         
            +
                    doc.should_receive(:to_bson)
         
     | 
| 
      
 89 
     | 
    
         
            +
                    mongo_collection.stub(:insert)
         
     | 
| 
      
 90 
     | 
    
         
            +
                    collection.insert(doc, options)
         
     | 
| 
       108 
91 
     | 
    
         
             
                  end
         
     | 
| 
       109 
92 
     | 
    
         | 
| 
       110 
93 
     | 
    
         
             
                  it "returns the delegates result" do
         
     | 
| 
       111 
94 
     | 
    
         
             
                    result = 'result'
         
     | 
| 
       112 
     | 
    
         
            -
                     
     | 
| 
       113 
     | 
    
         
            -
                     
     | 
| 
      
 95 
     | 
    
         
            +
                    mongo_collection.stub(:insert).and_return(result)
         
     | 
| 
      
 96 
     | 
    
         
            +
                    collection.insert(doc).should == result
         
     | 
| 
       114 
97 
     | 
    
         
             
                  end
         
     | 
| 
       115 
98 
     | 
    
         
             
                end
         
     | 
| 
       116 
99 
     | 
    
         | 
| 
       117 
100 
     | 
    
         
             
                context "#save" do
         
     | 
| 
       118 
     | 
    
         
            -
                   
     | 
| 
       119 
     | 
    
         
            -
                    @doc = { 'sample' => 'data' }
         
     | 
| 
       120 
     | 
    
         
            -
                    @options = {:safe => false}
         
     | 
| 
       121 
     | 
    
         
            -
                  end
         
     | 
| 
      
 101 
     | 
    
         
            +
                  let(:doc) { {'sample' => 'data'} }
         
     | 
| 
       122 
102 
     | 
    
         | 
| 
       123 
103 
     | 
    
         
             
                  it "delegates to the Mongo::Collection" do
         
     | 
| 
       124 
     | 
    
         
            -
                     
     | 
| 
       125 
     | 
    
         
            -
                     
     | 
| 
      
 104 
     | 
    
         
            +
                    options = {:safe => false}
         
     | 
| 
      
 105 
     | 
    
         
            +
                    mongo_collection.should_receive(:save).with(doc, options)
         
     | 
| 
      
 106 
     | 
    
         
            +
                    collection.save(doc, options)
         
     | 
| 
       126 
107 
     | 
    
         
             
                  end
         
     | 
| 
       127 
108 
     | 
    
         | 
| 
       128 
109 
     | 
    
         
             
                  it "converts the doc to bson" do
         
     | 
| 
       129 
     | 
    
         
            -
                     
     | 
| 
       130 
     | 
    
         
            -
                     
     | 
| 
       131 
     | 
    
         
            -
                     
     | 
| 
      
 110 
     | 
    
         
            +
                    doc.should_receive(:to_bson)
         
     | 
| 
      
 111 
     | 
    
         
            +
                    mongo_collection.stub(:save)
         
     | 
| 
      
 112 
     | 
    
         
            +
                    collection.save(doc)
         
     | 
| 
       132 
113 
     | 
    
         
             
                  end
         
     | 
| 
       133 
114 
     | 
    
         | 
| 
       134 
115 
     | 
    
         
             
                  it "returns the delegates result" do
         
     | 
| 
       135 
116 
     | 
    
         
             
                    result = 'result'
         
     | 
| 
       136 
     | 
    
         
            -
                     
     | 
| 
       137 
     | 
    
         
            -
                     
     | 
| 
      
 117 
     | 
    
         
            +
                    mongo_collection.stub(:save).and_return(result)
         
     | 
| 
      
 118 
     | 
    
         
            +
                    collection.save(doc).should == result
         
     | 
| 
       138 
119 
     | 
    
         
             
                  end
         
     | 
| 
       139 
120 
     | 
    
         
             
                end
         
     | 
| 
       140 
121 
     | 
    
         | 
| 
       141 
122 
     | 
    
         
             
                context "#update" do
         
     | 
| 
      
 123 
     | 
    
         
            +
                  let(:spec) { {'sample' => 'old'} }
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                  let(:doc) { {'sample' => 'data'} }
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                  let(:options) { {:safe => false} }
         
     | 
| 
      
 128 
     | 
    
         
            +
             
     | 
| 
       142 
129 
     | 
    
         
             
                  before do
         
     | 
| 
       143 
     | 
    
         
            -
                     
     | 
| 
       144 
     | 
    
         
            -
                     
     | 
| 
       145 
     | 
    
         
            -
                    @options = {:safe => false}
         
     | 
| 
       146 
     | 
    
         
            -
                    @database = stub('database', :command => nil)
         
     | 
| 
       147 
     | 
    
         
            -
                    MongoDoc.stub(:database).and_return(@database)
         
     | 
| 
      
 130 
     | 
    
         
            +
                    collection.stub(:last_error).and_return('updatedExisting' => true)
         
     | 
| 
      
 131 
     | 
    
         
            +
                    mongo_collection.stub(:update)
         
     | 
| 
       148 
132 
     | 
    
         
             
                  end
         
     | 
| 
       149 
133 
     | 
    
         | 
| 
       150 
134 
     | 
    
         
             
                  it "delegates to the Mongo::Collection" do
         
     | 
| 
       151 
     | 
    
         
            -
                     
     | 
| 
       152 
     | 
    
         
            -
                     
     | 
| 
      
 135 
     | 
    
         
            +
                    mongo_collection.should_receive(:update).with(spec, doc, options)
         
     | 
| 
      
 136 
     | 
    
         
            +
                    collection.update(spec, doc, options)
         
     | 
| 
       153 
137 
     | 
    
         
             
                  end
         
     | 
| 
       154 
138 
     | 
    
         | 
| 
       155 
139 
     | 
    
         
             
                  it "converts the doc to bson" do
         
     | 
| 
       156 
     | 
    
         
            -
                     
     | 
| 
       157 
     | 
    
         
            -
                     
     | 
| 
       158 
     | 
    
         
            -
                    MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options)
         
     | 
| 
      
 140 
     | 
    
         
            +
                    doc.should_receive(:to_bson)
         
     | 
| 
      
 141 
     | 
    
         
            +
                    collection.update(spec, doc, options)
         
     | 
| 
       159 
142 
     | 
    
         
             
                  end
         
     | 
| 
       160 
143 
     | 
    
         | 
| 
       161 
144 
     | 
    
         
             
                  it "gets the last error from the database" do
         
     | 
| 
       162 
     | 
    
         
            -
                     
     | 
| 
       163 
     | 
    
         
            -
                     
     | 
| 
       164 
     | 
    
         
            -
                    MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options)
         
     | 
| 
      
 145 
     | 
    
         
            +
                    collection.should_receive(:last_error)
         
     | 
| 
      
 146 
     | 
    
         
            +
                    collection.update(spec, doc, options)
         
     | 
| 
       165 
147 
     | 
    
         
             
                  end
         
     | 
| 
       166 
148 
     | 
    
         | 
| 
       167 
149 
     | 
    
         
             
                  it "returns the updateExisting value of get last error" do
         
     | 
| 
       168 
150 
     | 
    
         
             
                    result = 'check'
         
     | 
| 
       169 
     | 
    
         
            -
                     
     | 
| 
       170 
     | 
    
         
            -
                     
     | 
| 
       171 
     | 
    
         
            -
                    MongoDoc::Collection.new('collection_name').update(@spec, @doc, @options).should == result
         
     | 
| 
      
 151 
     | 
    
         
            +
                    collection.stub(:last_error).and_return({'updatedExisting' => result})
         
     | 
| 
      
 152 
     | 
    
         
            +
                    collection.update(spec, doc, options).should == result
         
     | 
| 
       172 
153 
     | 
    
         
             
                  end
         
     | 
| 
       173 
154 
     | 
    
         | 
| 
       174 
155 
     | 
    
         
             
                  it "returns false otherwise" do
         
     | 
| 
       175 
     | 
    
         
            -
                     
     | 
| 
       176 
     | 
    
         
            -
                     
     | 
| 
      
 156 
     | 
    
         
            +
                    collection.stub(:last_error)
         
     | 
| 
      
 157 
     | 
    
         
            +
                    collection.update(spec, doc, options).should be_false
         
     | 
| 
       177 
158 
     | 
    
         
             
                  end
         
     | 
| 
       178 
159 
     | 
    
         
             
                end
         
     | 
| 
       179 
160 
     | 
    
         
             
              end
         
     | 
    
        data/spec/connection_spec.rb
    CHANGED
    
    | 
         @@ -1,199 +1,147 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
             
     | 
| 
      
 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
         
     | 
| 
       4 
18 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
              it "default the configuration location to './mongodb.yml'" do
         
     | 
| 
       6 
     | 
    
         
            -
                MongoDoc.config_path.should == './mongodb.yml'
         
     | 
| 
       7 
19 
     | 
    
         
             
              end
         
     | 
| 
      
 20 
     | 
    
         
            +
            end
         
     | 
| 
       8 
21 
     | 
    
         | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
                before do
         
     | 
| 
       11 
     | 
    
         
            -
                  MongoDoc.config_path = nil
         
     | 
| 
       12 
     | 
    
         
            -
                  MongoDoc.connection = nil
         
     | 
| 
       13 
     | 
    
         
            -
                  MongoDoc.config = nil
         
     | 
| 
       14 
     | 
    
         
            -
                  @connection = stub('connection')
         
     | 
| 
       15 
     | 
    
         
            -
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
            describe "MongoDoc::Connection.Connections" do
         
     | 
| 
       16 
23 
     | 
    
         | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
                  Mongo::Connection.should_receive(:new).and_return(@connection)
         
     | 
| 
       19 
     | 
    
         
            -
                  MongoDoc.connect
         
     | 
| 
       20 
     | 
    
         
            -
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
              context "Non-rails environment" do
         
     | 
| 
       21 
25 
     | 
    
         | 
| 
       22 
     | 
    
         
            -
                it " 
     | 
| 
       23 
     | 
    
         
            -
                   
     | 
| 
       24 
     | 
    
         
            -
                  MongoDoc.connect
         
     | 
| 
       25 
     | 
    
         
            -
                  MongoDoc.connection.should == @connection
         
     | 
| 
      
 26 
     | 
    
         
            +
                it "does not see Rails" do
         
     | 
| 
      
 27 
     | 
    
         
            +
                  Object.const_defined?('Rails').should be_false
         
     | 
| 
       26 
28 
     | 
    
         
             
                end
         
     | 
| 
       27 
29 
     | 
    
         | 
| 
       28 
     | 
    
         
            -
                it " 
     | 
| 
       29 
     | 
    
         
            -
                   
     | 
| 
       30 
     | 
    
         
            -
                  lambda { MongoDoc.connect }.should raise_error(MongoDoc::NoConnectionError)
         
     | 
| 
      
 30 
     | 
    
         
            +
                it "default the configuration location to './mongodb.yml'" do
         
     | 
| 
      
 31 
     | 
    
         
            +
                  MongoDoc::Connection.config_path.should == './mongodb.yml'
         
     | 
| 
       31 
32 
     | 
    
         
             
                end
         
     | 
| 
       32 
33 
     | 
    
         | 
| 
       33 
     | 
    
         
            -
                context " 
     | 
| 
       34 
     | 
    
         
            -
                   
     | 
| 
       35 
     | 
    
         
            -
                    host = 'localhost'
         
     | 
| 
       36 
     | 
    
         
            -
                    Mongo::Connection.should_receive(:new).with(host, nil, {}).and_return(@connection)
         
     | 
| 
       37 
     | 
    
         
            -
                    MongoDoc.connect(host)
         
     | 
| 
       38 
     | 
    
         
            -
                  end
         
     | 
| 
      
 34 
     | 
    
         
            +
                context "without a configuration" do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  let(:connection) { stub('connection') }
         
     | 
| 
       39 
36 
     | 
    
         | 
| 
       40 
     | 
    
         
            -
                   
     | 
| 
       41 
     | 
    
         
            -
                     
     | 
| 
       42 
     | 
    
         
            -
                     
     | 
| 
       43 
     | 
    
         
            -
                    Mongo::Connection.should_receive(:new).with(host, 3000, {}).and_return(@connection)
         
     | 
| 
       44 
     | 
    
         
            -
                    MongoDoc.connect(host, port)
         
     | 
| 
      
 37 
     | 
    
         
            +
                  before do
         
     | 
| 
      
 38 
     | 
    
         
            +
                    MongoDoc::Connection.reset
         
     | 
| 
      
 39 
     | 
    
         
            +
                    MongoDoc::Connection.stub(:connect).and_return(connection)
         
     | 
| 
       45 
40 
     | 
    
         
             
                  end
         
     | 
| 
       46 
41 
     | 
    
         | 
| 
       47 
     | 
    
         
            -
                  it " 
     | 
| 
       48 
     | 
    
         
            -
                     
     | 
| 
       49 
     | 
    
         
            -
                     
     | 
| 
       50 
     | 
    
         
            -
                    MongoDoc.connect(opts)
         
     | 
| 
      
 42 
     | 
    
         
            +
                  it "creates a default connection" do
         
     | 
| 
      
 43 
     | 
    
         
            +
                    MongoDoc::Connection.should_receive(:connect).and_return(connection)
         
     | 
| 
      
 44 
     | 
    
         
            +
                    MongoDoc::Connection.connection
         
     | 
| 
       51 
45 
     | 
    
         
             
                  end
         
     | 
| 
       52 
46 
     | 
    
         | 
| 
       53 
     | 
    
         
            -
                  it " 
     | 
| 
       54 
     | 
    
         
            -
                     
     | 
| 
       55 
     | 
    
         
            -
                     
     | 
| 
       56 
     | 
    
         
            -
                    opts = {:slave_ok => true}
         
     | 
| 
       57 
     | 
    
         
            -
                    Mongo::Connection.should_receive(:new).with(host, 3000, opts).and_return(@connection)
         
     | 
| 
       58 
     | 
    
         
            -
                    MongoDoc.connect(host, port, opts)
         
     | 
| 
      
 47 
     | 
    
         
            +
                  it "creates a default database with strict false" do
         
     | 
| 
      
 48 
     | 
    
         
            +
                    connection.should_receive(:db).with("mongodoc", :strict => false)
         
     | 
| 
      
 49 
     | 
    
         
            +
                    MongoDoc::Connection.database
         
     | 
| 
       59 
50 
     | 
    
         
             
                  end
         
     | 
| 
       60 
51 
     | 
    
         
             
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
              end
         
     | 
| 
       61 
53 
     | 
    
         | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
                  before do
         
     | 
| 
       64 
     | 
    
         
            -
                    MongoDoc.config_path = './spec/mongodb.yml'
         
     | 
| 
       65 
     | 
    
         
            -
                    config = YAML.load_file(MongoDoc.config_path)
         
     | 
| 
       66 
     | 
    
         
            -
                    @host = config['host']
         
     | 
| 
       67 
     | 
    
         
            -
                    @port = config['port']
         
     | 
| 
       68 
     | 
    
         
            -
                    @db_options = config['options']
         
     | 
| 
       69 
     | 
    
         
            -
                  end
         
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
                  context "with a host config file" do
         
     | 
| 
      
 54 
     | 
    
         
            +
              context "Rails environment" do
         
     | 
| 
       72 
55 
     | 
    
         | 
| 
       73 
     | 
    
         
            -
             
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
                      MongoDoc.connect
         
     | 
| 
       76 
     | 
    
         
            -
                    end
         
     | 
| 
      
 56 
     | 
    
         
            +
                module FauxRails
         
     | 
| 
      
 57 
     | 
    
         
            +
                  extend self
         
     | 
| 
       77 
58 
     | 
    
         | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
       80 
     | 
    
         
            -
                      port = 890
         
     | 
| 
       81 
     | 
    
         
            -
                      options = {:option => 'p_opt'}
         
     | 
| 
       82 
     | 
    
         
            -
                      Mongo::Connection.should_receive(:new).with(host, port, options).and_return(@connection)
         
     | 
| 
       83 
     | 
    
         
            -
                      MongoDoc.connect(host, port, options)
         
     | 
| 
       84 
     | 
    
         
            -
                    end
         
     | 
| 
      
 59 
     | 
    
         
            +
                  def env
         
     | 
| 
      
 60 
     | 
    
         
            +
                    'development'
         
     | 
| 
       85 
61 
     | 
    
         
             
                  end
         
     | 
| 
       86 
62 
     | 
    
         | 
| 
       87 
     | 
    
         
            -
                   
     | 
| 
       88 
     | 
    
         
            -
                     
     | 
| 
       89 
     | 
    
         
            -
                      MongoDoc.config_path = './spec/mongodb_pairs.yml'
         
     | 
| 
       90 
     | 
    
         
            -
                      config = YAML.load_file(MongoDoc.config_path)
         
     | 
| 
       91 
     | 
    
         
            -
                      @host_pairs = config['host_pairs']
         
     | 
| 
       92 
     | 
    
         
            -
                      @port = config['port']
         
     | 
| 
       93 
     | 
    
         
            -
                      @db_options = config['options']
         
     | 
| 
       94 
     | 
    
         
            -
                    end
         
     | 
| 
       95 
     | 
    
         
            -
             
     | 
| 
       96 
     | 
    
         
            -
                    it "connects to the database with the host pairs value from the file" do
         
     | 
| 
       97 
     | 
    
         
            -
                      Mongo::Connection.should_receive(:new).with(@host_pairs, @port, @db_options).and_return(@connection)
         
     | 
| 
       98 
     | 
    
         
            -
                      MongoDoc.connect
         
     | 
| 
       99 
     | 
    
         
            -
                    end
         
     | 
| 
      
 63 
     | 
    
         
            +
                  def root
         
     | 
| 
      
 64 
     | 
    
         
            +
                    Pathname.new('rails_root')
         
     | 
| 
       100 
65 
     | 
    
         
             
                  end
         
     | 
| 
       101 
66 
     | 
    
         
             
                end
         
     | 
| 
       102 
     | 
    
         
            -
              end
         
     | 
| 
       103 
67 
     | 
    
         | 
| 
       104 
     | 
    
         
            -
              describe ".connect_to_database" do
         
     | 
| 
       105 
68 
     | 
    
         
             
                before do
         
     | 
| 
       106 
     | 
    
         
            -
                   
     | 
| 
       107 
     | 
    
         
            -
                  MongoDoc.connection = nil
         
     | 
| 
       108 
     | 
    
         
            -
                  MongoDoc.config = nil
         
     | 
| 
      
 69 
     | 
    
         
            +
                  Object.const_set('Rails', FauxRails)
         
     | 
| 
       109 
70 
     | 
    
         
             
                end
         
     | 
| 
       110 
71 
     | 
    
         | 
| 
       111 
     | 
    
         
            -
                 
     | 
| 
       112 
     | 
    
         
            -
                   
     | 
| 
       113 
     | 
    
         
            -
                  MongoDoc.stub(:connect).and_return(connection)
         
     | 
| 
       114 
     | 
    
         
            -
                  lambda {MongoDoc.connect_to_database}.should raise_error(MongoDoc::NoDatabaseError)
         
     | 
| 
      
 72 
     | 
    
         
            +
                after do
         
     | 
| 
      
 73 
     | 
    
         
            +
                  Object.send(:remove_const, 'Rails')
         
     | 
| 
       115 
74 
     | 
    
         
             
                end
         
     | 
| 
       116 
75 
     | 
    
         | 
| 
       117 
     | 
    
         
            -
                it " 
     | 
| 
       118 
     | 
    
         
            -
                   
     | 
| 
       119 
     | 
    
         
            -
                  connection = stub("connection", :db => db)
         
     | 
| 
       120 
     | 
    
         
            -
                  MongoDoc.stub(:connect).and_return(connection)
         
     | 
| 
       121 
     | 
    
         
            -
                  MongoDoc.connect_to_database.should == db
         
     | 
| 
      
 76 
     | 
    
         
            +
                it "sees Rails" do
         
     | 
| 
      
 77 
     | 
    
         
            +
                  Object.const_defined?('Rails').should be_true
         
     | 
| 
       122 
78 
     | 
    
         
             
                end
         
     | 
| 
       123 
79 
     | 
    
         | 
| 
       124 
     | 
    
         
            -
                 
     | 
| 
      
 80 
     | 
    
         
            +
                it "default the configuration location to Rails.root + '/config/mongodb.yml'" do
         
     | 
| 
      
 81 
     | 
    
         
            +
                  MongoDoc::Connection.config_path.should == FauxRails.root + 'config/mongodb.yml'
         
     | 
| 
      
 82 
     | 
    
         
            +
                end
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
                context "without a configuration" do
         
     | 
| 
      
 85 
     | 
    
         
            +
                  let(:connection) { stub('connection') }
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
       125 
87 
     | 
    
         
             
                  before do
         
     | 
| 
       126 
     | 
    
         
            -
                     
     | 
| 
       127 
     | 
    
         
            -
                     
     | 
| 
      
 88 
     | 
    
         
            +
                    MongoDoc::Connection.reset
         
     | 
| 
      
 89 
     | 
    
         
            +
                    MongoDoc::Connection.stub(:connect).and_return(connection)
         
     | 
| 
       128 
90 
     | 
    
         
             
                  end
         
     | 
| 
       129 
91 
     | 
    
         | 
| 
       130 
     | 
    
         
            -
                  it " 
     | 
| 
       131 
     | 
    
         
            -
                    MongoDoc.should_receive(: 
     | 
| 
       132 
     | 
    
         
            -
                    MongoDoc. 
     | 
| 
       133 
     | 
    
         
            -
                    MongoDoc.connect_to_database.should == @db
         
     | 
| 
      
 92 
     | 
    
         
            +
                  it "creates a default connection" do
         
     | 
| 
      
 93 
     | 
    
         
            +
                    MongoDoc::Connection.should_receive(:connect).and_return(connection)
         
     | 
| 
      
 94 
     | 
    
         
            +
                    MongoDoc::Connection.connection
         
     | 
| 
       134 
95 
     | 
    
         
             
                  end
         
     | 
| 
       135 
96 
     | 
    
         | 
| 
       136 
     | 
    
         
            -
                  it " 
     | 
| 
       137 
     | 
    
         
            -
                     
     | 
| 
       138 
     | 
    
         
            -
                    MongoDoc. 
     | 
| 
       139 
     | 
    
         
            -
                    MongoDoc.connect_to_database(nil, nil, nil, nil, true).should == @db
         
     | 
| 
      
 97 
     | 
    
         
            +
                  it "creates a default database with strict false" do
         
     | 
| 
      
 98 
     | 
    
         
            +
                    connection.should_receive(:db).with("rails_root_development", :strict => false)
         
     | 
| 
      
 99 
     | 
    
         
            +
                    MongoDoc::Connection.database
         
     | 
| 
       140 
100 
     | 
    
         
             
                  end
         
     | 
| 
       141 
101 
     | 
    
         
             
                end
         
     | 
| 
      
 102 
     | 
    
         
            +
              end
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
              context ".verify_server_version" do
         
     | 
| 
      
 105 
     | 
    
         
            +
                let(:connection) { stub('connection') }
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                it "raises when the server version is unsupported" do
         
     | 
| 
      
 108 
     | 
    
         
            +
                  connection.stub(:server_version).and_return(Mongo::ServerVersion.new('1.3.1'))
         
     | 
| 
      
 109 
     | 
    
         
            +
                  lambda { MongoDoc::Connection.send(:verify_server_version, connection) }.should raise_error(MongoDoc::UnsupportedServerVersionError)
         
     | 
| 
      
 110 
     | 
    
         
            +
                end
         
     | 
| 
       142 
111 
     | 
    
         | 
| 
       143 
     | 
    
         
            -
                 
     | 
| 
       144 
     | 
    
         
            -
                   
     | 
| 
       145 
     | 
    
         
            -
             
     | 
| 
       146 
     | 
    
         
            -
             
     | 
| 
       147 
     | 
    
         
            -
             
     | 
| 
       148 
     | 
    
         
            -
                    @connection = stub('connection')
         
     | 
| 
       149 
     | 
    
         
            -
                  end
         
     | 
| 
      
 112 
     | 
    
         
            +
                it "returns when the server version is supported" do
         
     | 
| 
      
 113 
     | 
    
         
            +
                  connection.stub(:server_version).and_return(Mongo::ServerVersion.new('1.3.2'))
         
     | 
| 
      
 114 
     | 
    
         
            +
                  lambda { MongoDoc::Connection.send(:verify_server_version, connection) }.should_not raise_error(MongoDoc::UnsupportedServerVersionError)
         
     | 
| 
      
 115 
     | 
    
         
            +
                end
         
     | 
| 
      
 116 
     | 
    
         
            +
              end
         
     | 
| 
       150 
117 
     | 
    
         | 
| 
       151 
     | 
    
         
            -
             
     | 
| 
       152 
     | 
    
         
            -
             
     | 
| 
       153 
     | 
    
         
            -
                    MongoDoc.should_receive(:connect).and_return(@connection)
         
     | 
| 
       154 
     | 
    
         
            -
                    MongoDoc.connect_to_database(@name)
         
     | 
| 
       155 
     | 
    
         
            -
                  end
         
     | 
| 
      
 118 
     | 
    
         
            +
              describe ".connect" do
         
     | 
| 
      
 119 
     | 
    
         
            +
                let(:connection) { stub('connection') }
         
     | 
| 
       156 
120 
     | 
    
         | 
| 
       157 
     | 
    
         
            -
             
     | 
| 
       158 
     | 
    
         
            -
             
     | 
| 
       159 
     | 
    
         
            -
                    port = 123
         
     | 
| 
       160 
     | 
    
         
            -
                    options = { :auto_reconnect => true }
         
     | 
| 
       161 
     | 
    
         
            -
                    @connection.should_receive(:db).with(@name).and_return(@db)
         
     | 
| 
       162 
     | 
    
         
            -
                    MongoDoc.should_receive(:connect).with(host, port, options).and_return(@connection)
         
     | 
| 
       163 
     | 
    
         
            -
                    MongoDoc.connect_to_database(@name, host, port, options)
         
     | 
| 
       164 
     | 
    
         
            -
                  end
         
     | 
| 
      
 121 
     | 
    
         
            +
                before do
         
     | 
| 
      
 122 
     | 
    
         
            +
                  MongoDoc::Connection.stub(:verify_server_version)
         
     | 
| 
       165 
123 
     | 
    
         
             
                end
         
     | 
| 
       166 
124 
     | 
    
         | 
| 
       167 
     | 
    
         
            -
                 
     | 
| 
       168 
     | 
    
         
            -
                   
     | 
| 
       169 
     | 
    
         
            -
             
     | 
| 
       170 
     | 
    
         
            -
             
     | 
| 
       171 
     | 
    
         
            -
             
     | 
| 
       172 
     | 
    
         
            -
             
     | 
| 
       173 
     | 
    
         
            -
             
     | 
| 
       174 
     | 
    
         
            -
             
     | 
| 
       175 
     | 
    
         
            -
             
     | 
| 
       176 
     | 
    
         
            -
             
     | 
| 
       177 
     | 
    
         
            -
                  end
         
     | 
| 
      
 125 
     | 
    
         
            +
                it "creates a Mongo::Connection" do
         
     | 
| 
      
 126 
     | 
    
         
            +
                  host = 'host'
         
     | 
| 
      
 127 
     | 
    
         
            +
                  port = 'port'
         
     | 
| 
      
 128 
     | 
    
         
            +
                  options = 'options'
         
     | 
| 
      
 129 
     | 
    
         
            +
                  MongoDoc::Connection.stub(:host).and_return(host)
         
     | 
| 
      
 130 
     | 
    
         
            +
                  MongoDoc::Connection.stub(:port).and_return(port)
         
     | 
| 
      
 131 
     | 
    
         
            +
                  MongoDoc::Connection.stub(:options).and_return(options)
         
     | 
| 
      
 132 
     | 
    
         
            +
                  Mongo::Connection.should_receive(:new).with(host, port, options).and_return(connection)
         
     | 
| 
      
 133 
     | 
    
         
            +
                  MongoDoc::Connection.send(:connect)
         
     | 
| 
      
 134 
     | 
    
         
            +
                end
         
     | 
| 
       178 
135 
     | 
    
         | 
| 
       179 
     | 
    
         
            -
             
     | 
| 
       180 
     | 
    
         
            -
             
     | 
| 
       181 
     | 
    
         
            -
             
     | 
| 
       182 
     | 
    
         
            -
             
     | 
| 
       183 
     | 
    
         
            -
             
     | 
| 
       184 
     | 
    
         
            -
             
     | 
| 
       185 
     | 
    
         
            -
             
     | 
| 
       186 
     | 
    
         
            -
             
     | 
| 
       187 
     | 
    
         
            -
             
     | 
| 
       188 
     | 
    
         
            -
                      database = 'p_database'
         
     | 
| 
       189 
     | 
    
         
            -
                      host = 'p_host'
         
     | 
| 
       190 
     | 
    
         
            -
                      port = 890
         
     | 
| 
       191 
     | 
    
         
            -
                      options = {:option => 'p_opt'}
         
     | 
| 
       192 
     | 
    
         
            -
                      @connection.should_receive(:db).with(database).and_return(@db)
         
     | 
| 
       193 
     | 
    
         
            -
                      MongoDoc.should_receive(:connect).with(host, port, options).and_return(@connection)
         
     | 
| 
       194 
     | 
    
         
            -
                      MongoDoc.connect_to_database(database, host, port, options)
         
     | 
| 
       195 
     | 
    
         
            -
                    end
         
     | 
| 
       196 
     | 
    
         
            -
                  end
         
     | 
| 
      
 136 
     | 
    
         
            +
                it "raises NoConnectionError if the connection fails" do
         
     | 
| 
      
 137 
     | 
    
         
            +
                  Mongo::Connection.stub(:new).and_return(nil)
         
     | 
| 
      
 138 
     | 
    
         
            +
                  lambda { MongoDoc::Connection.send(:connect) }.should raise_error(MongoDoc::NoConnectionError)
         
     | 
| 
      
 139 
     | 
    
         
            +
                end
         
     | 
| 
      
 140 
     | 
    
         
            +
             
     | 
| 
      
 141 
     | 
    
         
            +
                it "verifies the connection version" do
         
     | 
| 
      
 142 
     | 
    
         
            +
                  Mongo::Connection.stub(:new).and_return(connection)
         
     | 
| 
      
 143 
     | 
    
         
            +
                  MongoDoc::Connection.should_receive(:verify_server_version)
         
     | 
| 
      
 144 
     | 
    
         
            +
                  MongoDoc::Connection.send(:connect)
         
     | 
| 
       197 
145 
     | 
    
         
             
                end
         
     | 
| 
       198 
146 
     | 
    
         
             
              end
         
     | 
| 
       199 
147 
     | 
    
         
             
            end
         
     |