spec/lib/qreport/connection_spec.rb in qreport-0.0.10 vs spec/lib/qreport/connection_spec.rb in qreport-0.1.0

- old
+ new

@@ -1,32 +1,30 @@ require 'spec_helper' require 'qreport/connection' describe Qreport::Connection do - QREPORT_TEST_CONN = [ nil ] - def conn - QREPORT_TEST_CONN[0] ||= Qreport::Connection.new - end + let(:conn) { Qreport::Connection.new } + after(:each) { conn._close } it "can to connect to a test database." do conn.conn.class.should == PG::Connection end it "can manage transaction state." do - conn.should_receive(:_transaction_begin).once - conn.should_receive(:_transaction_commit).once + conn.should_receive(:_transaction_begin).exactly(1).times + conn.should_receive(:_transaction_commit).exactly(1).times conn.should_receive(:_transaction_abort).exactly(0).times conn.in_transaction?.should == false conn.transaction do conn.in_transaction?.should == true end conn.in_transaction?.should == false end it "can manage nested transactions." do - conn.should_receive(:_transaction_begin).once - conn.should_receive(:_transaction_commit).once + conn.should_receive(:_transaction_begin).exactly(1).times + conn.should_receive(:_transaction_commit).exactly(1).times conn.should_receive(:_transaction_abort).exactly(0).times conn.in_transaction?.should == false conn.transaction do conn.in_transaction?.should == true conn.transaction do @@ -36,21 +34,34 @@ end conn.in_transaction?.should == false end it "can manage transaction state during raised exceptions" do - conn.should_receive(:_transaction_begin).once + conn.should_receive(:_transaction_begin).exactly(1).times conn.should_receive(:_transaction_commit).exactly(0).times - conn.should_receive(:_transaction_abort).once + conn.should_receive(:_transaction_abort).exactly(1).times lambda do conn.transaction do raise Qreport::Error, "#{__LINE__}" end end.should raise_error(Qreport::Error) conn.in_transaction?.should == false end + + it "will error upon #close during transaction" do + conn.should_receive(:_transaction_begin).exactly(1).times + conn.should_receive(:_transaction_commit).exactly(0).times + conn.should_receive(:_transaction_abort).exactly(1).times + lambda do + conn.transaction do + conn.close + end + end.should raise_error(Qreport::Error) + conn.in_transaction?.should == false + end + it "can dup to create another connection." do conn1 = Qreport::Connection.new conn1.fd.should == nil conn1.conn conn1.fd.class.should == Fixnum @@ -128,9 +139,17 @@ [ :a_symbol!, "'a_symbol!'", :a_symbol!.to_s ], [ Time.parse('2011-04-27T13:23:00.000000Z'), "'2011-04-27T13:23:00.000000Z'::timestamp", Time.parse('2011-04-27T13:23:00.000000') ], [ Time.parse('2011-04-27 13:23:00 -0500'), "'2011-04-27T13:23:00.000000-05:00'::timestamp", Time.parse('2011-04-27 13:23:00 -0500') ], [ :IGNORE, "'13:23'::time", '13:23:00' ], [ [ 1, "2", :three ], "'[1,\"2\",\"three\"]'", :IGNORE ], +=begin +DOES NOT WORK YET + [ [ 1, 2, 3 ], 'ARRAY[1,2,3]', ], + [ [ 1, 2, nil, 3 ], 'ARRAY[1,2,NULL,3]', ], + [ [ 1, 2.2, 3 ], 'ARRAY[1,2.2,3]', [ 1.0, 2.2, 3.0 ] ], + [ [ 1, nil, 2.2, 3 ], 'ARRAY[1,NULL,2.2,3]', [ 1.0, nil, 2.2, 3.0 ] ], + [ :IGNORE, 'ARRAY[1,NULL,2.2,3]', [ 1.0, nil, 2.2, 3.0 ] ], +=end [ { :a => 1, "b" => 2 }, "'{\"a\":1,\"b\":2}'", :IGNORE ], ].each do | value, sql, return_value, sql_expr, sql_value | if value != :IGNORE it "can handle encoding #{value.class.name} value #{value.inspect} as #{sql.inspect}." do conn.escape_value(value).should == sql