Skip to content

Commit

Permalink
Segfaults when running 'crystal spec'
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciek Sakrejda committed May 29, 2015
1 parent cafe1ea commit 4da9d40
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
24 changes: 12 additions & 12 deletions spec/pg/connection_spec.cr
@@ -1,6 +1,6 @@
describe PG::Connection, "#initialize" do
it "works on a good connection" do
PG::Connection.new(DB_URL)
PG::Connection.new($DB_URL)

This comment has been minimized.

Copy link
@will

will May 29, 2015

This was incorrectly seded.

end

it "raises on bad connections" do
Expand All @@ -10,51 +10,51 @@ end

describe PG::Connection, "#exec untyped" do
it "returns a Result" do
res = DB.exec("select 1")
res = $DB.exec("select 1")
res.class.should eq(PG::Result(Array(PG::PGValue)))
end

it "raises on bad queries" do
expect_raises(PG::ResultError) { DB.exec("select nocolumn from notable") }
expect_raises(PG::ResultError) { $DB.exec("select nocolumn from notable") }
end

it "returns a Result when create table" do
res = DB.exec("create table if not exists test()")
res = $DB.exec("create table if not exists test()")
res.class.should eq(PG::Result(Array(PG::PGValue)))
DB.exec("drop table test")
$DB.exec("drop table test")
end
end

describe PG::Connection, "#exec typed" do
it "returns a Result" do
res = DB.exec({Int32}, "select 1")
res = $DB.exec({Int32}, "select 1")
res.class.should eq( PG::Result({Int32.class}) )
end

it "raises on bad queries" do
expect_raises(PG::ResultError) { DB.exec({Int32}, "select nocolumn from notable") }
expect_raises(PG::ResultError) { $DB.exec({Int32}, "select nocolumn from notable") }
end
end

describe PG::Connection, "#exec typed with params" do
it "returns a Result" do
res = DB.exec({Float64}, "select $1::float * $2::float ", [3.4, -2])
res = $DB.exec({Float64}, "select $1::float * $2::float ", [3.4, -2])
res.class.should eq( PG::Result({Float64.class}) )
end

it "raises on bad queries" do
expect_raises(PG::ResultError) { DB.exec("select $1::text from notable", ["hello"]) }
expect_raises(PG::ResultError) { $DB.exec("select $1::text from notable", ["hello"]) }
end
end

describe PG::Connection, "#exec untyped with params" do
it "returns a Result" do
res = DB.exec("select $1::text, $2::text, $3::text", ["hello", "", "world"])
res = $DB.exec("select $1::text, $2::text, $3::text", ["hello", "", "world"])
res.class.should eq(PG::Result(Array(PG::PGValue)))
end

it "raises on bad queries" do
expect_raises(PG::ResultError) { DB.exec("select $1::text from notable", ["hello"]) }
expect_raises(PG::ResultError) { $DB.exec("select $1::text from notable", ["hello"]) }
end

it "can properly encode various types" do
Expand All @@ -63,7 +63,7 @@ describe PG::Connection, "#exec untyped with params" do
query = "select
$1::text, $2::int, $3::text, $4::float, $5::timestamptz, $6::date, $7::bool"
param = ["hello", 2, nil, -4.23, time, date, true]
res = DB.exec(query, param)
res = $DB.exec(query, param)
res.rows.should eq([param])
end

Expand Down
14 changes: 7 additions & 7 deletions spec/pg/result_spec.cr
Expand Up @@ -2,7 +2,7 @@ require "../spec_helper"

macro test_decode(name, select, expected, file = __FILE__, line = __LINE__)
it {{name}}, {{file}}, {{line}} do
rows = DB.exec("select #{{{select}}}").rows
rows = $DB.exec("select #{{{select}}}").rows
rows.size.should eq( 1 )
rows.first.size.should eq( 1 )
rows.first.first.should eq( {{expected}} )
Expand All @@ -12,13 +12,13 @@ end
describe PG::Result, "#fields" do
it "is empty on empty results" do
if Helper.db_version_gte(9,4)
fields = DB.exec("select").fields
fields = $DB.exec("select").fields
fields.size.should eq(0)
end
end

it "is is a list of the fields" do
fields = DB.exec("select 1 as one, 2 as two, 3 as three").fields
fields = $DB.exec("select 1 as one, 2 as two, 3 as three").fields
fields.map(&.name).should eq(["one", "two", "three"])
fields.map(&.oid).should eq([23,23,23])
end
Expand All @@ -27,14 +27,14 @@ end
describe PG::Result, "#rows" do
it "is an empty 2d array on empty results" do
if Helper.db_version_gte(9,4)
rows = DB.exec("select").rows
rows = $DB.exec("select").rows
rows.size.should eq(1)
rows[0].size.should eq(0)
end
end

it "can handle several types and several rows" do
rows = DB.exec(
rows = $DB.exec(
{String, PG::NilableString, Bool, Int32},
"select 'a', 'b', true, 22 union all select '', null, false, 53"
).rows
Expand Down Expand Up @@ -78,7 +78,7 @@ end

describe PG::Result, "#to_hash" do
it "represents the rows and fields as a hash" do
res = DB.exec("select 'a' as foo, 'b' as bar, true as baz, 1.0 as uhh
res = $DB.exec("select 'a' as foo, 'b' as bar, true as baz, 1.0 as uhh
union all
select '', null, false, -3.2")
res.to_hash.should eq([
Expand All @@ -88,7 +88,7 @@ describe PG::Result, "#to_hash" do
end

it "raises if there are columns with the same name" do
res = DB.exec("select 'a' as foo, 'b' as foo, 'c' as bar")
res = $DB.exec("select 'a' as foo, 'b' as foo, 'c' as bar")
expect_raises { res.to_hash }
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Expand Up @@ -2,11 +2,11 @@ require "spec"
require "../src/pg"

DB_URL = ENV["DATABASE_URL"]? || "postgres:///"
DB = PG.connect(DB_URL)
$DB = PG.connect(DB_URL)

module Helper
def self.db_version_gte(major, minor, patch=0)
ver = DB.version
ver = $DB.version
ver[:major] >= major && ver[:minor] >= minor && ver[:patch] >= patch
end
end
4 changes: 3 additions & 1 deletion src/pg.cr
Expand Up @@ -3,6 +3,8 @@ require "./pg/*"

module PG
def self.connect(conninfo)
Connection.new(conninfo)
conn = Connection.new(conninfo)
conn.exec("SET extra_float_digits = 3")
conn
end
end

0 comments on commit 4da9d40

Please sign in to comment.