88
99 def stub_connection
1010 @connection = mock ( )
11+ error_mock = mock ( )
12+ error_mock . stubs ( :count ) . returns ( 0 )
13+
1114 subject . stubs ( :create_connection ) . returns ( @connection )
1215 @connection . stubs ( :State ) . returns ( PuppetX ::Sqlserver ::CONNECTION_CLOSED )
16+ @connection . stubs ( :Errors ) . returns ( error_mock )
1317 subject . stubs ( :win32_exception ) . returns ( Exception )
1418 end
1519
@@ -20,11 +24,12 @@ def stub_connection
2024 @connection . stubs ( :Open ) . with ( 'Provider=SQLNCLI11;Initial Catalog=master;Application Name=Puppet;Data Source=.;DataTypeComptibility=80;User ID=sa;Password=Pupp3t1@' )
2125 end
2226 it 'should not raise an error but populate has_errors with message' do
23- subject . stubs ( :execute ) . raises ( Exception . new ( "SQL Server\n error has happened" ) )
27+ @connection . Errors . stubs ( :count ) . returns ( 1 )
28+ @connection . Errors . stubs ( :Description ) . returns ( "SQL Error in Connection" )
2429 expect {
2530 result = subject . open_and_run_command ( 'whacka whacka whacka' , config )
2631 expect ( result . exitstatus ) . to eq ( 1 )
27- expect ( result . error_message ) . to eq ( 'error has happened ' )
32+ expect ( result . error_message ) . to eq ( 'SQL Error in Connection ' )
2833 } . to_not raise_error ( Exception )
2934
3035 end
@@ -114,20 +119,22 @@ def stub_connection
114119 end
115120 context 'return result with errors' do
116121 it {
117- subject . stubs ( :win32_exception ) . returns ( Exception )
122+ stub_connection
123+ @connection . Errors . stubs ( :count ) . returns ( 1 )
124+ @connection . Errors . stubs ( :Description ) . returns ( "SQL Error in Connection" )
125+ @connection . stubs ( :Execute ) . raises ( Exception )
118126 subject . expects ( :open ) . with ( { :admin_user => 'sa' , :admin_pass => 'Pupp3t1@' , :instance_name => 'MSSQLSERVER' } )
119- subject . expects ( :execute ) . with ( 'SELECT * FROM sys.databases' ) . raises ( Exception . new ( "SQL Server\n invalid syntax provider" ) )
120127 subject . expects ( :close ) . once
121- result =
122- subject . open_and_run_command ( 'SELECT * FROM sys.databases' , config )
128+
129+ result = subject . open_and_run_command ( 'SELECT * FROM sys.databases' , config )
123130 expect ( result . exitstatus ) . to eq ( 1 )
124- expect ( result . error_message ) . to eq ( 'invalid syntax provider ' )
131+ expect ( result . error_message ) . to eq ( 'SQL Error in Connection ' )
125132 }
126133 end
127134 context 'open connection failure' do
128135 it {
129136 stub_connection
130- err_message = "SQL Server \n ConnectionFailed"
137+ err_message = "ConnectionFailed"
131138 @connection . stubs ( :Open ) . raises ( Exception . new ( err_message ) )
132139 expect {
133140 result = subject . open_and_run_command ( 'whacka whacka whacka' , config )
0 commit comments