Sha256: 75c5c1c64111dc84fbfaf979d46c964b17dbc7ecb7b7b6cdf0f428ddbfc4b15e

Contents?: true

Size: 934 Bytes

Versions: 2

Compression:

Stored size: 934 Bytes

Contents

require 'spec_helper'
require 'csv'

describe DbMod::Statements::Configuration::As::Csv do
  subject do
    Module.new do
      include DbMod

      def_statement(:statement, 'SELECT a, b FROM foo') { as(:csv) }
      def_prepared(:prepared, 'SELECT a, b FROM bar') { as(:csv) }
    end.create(db: 'testdb')
  end

  before do
    @conn = instance_double 'PGconn'
    allow(@conn).to receive(:prepare)
    allow(PGconn).to receive(:connect).and_return @conn
  end

  {
    statement: :query,
    prepared: :exec_prepared
  }.each do |method_type, exec_type|
    context "#{method_type} methods" do
      it 'coerces results to csv' do
        expect(@conn).to receive(exec_type).and_return([
          { 'a' => '1', 'b' => '2' },
          { 'a' => '3', 'b' => '4' },
          { 'a' => nil, 'b' => '5' }
        ])

        csv = subject.send(method_type)
        expect(csv).to eq("a,b\n1,2\n3,4\n,5\n")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
db_mod-0.0.6 spec/db_mod/statements/configuration/as/csv_spec.rb
db_mod-0.0.5 spec/db_mod/statements/configuration/as/csv_spec.rb