aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/arel
diff options
context:
space:
mode:
authorNikolai B <nikolai-b@users.noreply.github.com>2018-04-25 17:16:48 +0100
committerNikolai B <nikolai-b@users.noreply.github.com>2018-05-14 19:36:56 +0100
commit2beafeddf0378c0b4b0780c0d57c694b79f1f710 (patch)
tree1184e4c61b0eeec89914d9747cf3edc6d3380d3c /activerecord/test/cases/arel
parentdf850b8d7126044a820291bbfd2bce80636a442c (diff)
downloadrails-2beafeddf0378c0b4b0780c0d57c694b79f1f710.tar.gz
rails-2beafeddf0378c0b4b0780c0d57c694b79f1f710.tar.bz2
rails-2beafeddf0378c0b4b0780c0d57c694b79f1f710.zip
Add math tests
After #449 was merged math can be done on these nodes, adding a test file to unit test all the math operators.
Diffstat (limited to 'activerecord/test/cases/arel')
-rw-r--r--activerecord/test/cases/arel/attributes/math_test.rb83
-rw-r--r--activerecord/test/cases/arel/nodes/count_test.rb9
2 files changed, 83 insertions, 9 deletions
diff --git a/activerecord/test/cases/arel/attributes/math_test.rb b/activerecord/test/cases/arel/attributes/math_test.rb
new file mode 100644
index 0000000000..f3aabe4f60
--- /dev/null
+++ b/activerecord/test/cases/arel/attributes/math_test.rb
@@ -0,0 +1,83 @@
+# frozen_string_literal: true
+
+require_relative "../helper"
+
+module Arel
+ module Attributes
+ class MathTest < Arel::Spec
+ %i[* /].each do |math_operator|
+ it "average should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].average.public_send(math_operator, 2)).to_sql.must_be_like %{
+ AVG("users"."id") #{math_operator} 2
+ }
+ end
+
+ it "count should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].count.public_send(math_operator, 2)).to_sql.must_be_like %{
+ COUNT("users"."id") #{math_operator} 2
+ }
+ end
+
+ it "maximum should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].maximum.public_send(math_operator, 2)).to_sql.must_be_like %{
+ MAX("users"."id") #{math_operator} 2
+ }
+ end
+
+ it "minimum should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].minimum.public_send(math_operator, 2)).to_sql.must_be_like %{
+ MIN("users"."id") #{math_operator} 2
+ }
+ end
+
+ it "attribute node should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].public_send(math_operator, 2)).to_sql.must_be_like %{
+ "users"."id" #{math_operator} 2
+ }
+ end
+ end
+
+ %i[+ - & | ^ << >>].each do |math_operator|
+ it "average should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].average.public_send(math_operator, 2)).to_sql.must_be_like %{
+ (AVG("users"."id") #{math_operator} 2)
+ }
+ end
+
+ it "count should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].count.public_send(math_operator, 2)).to_sql.must_be_like %{
+ (COUNT("users"."id") #{math_operator} 2)
+ }
+ end
+
+ it "maximum should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].maximum.public_send(math_operator, 2)).to_sql.must_be_like %{
+ (MAX("users"."id") #{math_operator} 2)
+ }
+ end
+
+ it "minimum should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].minimum.public_send(math_operator, 2)).to_sql.must_be_like %{
+ (MIN("users"."id") #{math_operator} 2)
+ }
+ end
+
+ it "attribute node should be compatiable with #{math_operator}" do
+ table = Arel::Table.new :users
+ (table[:id].public_send(math_operator, 2)).to_sql.must_be_like %{
+ ("users"."id" #{math_operator} 2)
+ }
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/test/cases/arel/nodes/count_test.rb b/activerecord/test/cases/arel/nodes/count_test.rb
index 3107659e77..daabea6c4c 100644
--- a/activerecord/test/cases/arel/nodes/count_test.rb
+++ b/activerecord/test/cases/arel/nodes/count_test.rb
@@ -32,13 +32,4 @@ class Arel::Nodes::CountTest < Arel::Spec
assert_equal 2, array.uniq.size
end
end
-
- describe "math" do
- it "allows mathematical functions" do
- table = Arel::Table.new :users
- (table[:id].count + 1).to_sql.must_be_like %{
- (COUNT("users"."id") + 1)
- }
- end
- end
end