Skip to content

Commit

Permalink
Add acceptance test suite
Browse files Browse the repository at this point in the history
This change adds an acceptance test suite. These tests don't have great
failure messages currently and could likely stand to be broken into
smaller pieces.

Changes:
- Split up `spec` task to run all unittests before the acceptance tests.
  • Loading branch information
teoljungberg committed Jun 30, 2016
1 parent a229cdc commit 156a171
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Rakefile
Expand Up @@ -6,6 +6,18 @@ namespace :dummy do
Dummy::Application.load_tasks
end

RSpec::Core::RakeTask.new(:spec)
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

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

describe "User manages functions" do
it "handles simple functions" do
successfully "rails generate fx:function test"
write_definition "test_v01", <<~EOS
CREATE OR REPLACE FUNCTION test() RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
successfully "rake db:migrate"

result = execute("SELECT * FROM test() AS result")
expect(result).to eq("result" => "test")

successfully "rails generate fx:function test"
write_definition "test_v02", <<~EOS
CREATE OR REPLACE FUNCTION test() RETURNS text AS $$
BEGIN
RETURN 'testest';
END;
$$ LANGUAGE plpgsql;
EOS
successfully "rake db:migrate"

result = execute("SELECT * FROM test() AS result")
expect(result).to eq("result" => "testest")
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/functions/#{file}.sql", File::WRONLY) do |definition|
definition.truncate(0)
definition.write(contents)
end
end

def execute(command)
ActiveRecord::Base.connection.execute(command).first
end
end
33 changes: 33 additions & 0 deletions spec/acceptance_helper.rb
@@ -0,0 +1,33 @@
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
ActiveRecord::Base.connection.disconnect!
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

0 comments on commit 156a171

Please sign in to comment.