Skip to content

Commit

Permalink
test_vtable: use assert_operator/assert_equal instead of simple asser…
Browse files Browse the repository at this point in the history
…t for better error messages
  • Loading branch information
lionelperrin committed Apr 4, 2016
1 parent 2bea180 commit 65717fa
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions test/test_vtable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_working

#execute an sql statement
nb_row = @db.execute("select x, sum(y), avg(y), avg(y*y), min(y), max(y), count(y) from TestVTable group by x").each.count
assert( nb_row > 0 )
assert_operator nb_row, :>, 0
end

def test_vtable
Expand All @@ -77,58 +77,58 @@ def test_vtable
[3, 6, 9]
])
nb_row = @db.execute('select count(*) from TestVTable2').each.first[0]
assert( nb_row == 3 )
assert_equal( 3, nb_row )
sum_a, sum_b, sum_c = *@db.execute('select sum(a), sum(b), sum(c) from TestVTable2').each.first
assert( sum_a = 6 )
assert( sum_b == 12 )
assert( sum_c == 18 )
assert_equal( 6, sum_a )
assert_equal( 12, sum_b )
assert_equal( 18, sum_c )
end

def test_multiple_vtable
SQLite3.vtable(@db, 'TestVTable3', 'col1', [['a'], ['b']])
SQLite3.vtable(@db, 'TestVTable4', 'col2', [['c'], ['d']])
rows = @db.execute('select col1, col2 from TestVTable3, TestVTable4').each.to_a
assert( rows.include?(['a', 'c']) )
assert( rows.include?(['a', 'd']) )
assert( rows.include?(['b', 'c']) )
assert( rows.include?(['b', 'd']) )
assert_includes rows, ['a', 'c']
assert_includes rows, ['a', 'd']
assert_includes rows, ['b', 'c']
assert_includes rows, ['b', 'd']
end

def test_best_filter
test = self
SQLite3.vtable(@db, 'TestVTable5', 'col1, col2', [['a', 1], ['b', 2]]).tap do |vtable|
vtable.send(:define_method, :best_index) do |constraint, order_by|
# check constraint
test.assert( constraint.include?([0, :<=]) ) # col1 <= 'c'
test.assert( constraint.include?([0, :>]) ) # col1 > 'a'
test.assert( constraint.include?([1, :<]) ) # col2 < 3
test.assert_includes constraint, [0, :<=] # col1 <= 'c'
test.assert_includes constraint, [0, :>] # col1 > 'a'
test.assert_includes constraint, [1, :<] # col2 < 3
@constraint = constraint

# check order by
test.assert( order_by == [
test.assert_equal( [
[1, 1], # col2
[0, -1], # col1 desc
] )
], order_by )

{ idxNum: 45 }
end
vtable.send(:alias_method, :orig_filter, :filter)
vtable.send(:define_method, :filter) do |idxNum, args|
# idxNum should be the one returned by best_index
test.assert( idxNum == 45 )
test.assert_equal( 45, idxNum )

# args should be consistent with the constraint given to best_index
test.assert( args.size == @constraint.size )
test.assert_equal( @constraint.size, args.size )
filters = @constraint.zip(args)
test.assert( filters.include?([[0, :<=], 'c']) ) # col1 <= 'c'
test.assert( filters.include?([[0, :>], 'a']) ) # col1 > 'a'
test.assert( filters.include?([[1, :<], 3]) ) # col2 < 3
test.assert_includes filters, [[0, :<=], 'c'] # col1 <= 'c'
test.assert_includes filters, [[0, :>], 'a'] # col1 > 'a'
test.assert_includes filters, [[1, :<], 3] # col2 < 3

orig_filter(idxNum, args)
end
end
rows = @db.execute('select col1 from TestVTable5 where col1 <= \'c\' and col1 > \'a\' and col2 < 3 order by col2, col1 desc').each.to_a
assert( rows == [['b']] )
assert_equal( [['b']], rows )
end

end if defined?(SQLite3::Module)
Expand Down

0 comments on commit 65717fa

Please sign in to comment.