$yellow_begin = "\033[01;33m" $green_begin = "\033[01;32m" $red_begin = "\033[01;31m" $color_end = "\033[00m" $succeeded = "#{$green_begin}succeeded#{$color_end}." $failed = "#{$red_begin}failed#{$color_end}." $search_path = "SET SEARCH_PATH TO test, public" FileList['help_*.pgsql'].each do |help_fname| file help_fname => [:pg_conn, :clear_test_schema, :load_global_helper] do print "#{$yellow_begin}Loading helper #{help_fname}: #{$color_end}" begin $db_conn.exec $search_path $db_conn.exec File.open(help_fname).read rescue PGError puts $failed raise else puts $succeeded end end end # Traverse all filenames prefixed with 'test_' FileList['test_*.pgsql'].each do |test_fname| # Find files with helper functions test_basename = test_fname.gsub(/\..*sql$/, '').gsub(/^test_/, '') test_basename.count('_').times do test_basename = test_basename.gsub(/_[^_]+$/, '') help_fname = "help_#{test_basename}.pgsql" if File.exists? help_fname file test_fname => help_fname do end end end file test_fname => [:pg_conn, :clear_test_schema, :load_global_helper] do file_basename = File.basename(test_fname, '.pgsql') print "#{$yellow_begin}Loading test #{test_fname}: #{$color_end}" # Only show me bad things. I don't want to drown in NOTICEs $db_conn.exec "SET client_min_messages TO 'WARNING'" begin # (Re)load test function $db_conn.exec $search_path $db_conn.exec File.open(test_fname).read rescue PGError puts $failed raise else puts $succeeded end print "#{$yellow_begin}Running test #{file_basename}(): #{$color_end}" begin # Execute the test function, and rollback any changes it might have made $db_conn.exec "BEGIN" $db_conn.exec "SELECT test.#{test_fname.gsub(/\..*sql$/, '')}()" $db_conn.exec "ROLLBACK" rescue PGError puts $failed raise else puts $succeeded end end end task :load_global_helper => :clear_test_schema do print "#{$yellow_begin}Loading global helper: #{$color_end}" begin $db_conn.exec $search_path $db_conn.exec File.open("help_global.pgsql").read rescue PGError puts $failed raise else puts $succeeded end end task :clear_test_schema => :pg_conn do print "#{$yellow_begin}Clearing test schema: #{$color_end}" begin $db_conn.exec "SET client_min_messages TO 'WARNING'" $db_conn.exec "BEGIN" $db_conn.exec "DROP SCHEMA test CASCADE" $db_conn.exec "CREATE SCHEMA test" $db_conn.exec "COMMIT" rescue PGError puts $failed raise else puts $succeeded end end task :pg_conn => :pg_env do require 'postgres' puts "Connecting to #{ENV['PGDATABASE']} on #{ENV['PGHOST']}:#{ENV['PGPORT']}." $db_conn = PGconn.open( ENV['PGHOST'], ENV['PGPORT'].to_i, "", "", ENV['PGDATABASE'], ENV['PGUSER'], ENV['PGPASSWORD'] ) end # Change this task to fit your particular environment task :pg_env do require 'yaml' require 'erb' RAILS_ROOT = File.dirname(__FILE__) + '/../../' db_cfg = YAML.load( ERB.new( File.open(File.dirname(__FILE__) + '/../../config/database.yml').read ).result )[ ENV['RAILS_ENV'] || 'development' ] ENV['PGHOST'] = db_cfg['host'] ENV['PGPORT'] = db_cfg['port'].to_s ENV['PGUSER'] = db_cfg['username'] ENV['PGPASSWORD'] = db_cfg['password'] ENV['PGDATABASE'] = db_cfg['database'] end # vim: set filetype=ruby shiftwidth=2 tabstop=2 expandtab: