diff --git a/spec/adapter/adapters_spec.cr b/spec/adapter/adapters_spec.cr index 3b4cc5c2..41c70290 100644 --- a/spec/adapter/adapters_spec.cr +++ b/spec/adapter/adapters_spec.cr @@ -15,9 +15,21 @@ describe Granite::Connections do it "should allow connections to be be saved and looked up" do Granite::Connections.registered_connections.size.should eq 3 - Granite::Connections["mysql"].not_nil!.url.should eq ENV["MYSQL_DATABASE_URL"] - Granite::Connections["pg"].not_nil!.url.should eq ENV["PG_DATABASE_URL"] - Granite::Connections["sqlite"].not_nil!.url.should eq ENV["SQLITE_DATABASE_URL"] + if connection = Granite::Connections["mysql"] + connection.url.should eq ENV["MYSQL_DATABASE_URL"] + else + connection.should_not be_falsey + end + if connection = Granite::Connections["pg"] + connection.url.should eq ENV["PG_DATABASE_URL"] + else + connection.should_not be_falsey + end + if connection = Granite::Connections["sqlite"] + connection.url.should eq ENV["SQLITE_DATABASE_URL"] + else + connection.should_not be_falsey + end end it "should disallow multiple connections with the same name" do diff --git a/spec/granite/associations/has_many_spec.cr b/spec/granite/associations/has_many_spec.cr index 161f30af..3fb7ebe9 100644 --- a/spec/granite/associations/has_many_spec.cr +++ b/spec/granite/associations/has_many_spec.cr @@ -68,9 +68,13 @@ describe "has_many" do klass3.teacher = teacher klass3.save - klass = teacher.klasses.find_by(name: "Test class with different name").not_nil! - klass.id.should eq klass3.id - klass.name.should eq "Test class with different name" + klass = teacher.klasses.find_by(name: "Test class with different name") + if klass + klass.id.should eq klass3.id + klass.name.should eq "Test class with different name" + else + klass.should_not be_nil + end end it "#find_by!" do @@ -93,7 +97,7 @@ describe "has_many" do klass3.teacher = teacher klass3.save - klass = teacher.klasses.find_by!(name: "Test class with different name").not_nil! + klass = teacher.klasses.find_by!(name: "Test class with different name") klass.id.should eq klass3.id klass.name.should eq "Test class with different name" @@ -125,9 +129,13 @@ describe "has_many" do klass3.teacher = teacher klass3.save - klass = teacher.klasses.find(klass1.id).not_nil! - klass.id.should eq klass1.id - klass.name.should eq "Test class X" + klass = teacher.klasses.find(klass1.id) + if klass + klass.id.should eq klass1.id + klass.name.should eq "Test class X" + else + klass.should_not be_nil + end end it "#find!" do @@ -150,7 +158,7 @@ describe "has_many" do klass3.teacher = teacher klass3.save - klass = teacher.klasses.find!(klass1.id).not_nil! + klass = teacher.klasses.find!(klass1.id) klass.id.should eq klass1.id klass.name.should eq "Test class X" diff --git a/spec/granite/associations/has_many_through_spec.cr b/spec/granite/associations/has_many_through_spec.cr index ae87745c..fda28ea0 100644 --- a/spec/granite/associations/has_many_through_spec.cr +++ b/spec/granite/associations/has_many_through_spec.cr @@ -91,9 +91,13 @@ describe "has_many, through:" do enrollment3.student = student enrollment3.save - klass = student.klasses.find_by(name: "Test class with different name").not_nil! - klass.id.should eq klass3.id - klass.name.should eq "Test class with different name" + klass = student.klasses.find_by(name: "Test class with different name") + if klass + klass.id.should eq klass3.id + klass.name.should eq "Test class with different name" + else + klass.should_not be_nil + end end it "#find_by!" do @@ -118,7 +122,7 @@ describe "has_many, through:" do enrollment3.student = student enrollment3.save - klass = student.klasses.find_by!(name: "Test class with different name").not_nil! + klass = student.klasses.find_by!(name: "Test class with different name") klass.id.should eq klass3.id klass.name.should eq "Test class with different name" @@ -152,9 +156,13 @@ describe "has_many, through:" do enrollment3.student = student enrollment3.save - klass = student.klasses.find(klass1.id).not_nil! - klass.id.should eq klass1.id - klass.name.should eq "Test class X" + klass = student.klasses.find(klass1.id) + if klass + klass.id.should eq klass1.id + klass.name.should eq "Test class X" + else + klass.should_not be_nil + end end it "#find!" do @@ -179,7 +187,7 @@ describe "has_many, through:" do enrollment3.student = student enrollment3.save - klass = student.klasses.find!(klass1.id).not_nil! + klass = student.klasses.find!(klass1.id) klass.id.should eq klass1.id klass.name.should eq "Test class X" diff --git a/spec/granite/querying/find_by_spec.cr b/spec/granite/querying/find_by_spec.cr index e4f10a16..c77e6d64 100644 --- a/spec/granite/querying/find_by_spec.cr +++ b/spec/granite/querying/find_by_spec.cr @@ -9,7 +9,11 @@ describe "#find_by, #find_by!" do model.save.should be_true found = Parent.find_by(name: name) - found.not_nil!.id.should eq model.id + if pa = found + pa.id.should eq model.id + else + pa.should_not be_nil + end found = Parent.find_by!(name: name) found.should be_a(Parent) @@ -23,7 +27,12 @@ describe "#find_by, #find_by!" do expected = Review.create(name: "review3", upvotes: 10.to_i64) - Review.find_by(name: "review3", upvotes: 10).not_nil!.id.should eq expected.id + r = Review.find_by(name: "review3", upvotes: 10) + if r + r.id.should eq expected.id + else + r.should_not be_nil + end expect_raises(Granite::Querying::NotFound, /No .*Review.* found where name = review1 and upvotes = 20/) do Review.find_by!(name: "review1", upvotes: 20) @@ -37,7 +46,11 @@ describe "#find_by, #find_by!" do model.save.should be_true found = Student.find_by(name: nil) - found.not_nil!.id.should eq model.id + if stu = found + stu.id.should eq model.id + else + stu.should_not be_nil + end found = Student.find_by!(name: nil) found.should be_a(Student) @@ -52,7 +65,11 @@ describe "#find_by, #find_by!" do model.save.should be_true found = ReservedWord.find_by(all: value) - found.not_nil!.id.should eq model.id + if rw = found + rw.id.should eq model.id + else + rw.should_not be_nil + end found = ReservedWord.find_by!(all: value) found.id.should eq model.id @@ -66,7 +83,11 @@ describe "#find_by, #find_by!" do model.save.should be_true found = Parent.find_by({"name" => name}) - found.not_nil!.id.should eq model.id + if pa = found + pa.id.should eq model.id + else + pa.should_not be_nil + end found = Parent.find_by!({"name" => name}) found.should be_a(Parent) diff --git a/spec/granite/querying/find_spec.cr b/spec/granite/querying/find_spec.cr index 6d94f5f3..fd70dc58 100644 --- a/spec/granite/querying/find_spec.cr +++ b/spec/granite/querying/find_spec.cr @@ -8,7 +8,7 @@ describe "#find, #find!" do found = Parent.find model.id found.should_not be_nil - found.not_nil!.id.should eq model.id + found && (found.id.should eq model.id) found = Parent.find! model.id found.id.should eq model.id diff --git a/spec/granite/querying/first_spec.cr b/spec/granite/querying/first_spec.cr index 2549e181..b9251889 100644 --- a/spec/granite/querying/first_spec.cr +++ b/spec/granite/querying/first_spec.cr @@ -14,7 +14,11 @@ describe "#first, #first!" do end found = Parent.first - found.not_nil!.id.should eq first.id + if pa = found + pa.id.should eq first.id + else + pa.should_not be_nil + end found = Parent.first! found.id.should eq first.id @@ -33,7 +37,11 @@ describe "#first, #first!" do end found = Parent.first("ORDER BY id DESC") - found.not_nil!.id.should eq second.id + if pa = found + found.id.should eq second.id + else + pa.should_not be_nil + end found = Parent.first!("ORDER BY id DESC") found.id.should eq second.id diff --git a/spec/mocks/db_mock.cr b/spec/mocks/db_mock.cr index 71ea52b4..1c2a8646 100644 --- a/spec/mocks/db_mock.cr +++ b/spec/mocks/db_mock.cr @@ -88,4 +88,8 @@ class FieldEmitter < DB::ResultSet def column_name(index : Int32) : String "Column #{index}" end + + def next_column_index : Int32 + @field_position + end end diff --git a/src/granite/query/executors/value.cr b/src/granite/query/executors/value.cr index 501f0524..2e03f1df 100644 --- a/src/granite/query/executors/value.cr +++ b/src/granite/query/executors/value.cr @@ -10,10 +10,12 @@ module Granite::Query::Executor # db.scalar raises when a query returns 0 results, so I'm using query_one? # https://github.com/crystal-lang/crystal-db/blob/7d30e9f50e478cb6404d16d2ce91e639b6f9c476/src/db/statement.cr#L18 - raise "No default provided" if @default.nil? - - Model.adapter.open do |db| - db.query_one?(@sql, args: @args, as: Scalar) || @default.not_nil! + if @default.nil? + raise "No default provided" + else + Model.adapter.open do |db| + db.query_one?(@sql, args: @args, as: Scalar) || @default + end end end