aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/arel/attributes/math_test.rb
blob: 41eea217c0e767b5a1142e9b0afb9de24753c66c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 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