Sha256: 98ddabf92fec3125bcf71bdb00b2e568b0fc0a9f556db4b5e761454b3384171e

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

require 'daru_lite/io/sql_data_source'
require 'sqlite3'
require 'dbi'
require 'active_record'

RSpec.describe DaruLite::IO::SqlDataSource do
  include_context 'with accounts table in sqlite3 database'

  let(:query) do
    'select * from accounts'
  end

  let(:source) do
    ActiveRecord::Base.establish_connection("sqlite3:#{db_name}")
    ActiveRecord::Base.connection
  end

  describe '.make_dataframe' do
    subject(:df) { DaruLite::IO::SqlDataSource.make_dataframe(source, query) }

    context 'with DBI::DatabaseHandle' do
      let(:source) { DBI.connect("DBI:SQLite3:#{db_name}") }
      it { is_expected.to be_a(DaruLite::DataFrame) }
      it { expect(df.row[0]).to have_attributes(id: 1, age: 20) }
      its(:nrows) { is_expected.to eq 2 }
    end

    context 'with ActiveRecord::Connection' do
      it { is_expected.to be_a(DaruLite::DataFrame) }
      it { expect(df.row[0]).to have_attributes(id: 1, age: 20) }
      its(:nrows) { is_expected.to eq 2 }
    end

    context 'with path to sqlite3 file' do
      let(:source) { db_name }
      it { is_expected.to be_a(DaruLite::DataFrame) }
      it { expect(df.row[0]).to have_attributes(id: 1, age: 20) }
      its(:nrows) { is_expected.to eq 2 }
    end

    context 'with an object not a string as a query' do
      let(:query) { Object.new }
      it { expect { df }.to raise_error(ArgumentError) }
    end

    context 'with an object not a database connection' do
      let(:source) { Object.new }
      it { expect { df }.to raise_error(ArgumentError) }
    end

    context 'with path to unsupported db file' do
      let(:source) { 'spec/fixtures/bank2.dat' }
      it { expect { df }.to raise_error(ArgumentError) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
daru_lite-0.1.3 spec/io/sql_data_source_spec.rb
daru_lite-0.1.2 spec/io/sql_data_source_spec.rb
daru_lite-0.1.1 spec/io/sql_data_source_spec.rb
daru_lite-0.1 spec/io/sql_data_source_spec.rb