All files / lib/checkers postgresql.js

63.63% Statements 14/22
33.33% Branches 2/6
50% Functions 3/6
63.63% Lines 14/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58  1x   1x 1x                           2x   2x 2x   2x 1x 1x 1x   1x 1x 1x                                                   1x  
'use strict';
var pg = require('pg');
 
const Result = require('../result');
const checker = require('../checker');
 
/**
 * Health check a postgresql resource.
 *
 * @param {Object} setup: postgresql check setup with fields:
 * - uri: a resource URI to check
 * - testQueries: test queries to run with expected responses
 * @param {Function} cb: standard cb(err, result) callback
 */
 
var client;
 
function check(setup, cb) {
    client = new pg.Client(setup.uri);
 
    client.connect(function (err) {
        var result = new Result();
 
        if (err) {
            result.addError(err.message);
            result.setStatusByStats();
            cb(null, result);
        } else {
            checker.checkAttribute('testQueries', setup, _runTestQuery, result, function () {
                result.setStatusByStats();
                cb(null, result);
            });
        }
 
    });
}
 
function _runTestQuery(testCase, cb) {
    client.query(testCase.query, function (err, result) {
        if (err) {
            cb(err);
        }
 
        var match = Object.keys(testCase.expected).every(function (field) {
            return result.rows[0][field] === testCase.expected[field];
        });
 
        if (match) {
            cb(null, 'Result contains the expected fields');
        } else {
            cb(new Error('Result ' + JSON.stringify(result.rows[0])
                + ' does not contain the expected ' + JSON.stringify(testCase.expected)));
        }
    });
}
 
exports.check = check;