Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
aa03ba1
commit 13ad3bd
Showing
4 changed files
with
129 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.