Class: TC_Integration_Aggregate

Inherits:
SQLite3::TestCase show all
Defined in:
lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb,
lib/sqlite3-1.5.3-x86_64-darwin/test/test_integration_aggregate.rb

Defined Under Namespace

Classes: AccumulateAggregator, AccumulateAggregator2, AggregateHandler, CustomException, RaiseExceptionNewAggregateHandler, RaiseExceptionStepAggregateHandler

Instance Method Summary collapse

Methods inherited from SQLite3::TestCase

#assert_nothing_raised

Instance Method Details

#setupObject



4
5
6
7
8
9
10
11
12
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 4

def setup
  @db = SQLite3::Database.new(":memory:")
  @db.transaction do
    @db.execute "create table foo ( a integer primary key, b text, c integer )"
    @db.execute "insert into foo ( b, c ) values ( 'foo', 10 )"
    @db.execute "insert into foo ( b, c ) values ( 'bar', 11 )"
    @db.execute "insert into foo ( b, c ) values ( 'bar', 12 )"
  end
end

#teardownObject



14
15
16
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 14

def teardown
  @db.close
end

#test_aggregate_initialized_twiceObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 238

def test_aggregate_initialized_twice
  initialized = 0
  handler = Class.new(AggregateHandler) do
    define_method(:initialize) do
      initialized += 1
      super()
    end
  end

  @db.create_aggregate_handler handler
  @db.get_first_value( "select multiply(a) from foo" )
  @db.get_first_value( "select multiply(a) from foo" )
  assert_equal 2, initialized
end

#test_create_aggregate_handlerObject



298
299
300
301
302
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 298

def test_create_aggregate_handler
  @db.create_aggregate_handler AggregateHandler
  value = @db.get_first_value( "select multiply(a) from foo" )
  assert_equal 6, value
end

#test_create_aggregate_handler_call_with_wrong_arityObject



253
254
255
256
257
258
259
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 253

def test_create_aggregate_handler_call_with_wrong_arity
  @db.create_aggregate_handler AggregateHandler

  assert_raise(SQLite3::SQLException) do
   @db.get_first_value( "select multiply(a,c) from foo" )
 end
end

#test_create_aggregate_handler_with_exception_newObject



291
292
293
294
295
296
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 291

def test_create_aggregate_handler_with_exception_new
  @db.create_aggregate_handler RaiseExceptionNewAggregateHandler
  assert_raise CustomException do
    @db.get_first_value( "select raiseexception(a) from foo")
  end
end

#test_create_aggregate_handler_with_exception_stepObject



273
274
275
276
277
278
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 273

def test_create_aggregate_handler_with_exception_step
  @db.create_aggregate_handler RaiseExceptionStepAggregateHandler
  assert_raise CustomException do
    @db.get_first_value( "select raiseexception(a) from foo")
  end
end

#test_create_aggregate_overwrite_functionObject



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
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 114

def test_create_aggregate_overwrite_function
  @db.create_aggregate( "accumulate", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 0
      ctx[:sum] += a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  value = @db.get_first_value( "select accumulate(c) from foo")
  assert_equal 33, value

  GC.start

  @db.create_aggregate( "accumulate", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 1
      ctx[:sum] *= a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  value = @db.get_first_value( "select accumulate(c) from foo")
  assert_equal 1320, value
end

#test_create_aggregate_overwrite_function_with_different_arityObject



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
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 142

def test_create_aggregate_overwrite_function_with_different_arity
  @db.create_aggregate( "accumulate", -1 ) do
    step do |ctx,*args|
      ctx[:sum] ||= 0
      args.each { |a| ctx[:sum] += a.to_i }
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  @db.create_aggregate( "accumulate", 2 ) do
    step do |ctx,a,b|
      ctx[:sum] ||= 1
      ctx[:sum] *= (a.to_i + b.to_i)
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  GC.start

  values = @db.get_first_row( "select accumulate(c), accumulate(a,c) from foo")
  assert_equal 33, values[0]
  assert_equal 2145, values[1]
end

#test_create_aggregate_with_blockObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 36

def test_create_aggregate_with_block
  @db.create_aggregate( "accumulate", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 0
      ctx[:sum] += a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  value = @db.get_first_value( "select accumulate(a) from foo" )
  assert_equal 6, value
end

#test_create_aggregate_with_exception_in_finalizeObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 194

def test_create_aggregate_with_exception_in_finalize
  @db.create_aggregate( "raiseexception", 1 ) do
    step do |ctx,a|
      raise CustomException.new( "bogus aggregate handler" )
    end

    finalize do |ctx|
      raise CustomException.new( "bogus aggregate handler" )
    end
  end

  assert_raise CustomException do
    @db.get_first_value( "select raiseexception(a) from foo")
  end
end

#test_create_aggregate_with_exception_in_stepObject



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 180

def test_create_aggregate_with_exception_in_step
  @db.create_aggregate( "raiseexception", 1 ) do
    step do |ctx,a|
      raise CustomException.new( "bogus aggregate handler" )
    end

    finalize { |ctx| ctx.result = 42 }
  end

  assert_raise CustomException do
    @db.get_first_value( "select raiseexception(a) from foo")
  end
end

#test_create_aggregate_with_group_byObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 50

def test_create_aggregate_with_group_by
  @db.create_aggregate( "accumulate", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 0
      ctx[:sum] += a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  values = @db.execute( "select b, accumulate(c) from foo group by b order by b" )
  assert_equal "bar", values[0][0]
  assert_equal 23, values[0][1]
  assert_equal "foo", values[1][0]
  assert_equal 10, values[1][1]
end

#test_create_aggregate_with_invalid_arityObject



168
169
170
171
172
173
174
175
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 168

def test_create_aggregate_with_invalid_arity
  assert_raise ArgumentError do
    @db.create_aggregate( "accumulate", 1000 ) do
      step {|ctx,*args| }
      finalize { |ctx| }
    end
  end
end

#test_create_aggregate_with_no_dataObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 210

def test_create_aggregate_with_no_data
  @db.create_aggregate( "accumulate", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 0
      ctx[:sum] += a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] || 0 }
  end

  value = @db.get_first_value(
    "select accumulate(a) from foo where a = 100" )
  assert_equal 0, value
end

#test_create_aggregate_with_the_same_function_twice_in_a_queryObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 67

def test_create_aggregate_with_the_same_function_twice_in_a_query
  @db.create_aggregate( "accumulate", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 0
      ctx[:sum] += a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  values = @db.get_first_row( "select accumulate(a), accumulate(c) from foo" )
  assert_equal 6, values[0]
  assert_equal 33, values[1]
end

#test_create_aggregate_with_two_different_functionsObject



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
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 82

def test_create_aggregate_with_two_different_functions
  @db.create_aggregate( "accumulate", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 0
      ctx[:sum] += a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  @db.create_aggregate( "multiply", 1 ) do
    step do |ctx,a|
      ctx[:sum] ||= 1
      ctx[:sum] *= a.to_i
    end

    finalize { |ctx| ctx.result = ctx[:sum] }
  end

  GC.start
  
  values = @db.get_first_row( "select accumulate(a), multiply(c) from foo" )
  assert_equal 6, values[0]
  assert_equal 1320, values[1]

  value = @db.get_first_value( "select accumulate(c) from foo")
  assert_equal 33, value

  value = @db.get_first_value( "select multiply(a) from foo")
  assert_equal 6, value
end

#test_create_aggregate_without_blockObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 18

def test_create_aggregate_without_block
  step = proc do |ctx,a|
    ctx[:sum] ||= 0
    ctx[:sum] += a.to_i
  end

  final = proc { |ctx| ctx.result = ctx[:sum] }

  @db.create_aggregate( "accumulate", 1, step, final )

  value = @db.get_first_value( "select accumulate(a) from foo" )
  assert_equal 6, value

  # calling #get_first_value twice don't add up to the latest result
  value = @db.get_first_value( "select accumulate(a) from foo" )
  assert_equal 6, value
end

#test_define_aggregator_with_two_different_aritiesObject



326
327
328
329
330
331
332
333
334
335
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_aggregate.rb', line 326

def test_define_aggregator_with_two_different_arities
  @db.define_aggregator( "accumulate", AccumulateAggregator.new )
  @db.define_aggregator( "accumulate", AccumulateAggregator2.new )

  GC.start

  values = @db.get_first_row( "select accumulate(c), accumulate(a,c) from foo")
  assert_equal 33, values[0]
  assert_equal 2145, values[1]
end