Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Close connection from specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Schirp committed Sep 23, 2011
1 parent 662c662 commit 3851449
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
18 changes: 17 additions & 1 deletion lib/dm-mongo-adapter/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,23 @@ def connection
end
end

private
# Closes the active connection if any
#
# This method is mostly used from specs. But you can force a
# connection close from your code if needed.
#
# This method is a noop when no connection exists.
# Connections are established on demand.
#
# @return nil
#
# @api public
def close_connection
@connection and @connection.close
@connection = nil
end

private

def initialize(name, options = {})
# When giving a repository URI rather than a hash, the database name
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def setup_connection
end

def teardown_connection
#DataMapper.repository(:default).adapter.connection.close
#DataMapper::Repository.adapters.delete :default
DataMapper.repository(:default).adapter.close_connection
DataMapper::Repository.adapters.delete :default
end
end

Expand Down
32 changes: 32 additions & 0 deletions spec/unit/dm-mongo-adapter/adapter/close_connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe DataMapper::Mongo::Adapter, '#close_connection' do
let(:object) { described_class.new(:default,{}) }

subject { object.close_connection }


context 'when adapter is connected' do
let(:connection) { mock(:close => true) }

before do
object.instance_variable_set(:@connection,connection)
end

it 'should close the connection' do
connection.should_receive(:close)
subject
end

it 'should remove the connection' do
subject
object.instance_variable_get(:@connection).should be_false
end
end

context 'when adapter is not connected' do
it 'should be a noop' do
subject
end
end
end

0 comments on commit 3851449

Please sign in to comment.