Permalink
Browse files

spec cleanup, put in some more test cases to clean up coverage

20080519000148-2f820-0724fa44b42450a3f3e5603d976d6d1555aa8167.gz
  • Loading branch information...
copiousfreetime authored and git-darcs-import committed May 19, 2008
1 parent 42621cf commit f6110570edb37a393acd1b6b23d5b3a0c3a226f6
@@ -243,7 +243,6 @@ def trace_tap=( tap_obj )
raise Amalgalite::Error, "#{tap_obj.class.name} cannot be used to tap. It has no 'write' or 'trace' method. Look at wrapping it in a Tap instances."
end
puts "tracing with #{@trace_tap.class.name}"
# and do the low level registration
#
@api.register_trace_tap( @trace_tap )
@@ -64,7 +64,7 @@ def to_a
# return all the avlues as a hash
#
def to_h
{ 'name' => @name,
{ 'name' => @name, 'n' => @n,
'sum' => @sum, 'sumsq' => @sumsq, 'mean' => mean,
'stddev' => stddev, 'min' => @min, 'max' => @max }
end
@@ -15,7 +15,7 @@ class Statement
attr_reader :sql
attr_reader :api
#
##
# Initialize a new statement on the database.
#
def initialize( db, sql )
@@ -25,7 +25,7 @@ def initialize( db, sql )
@stmt_api = @db.api.send( prepare_method, sql )
end
#
##
# reset the Statement back to it state right after the constructor returned,
# except if any variables have been bound to parameters, those are still
# bound.
@@ -36,7 +36,6 @@ def reset!
@param_positions = {}
end
##
# reset the Statement back to it state right after the constructor returned,
# AND clear all parameter bindings.
@@ -63,7 +62,6 @@ def execute( *params )
end
end
##
# Bind parameters to the sql statement.
#
@@ -91,7 +89,10 @@ def execute( *params )
# If bind is not passed any parameters, or nil, then nothing happens.
#
def bind( *params )
return nil if params.nil? or params.empty?
if params.nil? or params.empty? then
check_parameter_count!( 0 )
return nil
end
if params.first.instance_of?( Hash ) then
bind_named_parameters( params.first )
@@ -110,7 +111,7 @@ def bind_named_parameters( params )
if position > 0 then
bind_parameter_to( position, value )
else
raise "Unable to find parameter '#{param}' in SQL statement [#{sql}]"
raise Amalgalite::Error, "Unable to find parameter '#{param}' in SQL statement [#{sql}]"
end
end
end
@@ -42,11 +42,13 @@ def output_profile_event( msg, time )
end
def dump_profile
io.puts profile_tap.to_s
samplers.each do |s|
io.puts s.to_s
end
end
def samplers
@profile_tap.samplers
profile_tap.samplers
end
end
@@ -1,5 +1,7 @@
require File.expand_path(File.join(File.dirname(__FILE__),%w[ .. spec_helper.rb ]))
require 'amalgalite/sqlite3/result_code'
describe Amalgalite::SQLite3::Constants do
it "has Open constants" do
@@ -14,4 +16,10 @@
Amalgalite::SQLite3::Constants::ResultCode::OK.should == 0
end
it "can return the constant from a number" do
c = Amalgalite::SQLite3::Constants::ResultCode.from_int( 21 )
c.should == "MISUSE"
end
end
View
@@ -42,6 +42,13 @@
end
end
it "raises an error if there are not enough parameters are passed in a statement" do
@iso_db.prepare("SELECT * FROM country WHERE two_letter = :two") do |stmt|
lambda{ stmt.execute }.should raise_error( Amalgalite::Error )
end
end
it "can run a query with a named parameter" do
@iso_db.prepare("SELECT * FROM country WHERE two_letter = :two") do |stmt|
all_rows = stmt.execute( ":two" => "JP" )
@@ -50,6 +57,19 @@
end
end
it "it can execute a query with a named parameter and yield the rows" do
@iso_db.prepare("SELECT * FROM country WHERE id = @id ORDER BY name") do |stmt|
rows = []
stmt.execute( "@id" => 891 ) do |row|
rows << row
end
rows.size.should == 2
rows.last['name'].should == "Yugoslavia"
rows.first['two_letter'].should == "CS"
end
end
it "binds a integer variable correctly" do
@iso_db.prepare("SELECT * FROM country WHERE id = ? ORDER BY name ") do |stmt|
all_rows = stmt.execute( 891 )
@@ -59,6 +79,18 @@
end
end
it "raises and error if an invaliding binding is attempted" do
@iso_db.prepare("SELECT * FROM country WHERE id = :somevar ORDER BY name ") do |stmt|
lambda{ stmt.execute( "blah" => 42 ) }.should raise_error(Amalgalite::Error)
end
end
it "can reset the statement to the state it was before executing" do
stmt = @iso_db.prepare("SELECT * FROM country WHERE id = :somevar ORDER BY name ")
stmt.reset_and_clear_bindings!
stmt.close
end
it "can execute a single sql command and say if there is remaining sql to execute" do
db = Amalgalite::Database.new( SpecInfo.test_db )
View
@@ -0,0 +1,51 @@
require 'rubygems'
require 'spec'
$: << File.expand_path(File.join(File.dirname(__FILE__),"..","lib"))
require 'amalgalite'
require 'amalgalite/trace_tap'
require 'amalgalite/profile_tap'
require 'amalgalite/taps/console'
require 'stringio'
describe Amalgalite::TraceTap do
it "wraps up an object and delegates the 'trace' method to a method on that object" do
s = StringIO.new
tt = ::Amalgalite::TraceTap.new( s, 'puts' )
tt.trace('test trace')
s.string.should == "test trace\n"
end
it "raises an error if an the wrapped object does not respond to the indicated method" do
lambda{ ::Amalgalite::TraceTap.new( Object.new ) }.should raise_error( Amalgalite::Error )
end
end
describe Amalgalite::ProfileTap do
it "raises an error if an the wrapped object does not respond to the indicated method" do
lambda{ ::Amalgalite::ProfileTap.new( Object.new ) }.should raise_error( Amalgalite::Error )
end
end
describe Amalgalite::Taps::StringIO do
it "dumps profile information" do
s = ::Amalgalite::Taps::StringIO.new
s.profile( 'test', 42 )
s.dump_profile
s.string.should == "42 : test\ntest[test] => sum: 42, sumsq: 1764, n: 1, mean: 42.000000, stddev: 0.000000, min: 42, max: 42\n"
end
end
describe Amalgalite::ProfileSampler do
it "aggregates samples" do
s = Amalgalite::ProfileSampler.new( 'test1' )
s.sample( 42 )
s.sample( 84 )
s.sample( 21 )
h = s.to_h
h['min'].should == 21
h['max'].should == 84
h['mean'].should == 49
h['n'].should == 3
end
end

0 comments on commit f611057

Please sign in to comment.