pg_tester 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6cc3642ebdc410f3d73463b945c6cdead08b218
4
+ data.tar.gz: 9ba9d4a3f7686fea32f2d7cb8af152648c25d8d0
5
+ SHA512:
6
+ metadata.gz: 0e50d0e50003c0082852aa14275de5b1d2e69a9845d282a7705a21f57144007cdfcb09e4aa26b0656844e5b2fe585b6b4a3dd8a130b7005e1cc7d8811057ed3a
7
+ data.tar.gz: c91ea27012d4b4406b7dd3b7166d253fa821ab63a0fc93d4e49be408d80ac13fe7ab95e586abd82b332a6572b5de9d0283237788eda10acdecc3f114eb3c8900
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_tester.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ gem 'pry-byebug'
9
+ end
@@ -0,0 +1,77 @@
1
+ # PgTester
2
+
3
+ A handy gem to help with testing postgresql related scripts or anything postgresql related
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pg_tester'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pg_tester
20
+
21
+ ## Usage
22
+
23
+
24
+ ```
25
+ require 'pg_tester'
26
+
27
+ psql = PgTester.new({
28
+
29
+ port: '312',
30
+ host: 'localhost',
31
+ db_name: 'testpostgresql',
32
+ user_name: 'testpostgresql',
33
+ data_dir: '/tmp/',
34
+ initdb_path: '/usr/local/bin/initdb',
35
+ pgctl_path: '/usr/local/bin/which pg_ctl',
36
+ createuser_path: '/usr/local/bin/which createuser',
37
+ createdb_path: '/usr/local/bin/which createdb',
38
+ })
39
+
40
+ ## use case 1
41
+ psql.setup # This will create a test postgres cluster in /tmp, connection as testpostgresql user and database name testpostgresql
42
+ result = psql.exec(query)
43
+ # ... do some expectation on result
44
+ psql.teardown # Cluster is torn down and dir in /tmp deleted
45
+
46
+ ## use case 2 -- this setup, execute the block and teardown
47
+ psql.exec(query) do |result|
48
+ # ... do some expectation on result
49
+ end
50
+
51
+ ## use case 3 -- all in one using default
52
+ PgTester.new({
53
+ port: '312',
54
+ data_dir: '/tmp/',
55
+ }).exec(query) { |result| # some expectation }
56
+
57
+ ## use case 4 -- the rspec usage
58
+
59
+ context 'run query in block' do
60
+ it 'should run exec query in a block' do
61
+ PgTester.new().exec('SELECT 2') do |result|
62
+ expect(result.getvalue(0,0)).to eq("2")
63
+ end
64
+ end
65
+
66
+ it 'should run exec query in a curly block' do
67
+ PgTester.new().exec('SELECT 3') { |result| expect(result.getvalue(0,0)).to eq("3") }
68
+ end
69
+ end
70
+
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ Pull requests are welcome! You can run the tests by doing
76
+
77
+ `bundle exec rspec`
@@ -0,0 +1,6 @@
1
+ require "rspec/core/rake_task"
2
+
3
+ RSpec::Core::RakeTask.new("spec")
4
+
5
+ # If you want to make this the default task
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pg_tester"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,100 @@
1
+ require "pg_tester/version"
2
+ require 'pg'
3
+ require 'fileutils'
4
+
5
+ class PgTester
6
+
7
+ attr_reader :user, :database, :port, :host, :data_dir, :initdb_path,
8
+ :pgctl_path, :createuser_path, :createdb_path
9
+
10
+ def initialize(opts={})
11
+ @user = opts.fetch(:user) { 'pgtester' }
12
+ @database = opts.fetch(:database) { 'pgtester' }
13
+ @port = opts.fetch(:port) { 6433 }
14
+ @host = opts.fetch(:host) { 'localhost' }
15
+ @data_dir = opts.fetch(:data_dir) { '/tmp/pg_tester' }
16
+ @initdb_path = opts.fetch(:initdb_path) { shell_out('which initdb') }
17
+ @pgctl_path = opts.fetch(:pgctl_path) { shell_out('which pg_ctl') }
18
+ @createuser_path = opts.fetch(:createuser_path) { shell_out('which createuser') }
19
+ @createdb_path = opts.fetch(:createdb_path) { shell_out('which createdb') }
20
+ @connection = nil
21
+
22
+ raise 'please install postgresql' unless @initdb_path && !@initdb_path.empty?
23
+ end
24
+
25
+ def create_data_dir()
26
+ Dir.mkdir(@data_dir) unless File.exists? @data_dir
27
+ end
28
+
29
+ def remove_data_dir()
30
+ FileUtils.remove_dir(@data_dir)
31
+ end
32
+
33
+ def initdb()
34
+ shell_out "#{@initdb_path} #{@data_dir} -A trust -E utf-8"
35
+ end
36
+
37
+ def rundb()
38
+ pid = Process.fork { shell_out "#{@pgctl_path} start -o '-p #{@port}' -D #{@data_dir}" }
39
+ # give a second for postgresql to startup
40
+ sleep(1)
41
+ Process.detach(pid)
42
+ end
43
+
44
+ def setup_test_user()
45
+ shell_out "#{@createuser_path} -s -p #{@port} -l #{user} -w"
46
+ end
47
+
48
+ def setup_test_database()
49
+ shell_out "#{@createdb_path} -p #{@port} #{database} -O #{user}"
50
+ end
51
+
52
+ def setup_pg_connection()
53
+ @connection = PG::Connection.open(:user => @user, :dbname => @database, :port => @port)
54
+ end
55
+
56
+ def setup()
57
+ create_data_dir
58
+ initdb
59
+ rundb
60
+ setup_test_user
61
+ setup_test_database
62
+ setup_pg_connection
63
+ end
64
+
65
+ def teardown()
66
+ @connection.close() if @connection
67
+ shell_out "#{@pgctl_path} stop -m fast -o '-p #{@port}' -D #{data_dir}"
68
+ remove_data_dir
69
+ end
70
+
71
+ def exec(query)
72
+ if block_given?
73
+ setup
74
+ yield(@connection.exec(query))
75
+ teardown
76
+ else
77
+ raise 'please run setup' unless @connection
78
+ return @connection.exec(query)
79
+ end
80
+ end
81
+
82
+ def exec_params(query, params)
83
+ if block_given?
84
+ setup
85
+ yield(@connection.exec_params(query, params))
86
+ teardown
87
+ else
88
+ raise 'please run setup' unless @connection
89
+ return @connection.exec_params(query, params)
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def shell_out(cmd)
96
+ result = `#{cmd}`
97
+ result.strip
98
+ end
99
+
100
+ end
@@ -0,0 +1,3 @@
1
+ class PgTester
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pg_tester/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pg_tester"
8
+ spec.version = PgTester::VERSION
9
+ spec.authors = ["yann ARMAND", "neeran GUL"]
10
+ spec.email = ["yarmand@yammer-inc.com", "ngul@yammer-inc.com"]
11
+ spec.license = "MIT"
12
+ spec.summary = %q{Runs tests against a temporary postgresql instance}
13
+ spec.description = %q{Test postgresql scripts and run queries against a temporary postgresql instance}
14
+ spec.homepage = "https://github.int.yammer.com/yammer/pgtester"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.9"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.3"
23
+ spec.add_runtime_dependency 'pg', '~> 0.18', '>= 0.18.2'
24
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_tester
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yann ARMAND
8
+ - neeran GUL
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.9'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.9'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.3'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.3'
56
+ - !ruby/object:Gem::Dependency
57
+ name: pg
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.18'
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 0.18.2
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '0.18'
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.18.2
76
+ description: Test postgresql scripts and run queries against a temporary postgresql
77
+ instance
78
+ email:
79
+ - yarmand@yammer-inc.com
80
+ - ngul@yammer-inc.com
81
+ executables:
82
+ - console
83
+ - setup
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - ".gitignore"
88
+ - Gemfile
89
+ - README.md
90
+ - Rakefile
91
+ - bin/console
92
+ - bin/setup
93
+ - lib/pg_tester.rb
94
+ - lib/pg_tester/version.rb
95
+ - pg_tester.gemspec
96
+ homepage: https://github.int.yammer.com/yammer/pgtester
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.2.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Runs tests against a temporary postgresql instance
120
+ test_files: []