Sha256: b03fa8fb04b3f9ee89077c4a97255a599d002759752ebe54f2aced9bd12e912a

Contents?: true

Size: 938 Bytes

Versions: 1

Compression:

Stored size: 938 Bytes

Contents

require 'spec_helper'
require 'json'

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

      def_statement(:statement, 'SELECT a, b FROM foo').as(:json)
      def_prepared(:prepared, 'SELECT a, b FROM bar').as(:json)
    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 json' do
        expect(@conn).to receive(exec_type).and_return([
          { 'a' => '1', 'b' => 'foo' },
          { 'a' => '2', 'b' => nil }
        ])

        json = subject.send(method_type)
        expect(json).to eq(
          '[{"a":"1","b":"foo"},{"a":"2","b":null}]'
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
db_mod-0.0.4 spec/db_mod/statements/configuration/as/json_spec.rb