Permalink
Browse files

Modified all tests to be run against a live database, no more fixtures

  • Loading branch information...
1 parent 07a1e11 commit 79da65d6a67fb1209f0a79d7389743b04594befc @kreynolds committed Nov 2, 2011
Showing with 777 additions and 859 deletions.
  1. +1 −0 .gitignore
  2. +35 −34 Rakefile
  3. +3 −7 lib/cassandra-cql/database.rb
  4. +21 −5 lib/cassandra-cql/result.rb
  5. +5 −9 lib/cassandra-cql/row.rb
  6. +2 −4 lib/cassandra-cql/schema.rb
  7. +4 −17 lib/cassandra-cql/statement.rb
  8. +1 −1 lib/cassandra-cql/version.rb
  9. +27 −38 spec/column_family_spec.rb
  10. +15 −19 spec/{roundtrip_comparator_spec.rb → comparator_spec.rb}
  11. 0 { → spec}/conf/0.8/cassandra.in.sh
  12. 0 { → spec}/conf/0.8/cassandra.yaml
  13. 0 { → spec}/conf/0.8/log4j-server.properties
  14. 0 { → spec}/conf/0.8/schema.txt
  15. +41 −0 spec/conf/1.0/cassandra.in.sh
  16. +416 −0 spec/conf/1.0/cassandra.yaml
  17. +40 −0 spec/conf/1.0/log4j-server.properties
  18. +10 −0 spec/conf/1.0/schema.txt
  19. +0 −33 spec/fixtures/result_for_sparse_columns.yaml
  20. +0 −16 spec/fixtures/result_for_standard_counter.yaml
  21. +0 −28 spec/fixtures/result_for_standard_with_long_validation.yaml
  22. +0 −65 spec/fixtures/result_for_standard_with_validations.yaml
  23. +0 −17 spec/fixtures/result_for_timeuuid.yaml
  24. +0 −2 spec/fixtures/result_for_void_operations.yaml
  25. +0 −26 spec/fixtures/standard_column_family.yaml
  26. +0 −26 spec/fixtures/standard_counter.yaml
  27. +0 −26 spec/fixtures/standard_with_long_validation.yaml
  28. +0 −40 spec/fixtures/standard_with_validations.yaml
  29. +0 −27 spec/fixtures/super_column_family.yaml
  30. +0 −164 spec/fixtures/system_schema.yaml
  31. +57 −79 spec/result_spec.rb
  32. +20 −49 spec/row_spec.rb
  33. +15 −19 spec/{roundtrip_rowkey_spec.rb → rowkey_spec.rb}
  34. +12 −14 spec/schema_spec.rb
  35. +10 −0 spec/spec_helper.rb
  36. +27 −75 spec/statement_spec.rb
  37. +15 −19 spec/{roundtrip_validation_spec.rb → validation_spec.rb}
