Sha256: af706c22057900307e24443a5e1cdfebc3e9c113e82b30f3a5190e86706c8616

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

require 'spec_helper'
require 'csv'

describe DbMod::Statements::Configuration::Returning do
  before do
    @conn = instance_double 'PGconn'
    allow(@conn).to receive(:prepare)
    allow(PGconn).to receive(:connect).and_return(@conn)

    allow(@conn).to receive(:exec_prepared).with('a').and_return([
      { 'name' => 'cow', 'sound' => 'moo' },
      { 'name' => 'dog', 'sound' => 'woof' }
    ])
  end

  subject do
    Module.new do
      include DbMod

      def_prepared(:a, 'SELECT name, sound FROM animals') do
        returning do |animals|
          animals.map do |animal|
            "the #{animal['name']} goes #{animal['sound']}"
          end.join ' and '
        end
      end

      def send_email(*); end

      def_prepared(:b, 'SELECT address FROM email WHERE id = $1') do
        single(:value)

        returning { |email| send_email(email, a) }
      end

      def_prepared(:c, 'SELECT * FROM bar') do
        as(:csv)

        returning { |json| send_email('som@body', json) }
      end
    end.create db: 'test'
  end

  it 'performs arbitrary result transformations' do
    expect(subject.a).to eq(
      'the cow goes moo and the dog goes woof'
    )
  end

  it 'has access to module instance scope' do
    allow(@conn).to receive(:exec_prepared).with('b', [1]).and_return([
      { 'address' => 'ex@mple' }
    ])
    expect(subject).to receive(:send_email).with(
      'ex@mple',
      'the cow goes moo and the dog goes woof'
    )
    subject.b(1)
  end

  it 'works with as' do
    allow(@conn).to receive(:exec_prepared).and_return([
      { 'a' => '1', 'b' => '2' },
      { 'a' => '3', 'b' => '4' }
    ])
    expect(subject).to receive(:send_email).with('som@body', "a,b\n1,2\n3,4\n")
    subject.c
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
db_mod-0.0.6 spec/db_mod/statements/configuration/returning_spec.rb