Open Jaman-dedy opened 5 years ago
I'm setting up a reful api in nodejs, the first part was to set it with data strucutre and all test were running right good localy and on travis but now I'm using posgresql and I've add some instructions in my .travis.yml... find it below
language: node_js node_js: - "stable" services: - postgresql addons: postgresql: "10" apt: packages: - postgresql-10 - postgresql-client-10 env: global: - PGPORT=5432 - DB_NAME=questioner - DB_USER=jaman before_script: - psql --command="CREATE USER ${DB_USER};" - psql --command="CREATE DATABASE ${DB_NAME} WITH OWNER = ${DB_USER};"
connection instructions are gathered in one file, here it is
const pg = require('pg'); const config = { user: 'jaman', database: 'questioner', password: '123', port: 5432 }; const pool = new pg.Pool(config); module.exports = pool;
This connection is imported in user.js and userTest.js respectivly below
const express = require('express'); const pool = require('./connection'); const router = express.Router(); router.get('/', (req, res, next) => { pool.query('SELECT * FROM users', (err, result) => { if (err) { throw err; } res.status(200).json({ status: 200, data: result.rows }); }); }); router.get('/:userId', (req, res, next) => { const userId = parseInt(req.params.userId, 10); pool.query('SELECT * FROM users WHERE id_user = $1', [userId], (err, result) => { if (err) { throw err; } res.status(200).json({ status: 200, data: result.rows }); }); }); router.post('/', (req, res, next) => { const { firstname, lastname, othername, email, phonenumber, username, registered, isadmin } = req.body; pool.query('INSERT INTO users (firstname, lastname, othername, email, phonenumber, username, registered, isadmin) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)', [firstname, lastname, othername, email, phonenumber, username, registered, isadmin], (err, results) => { if (err) { throw err; } else { res.status(201).json({ status: 201, data: [req.body] }); } }); }); router.patch('/:userId', (req, res, next) => { const userId = parseInt(req.params.userId, 10); const { firstname, lastname, othername } = req.body; pool.query( 'UPDATE users SET firstname = $1, lastname = $2, othername = $3 WHERE id_user = $4', [firstname, lastname, othername, userId], (err, results) => { if (err) { throw err; } res.status(200).json({ status: 200, data: [req.body] }); } ); }); router.delete('/:userId', (req, res, next) => { const userId = parseInt(req.params.userId, 10); pool.query('DELETE FROM users WHERE id_user = $1', [userId], (err, results) => { if (err) { throw err; } res.status(200).json({ status: 200, data: `User deleted with ID: ${userId}` }); }); }); module.exports = router;
and the test file
const assert = require('chai').assert; const request = require('supertest'); const app = require('../app'); const connection = require('../api/routes/connection'); describe('Testing user endpoints', () => { describe('All users', () => { it('All users', (done) => { request(app) .get('/users') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); }); describe('Find a specific user', () => { it('Get a specific user', (done) => { request(app) .get('/users/1') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); }); describe('Post user', () => { const user = { id: 1, firstname: 'Emmanuel', lastname: 'Bush', othername: 'King', email: 'emabush@gmail.com', phonenumber: '+250789813478', username: 'EmaBush', registered: '2018-02-16', isAdmin: true }; it('Create a user', (done) => { request(app) .post('/users') .send(user) .set('Accept', 'application/json') .expect(201) .end((err) => { if (err) return done(err); done(); }); }); }); describe('udpate user', () => { it('Updated user', (done) => { request(app) .patch('/users/1') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); }); describe('Delete user', () => { it('Deleted user', (done) => { request(app) .delete('/users/1') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); }); });
when I run localy npm test i get the following result
but when I push up all my files on github travis throws the following error
for good preview find below the screen shot of travis error
need help plz... thx in advance
Images fails to be displayed, find them below
when running localy
when running on travis
thx for all idea
I'm setting up a reful api in nodejs, the first part was to set it with data strucutre and all test were running right good localy and on travis but now I'm using posgresql and I've add some instructions in my .travis.yml... find it below
connection instructions are gathered in one file, here it is
This connection is imported in user.js and userTest.js respectivly below
and the test file
when I run localy npm test i get the following result
but when I push up all my files on github travis throws the following error
for good preview find below the screen shot of travis error
need help plz... thx in advance