Skip to content

Commit

Permalink
Convert smoke tests to RSpec
Browse files Browse the repository at this point in the history
We've had a few bugs released of late that would have been preventable
with better test coverage. The `smoke` shell script was daunting to add
coverage to and often had confusing output when running locally (it's
not clear when it fails). I often found myself looking for excuses to
avoid adding acceptance level tests for features.

This change moves those tests to RSpec and adds critical coverage for
reloading views and indexes from database schema. These tests don't have
great failure messages currently and could likely stand to be broken
into smaller pieces but I think this is a good start to more
maintainable and thorough acceptance tests.

Closes #114
  • Loading branch information
derekprior committed Feb 7, 2016
1 parent aa03ba1 commit 13ad3bd
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 136 deletions.
16 changes: 15 additions & 1 deletion Rakefile
Expand Up @@ -12,4 +12,18 @@ namespace :dummy do
Dummy::Application.load_tasks
end

task default: [:spec, :smoke]
task(:spec).clear
desc "Run specs other than spec/acceptance"
RSpec::Core::RakeTask.new("spec") do |task|
task.exclude_pattern = "spec/acceptance/**/*_spec.rb"
task.verbose = false
end

desc "Run acceptance specs in spec/acceptance"
RSpec::Core::RakeTask.new("spec:acceptance") do |task|
task.pattern = "spec/acceptance/**/*_spec.rb"
task.verbose = false
end

desc "Run the specs and acceptance tests"
task default: %w(spec spec:acceptance)
82 changes: 82 additions & 0 deletions spec/acceptance/user_manages_views_spec.rb
@@ -0,0 +1,82 @@
require "acceptance_helper"

describe "User manages views" do
it "handles simple views" do
successfully "rails generate scenic:model search_result"
write_definition "search_results_v01", "SELECT 'needle'::text AS term"

successfully "rake db:migrate"
verify_result "SearchResult.take.term", "needle"

successfully "rails generate scenic:view search_results"
verify_identical_view_definitions "search_results_v01", "search_results_v02"

write_definition "search_results_v02", "SELECT 'haystack'::text AS term"
successfully "rake db:migrate"

successfully "rake db:reset"
verify_result "SearchResult.take.term", "haystack"

successfully "rake db:rollback"
successfully "rake db:rollback"
successfully "rails destroy scenic:model search_result"
end

it "handles materialized views" do
successfully "rails generate scenic:model child --materialized"
write_definition "children_v01", "SELECT 'Owen'::text AS name, 5 AS age"

successfully "rake db:migrate"
verify_result "Child.take.name", "Owen"

add_index "children", "name"
add_index "children", "age"

successfully "rails runner 'Child.refresh'"

successfully "rails generate scenic:view child --materialized"
verify_identical_view_definitions "children_v01", "children_v02"

write_definition "children_v02", "SELECT 'Elliot'::text AS name"
successfully "rake db:migrate"

successfully "rake db:reset"
verify_result "Child.take.name", "Elliot"
verify_schema_contains 'add_index "children"'

successfully "rake db:rollback"
successfully "rake db:rollback"
successfully "rails destroy scenic:model child"
end

def successfully(command)
`RAILS_ENV=test #{command}`
expect($?.exitstatus).to eq(0), "'#{command}' was unsuccessful"
end

def write_definition(file, contents)
File.open("db/views/#{file}.sql", File::WRONLY) do |definition|
definition.truncate(0)
definition.write(contents)
end
end

def verify_result(command, expected_output)
successfully %{rails runner "#{command} == '#{expected_output}' || exit(1)"}
end

def verify_identical_view_definitions(def_a, def_b)
successfully "cmp db/views/#{def_a}.sql db/views/#{def_b}.sql"
end

def add_index(table, column)
successfully(<<-CMD.strip)
rails runner 'ActiveRecord::Migration.add_index "#{table}", "#{column}"'
CMD
end

def verify_schema_contains(statement)
expect(File.readlines("db/schema.rb").grep(/#{statement}/))
.not_to be_empty, "Schema does not contain '#{statement}'"
end
end
32 changes: 32 additions & 0 deletions spec/acceptance_helper.rb
@@ -0,0 +1,32 @@
require "bundler"

ENV["RAILS_ENV"] = "test"

RSpec.configure do |config|
config.around(:each) do |example|
Dir.chdir("spec/dummy") do
example.run
end
end

config.before(:suite) do
Dir.chdir("spec/dummy") do
system <<-CMD
git init 1>/dev/null &&
git add -A &&
git commit --no-gpg-sign --message 'initial' 1>/dev/null
CMD
end
end

config.after(:suite) do
Dir.chdir("spec/dummy") do
system <<-CMD
rake db:drop db:create &&
git add -A &&
git reset --hard HEAD 1>/dev/null &&
rm -rf .git/ 1>/dev/null
CMD
end
end
end
135 changes: 0 additions & 135 deletions spec/smoke

This file was deleted.

0 comments on commit 13ad3bd

Please sign in to comment.