Skip to content

Commit

Permalink
Add Float64 to ColumnType (#637)
Browse files Browse the repository at this point in the history
* Add Float64 to ColumnType

* Add some missing tests on the Float64 adapter

* Crystal tool format

* Add some tests on Double Precision

* Add a simple where double query

* Missing crystal tool format
  • Loading branch information
brunto committed Mar 4, 2021
1 parent b3feadc commit c1ef94c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
@@ -0,0 +1,16 @@
class AddLatitudeAndLongitudeToBusinesses::V20210217224833 < Avram::Migrator::Migration::V1
def migrate
execute <<-SQL
ALTER TABLE businesses
ADD COLUMN latitude DOUBLE PRECISION,
ADD COLUMN longitude DOUBLE PRECISION;
SQL
end

def rollback
alter :businesses do
remove :latitude
remove :longitude
end
end
end
4 changes: 2 additions & 2 deletions spec/operations/nested_save_operation_spec.cr
Expand Up @@ -9,7 +9,7 @@ private class SaveBusiness < Business::SaveOperation
permit_columns number
end

permit_columns name
permit_columns name, latitude, longitude
has_one save_email_address : SaveEmailAddress
has_one save_tax_id : SaveTaxId
end
Expand Down Expand Up @@ -136,7 +136,7 @@ describe "Avram::SaveOperation with nested operation" do

context "when all forms are valid" do
it "sets the relationship and creates both" do
params = NestedParams.new business: {"name" => "Fubar"},
params = NestedParams.new business: {"name" => "Fubar", "latitude" => "46.383488", "longitude" => "22.774896"},
email_address: {"address" => "foo@bar.com"},
tax_id: {"number" => "123"}

Expand Down
13 changes: 13 additions & 0 deletions spec/queryable_spec.cr
Expand Up @@ -973,6 +973,19 @@ describe Avram::Queryable do
end
end

context "when querying double" do
describe "simple where double" do
it "returns 1 result" do
business = BusinessFactory.new.create

query = BusinessQuery.new.name(business.name)
query.to_sql.should eq ["SELECT #{Business::COLUMN_SQL} FROM businesses WHERE businesses.name = $1", business.name]
result = query.first
result.should eq business
end
end
end

describe ".truncate" do
it "truncates the table" do
10.times { UserFactory.create }
Expand Down
2 changes: 2 additions & 0 deletions spec/support/factories/business_factory.cr
@@ -1,5 +1,7 @@
class BusinessFactory < BaseFactory
def initialize
name "My Biz"
latitude 46.383488
longitude 22.774896
end
end
7 changes: 7 additions & 0 deletions spec/support/models/business.cr
@@ -1,6 +1,10 @@
class Business < BaseModel
COLUMN_SQL = "businesses.id, businesses.created_at, businesses.updated_at, businesses.name, businesses.latitude, businesses.longitude"

table do
column name : String
column latitude : Float64?
column longitude : Float64?
has_one tax_id : TaxId
has_one email_address : EmailAddress
end
Expand All @@ -12,3 +16,6 @@ class TaxId < BaseModel
belongs_to business : Business
end
end

class BusinessQuery < Business::BaseQuery
end
48 changes: 48 additions & 0 deletions spec/type_extensions/float64_spec.cr
@@ -0,0 +1,48 @@
require "../spec_helper"

describe "Float64" do
it "parses Float64 from Float64" do
result = Float64.adapter.parse(10.0)
result.value.should eq(10.0)
end

it "parses Array(Float64) from Arrray(Float64)" do
result = Float64.adapter.parse([10.0, 20.0])
result.value.should eq([10.0, 20.0])
end

it "parses Float64 from PG::Numeric" do
result = Float64.adapter.parse(n(0, 0, 0, 1, [] of Int16))
result.value.should eq(0.0)
end

it "parses Array(Float64) from Array(PG::Numeric)" do
result = Float64.adapter.parse(
[
n(0, 0, 0, 1, [] of Int16),
n(1, 0, 0, 0, [1]),
]
)
result.value.should eq([0.0, 1.0])
end

it "parses Float64 from String" do
result = Float64.adapter.parse("10.0")
result.value.should eq(10.0)
end

it "parses Float64 from Int32" do
result = Float64.adapter.parse(10.to_i32)
result.value.should eq(10.0)
end

it "parses Float64 from Int64" do
result = Float64.adapter.parse(10.to_i64)
result.value.should eq(10.0)
end
end

# See: https://github.com/will/crystal-pg/blob/master/spec/pg/numeric_spec.cr#L4
private def n(nd, w, s, ds, d)
PG::Numeric.new(nd.to_i16, w.to_i16, s.to_i16, ds.to_i16, d.map(&.to_i16))
end
2 changes: 1 addition & 1 deletion src/avram/charms/float64_extensions.cr
Expand Up @@ -4,7 +4,7 @@ struct Float64
end

module Lucky
alias ColumnType = ::PG::Numeric
alias ColumnType = ::PG::Numeric | Float64
include Avram::Type

def self.criteria(query : T, column) forall T
Expand Down

0 comments on commit c1ef94c

Please sign in to comment.