Skip to content

Commit

Permalink
Merge pull request #208 from ifad/chore/refactor-specs
Browse files Browse the repository at this point in the history
Refactor aruba specs
  • Loading branch information
tagliala authored Aug 30, 2023
2 parents 3b4615b + 888bc9c commit b195369
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace :testapp do
FileUtils.rm_rf('railsapp')
sh 'rails new railsapp --skip-bundle --skip-javascript --skip-webpack-install --skip-git'
end
FileUtils.cp_r('spec/aruba/fixtures/railsapp/.', 'tmp/railsapp/')
FileUtils.cp_r('spec/fixtures/railsapp/.', 'tmp/railsapp/')
FileUtils.rm('tmp/railsapp/Gemfile')
end
end
10 changes: 3 additions & 7 deletions spec/aruba/dbconsole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
require 'spec_helper'

RSpec.describe 'rails dbconsole' do
before do
write_file(
'config/database.yml',
File.read(File.expand_path('fixtures/database_without_username_and_password.yml', __dir__)))
end
before { copy_db_config }

describe 'rails dbconsole', type: :aruba do
let(:action) { run_command("bash -c \"echo 'select 1 as foo_column; \\q' | bundle exec rails db\"") }
let(:last_command) { action && last_command_started }

specify { expect(last_command).to be_successfully_executed }
specify { expect(last_command).to have_output(/\bfoo_column\b/) }
it { expect(last_command).to be_successfully_executed }
it { expect(last_command).to have_output(/\bfoo_column\b/) }
end
end

Expand Down
28 changes: 9 additions & 19 deletions spec/aruba/migrations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
require 'spec_helper'

RSpec.describe 'database migrations', type: :aruba do
context 'after a migration was generated' do
before { write_file('config/database.yml',
File.read(File.expand_path('fixtures/database_without_username_and_password.yml', __dir__))) }
before { copy_db_config }

context 'after a migration was generated' do
before { run_command_and_stop('bundle exec rails g migration CreateModels name:string') }

describe 'bundle exec rake db:migrate' do
let(:action) { run_command('bundle exec rake db:migrate') }
let(:last_command) { action && last_command_started }

specify { expect(last_command).to be_successfully_executed }
specify { expect(last_command).to have_output(/CreateModels: migrated/) }
it { expect(last_command).to be_successfully_executed }
it { expect(last_command).to have_output(/CreateModels: migrated/) }
end
end

describe 'rerun bundle exec rake db:drop db:create db:migrate', issue: 56 do
let(:command) { 'bundle exec rake db:drop db:create db:migrate' }
before do
copy(
'../../spec/aruba/fixtures/database_without_username_and_password.yml',
'config/database.yml'
)
copy(
'../../spec/aruba/fixtures/migrations/56/',
'db/migrate'
)
end
before { copy('%/migrations/56/', 'db/migrate') }

let(:action) { run_command(command) }
let(:regex) { /-- change_table\(:impressions, {:temporal=>true, :copy_data=>true}\)/ }

describe 'once' do
let(:last_command) { action && last_command_started }
specify { expect(last_command).to be_successfully_executed }
specify { expect(last_command).to have_output(regex) }
it { expect(last_command).to be_successfully_executed }
it { expect(last_command).to have_output(regex) }
end

describe 'twice' do
let(:last_command) { run_command_and_stop(command) && action && last_command_started }
specify { expect(last_command).to be_successfully_executed }
specify { expect(last_command).to have_output(regex) }
it { expect(last_command).to be_successfully_executed }
it { expect(last_command).to have_output(regex) }
end
end
end
46 changes: 21 additions & 25 deletions spec/aruba/rake_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
end

describe "#{dump_schema_task}" do
let(:database_yml) { 'fixtures/database_without_username_and_password.yml' }
before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) }
before { copy_db_config }

before { run_command_and_stop("bundle exec rake #{dump_schema_task} SCHEMA=db/test.sql") }

Expand All @@ -24,7 +23,7 @@
it { expect('db/test.sql').not_to have_file_content(/\A--/) }

context 'with schema_search_path option' do
let(:database_yml) { 'fixtures/database_with_schema_search_path.yml' }
before { copy_db_config('database_with_schema_search_path.yml') }

before { run_command_and_stop("bundle exec rake #{dump_schema_task} SCHEMA=db/test.sql") }

Expand All @@ -37,11 +36,10 @@
end

describe 'db:schema:load' do
let(:database_yml) { 'fixtures/database_without_username_and_password.yml' }
before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) }

let(:structure_sql) { 'fixtures/set_config.sql' }
before { write_file('db/test.sql', File.read(File.expand_path(structure_sql, __dir__))) }
before do
copy_db_config
copy('%/set_config.sql', 'db/test.sql')
end

before { run_command_and_stop('bundle exec rake db:schema:load SCHEMA=db/test.sql') }

Expand All @@ -53,18 +51,15 @@
let(:last_command) { action && last_command_started }

context 'given a file db/structure.sql' do
let(:structure_sql) { 'fixtures/empty_structure.sql' }
before { write_file('db/structure.sql', File.read(File.expand_path(structure_sql, __dir__))) }
before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) }

subject { expect(last_command).to be_successfully_executed }
before do
copy('%/empty_structure.sql', 'db/structure.sql')
end

context 'with default username and password', issue: 55 do
let(:database_yml) { 'fixtures/database_with_default_username_and_password.yml' }
before { copy_db_config('database_with_default_username_and_password.yml') }

# Handle Homebrew on MacOS, whose database superuser name is
# equal to the name of the current user.
#
before do
if which 'brew'
file_mangle!('config/database.yml') do |contents|
Expand All @@ -73,20 +68,22 @@
end
end

specify { subject }
it { expect(last_command).to be_successfully_executed }
end

context 'without a specified username and password', issue: 55 do
let(:database_yml) { 'fixtures/database_without_username_and_password.yml' }
before { copy_db_config }

specify { subject }
it { expect(last_command).to be_successfully_executed }
end
end
end

describe 'db:data:dump' do
let(:database_yml) { 'fixtures/database_without_username_and_password.yml' }
before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) }
before do
copy_db_config
copy('%/set_config.sql', 'db/test.sql')
end

before { run_command_and_stop('bundle exec rake db:data:dump DUMP=db/test.sql') }

Expand All @@ -95,11 +92,10 @@
end

describe 'db:data:load' do
let(:database_yml) { 'fixtures/database_without_username_and_password.yml' }
before { write_file('config/database.yml', File.read(File.expand_path(database_yml, __dir__))) }

let(:structure_sql) { 'fixtures/empty_structure.sql' }
before { write_file('db/test.sql', File.read(File.expand_path(structure_sql, __dir__))) }
before do
copy_db_config
copy('%/empty_structure.sql', 'db/test.sql')
end

before { run_command_and_stop('bundle exec rake db:data:load DUMP=db/test.sql') }

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions spec/support/aruba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def load_schema_task(as_regexp: false)
as_regexp ? Regexp.new(str) : str
end

def copy_db_config(file = 'database_without_username_and_password.yml')
copy("%/#{file}", 'config/database.yml')
end

def dump_schema_task(as_regexp: false)
str =
if Rails.version < '7.0'
Expand Down

0 comments on commit b195369

Please sign in to comment.