Skip to content

Commit

Permalink
ameba Lint/NotNil: Avoid using not_nil!
Browse files Browse the repository at this point in the history
  • Loading branch information
pan committed Dec 2, 2022
1 parent 65a1fbc commit 10a7893
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 31 deletions.
18 changes: 15 additions & 3 deletions spec/adapter/adapters_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions spec/granite/associations/has_many_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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"

Expand Down
24 changes: 16 additions & 8 deletions spec/granite/associations/has_many_through_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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"

Expand Down
31 changes: 26 additions & 5 deletions spec/granite/querying/find_by_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/granite/querying/find_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions spec/granite/querying/first_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/mocks/db_mock.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions src/granite/query/executors/value.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 10a7893

Please sign in to comment.