Skip to content

Commit

Permalink
Beef up tests for get_indexed_slices, test :start_key/:key_start
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Aug 6, 2012
1 parent e5e4016 commit 6c80a22
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 49 deletions.
6 changes: 5 additions & 1 deletion conf/1.0/schema.txt
Expand Up @@ -15,7 +15,11 @@ create column family UserRelationships with
column_type = 'Super' and column_type = 'Super' and
subcomparator = 'TimeUUIDType'; subcomparator = 'TimeUUIDType';
create column family Usernames with comparator = 'UTF8Type'; create column family Usernames with comparator = 'UTF8Type';
create column family Statuses with comparator = 'UTF8Type'; create column family Statuses
with comparator = 'UTF8Type'
and column_metadata = [
{column_name: 'tags', validation_class: 'BytesType', index_type: 'KEYS'}
];
create column family StatusAudits with comparator = 'UTF8Type'; create column family StatusAudits with comparator = 'UTF8Type';
create column family StatusRelationships with create column family StatusRelationships with
comparator = 'UTF8Type' and comparator = 'UTF8Type' and
Expand Down
273 changes: 225 additions & 48 deletions test/cassandra_test.rb
Expand Up @@ -3,6 +3,10 @@
class CassandraTest < Test::Unit::TestCase class CassandraTest < Test::Unit::TestCase
include Cassandra::Constants include Cassandra::Constants


def assert_has_keys(keys, hash)
assert_equal Set.new(keys), Set.new(hash.keys)
end

def setup def setup
@twitter = Cassandra.new('Twitter', "127.0.0.1:9160", :retries => 2, :connect_timeout => 0.1, :timeout => 5, :exception_classes => []) @twitter = Cassandra.new('Twitter', "127.0.0.1:9160", :retries => 2, :connect_timeout => 0.1, :timeout => 5, :exception_classes => [])
@twitter.clear_keyspace! @twitter.clear_keyspace!
Expand Down Expand Up @@ -720,69 +724,242 @@ def test_each_column_types


if CASSANDRA_VERSION.to_f >= 0.7 if CASSANDRA_VERSION.to_f >= 0.7
def test_creating_and_dropping_new_index def test_creating_and_dropping_new_index
@twitter.create_index('Twitter', 'Statuses', 'column_name', 'LongType') @twitter.create_index('Twitter', 'Statuses', 'column_name', 'BytesType')
assert_nil @twitter.create_index('Twitter', 'Statuses', 'column_name', 'LongType') assert_nil @twitter.create_index('Twitter', 'Statuses', 'column_name', 'BytesType')


@twitter.drop_index('Twitter', 'Statuses', 'column_name') @twitter.drop_index('Twitter', 'Statuses', 'column_name')
assert_nil @twitter.drop_index('Twitter', 'Statuses', 'column_name') assert_nil @twitter.drop_index('Twitter', 'Statuses', 'column_name')


# Recreating and redropping the same index should not error either. # Recreating and redropping the same index should not error either.
@twitter.create_index('Twitter', 'Statuses', 'column_name', 'LongType') @twitter.create_index('Twitter', 'Statuses', 'column_name', 'BytesType')
@twitter.drop_index('Twitter', 'Statuses', 'column_name') @twitter.drop_index('Twitter', 'Statuses', 'column_name')
end end


def test_get_indexed_slices def test_get_indexed_slices
@twitter.create_index('Twitter', 'Statuses', 'x', 'LongType') @twitter.insert(:Statuses, 'row_a', {

'tags' => 'a',
@twitter.insert(:Statuses, 'row1', { 'x' => [0,10].pack("NN") }) 'y' => 'foo'

})
(2..10).to_a.each do |i| @twitter.insert(:Statuses, 'row_b', {
@twitter.insert(:Statuses, 'row' + i.to_s, { 'x' => [0,20].pack("NN"), 'non_indexed' => [i].pack('N*') }) 'tags' => 'b',
'y' => 'foo'
})
[1,2].each do |i|
@twitter.insert(:Statuses, "row_c_#{i}", {
'tags' => 'c',
'y' => 'a'
})
end
[3,4].each do |i|
@twitter.insert(:Statuses, "row_c_#{i}", {
'tags' => 'c',
'y' => 'b'
})
end
@twitter.insert(:Statuses, 'row_d', {
'tags' => 'd',
'y' => 'foo'
})
@twitter.insert(:Statuses, 'row_e', {
'tags' => 'e',
'y' => 'bar'
})

