pg-testdb
Richard Wen
rrwen.dev@gmail.com
Module for testing PostgreSQL queries inside isolated databases

Install
- Install Node.js (v6.0.0+)
- Install pg-testdb via
npm
npm install pg-testdb --save-dev
For the latest developer version, see Developer Install.
Usage
The easiest way to use this package is to first install the template generator pg-testdb-template globally with npm
:
npm install -g pg-testdb-template
Use pg-testdb-template
to generate a template file named pg-testdb-template.js
in the current directory for editing:
pg-testdb-template
The template generated will be similar to the Full Example provided in the Guide.
See the Guide for more details.
Guide
This guide will help you understand how to run tests inside a test PostgreSQL database using pg-testdb
.
A step-by-step guide, full example, and tape example are provided.
Step 1. Define Connection Options
First create an object options
to store the temporary database name and connection details:
- testdb: Name of the temporary database to create and test on (must not already exist)
- messages: Set to
true
to enable create, drop, and error messages or false
to disable - connection: Object containing PostgreSQL connection details
- connection.host: Host IP address of the PostgreSQL database to connect to
- connection.port: Port of the PostgreSQL database to connect to
- connection.user: Name of PostgreSQL user with administrative privileges
- connection.password: Password for
connection.user
var options = {
testdb: 'pgtestdb',
messages: false,
connection: {
host: 'localhost',
port: 5432,
user: 'user_name',
password: 'secret_password'
}
};
Step 2. Define Test Queries
Define an Array []
of test queries to be run:
- Each test query in the array is a callback function that consumes a client object from the pg package
- Test queries in the array are run one after another (in order)
options.tests = [];
2.1 Creating a Test Table
Initialize your tests by connecting the client object and creating a table named created_table
with columns some_text
and some_number
:
options.tests[0] = client => {
client.connect();
return client.query('CREATE TABLE created_table (some_text text, some_number numeric);')
.then(() => {
console.log('Test table created!');
})
.catch(err => {
console.error('Test table creation failed.');
});
};
2.2 Inserting Values into the Test Table
Insert values 'text data 1', 1), ('text data 2', 2)
into created_table
after its creation:
some_text | some_number |
---|
text data 1 | 1 |
text data 2 | 2 |
options.tests[1] = client => {
return client.query("INSERT INTO created_table VALUES ('text data 1', 1), ('text data 2', 2);")
.then(() => {
console.log('INSERT test passed!');
})
.catch(err => {
console.error('INSERT test failed.');
});
};
2.3 Querying Values from the Test Table
Select all values from created_table
after inserting the values:
options.tests[2] = client => {
return client.query('SELECT * FROM created_table;')
.then(res => {
console.log('SELECT test passed!');
console.log(res.rows[0]);
console.log(res.rows[1]);
})
.catch(err => {
console.log('SELECT test failed.');
});
};
3. Run the Test Queries
Using the options
object with the connection details defined from Step 1 and test queries defined from Step 2, the test queries can then be executed in order.
Running pgtestdb
will:
- Create the temporary database
options.testdb
- Run the test queries
- Drop the temporary database
options.testdb
whether tests passed or failed
var pgtestdb = require('pg-testdb');
pgtestdb(options, (err, res) => {
console.log('Testing ended.');
});
4. Full Example
The code below sets up a temporary test database in PostgreSQL and runs queries in it.
When the code is executed, the following happens:
- Test database details are defined in the
options
object - Test functions are defined for the test database inside the
options.tests
array - Test functions in
options.tests
are run in order inside the test database named by options.testdb
- Test database is dropped after the test functions in
options.tests
are run or if an error occurs - Steps 1 to 4 are repeated when the code is run again to isolate
options.tests
inside of options.testdb
var pgtestdb = require('pg-testdb');
var options = {
testdb: 'pgtestdb',
messages: false,
connection: {
host: 'localhost',
port: 5432,
user: 'user_name',
password: 'secret_password'
}
};
options.tests = [
client => {
client.connect();
return client.query('CREATE TABLE created_table (some_text text, some_number numeric);')
.then(() => {
console.log('Test table "created_table" created.');
})
.catch(err => {
console.log('Test table "created_table" creation failed.');
});
},
client => {
return client.query("INSERT INTO created_table VALUES ('text data 1', 1), ('text data 2', 2);")
.then(() => {
console.log('INSERT test passed!');
})
.catch(err => {
console.log('INSERT test failed.');
});
},
client => {
return client.query('SELECT * FROM created_table;')
.then(res => {
console.log('SELECT test passed!');
console.log(res.rows[0]);
console.log(res.rows[1]);
})
.catch(err => {
console.log('SELECT test failed.');
});
}
];
pgtestdb(options, (err, res) => {
console.log('Test database "pgtestdb" dropped.');
});
This example can be generated with pg-testdb-template as shown in Usage.
5. Example with tape
A testing framework such as tape can be used with pg-testdb
such that the test functions and execution is inside tape's test function call:
var pgtestdb = require('pg-testdb');
var test = require('tape');
var options = {
testdb: 'pgtestdb',
messages: false,
connection: {
host: 'localhost',
port: 5432,
user: 'user_name',
password: 'secret_password'
}
};
test('Tests for tape example', t => {
options.tests = [
client => {
client.connect();
return client.query('CREATE TABLE created_table (some_text text, some_number numeric);')
.then(() => {
t.pass('Test table "created_table" created.');
})
.catch(err => {
t.fail('Test table "created_table" creation failed.');
});
},
client => {
return client.query("INSERT INTO created_table VALUES ('text data 1', 1), ('text data 2', 2);")
.then(() => {
t.pass('INSERT test passed!');
})
.catch(err => {
t.fail('INSERT test failed.');
});
},
client => {
return client.query('SELECT * FROM created_table;')
.then(res => {
t.pass('SELECT test passed!');
})
.catch(err => {
t.fail('SELECT test failed.');
});
}
];
pgtestdb(options, (err, res) => {
t.comment('Test database "pgtestdb" dropped.');
});
});
See tests/test.js for more examples with tape.
Developer Notes
Developer Install
Install the latest developer version with npm
from github:
npm install git+https://github.com/rrwen/pg-testdb
Install from git
cloned source:
- Ensure git is installed
- Clone into current path
- Install via
npm
git clone https://github.com/rrwen/pg-testdb
cd pg-testdb
npm install
Tests
- Clone into current path
git clone https://github.com/rrwen/pg-testdb
- Enter into folder
cd pg-testdb
- Ensure tape and moment are available
- Setup test environment (See tests/README.md)
- Run tests
- Results are saved to
./tests/log
with each file corresponding to a version tested
npm install
npm test
Upload to Github
- Ensure git is installed
- Inside the
pg-testdb
folder, add all files and commit changes - Push to github
git add .
git commit -a -m "Generic update"
git push
Upload to npm
- Update the version in
package.json
- Run tests and check for OK status
- Login to npm
- Publish to npm
npm test
npm login
npm publish
Implementation
The npm package pg-testdb was implemented with pg and pgtools. pg
was used for creating client connections to PostgreSQL databases in Node.js, while pgtools
was used to temporarily create and drop PostgreSQL databases:
- Create a temporary database with
pgtools
- Create a client connection to the temporary database with
pg
- Drop the temporary database with
pgtools