A handy gem to help with testing postgresql related scripts or anything postgresql related
Перейти к файлу
Neeran ff3eedd8a1 remove the postgres 2016-06-14 21:56:42 +01:00
bin adding pgtester files 2016-05-20 17:18:10 +01:00
lib adding pgtester files 2016-05-20 17:18:10 +01:00
spec better host name 2016-06-14 15:45:04 +01:00
.gitignore add a gitignore 2016-06-14 17:41:39 +01:00
.travis.yml remove the postgres 2016-06-14 21:56:42 +01:00
Gemfile adding pgtester files 2016-05-20 17:18:10 +01:00
LICENSE.txt adding pgtester files 2016-05-20 17:18:10 +01:00
README.md updating the image 2016-06-14 17:22:16 +01:00
Rakefile adding pgtester files 2016-05-20 17:18:10 +01:00
pg_tester.gemspec to make travis happy 2016-06-14 18:15:16 +01:00

README.md

PgTester Build Status Dependency Status

A handy gem to help with testing postgresql related scripts or anything postgresql related

Installation

Add this line to your application's Gemfile (source https://rubygems.org):

gem 'pg_tester'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pg_tester

Usage

Initialize PgTester instance

require 'pg_tester'

psql = PgTester.new({

  port:             '312',
  host:             'localhost',
  db_name:          'testpostgresql',
  user_name:        'testpostgresql',
  data_dir:         '/tmp/',
  initdb_path:      '/usr/local/bin/initdb',
  pgctl_path:       '/usr/local/bin/which pg_ctl',
  createuser_path:  '/usr/local/bin/which createuser',
  createdb_path:    '/usr/local/bin/which createdb',
  })

Case 1

Create a test postgres cluster in /tmp, connection as testpostgresql user and database name testpostgresql and run queries against the test database.

psql.setup 
result = psql.exec(query)
# ... do some expectation on result

Remember to teardown the database to stop postgresql

psql.teardown # Cluster is torn down and dir in /tmp deleted

Case 2

Execute the block and teardown database after block execution

psql.exec(query) do |result|
  # ... do some expectation on result
end

Case 3

Pass custom arguments and execute query in block

PgTester.new({
  port:             '312',
  data_dir:         '/tmp/',
}).exec(query) { |result| # some expectation }

Case 4

The rspec usage

context 'run query in block' do
  it 'should run exec query in a block' do
    PgTester.new().exec('SELECT 2') do |result|
      expect(result.getvalue(0,0)).to eq("2")
    end
  end

  it 'should run exec query in a curly block' do
    PgTester.new().exec('SELECT 3') { |result| expect(result.getvalue(0,0)).to eq("3") }
  end
end

Contributing

Pull requests are welcome!

Tests

You can run the tests by doing

bundle exec rspec