Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Beef up tests for get_indexed_slices, test :start_key/:key_start

  • Loading branch information...
commit 6c80a220b65b810109fc0de4006c6fec37ce4c9d 1 parent e5e4016
@mcmire mcmire authored
Showing with 230 additions and 49 deletions.
  1. +5 −1 conf/1.0/schema.txt
  2. +225 −48 test/cassandra_test.rb
View
6 conf/1.0/schema.txt
@@ -15,7 +15,11 @@ create column family UserRelationships with
column_type = 'Super' and
subcomparator = 'TimeUUIDType';
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 StatusRelationships with
comparator = 'UTF8Type' and
View
273 test/cassandra_test.rb
@@ -3,6 +3,10 @@
class CassandraTest < Test::Unit::TestCase
include Cassandra::Constants
+ def assert_has_keys(keys, hash)
+ assert_equal Set.new(keys), Set.new(hash.keys)
+ end
+
def setup
@twitter = Cassandra.new('Twitter', "127.0.0.1:9160", :retries => 2, :connect_timeout => 0.1, :timeout => 5, :exception_classes => [])
@twitter.clear_keyspace!
@@ -720,69 +724,242 @@ def test_each_column_types
if CASSANDRA_VERSION.to_f >= 0.7
def test_creating_and_dropping_new_index
- @twitter.create_index('Twitter', 'Statuses', 'column_name', 'LongType')
- assert_nil @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', 'BytesType')
@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.
- @twitter.create_index('Twitter', 'Statuses', 'column_name', 'LongType')
+ @twitter.create_index('Twitter', 'Statuses', 'column_name', 'BytesType')
@twitter.drop_index('Twitter', 'Statuses', 'column_name')
end
def test_get_indexed_slices
- @twitter.create_index('Twitter', 'Statuses', 'x', 'LongType')
-
- @twitter.insert(:Statuses, 'row1', { 'x' => [0,10].pack("NN") })
-
- (2..10).to_a.each do |i|
- @twitter.insert(:Statuses, 'row' + i.to_s, { 'x' => [0,20].pack("NN"), 'non_indexed' => [i].pack('N*') })
+ @twitter.insert(:Statuses, 'row_a', {
+ 'tags' => 'a',
+ 'y' => 'foo'
+ })
+ @twitter.insert(:Statuses, 'row_b', {
+ '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
- @twitter.insert(:Statuses, 'row11', { 'x' => [0,30].pack("NN") })
-
- expressions = [{:column_name => 'x', :value => [0,20].pack("NN"), :comparison => "=="}]
-
- # verify multiples will be returned
- assert_equal 9, @twitter.get_indexed_slices(:Statuses, expressions).length
-
- # verify that GT and LT queries perform properly
- expressions = [
- {:column_name => 'x', :value => [0,20].pack("NN"), :comparison => "=="},
- {:column_name => 'non_indexed', :value => [5].pack("N*"), :comparison => ">"}
- ]
- assert_equal(5, @twitter.get_indexed_slices(:Statuses, expressions).length)
+ # Test start key
+ rows = @twitter.get_indexed_slices(:Statuses, [
+ {:column_name => 'tags',
+ :value => 'c',
+ :comparison => "=="},
+ ], :start_key => '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_start option
+ rows = @twitter.get_indexed_slices(:Statuses, [
+ {:column_name => 'tags',
+ :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
- def test_old_get_indexed_slices
- @twitter.create_index('Twitter', 'Statuses', 'x', 'LongType')
-
- @twitter.insert(:Statuses, 'row1', { 'x' => [0,10].pack("NN") })
-
- (2..10).to_a.each do |i|
- @twitter.insert(:Statuses, 'row' + i.to_s, { 'x' => [0,20].pack("NN"), 'non_indexed' => [i].pack('N*') })
+ def test_get_indexed_slices_with_IndexClause_objects
+ @twitter.insert(:Statuses, 'row_a', {
+ 'tags' => 'a',
+ 'y' => 'foo'
+ })
+ @twitter.insert(:Statuses, 'row_b', {
+ '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
- @twitter.insert(:Statuses, 'row11', { 'x' => [0,30].pack("NN") })
-
- idx_expr = @twitter.create_idx_expr('x', [0,20].pack("NN"), "==")
-
- # verify count is observed
- idx_clause = @twitter.create_idx_clause([idx_expr], "", 1)
- assert_equal 1, @twitter.get_indexed_slices(:Statuses, idx_clause).length
-
- # verify multiples will be returned
- idx_clause = @twitter.create_idx_clause([idx_expr])
- assert_equal 9, @twitter.get_indexed_slices(:Statuses, idx_clause).length
-
- # verify that GT and LT queries perform properly
- idx_expr = [
- @twitter.create_idx_expr('x', [0,20].pack("NN"), "=="),
- @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)
+ # Test start key
+ index_clause = @twitter.create_index_clause([
+ @twitter.create_index_expression('tags', 'c', '==')
+ ], 'row_c_2')
+ rows = @twitter.get_indexed_slices(:Statuses, index_clause)
+ 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
+ index_clause = @twitter.create_index_clause([
+ @twitter.create_index_expression('tags', 'c', '==')
+ ], "", 2)
+ rows = @twitter.get_indexed_slices(:Statuses, index_clause)
+ assert_equal 2, rows.length
+ # <- can't test which keys are present since it's going to be random
end
def test_column_family_mutation
Please sign in to comment.
Something went wrong with that request. Please try again.