require 'bsf/database'

describe Bsf::Database do

  before(:each) do
    in_memory_db = Sequel.sqlite
    Sequel.stub(:connect).and_return(in_memory_db)
  end

  describe '.initialize' do

    it 'connects to the database' do
      described_class.new(options).instance_variable_get(:@connection).class.
        should == Sequel::SQLite::Database
    end

    context 'exception handling' do

      before(:each) do
        Sequel.stub(:connect).and_raise StandardError, "Test error"
      end

      it "raises an exception" do
        lambda { described_class.new(options) }.
          should raise_error StandardError, /Test error/
      end

    end

  end

  def options
    { :database_name => 'test', :database_user => 'test',
      :database_password => 'test' }
  end

end
require 'bsf/database'

describe Bsf::Database do

  before(:each) do
    in_memory_db = Sequel.sqlite
    Sequel.stub(:connect).and_return(in_memory_db)
  end

  describe '.initialize' do

    it 'connects to the database via socket' do
      described_class.new(options).instance_variable_get(:@connection).class.
        should == Sequel::SQLite::Database
    end

    it 'connects to the database via TCP/IP' do
      described_class.new(options(true)).instance_variable_get(:@connection).class.
        should == Sequel::SQLite::Database
    end

    context 'exception handling' do

      before(:each) do
        Sequel.stub(:connect).and_raise StandardError, "Test error"
      end

      it "raises an exception" do
        lambda { described_class.new(options) }.
          should raise_error StandardError, /Test error/
      end

    end

  end

  def options(host = false)
    hash = { :database_name => 'test', :database_user => 'test',
      :database_password => 'test' }
    hash[:database_host] = '127.0.0.1' if host
    hash
  end

end
Add database TCP/IP connection test.
Add database TCP/IP connection test.

Add a test to make sure that our Database class will connect via
TCP/IP if the option is included in the hash.

While in the background the database is still an in memory
database that doesn't care about the hash passed to it we should
still test to see that the line adding that hash key is executed
