Skip to content

Commit

Permalink
Add database spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Genki Sugawara committed Feb 24, 2016
1 parent 011510d commit 2c8fb07
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 0 deletions.
219 changes: 219 additions & 0 deletions spec/posgra_databases_spec.rb
@@ -0,0 +1,219 @@
describe 'databases' do
include SpecHelper

subject { export_databases }

before do
apply_roles do
<<-RUBY
group "engineer" do
user "bob"
end
group "staff" do
user "alice"
end
RUBY
end

apply_databases do
<<-RUBY
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
grant "CREATE"
end
end
RUBY
end
end

context 'nothing to do' do
it do
expect(
apply_databases do
<<-RUBY
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
grant "CREATE"
end
end
RUBY
end
).to be_falsey

is_expected.to eq <<-RUBY.unindent.chomp
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
grant "CREATE"
end
end
RUBY
end
end
context 'when grant' do
it do
expect(
apply_databases do
<<-RUBY
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
grant "CREATE"
grant "TEMPORARY"
end
end
role "bob" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
grant "TEMPORARY"
end
end
RUBY
end
).to be_truthy

is_expected.to eq <<-RUBY.unindent.chomp
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
grant "CREATE"
grant "TEMPORARY"
end
end
role "bob" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
grant "TEMPORARY"
end
end
RUBY
end
end

context 'when revoke' do
it do
expect(
apply_databases do
<<-RUBY
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
end
end
role "bob" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
grant "TEMPORARY"
end
end
RUBY
end
).to be_truthy

is_expected.to eq <<-RUBY.unindent.chomp
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
end
end
role "bob" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
grant "TEMPORARY"
end
end
RUBY
end
end

context 'when revoke all' do
it do
expect(
apply_databases do
<<-RUBY
role "bob" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
grant "TEMPORARY"
end
end
RUBY
end
).to be_truthy

is_expected.to eq <<-RUBY.unindent.chomp
role "bob" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
grant "TEMPORARY"
end
end
RUBY
end
end

context 'when grant grant_option' do
it do
expect(
apply_databases do
<<-RUBY
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
grant "CREATE", :grantable => true
end
end
RUBY
end
).to be_truthy

is_expected.to eq <<-RUBY.unindent.chomp
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT", :grantable => true
grant "CREATE", :grantable => true
end
end
RUBY
end
end

context 'when revoke grant_option' do
it do
expect(
apply_databases do
<<-RUBY
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
end
end
RUBY
end
).to be_truthy

is_expected.to eq <<-RUBY.unindent.chomp
role "alice" do
database "#{SpecHelper::DBNAME}" do
grant "CONNECT"
grant "CREATE"
end
end
RUBY
end
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -67,6 +67,20 @@ def export_grants(options = {})
end
end

def apply_databases(options = {})
tempfile(yield) do |f|
run_client(options) do |client|
client.apply_databases(f.path)
end
end
end

def export_databases(options = {})
run_client(options) do |client|
client.export_databases
end
end

def run_client(options = {})
options = {
host: DBHOST,
Expand Down

0 comments on commit 2c8fb07

Please sign in to comment.