Skip to content

Commit

Permalink
More criteria functions (#1010)
Browse files Browse the repository at this point in the history
* Added LENGTH criteria function to String

* Added REVERSE criteria function to String

* Added ABS criteria function to Int

* Added ABS criteria function to Float64

* Added CEIL criteria function to Float64

* Added FLOOR criteria function to Float64
  • Loading branch information
jwoertink authored Mar 11, 2024
1 parent a88fdbc commit d8909ef
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 0 deletions.
33 changes: 33 additions & 0 deletions spec/avram/float_criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "../spec_helper"

private class QueryMe < BaseModel
COLUMN_SQL = "purchases.id, purchases.created_at, purchases.updated_at, purchases.amount"

table purchases do
column amount : Float64
end
end

describe Float64::Lucky::Criteria do
describe "abs" do
it "uses ABS" do
amount.abs.eq(39.99).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE ABS(purchases.amount) = $1", "39.99"]
end
end

describe "ceil" do
it "uses CEIL" do
amount.ceil.eq(40.0).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE CEIL(purchases.amount) = $1", "40.0"]
end
end

describe "floor" do
it "uses FLOOR" do
amount.floor.eq(39.0).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE FLOOR(purchases.amount) = $1", "39.0"]
end
end
end

private def amount
QueryMe::BaseQuery.new.amount
end
34 changes: 34 additions & 0 deletions spec/avram/integer_criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "../spec_helper"

private class QueryMe < BaseModel
COLUMN_SQL = "transactions.id, transactions.created_at, transactions.updated_at, transactions.small_amount, transactions.amount, transactions.big_amount"

table transactions do
column small_amount : Int16
column amount : Int32
column big_amount : Int64
end
end

# These specs handle all ints: Int16, Int32, Int64
describe "Int::Lucky::Criteria" do
describe "abs" do
it "uses ABS" do
small_amount.abs.eq(4).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(transactions.small_amount) = $1", "4"]
amount.abs.eq(400).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(transactions.amount) = $1", "400"]
big_amount.abs.eq(40000).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(transactions.big_amount) = $1", "40000"]
end
end
end

private def small_amount
QueryMe::BaseQuery.new.small_amount
end

private def amount
QueryMe::BaseQuery.new.amount
end

private def big_amount
QueryMe::BaseQuery.new.big_amount
end
12 changes: 12 additions & 0 deletions spec/avram/string_criteria_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ describe String::Lucky::Criteria do
end
end
end

describe "length" do
it "uses LENGTH" do
name.length.eq(4).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE LENGTH(users.name) = $1", "4"]
end
end

describe "reverse" do
it "uses REVERSE" do
name.reverse.eq("revir").to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE REVERSE(users.name) = $1", "revir"]
end
end
end

private def name
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/float64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ struct Float64
def select_sum! : Float64
select_sum || 0_f64
end

define_function_criteria(abs, V)
define_function_criteria(ceil, V)
define_function_criteria(floor, V)
end
end
end
2 changes: 2 additions & 0 deletions src/avram/charms/int16_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ struct Int16
def select_sum! : Int64
select_sum || 0_i64
end

define_function_criteria(abs, V)
end
end
end
2 changes: 2 additions & 0 deletions src/avram/charms/int32_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ struct Int32
def select_sum! : Int64
select_sum || 0_i64
end

define_function_criteria(abs, V)
end
end
end
2 changes: 2 additions & 0 deletions src/avram/charms/int64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct Int64
def select_sum! : Int64
select_sum || 0_i64
end

define_function_criteria(abs, V)
end
end
end
2 changes: 2 additions & 0 deletions src/avram/charms/string_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class String
define_function_criteria(upper, V)
define_function_criteria(lower, V)
define_function_criteria(trim, String)
define_function_criteria(length, Int64)
define_function_criteria(reverse, String)
end
end
end

0 comments on commit d8909ef

Please sign in to comment.