aboutsummaryrefslogtreecommitdiffstats
path: root/test/visitors/test_depth_first.rb
blob: baa8f6418417e9e01c2acadf702473389f7e59dc (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
require 'helper'

module Arel
  module Visitors
    class TestDepthFirst < Minitest::Test
      Collector = Struct.new(:calls) do
        def call object
          calls << object
        end
      end

      def setup
        @collector = Collector.new []
        @visitor = Visitors::DepthFirst.new @collector
      end

      def test_raises_with_object
        assert_raises(TypeError) do
          @visitor.accept(Object.new)
        end
      end


      # unary ops
      [
        Arel::Nodes::Not,
        Arel::Nodes::Group,
        Arel::Nodes::On,
        Arel::Nodes::Grouping,
        Arel::Nodes::Offset,
        Arel::Nodes::Ordering,
        Arel::Nodes::Having,
        Arel::Nodes::StringJoin,
        Arel::Nodes::UnqualifiedColumn,
        Arel::Nodes::Top,
        Arel::Nodes::Limit,
      ].each do |klass|
        define_method("test_#{klass.name.gsub('::', '_')}") do
          op = klass.new(:a)
          @visitor.accept op
          assert_equal [:a, op], @collector.calls
        end
      end

      # functions
      [
        Arel::Nodes::Exists,
        Arel::Nodes::Avg,
        Arel::Nodes::Min,
        Arel::Nodes::Max,
        Arel::Nodes::Sum,
      ].each do |klass|
        define_method("test_#{klass.name.gsub('::', '_')}") do
          func = klass.new(:a, "b")
          @visitor.accept func
          assert_equal [:a, "b", false, func], @collector.calls
        end
      end

      def test_named_function
        func = Arel::Nodes::NamedFunction.new(:a, :b, "c")
        @visitor.accept func
        assert_equal [:a, :b, false, "c", func], @collector.calls
      end

      def test_lock
        lock = Nodes::Lock.new true
        @visitor.accept lock
        assert_equal [lock], @collector.calls
      end

      def test_count
        count = Nodes::Count.new :a, :b, "c"
        @visitor.accept count
        assert_equal [:a, "c", :b, count], @collector.calls
      end

      def test_inner_join
        join = Nodes::InnerJoin.new :a, :b
        @visitor.accept join
        assert_equal [:a, :b, join], @collector.calls
      end

      def test_full_outer_join
        join = Nodes::FullOuterJoin.new :a, :b
        @visitor.accept join
        assert_equal [:a, :b, join], @collector.calls
      end

      def test_outer_join
        join = Nodes::OuterJoin.new :a, :b
        @visitor.accept join
        assert_equal [:a, :b, join], @collector.calls
      end

      def test_right_outer_join
        join = Nodes::RightOuterJoin.new :a, :b
        @visitor.accept join
        assert_equal [:a, :b, join], @collector.calls
      end

      [
        Arel::Nodes::Assignment,
        Arel::Nodes::Between,
        Arel::Nodes::DoesNotMatch,
        Arel::Nodes::Equality,
        Arel::Nodes::GreaterThan,
        Arel::Nodes::GreaterThanOrEqual,
        Arel::Nodes::In,
        Arel::Nodes::LessThan,
        Arel::Nodes::LessThanOrEqual,
        Arel::Nodes::Matches,
        Arel::Nodes::NotEqual,
        Arel::Nodes::NotIn,
        Arel::Nodes::Or,
        Arel::Nodes::TableAlias,
        Arel::Nodes::Values,
        Arel::Nodes::As,
        Arel::Nodes::DeleteStatement,
        Arel::Nodes::JoinSource,
      ].each do |klass|
        define_method("test_#{klass.name.gsub('::', '_')}") do
          binary = klass.new(:a, :b)
          @visitor.accept binary
          assert_equal [:a, :b, binary], @collector.calls
        end
      end

      def test_Arel_Nodes_InfixOperation
        binary = Arel::Nodes::InfixOperation.new(:o, :a, :b)
        @visitor.accept binary
        assert_equal [:a, :b, binary], @collector.calls
      end

      # N-ary
      [
        Arel::Nodes::And,
      ].each do |klass|
        define_method("test_#{klass.name.gsub('::', '_')}") do
          binary = klass.new([:a, :b, :c])
          @visitor.accept binary
          assert_equal [:a, :b, :c, binary], @collector.calls
        end
      end

      [
        Arel::Attributes::Integer,
        Arel::Attributes::Float,
        Arel::Attributes::String,
        Arel::Attributes::Time,
        Arel::Attributes::Boolean,
        Arel::Attributes::Attribute
      ].each do |klass|
        define_method("test_#{klass.name.gsub('::', '_')}") do
          binary = klass.new(:a, :b)
          @visitor.accept binary
          assert_equal [:a, :b, binary], @collector.calls
        end
      end

      def test_table
        relation = Arel::Table.new(:users)
        @visitor.accept relation
        assert_equal ['users', relation], @collector.calls
      end

      def test_array
        node = Nodes::Or.new(:a, :b)
        list = [node]
        @visitor.accept list
        assert_equal [:a, :b, node, list], @collector.calls
      end

      def test_hash
        node = Nodes::Or.new(:a, :b)
        hash = { node => node }
        @visitor.accept hash
        assert_equal [:a, :b, node, :a, :b, node, hash], @collector.calls
      end

      def test_update_statement
        stmt = Nodes::UpdateStatement.new
        stmt.relation = :a
        stmt.values << :b
        stmt.wheres << :c
        stmt.orders << :d
        stmt.limit = :e

        @visitor.accept stmt
        assert_equal [:a, :b, stmt.values, :c, stmt.wheres, :d, stmt.orders,
          :e, stmt], @collector.calls
      end

      def test_select_core
        core = Nodes::SelectCore.new
        core.projections << :a
        core.froms = :b
        core.wheres << :c
        core.groups << :d
        core.windows << :e
        core.having = :f

        @visitor.accept core
        assert_equal [
          :a, core.projections,
          :b, [],
          core.source,
          :c, core.wheres,
          :d, core.groups,
          :e, core.windows,
          :f,
          core], @collector.calls
      end

      def test_select_statement
        ss = Nodes::SelectStatement.new
        ss.cores.replace [:a]
        ss.orders << :b
        ss.limit = :c
        ss.lock = :d
        ss.offset = :e

        @visitor.accept ss
        assert_equal [
          :a, ss.cores,
          :b, ss.orders,
          :c,
          :d,
          :e,
          ss], @collector.calls
      end

      def test_insert_statement
        stmt = Nodes::InsertStatement.new
        stmt.relation = :a
        stmt.columns << :b
        stmt.values = :c

        @visitor.accept stmt
        assert_equal [:a, :b, stmt.columns, :c, stmt], @collector.calls
      end

      def test_node
        node = Nodes::Node.new
        @visitor.accept node
        assert_equal [node], @collector.calls
      end
    end
  end
end