99 def stub_connection
1010 @connection = mock ( )
1111 subject . stubs ( :create_connection ) . returns ( @connection )
12+ @connection . stubs ( :State ) . returns ( PuppetX ::Sqlserver ::CONNECTION_CLOSED )
1213 subject . stubs ( :win32_exception ) . returns ( Exception )
1314 end
1415
15- def stub_no_errors
16- subject . stubs ( :has_errors ) . returns ( false )
17- subject . stubs ( :error_message ) . returns ( nil )
18- end
19-
2016 describe 'open_and_run_command' do
21- context 'command' do
17+ context 'command execution ' do
2218 before :each do
2319 stub_connection
24- @connection . stubs ( :State ) . returns ( 0 )
2520 @connection . stubs ( :Open ) . with ( 'Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost' )
2621 end
2722 it 'should not raise an error but populate has_errors with message' do
28- subject . stubs ( :win32_exception ) . returns ( Exception )
2923 subject . stubs ( :execute ) . raises ( Exception . new ( "SQL Server\n error has happened" ) )
3024 expect {
3125 result = subject . open_and_run_command ( 'whacka whacka whacka' , config )
@@ -44,36 +38,32 @@ def stub_no_errors
4438 context 'closed connection' do
4539 before :each do
4640 stub_connection
47- stub_no_errors
48- @connection . stubs ( :State ) . returns ( 0 )
41+ @connection . stubs ( :Execute )
4942 end
5043 it 'should not add MSSQLSERVER to connection string' do
51- @connection . stubs ( :Open ) . with ( 'Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost' )
44+ @connection . expects ( :Open ) . with ( 'Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost' )
5245 subject . open_and_run_command ( 'query' , config )
5346 end
5447 it 'should add a non default instance to connection string' do
55- @connection . stubs ( :Open ) . with ( 'Provider=SQLOLEDB.1;User ID=superuser;Password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING' )
48+ @connection . expects ( :Open ) . with ( 'Provider=SQLOLEDB.1;User ID=superuser;Password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING' )
5649 subject . open_and_run_command ( 'query' , { :admin_user => 'superuser' , :admin_pass => 'puppetTested' , :instance_name => 'LOGGING' } )
5750 end
5851 end
5952 context 'open connection' do
6053 it 'should not reopen an existing connection' do
6154 stub_connection
6255 @connection . expects ( :open ) . never
63- @connection . stubs ( :State ) . returns ( 1 )
56+ @connection . stubs ( :State ) . returns ( 1 ) # any value other than CONNECTION_CLOSED
6457 @connection . expects ( :Execute ) . with ( 'query' , nil , nil )
6558 subject . open_and_run_command ( 'query' , config )
6659 end
6760 end
6861 context 'return result with errors' do
6962 it {
63+ subject . stubs ( :win32_exception ) . returns ( Exception )
7064 subject . expects ( :open ) . with ( { :admin_user => 'sa' , :admin_pass => 'Pupp3t1@' , :instance_name => 'MSSQLSERVER' } )
71- subject . expects ( :command ) . with ( 'SELECT * FROM sys.databases' )
65+ subject . expects ( :execute ) . with ( 'SELECT * FROM sys.databases' ) . raises ( Exception . new ( "SQL Server \n invalid syntax provider" ) )
7266 subject . expects ( :close ) . once
73- subject . stubs ( :has_errors ) . returns ( :true )
74- subject . stubs ( :error_message ) . returns (
75- 'SQL Server
76- invalid syntax provider' )
7767 result =
7868 subject . open_and_run_command ( 'SELECT * FROM sys.databases' , config )
7969 expect ( result . exitstatus ) . to eq ( 1 )
@@ -85,8 +75,6 @@ def stub_no_errors
8575 stub_connection
8676 err_message = "SQL Server\n ConnectionFailed"
8777 @connection . stubs ( :Open ) . raises ( Exception . new ( err_message ) )
88- subject . stubs ( :has_errors ) . returns ( true )
89- subject . stubs ( :error_message ) . returns ( err_message )
9078 expect {
9179 result = subject . open_and_run_command ( 'whacka whacka whacka' , config )
9280 expect ( result . exitstatus ) . to eq ( 1 )
0 commit comments