newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / spec / unit / puppet / provider / postgresql_psql / ruby_spec.rb
1 require 'spec_helper'
2
3 describe Puppet::Type.type(:postgresql_psql).provider(:ruby) do
4   let(:name) { 'rspec psql test' }
5   let(:resource) do
6     Puppet::Type.type(:postgresql_psql).new({ :name => name, :provider => :ruby }.merge attributes)
7   end
8
9   let(:provider) { resource.provider }
10
11   context("#run_sql_command") do
12     describe "with default attributes" do
13       let(:attributes) do { :db => 'spec_db' } end
14
15       it "executes with the given psql_path on the given DB" do
16         expect(provider).to receive(:run_command).with(['psql', '-d',
17           attributes[:db], '-t', '-c', '"SELECT \'something\' as \"Custom column\""'], 'postgres',
18           'postgres', {})
19
20         provider.run_sql_command('SELECT \'something\' as "Custom column"')
21       end
22     end
23     describe "with psql_path and db" do
24       let(:attributes) do {
25         :psql_path  => '/opt/postgres/psql',
26         :psql_user  => 'spec_user',
27         :psql_group => 'spec_group',
28         :cwd        => '/spec',
29         :db         => 'spec_db'
30       } end
31
32       it "executes with the given psql_path on the given DB" do
33         expect(Dir).to receive(:chdir).with(attributes[:cwd]).and_yield
34         expect(provider).to receive(:run_command).with([attributes[:psql_path],
35           '-d', attributes[:db], '-t', '-c', '"SELECT \'something\' as \"Custom column\""'],
36           attributes[:psql_user], attributes[:psql_group], {})
37
38         provider.run_sql_command('SELECT \'something\' as "Custom column"')
39       end
40     end
41     describe "with search_path string" do
42       let(:attributes) do {
43         :search_path => "schema1"
44       } end
45
46       it "executes with the given search_path" do
47         expect(provider).to receive(:run_command).with(['psql', '-t', '-c',
48           '"set search_path to schema1; SELECT \'something\' as \"Custom column\""'],
49           'postgres', 'postgres', {})
50
51         provider.run_sql_command('SELECT \'something\' as "Custom column"')
52       end
53     end
54     describe "with search_path array" do
55       let(:attributes) do {
56         :search_path => ['schema1','schema2'],
57       } end
58
59       it "executes with the given search_path" do
60         expect(provider).to receive(:run_command).with(['psql', '-t', '-c',
61           '"set search_path to schema1,schema2; SELECT \'something\' as \"Custom column\""'],
62           'postgres',
63           'postgres',
64           {}
65         )
66
67         provider.run_sql_command('SELECT \'something\' as "Custom column"')
68       end
69     end
70   end
71    describe "with port string" do
72       let(:attributes) do { :port => '5555' } end
73
74       it "executes with the given port" do
75         expect(provider).to receive(:run_command).with(["psql",
76         "-p", "5555",
77         "-t", "-c", "\"SELECT something\""],
78         "postgres", "postgres", {} )
79
80         provider.run_sql_command("SELECT something")
81       end
82     end
83     describe "with connect_settings" do
84       let(:attributes) do { :connect_settings => { 'PGHOST' => '127.0.0.1' } } end
85
86       it "executes with the given host" do
87         expect(provider).to receive(:run_command).with(["psql",
88           "-t", "-c",
89           "\"SELECT something\""],
90           "postgres", "postgres", { 'PGHOST' => '127.0.0.1' } )
91
92         provider.run_sql_command("SELECT something")
93       end
94     end
95
96   context("#run_unless_sql_command") do
97     let(:attributes) do { } end
98
99     it "calls #run_sql_command with SQL" do
100       expect(provider).to receive(:run_sql_command).with('SELECT COUNT(*) FROM (SELECT 1) count')
101       provider.run_unless_sql_command('SELECT 1')
102     end
103   end
104 end