diff --git a/spec/avram/float_criteria_spec.cr b/spec/avram/float_criteria_spec.cr new file mode 100644 index 000000000..9a2dc145f --- /dev/null +++ b/spec/avram/float_criteria_spec.cr @@ -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 diff --git a/spec/avram/integer_criteria_spec.cr b/spec/avram/integer_criteria_spec.cr new file mode 100644 index 000000000..27396c545 --- /dev/null +++ b/spec/avram/integer_criteria_spec.cr @@ -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 diff --git a/spec/avram/string_criteria_spec.cr b/spec/avram/string_criteria_spec.cr index 25621e696..b699690da 100644 --- a/spec/avram/string_criteria_spec.cr +++ b/spec/avram/string_criteria_spec.cr @@ -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 diff --git a/src/avram/charms/float64_extensions.cr b/src/avram/charms/float64_extensions.cr index a06cca6b6..ffed54e66 100644 --- a/src/avram/charms/float64_extensions.cr +++ b/src/avram/charms/float64_extensions.cr @@ -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 diff --git a/src/avram/charms/int16_extensions.cr b/src/avram/charms/int16_extensions.cr index a8b8f2eb0..9c48472bc 100644 --- a/src/avram/charms/int16_extensions.cr +++ b/src/avram/charms/int16_extensions.cr @@ -58,6 +58,8 @@ struct Int16 def select_sum! : Int64 select_sum || 0_i64 end + + define_function_criteria(abs, V) end end end diff --git a/src/avram/charms/int32_extensions.cr b/src/avram/charms/int32_extensions.cr index 42f8458a7..d2ca065ac 100644 --- a/src/avram/charms/int32_extensions.cr +++ b/src/avram/charms/int32_extensions.cr @@ -59,6 +59,8 @@ struct Int32 def select_sum! : Int64 select_sum || 0_i64 end + + define_function_criteria(abs, V) end end end diff --git a/src/avram/charms/int64_extensions.cr b/src/avram/charms/int64_extensions.cr index 3ae83c747..665c4f49d 100644 --- a/src/avram/charms/int64_extensions.cr +++ b/src/avram/charms/int64_extensions.cr @@ -57,6 +57,8 @@ struct Int64 def select_sum! : Int64 select_sum || 0_i64 end + + define_function_criteria(abs, V) end end end diff --git a/src/avram/charms/string_extensions.cr b/src/avram/charms/string_extensions.cr index 25fe87655..c872661b1 100644 --- a/src/avram/charms/string_extensions.cr +++ b/src/avram/charms/string_extensions.cr @@ -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