# Test == operator (single clause)
rows = @twitter.get_indexed_slices(:Statuses, [
{:column_name => 'tags',
:value => 'c',
:comparison => "=="}
])
assert_has_keys %w[row_c_1 row_c_2 row_c_3 row_c_4], rows

# Test other operators
# Currently (as of Cassandra 1.1) you can't use anything but == as the
# primary query -- you must match on == first, and subsequent clauses are
# then applied as filters -- so that's what we are doing here.

# Test > operator
rows = @twitter.get_indexed_slices(:Statuses, [
{:column_name => 'tags',
:value => 'c',
:comparison => "=="},
{:column_name => 'y',
:value => 'a',
:comparison => ">"}
])
assert_has_keys %w[row_c_3 row_c_4], rows

# Test >= operator
rows = @twitter.get_indexed_slices(:Statuses, [
{:column_name => 'tags',
:value => 'c',
:comparison => "=="},
{:column_name => 'y',
:value => 'a',
:comparison => ">="}
])
assert_has_keys %w[row_c_1 row_c_2 row_c_3 row_c_4], rows

# Test < operator
rows = @twitter.get_indexed_slices(:Statuses, [
{:column_name => 'tags',
:value => 'c',
:comparison => "=="},
{:column_name => 'y',
:value => 'b',
:comparison => "<"}
])
assert_has_keys %w[row_c_1 row_c_2], rows

# Test <= operator
rows = @twitter.get_indexed_slices(:Statuses, [
{:column_name => 'tags',
:value => 'c',
:comparison => "=="},
{:column_name => 'y',
:value => 'b',
:comparison => "<="}
])
assert_has_keys %w[row_c_1 row_c_2 row_c_3 row_c_4], rows

# Test query on a non-indexed column
unless self.is_a?(CassandraMockTest)
assert_raises(CassandraThrift::InvalidRequestException) do
@twitter.get_indexed_slices(:Statuses, [
{:column_name => 'y',
:value => 'foo',
:comparison => "=="}
])
end
end end


@twitter.insert(:Statuses, 'row11', { 'x' => [0,30].pack("NN") }) # Test start key

