Sha256: 3f0e07d4325aae13866e0baf36c2008dd46174c91e0d629e248831529db85e04

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8

require 'spec_helper'
require 'axiom/adapter/data_objects'

describe Adapter::DataObjects, '#read' do
  let(:uri)       { double                   }
  let(:object)    { described_class.new(uri) }
  let(:relation)  { double('Relation')       }
  let(:statement) { double('Statement')      }
  let(:rows)      { [[1], [2], [3]]          }
  let(:yields)    { []                       }

  before do
    allow(statement).to receive(:each, &rows.method(:each))
    allow(described_class::Statement).to receive(:new).and_return(statement)
  end

  context 'with a block' do
    subject { object.read(relation) { |row| yields << row } }

    let(:connection) { double('Connection', close: nil) }

    before do
      allow(DataObjects::Connection).to receive(:new).and_return(connection)
    end

    it_should_behave_like 'a command method'

    it 'opens a connection' do
      expect(DataObjects::Connection).to receive(:new).with(uri).and_return(connection)
      subject
    end

    it 'closes a connection' do
      expect(connection).to receive(:close).with(no_args)
      subject
    end

    it 'does not close a connection if the constructor throws an exception' do
      mock_exception = Class.new(Exception)
      expect(DataObjects::Connection).to receive(:new).and_raise(mock_exception)
      expect(connection).not_to receive(:close)
      expect { subject }.to raise_error(mock_exception)
    end

    it 'yields each row' do
      expect { subject }.to change { yields.dup }.from([]).to(rows)
    end

    it 'initializes a statement' do
      expect(described_class::Statement).to receive(:new).with(connection, relation).and_return(statement)
      subject
    end
  end

  context 'without a block' do
    subject { object.read(relation) }

    it { should be_instance_of(to_enum.class) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axiom-do-adapter-0.2.0 spec/unit/axiom/adapter/data_objects/read_spec.rb