-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlserver__tsql_spec.rb
85 lines (74 loc) · 2.51 KB
/
sqlserver__tsql_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'spec_helper'
require 'mocha'
RSpec.describe Puppet::Type.type(:sqlserver_tsql).provider(:mssql) do
subject { described_class }
let(:config) { {:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'MSSQLSERVER'} }
def stub_open_and_run(query, config)
sqlconn = mock()
sqlconn.expects(:open_and_run_command).with(gen_query(query), config)
PuppetX::Sqlserver::SqlConnection.expects(:new).returns(sqlconn)
end
def create_sqlserver_tsql(args)
@resource = Puppet::Type::Sqlserver_tsql.new(args)
@provider = subject.new(@resource)
end
def stub_get_instance_config(config)
@provider.expects(:get_config).returns(config)
end
def gen_query(query)
quoted_query = query.gsub('\'', '\'\'')
<<-PP
BEGIN TRY
DECLARE @sql_text as NVARCHAR(max);
SET @sql_text = N'#{quoted_query}'
EXECUTE sp_executesql @sql_text;
END TRY
BEGIN CATCH
DECLARE @msg as VARCHAR(max);
SELECT @msg = 'THROW CAUGHT: ' + ERROR_MESSAGE();
THROW 51000, @msg, 10
END CATCH
PP
end
context 'run_update' do
describe 'against non master database' do
it {
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER', :database => 'myDb'})
stub_get_instance_config(config)
stub_open_and_run('whacka whacka', config.merge({:database => 'myDb'}))
@provider.run_update
}
end
describe 'against default database' do
it {
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER'})
stub_get_instance_config(config)
stub_open_and_run('whacka whacka', config.merge({:database => 'master'}))
@provider.run_update
}
end
end
context 'run_check' do
describe 'against default database' do
it {
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :onlyif => 'fozy wozy', :instance => 'MSSQLSERVER'})
stub_get_instance_config(config)
stub_open_and_run('fozy wozy', config.merge({:database => 'master'}))
@provider.run_check
}
end
describe 'against non master database' do
it {
create_sqlserver_tsql(
{:title => 'runme',
:command => 'whacka whacka',
:onlyif => 'fozy wozy',
:instance => 'MSSQLSERVER',
:database => 'myDb'})
stub_get_instance_config(config)
stub_open_and_run('fozy wozy', config.merge({:database => 'myDb'}))
@provider.run_check
}
end
end
end