9
9
def stub_connection
10
10
@connection = mock ( )
11
11
subject . stubs ( :create_connection ) . returns ( @connection )
12
+ @connection . stubs ( :State ) . returns ( PuppetX ::Sqlserver ::CONNECTION_CLOSED )
12
13
subject . stubs ( :win32_exception ) . returns ( Exception )
13
14
end
14
15
15
- def stub_no_errors
16
- subject . stubs ( :has_errors ) . returns ( false )
17
- subject . stubs ( :error_message ) . returns ( nil )
18
- end
19
-
20
16
describe 'open_and_run_command' do
21
- context 'command' do
17
+ context 'command execution ' do
22
18
before :each do
23
19
stub_connection
24
- @connection . stubs ( :State ) . returns ( 0 )
25
20
@connection . stubs ( :Open ) . with ( 'Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost' )
26
21
end
27
22
it 'should not raise an error but populate has_errors with message' do
28
- subject . stubs ( :win32_exception ) . returns ( Exception )
29
23
subject . stubs ( :execute ) . raises ( Exception . new ( "SQL Server\n error has happened" ) )
30
24
expect {
31
25
result = subject . open_and_run_command ( 'whacka whacka whacka' , config )
@@ -44,36 +38,32 @@ def stub_no_errors
44
38
context 'closed connection' do
45
39
before :each do
46
40
stub_connection
47
- stub_no_errors
48
- @connection . stubs ( :State ) . returns ( 0 )
41
+ @connection . stubs ( :Execute )
49
42
end
50
43
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' )
52
45
subject . open_and_run_command ( 'query' , config )
53
46
end
54
47
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' )
56
49
subject . open_and_run_command ( 'query' , { :admin_user => 'superuser' , :admin_pass => 'puppetTested' , :instance_name => 'LOGGING' } )
57
50
end
58
51
end
59
52
context 'open connection' do
60
53
it 'should not reopen an existing connection' do
61
54
stub_connection
62
55
@connection . expects ( :open ) . never
63
- @connection . stubs ( :State ) . returns ( 1 )
56
+ @connection . stubs ( :State ) . returns ( 1 ) # any value other than CONNECTION_CLOSED
64
57
@connection . expects ( :Execute ) . with ( 'query' , nil , nil )
65
58
subject . open_and_run_command ( 'query' , config )
66
59
end
67
60
end
68
61
context 'return result with errors' do
69
62
it {
63
+ subject . stubs ( :win32_exception ) . returns ( Exception )
70
64
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" ) )
72
66
subject . expects ( :close ) . once
73
- subject . stubs ( :has_errors ) . returns ( :true )
74
- subject . stubs ( :error_message ) . returns (
75
- 'SQL Server
76
- invalid syntax provider' )
77
67
result =
78
68
subject . open_and_run_command ( 'SELECT * FROM sys.databases' , config )
79
69
expect ( result . exitstatus ) . to eq ( 1 )
@@ -85,8 +75,6 @@ def stub_no_errors
85
75
stub_connection
86
76
err_message = "SQL Server\n ConnectionFailed"
87
77
@connection . stubs ( :Open ) . raises ( Exception . new ( err_message ) )
88
- subject . stubs ( :has_errors ) . returns ( true )
89
- subject . stubs ( :error_message ) . returns ( err_message )
90
78
expect {
91
79
result = subject . open_and_run_command ( 'whacka whacka whacka' , config )
92
80
expect ( result . exitstatus ) . to eq ( 1 )
0 commit comments