rows = @twitter.get_indexed_slices(:Statuses, [
expressions = [{:column_name => 'x', :value => [0,20].pack("NN"), :comparison => "=="}] {:column_name => 'tags',

:value => 'c',
# verify multiples will be returned :comparison => "=="},
assert_equal 9, @twitter.get_indexed_slices(:Statuses, expressions).length ], :start_key => 'row_c_2')

assert_equal 'row_c_2', rows.keys.first
# verify that GT and LT queries perform properly # <- can't test any keys after that since it's going to be random
expressions = [
{:column_name => 'x', :value => [0,20].pack("NN"), :comparison => "=="}, # Test key_start option
{:column_name => 'non_indexed', :value => [5].pack("N*"), :comparison => ">"} rows = @twitter.get_indexed_slices(:Statuses, [
] {:column_name => 'tags',
assert_equal(5, @twitter.get_indexed_slices(:Statuses, expressions).length) :value => 'c',
:comparison => "=="},
], :key_start => 'row_c_2')
assert_equal 'row_c_2', rows.keys.first
# <- can't test any keys after that since it's going to be random

# Test key count
rows = @twitter.get_indexed_slices(:Statuses, [
{:column_name => 'tags',
:value => 'c',
:comparison => "=="}
], :key_count => 2)
assert_equal 2, rows.length
# <- can't test which keys are present since it's going to be random
end end


def test_old_get_indexed_slices def test_get_indexed_slices_with_IndexClause_objects
@twitter.create_index('Twitter', 'Statuses', 'x', 'LongType') @twitter.insert(:Statuses, 'row_a', {

'tags' => 'a',
@twitter.insert(:Statuses, 'row1', { 'x' => [0,10].pack("NN") }) 'y' => 'foo'

})
(2..10).to_a.each do |i| @twitter.insert(:Statuses, 'row_b', {
@twitter.insert(:Statuses, 'row' + i.to_s, { 'x' => [0,20].pack("NN"), 'non_indexed' => [i].pack('N*') }) 'tags' => 'b',
'y' => 'foo'
})
[1,2].each do |i|
@twitter.insert(:Statuses, "row_c_#{i}", {
'tags' => 'c',
'y' => 'a'
})
end
[3,4].each do |i|
@twitter.insert(:Statuses, "row_c_#{i}", {
'tags' => 'c',
'y' => 'b'
})
end
@twitter.insert(:Statuses, 'row_d', {
'tags' => 'd',
'y' => 'foo'
})
@twitter.insert(:Statuses, 'row_e', {
'tags' => 'e',
'y' => 'bar'
})

# Test == operator (single clause)
index_clause = @twitter.create_index_clause([
@twitter.create_index_expression('tags', 'c', '==')
])
rows = @twitter.get_indexed_slices(:Statuses, index_clause)
assert_has_keys %w[row_c_1 row_c_2 row_c_3 row_c_4], rows

# Test other operators
# Currently (as of Cassandra 1.1) you can't use anything but == as the
# primary query -- you must match on == first, and subsequent clauses are
# then applied as filters -- so that's what we are doing here.

# Test > operator
index_clause = @twitter.create_index_clause([
@twitter.create_index_expression('tags', 'c', '=='),
@twitter.create_index_expression('y', 'a', '>'),
])
rows = @twitter.get_indexed_slices(:Statuses, index_clause)
assert_has_keys %w[row_c_3 row_c_4], rows

# Test >= operator
index_clause = @twitter.create_index_clause([
@twitter.create_index_expression('tags', 'c', '=='),
@twitter.create_index_expression('y', 'a', '>=')
])
rows = @twitter.get_indexed_slices(:Statuses, index_clause)
assert_has_keys %w[row_c_1 row_c_2 row_c_3 row_c_4], rows

# Test < operator
index_clause = @twitter.create_index_clause([
@twitter.create_index_expression('tags', 'c', '=='),
@twitter.create_index_expression('y', 'b', '<')
])
rows = @twitter.get_indexed_slices(:Statuses, index_clause)
assert_has_keys %w[row_c_1 row_c_2], rows

# Test <= operator
index_clause = @twitter.create_index_clause([
@twitter.create_index_expression('tags', 'c', '=='),
@twitter.create_index_expression('y', 'b', '<=')
])
rows = @twitter.get_indexed_slices(:Statuses, index_clause)
assert_has_keys %w[row_c_1 row_c_2 row_c_3 row_c_4], rows

# Test query on a non-indexed column
unless self.is_a?(CassandraMockTest)
assert_raises(CassandraThrift::InvalidRequestException) do
index_clause = @twitter.create_index_clause([
@twitter.create_index_expression('y', 'foo', '==')
])
@twitter.get_indexed_slices(:Statuses, index_clause)
end
end end


@twitter.insert(:Statuses, 'row11', { 'x' => [0,30].pack("NN") }) # Test start key

index_clause = @twitter.create_index_clause([
idx_expr = @twitter.create_idx_expr('x', [0,20].pack("NN"), "==") @twitter.create_index_expression('tags', 'c', '==')

], 'row_c_2')
# verify count is observed rows = @twitter.get_indexed_slices(:Statuses, index_clause)
idx_clause = @twitter.create_idx_clause([idx_expr], "", 1) assert_equal 'row_c_2', rows.keys.first
assert_equal 1, @twitter.get_indexed_slices(:Statuses, idx_clause).length # <- can't test any keys after that since it's going to be random


# verify multiples will be returned # Test key count
idx_clause = @twitter.create_idx_clause([idx_expr]) index_clause = @twitter.create_index_clause([
assert_equal 9, @twitter.get_indexed_slices(:Statuses, idx_clause).length @twitter.create_index_expression('tags', 'c', '==')

], "", 2)
# verify that GT and LT queries perform properly rows = @twitter.get_indexed_slices(:Statuses, index_clause)
idx_expr = [ assert_equal 2, rows.length
@twitter.create_idx_expr('x', [0,20].pack("NN"), "=="), # <- can't test which keys are present since it's going to be random
@twitter.create_idx_expr('non_indexed', [5].pack("N*"), ">")
]
idx_clause = @twitter.create_idx_clause(idx_expr)
assert_equal(5, @twitter.get_indexed_slices(:Statuses, idx_clause).length)
end end


def test_column_family_mutation def test_column_family_mutation
Expand Down

0 comments on commit 6c80a22

Please sign in to comment.