View
@@ -6,3 +6,4 @@ coverage
.rspec-tm
doc
Gemfile.lock
+tmp
View
@@ -4,6 +4,16 @@ Bundler::GemHelper.install_tasks
require 'rake'
require 'rspec/core'
require 'rspec/core/rake_task'
+
+CassandraBinaries = {
+ '0.8' => 'http://archive.apache.org/dist/cassandra/0.8.4/apache-cassandra-0.8.4-bin.tar.gz',
+ '1.0' => 'http://archive.apache.org/dist/cassandra/1.0.1/apache-cassandra-1.0.1-bin.tar.gz',
+}
+
+CASSANDRA_VERSION = ENV['CASSANDRA_VERSION'] || '1.0'
+CASSANDRA_HOME = File.dirname(__FILE__) + '/tmp'
+CASSANDRA_PIDFILE = ENV['CASSANDRA_PIDFILE'] || "#{CASSANDRA_HOME}/cassandra.pid"
+
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end
@@ -14,15 +24,21 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rcov_opts = "--exclude 'spec/*'"
end
-task :default => :spec
-
-CassandraBinaries = {
- '0.8' => 'http://archive.apache.org/dist/cassandra/0.8.4/apache-cassandra-0.8.4-bin.tar.gz'
-}
+desc "Download Cassandra and run specs against it"
+task :spec_with_server do
+ Rake::Task["cassandra:clean"].invoke
+ Rake::Task["cassandra:start"].invoke
+ error = nil
+ begin
+ Rake::Task["spec"].invoke
+ rescue
+ error = $!
+ end
+ Rake::Task["cassandra:stop"].invoke
+ raise $! if $!
+end
-CASSANDRA_HOME = ENV['CASSANDRA_HOME'] || "#{ENV['HOME']}/cassandra"
-CASSANDRA_VERSION = ENV['CASSANDRA_VERSION'] || '0.8'
-CASSANDRA_PIDFILE = ENV['CASSANDRA_PIDFILE'] || "#{CASSANDRA_HOME}/cassandra.pid"
+task :default => :spec
def setup_cassandra_version(version = CASSANDRA_VERSION)
FileUtils.mkdir_p CASSANDRA_HOME
@@ -31,7 +47,7 @@ def setup_cassandra_version(version = CASSANDRA_VERSION)
unless File.exists?(File.join(destination_directory, 'bin','cassandra'))
download_source = CassandraBinaries[CASSANDRA_VERSION]
- download_destination = File.join("/tmp", File.basename(download_source))
+ download_destination = File.join(CASSANDRA_HOME, File.basename(download_source))
untar_directory = File.join(CASSANDRA_HOME, File.basename(download_source,'-bin.tar.gz'))
puts "downloading cassandra"
@@ -45,9 +61,9 @@ end
def setup_environment
env = ""
if !ENV["CASSANDRA_INCLUDE"]
- env << "CASSANDRA_INCLUDE=#{File.expand_path(Dir.pwd)}/conf/#{CASSANDRA_VERSION}/cassandra.in.sh "
+ env << "CASSANDRA_INCLUDE=#{File.expand_path(Dir.pwd)}/spec/conf/#{CASSANDRA_VERSION}/cassandra.in.sh "
env << "CASSANDRA_HOME=#{CASSANDRA_HOME}/cassandra-#{CASSANDRA_VERSION} "
- env << "CASSANDRA_CONF=#{File.expand_path(Dir.pwd)}/conf/#{CASSANDRA_VERSION}"
+ env << "CASSANDRA_CONF=#{File.expand_path(Dir.pwd)}/spec/conf/#{CASSANDRA_VERSION}"
else
env << "CASSANDRA_INCLUDE=#{ENV['CASSANDRA_INCLUDE']} "
env << "CASSANDRA_HOME=#{ENV['CASSANDRA_HOME']} "
@@ -85,6 +101,8 @@ namespace :cassandra do
Dir.chdir(File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}")) do
sh("env #{env} bin/cassandra #{'-f' unless args.daemonize} -p #{CASSANDRA_PIDFILE}")
end
+ $stdout.puts "Sleeping for 8 seconds to wait for Cassandra to start ..."
+ sleep(8)
end
desc "Stop Cassandra"
@@ -93,6 +111,12 @@ namespace :cassandra do
env = setup_environment
sh("kill $(cat #{CASSANDRA_PIDFILE})")
end
+
+ desc "Delete all data files in #{CASSANDRA_HOME}"
+ task :clean do
+ sh("rm -rf #{File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}", 'data')}")
+ end
+
end
desc "Start Cassandra"
@@ -122,28 +146,5 @@ task :java do
end
end
-namespace :data do
- desc "Reset test data"
- task :reset do
- puts "Resetting test data"
- sh("rm -rf #{File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}", 'data')}")
- end
-
- desc "Load test data structures."
- task :load do
- schema_path = "#{File.expand_path(Dir.pwd)}/conf/#{CASSANDRA_VERSION}/schema.txt"
- puts "Loading test data structures."
- Dir.chdir(File.join(CASSANDRA_HOME, "cassandra-#{CASSANDRA_VERSION}")) do
- begin
- sh("bin/cassandra-cli --host localhost --batch < #{schema_path}")
- rescue
- puts "Schema already loaded."
- end
- end
- end
-end
-
-#task :spec => 'data:load'
-
require 'yard'
YARD::Rake::YardocTask.new
@@ -83,13 +83,9 @@ def keyspaces
@connection.describe_keyspaces.map { |keyspace| Schema.new(keyspace) }
end
- def update_schema!
- if @keyspace.nil?
- @schema = nil
- else
- # TODO: This should be replaced with a CQL call that doesn't exist yet
- @schema = Schema.new(@connection.describe_keyspace(@keyspace))
- end
+ def schema
+ # TODO: This should be replaced with a CQL call that doesn't exist yet
+ Schema.new(@connection.describe_keyspace(@keyspace))
end
end
end
@@ -4,12 +4,28 @@ class InvalidResultType < Exception; end
class InvalidCursor < Exception; end
end
+ class ResultSchema
+ attr_reader :names, :values
+
+ def initialize(schema)
+ # When https://issues.apache.org/jira/browse/CASSANDRA-3436 is resolve, no more need to split/last
+ @names = Hash.new(schema.default_name_type.split(".").last)
+ schema.name_types.each_pair { |key, type|
+ @names[key] = type.split(".").last
+ }
+ @values = Hash.new(schema.default_value_type.split(".").last)
+ schema.value_types.each_pair { |key, type|
+ @values[key] = type.split(".").last
+ }
+ end
+ end
+
class Result
- attr_reader :result, :column_family, :cursor
+ attr_reader :result, :schema, :cursor
- def initialize(result, column_family=nil)
- @result, @column_family = result, column_family
- @column_family = @column_family.dup unless @column_family.nil?
+ def initialize(result)
+ @result = result
+ @schema = ResultSchema.new(result.schema) if rows?
@cursor = 0
end
@@ -40,7 +56,7 @@ def fetch_row
when CassandraCQL::Thrift::CqlResultType::ROWS
return nil if @cursor >= rows
- row = Row.new(@result.rows[@cursor], @column_family)
+ row = Row.new(@result.rows[@cursor], @schema)
@cursor += 1
return row
when CassandraCQL::Thrift::CqlResultType::VOID
@@ -2,8 +2,8 @@ module CassandraCQL
class Row
attr_reader :row
- def initialize(row, column_family)
- @row, @column_family = row, column_family
+ def initialize(row, schema)
+ @row, @schema = row, schema
end
def [](obj)
@@ -15,16 +15,12 @@ def [](obj)
def column_names
@names ||= @row.columns.map do |column|
- if column.name == @column_family.key_alias
- column.name
- else
- ColumnFamily.cast(column.name, @column_family.comparator_type)
- end
+ ColumnFamily.cast(column.name, @schema.names[column.name])
end
end
def column_values
- @values ||= @row.columns.map { |column| ColumnFamily.cast(column.value, @column_family.columns[column.name]) }
+ @values ||= @row.columns.map { |column| ColumnFamily.cast(column.value, @schema.values[column.name]) }
end
def columns
@@ -41,7 +37,7 @@ def to_hash
end
def key
- ColumnFamily.cast(@row.key, @column_family.key_validation_class)
+ ColumnFamily.cast(@row.key, @schema.values[@row.key])
end
end
end
@@ -62,10 +62,8 @@ def columns
def self.cast(value, type)
return nil if value.nil?
- # 3x faster than split
- klass = type[type.rindex('.')+1..-1]
- if CassandraCQL::Types.const_defined?(klass)
- CassandraCQL::Types.const_get(klass).cast(value)
+ if CassandraCQL::Types.const_defined?(type)
+ CassandraCQL::Types.const_get(type).cast(value)
else
CassandraCQL::Types::AbstractType.cast(value)
end
@@ -7,9 +7,7 @@ class UnescapableObject < Exception; end
class Statement
KS_CHANGE_RE = /^use (\w+)/i
- SCHEMA_CHANGE_RE = /\s*(create|drop|alter)\s+(\w+)/i
KS_DROP_RE = /^drop keyspace (\w+)/i
- COLFAM_RE = /\s*select.*from\s+'?(\w+)/i
attr_reader :statement
@@ -23,30 +21,19 @@ def prepare(statement)
end
def execute(bind_vars=[], options={})
- column_family = nil
- if @statement =~ COLFAM_RE
- column_family = @handle.schema.column_families[$1].dup
- end
-
if options[:compression]
- res = Result.new(@handle.execute_cql_query(Utility.compress(self.class.sanitize(@statement, bind_vars)), CassandraCQL::Thrift::Compression::GZIP), column_family)
+ res = Result.new(@handle.execute_cql_query(Utility.compress(self.class.sanitize(@statement, bind_vars)), CassandraCQL::Thrift::Compression::GZIP))
else
- res = Result.new(@handle.execute_cql_query(self.class.sanitize(@statement, bind_vars), CassandraCQL::Thrift::Compression::NONE), column_family)
+ res = Result.new(@handle.execute_cql_query(self.class.sanitize(@statement, bind_vars), CassandraCQL::Thrift::Compression::NONE))
end
# Change our keyspace if required
if @statement =~ KS_CHANGE_RE
@handle.keyspace = $1
+ elsif @statement =~ KS_DROP_RE
+ @handle.keyspace = nil
end
- # If we are dropping a keyspace, we should set it to nil
- @handle.keyspace = nil if @statement =~ KS_DROP_RE
-
- # Update the schema if it has changed
- if @statement =~ KS_CHANGE_RE or @statement =~ SCHEMA_CHANGE_RE or @statement =~ KS_DROP_RE
- @handle.update_schema!
- end
-
# We let ints be fetched for now because they'll probably be deprecated later
if res.void?
nil
@@ -1,3 +1,3 @@
module CassandraCQL
- VERSION = "1.1.1"
+ VERSION = "1.0.1"
end
Oops, something went wrong.

0 comments on commit 79da65d

Please sign in to comment.