Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More criteria functions #1010

Merged
merged 6 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +74 to +75
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although in postgres the value looks like an integer when using these, the type is numeric which maps to Float64 in Crystal.

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