aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/test/cases/arel/attributes/math_test.rb
blob: 41eea217c0e767b5a1142e9b0afb9de24753c66c (plain) (tree)
1
2
3
4
5
6
7
8
9







                                     
                                                                  





                                                                                   
                                                                





                                                                                 
                                                                  





                                                                                   
                                                                  





                                                                                   
                                                                         







                                                                           
                                                                  





                                                                                   
                                                                





                                                                                 
                                                                  





                                                                                   
                                                                  





                                                                                   
                                                                         








                                                                           
# frozen_string_literal: true

require_relative "../helper"

module Arel
  module Attributes
    class MathTest < Arel::Spec
      %i[* /].each do |math_operator|
        it "average should be compatible 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 compatible 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 compatible 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 compatible 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 compatible 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 compatible 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 compatible 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 compatible 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 compatible 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 compatible 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