class PgTester
Constants
- VERSION
Attributes
createdb_path[R]
createuser_path[R]
data_dir[R]
database[R]
host[R]
initdb_path[R]
pgctl_path[R]
port[R]
user[R]
Public Class Methods
new(opts={})
click to toggle source
# File lib/pg_tester.rb, line 10 def initialize(opts={}) @user = opts.fetch(:user) { 'pgtester' } @database = opts.fetch(:database) { 'pgtester' } @port = opts.fetch(:port) { 6433 } @host = opts.fetch(:host) { 'localhost' } @data_dir = opts.fetch(:data_dir) { '/tmp/pg_tester' } @initdb_path = opts.fetch(:initdb_path) { shell_out('which initdb') } @pgctl_path = opts.fetch(:pgctl_path) { shell_out('which pg_ctl') } @createuser_path = opts.fetch(:createuser_path) { shell_out('which createuser') } @createdb_path = opts.fetch(:createdb_path) { shell_out('which createdb') } @connection = nil raise 'please install postgresql' unless @initdb_path && !@initdb_path.empty? end
Public Instance Methods
create_data_dir()
click to toggle source
# File lib/pg_tester.rb, line 25 def create_data_dir() Dir.mkdir(@data_dir) unless File.exists? @data_dir end
exec(query) { |exec| ... }
click to toggle source
# File lib/pg_tester.rb, line 71 def exec(query) if block_given? setup yield(@connection.exec(query)) teardown else raise 'please run setup' unless @connection return @connection.exec(query) end end
exec_params(query, params) { |exec_params| ... }
click to toggle source
# File lib/pg_tester.rb, line 82 def exec_params(query, params) if block_given? setup yield(@connection.exec_params(query, params)) teardown else raise 'please run setup' unless @connection return @connection.exec_params(query, params) end end
initdb()
click to toggle source
# File lib/pg_tester.rb, line 33 def initdb() shell_out "#{@initdb_path} #{@data_dir} -A trust -E utf-8" end
remove_data_dir()
click to toggle source
# File lib/pg_tester.rb, line 29 def remove_data_dir() FileUtils.remove_dir(@data_dir) end
rundb()
click to toggle source
# File lib/pg_tester.rb, line 37 def rundb() pid = Process.fork { shell_out "#{@pgctl_path} start -o '-p #{@port}' -D #{@data_dir}" } # give a second for postgresql to startup sleep(1) Process.detach(pid) end
setup()
click to toggle source
# File lib/pg_tester.rb, line 56 def setup() create_data_dir initdb rundb setup_test_user setup_test_database setup_pg_connection end
setup_pg_connection()
click to toggle source
# File lib/pg_tester.rb, line 52 def setup_pg_connection() @connection = PG::Connection.open(:user => @user, :dbname => @database, :port => @port) end
setup_test_database()
click to toggle source
# File lib/pg_tester.rb, line 48 def setup_test_database() shell_out "#{@createdb_path} -p #{@port} #{database} -O #{user}" end
setup_test_user()
click to toggle source
# File lib/pg_tester.rb, line 44 def setup_test_user() shell_out "#{@createuser_path} -s -p #{@port} -l #{user} -w" end
teardown()
click to toggle source
# File lib/pg_tester.rb, line 65 def teardown() @connection.close() if @connection shell_out "#{@pgctl_path} stop -m fast -o '-p #{@port}' -D #{data_dir}" remove_data_dir end
Private Instance Methods
shell_out(cmd)
click to toggle source
# File lib/pg_tester.rb, line 95 def shell_out(cmd) result = `#{cmd}` result.strip end