Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions test/test_integration_aggregate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,127 @@ def test_create_aggregate_with_block
assert_equal 6, value
end

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

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

# This is likely to crash, as ruby-sqlite overwrites its only reference
# to the "accumulate" handler with the "multiply" handler, although
# SQLite still holds a pointer which it follows on next select.
#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

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

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 39, values[1]
end

class CustomException < Exception
end

def test_create_aggregate_with_exception
@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

def test_create_aggregate_with_no_data
@db.create_aggregate( "accumulate", 1 ) do
step do |ctx,a|
Expand Down Expand Up @@ -90,6 +211,33 @@ def test_aggregate_initialized_twice
assert_equal 2, initialized
end

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

class RaiseExceptionAggregateHandler
class << self
def arity; 1; end
def text_rep; SQLite3::Constants::TextRep::ANY; end
def name; "raiseexception"; end
end
def step(ctx, a)
raise CustomException.new( "bogus aggregate handler" )
end
def finalize(ctx); ctx.result = nil; end
end

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

def test_create_aggregate_handler
@db.create_aggregate_handler AggregateHandler
value = @db.get_first_value( "select multiply(a) from foo" )
Expand Down