aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/arel/nodes/select_core_test.rb
blob: 6860f2a3954b47b0b7d3d5bf6aa526ac4bdc129b (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
# frozen_string_literal: true

require_relative "../helper"

module Arel
  module Nodes
    class TestSelectCore < Arel::Test
      def test_clone
        core = Arel::Nodes::SelectCore.new
        core.froms       = %w[a b c]
        core.projections = %w[d e f]
        core.wheres      = %w[g h i]

        dolly = core.clone

        assert_equal core.froms, dolly.froms
        assert_equal core.projections, dolly.projections
        assert_equal core.wheres, dolly.wheres

        assert_not_same core.froms, dolly.froms
        assert_not_same core.projections, dolly.projections
        assert_not_same core.wheres, dolly.wheres
      end

      def test_set_quantifier
        core = Arel::Nodes::SelectCore.new
        core.set_quantifier = Arel::Nodes::Distinct.new
        viz = Arel::Visitors::ToSql.new Table.engine.connection_pool
        assert_match "DISTINCT", viz.accept(core, Collectors::SQLString.new).value
      end

      def test_equality_with_same_ivars
        core1 = SelectCore.new
        core1.froms       = %w[a b c]
        core1.projections = %w[d e f]
        core1.wheres      = %w[g h i]
        core1.groups      = %w[j k l]
        core1.windows     = %w[m n o]
        core1.havings     = %w[p q r]
        core1.comment     = Arel::Nodes::Comment.new(["comment"])
        core2 = SelectCore.new
        core2.froms       = %w[a b c]
        core2.projections = %w[d e f]
        core2.wheres      = %w[g h i]
        core2.groups      = %w[j k l]
        core2.windows     = %w[m n o]
        core2.havings     = %w[p q r]
        core2.comment     = Arel::Nodes::Comment.new(["comment"])
        array = [core1, core2]
        assert_equal 1, array.uniq.size
      end

      def test_inequality_with_different_ivars
        core1 = SelectCore.new
        core1.froms       = %w[a b c]
        core1.projections = %w[d e f]
        core1.wheres      = %w[g h i]
        core1.groups      = %w[j k l]
        core1.windows     = %w[m n o]
        core1.havings     = %w[p q r]
        core1.comment     = Arel::Nodes::Comment.new(["comment"])
        core2 = SelectCore.new
        core2.froms       = %w[a b c]
        core2.projections = %w[d e f]
        core2.wheres      = %w[g h i]
        core2.groups      = %w[j k l]
        core2.windows     = %w[m n o]
        core2.havings     = %w[l o l]
        core2.comment     = Arel::Nodes::Comment.new(["comment"])
        array = [core1, core2]
        assert_equal 2, array.uniq.size
        core2.havings     = %w[p q r]
        core2.comment     = Arel::Nodes::Comment.new(["other"])
        array = [core1, core2]
        assert_equal 2, array.uniq.size
      end
    end
  end
end