Sha256: bee67e41a5ae7d0913c835c51740a2c7bae95fff866b2b7ef14e00e9ff933185

Contents?: true

Size: 1.61 KB

Versions: 83

Compression:

Stored size: 1.61 KB

Contents

require "spec_helper"

describe "Simple::SQL conversions" do
  def expects(expected_result, sql, *args)
    expect(SQL.ask(sql, *args)).to eq(expected_result)
  end

  describe "data conversions" do
    it "returns arrays when asking for multiple columns" do
      expects ["one", "two", 3.5], "SELECT 'one', 'two', 3.5"
    end

    it "converts text arrays" do
      expects ["foo,bar"], "SELECT ARRAY[$1::varchar]", "foo,bar"
      expects %w(foo bar), "SELECT $1::varchar[]", %w(foo bar)
    end

    it "converts integer arrays" do
      expects [1, 2, 3], "SELECT ARRAY[1,2,3]"
    end

    it "converts weird strings properly" do
      expects "foo,\"bar}", "SELECT $1::varchar", "foo,\"bar}"
      expects ["one", "two", "3.5", "foo,\"bar}"], "SELECT ARRAY['one', 'two', '3.5', 'foo,\"bar}']"
      expects ["foo", "foo,bar}"], "SELECT $1::varchar[]", ["foo", "foo,bar}"]
    end

    it "parses JSON as expected" do
      expects({"a"=>1, "b"=>2}, 'SELECT \'{"a":1,"b":2}\'::json')
      expects({"a"=>1, "b"=>2}, 'SELECT \'{"a":1,"b":2}\'::jsonb')
    end

    it "converts double precision" do
      expects 1.0, "SELECT 1.0::double precision"
    end

    it "converts bool" do
      expects true, "SELECT TRUE"
      expects false, "SELECT FALSE"
    end

    it "converts hstore" do
      expects({ a: "1", b: "3" }, "SELECT 'a=>1,b=>3'::hstore")
    end

    xit "fails sometimes w/ malformed array literal, pt. 1" do
      expects  0, 'SELECT $1::varchar[]', [ "foo", 'foo,"bar}' ]
    end

    xit "fails sometimes w/ malformed array literal, pt. 2" do
      expects  0, 'SELECT $1::varchar[]', [ "foo", 'foo,"bar}' ]
    end
  end
end

Version data entries

83 entries across 83 versions & 1 rubygems

Version Path
simple-sql-0.5.31 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.30 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.29 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.28 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.27 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.26 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.25 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.24 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.23 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.22 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.21 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.20 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.19 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.18 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.17 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.16 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.15 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.14 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.13 spec/simple/sql/conversion_spec.rb
simple-sql-0.5.12 spec/simple/sql/conversion_spec.rb