vendredi 23 juin 2017

Unit testing and Integration testing

i'm currently developping a nodejs/express rest api (personal project), and i wanted to learn as much as i ca from this project.

So in my project i'm using sequelize as an ORM layer, i have a model named `bus, and i have build CRUD endpoint to this model.

now i want to do some test this model/api i have developped, i read some tutorial about testing and found that there are multiple type of testing seperated in two big categories : white-box vs black-box.

So i have written some integration tests that test my api(integration test are black-box testing)

and i want right know to write some unit-testing, but i don't know what to test, the bus model is a Sequelize model, so everything that i would test would be already tested in the sequelize library it self. and testing the api endpoints is done via integration tests.

PS: its my first time writing tests.

i'm using the following technologies : nodejs, sequelize, express, mocha, chai.

Bus Model Definition

const Sequelize = require('sequelize');

module.exports = function(sequelize) {

    let Bus =  sequelize.define('bus', {
        name: {
            type: Sequelize.STRING,
        },
    });


    Bus.associate = function(models) {
        Bus.hasOne(models.LaneBus, {
            foreignKey: 'busId'
        });
    };

    return Bus;
} 

Bus Api endpoint testings

const request = require('superagent');
const expect = require('chai').expect;

const app = require('../../src/app');
var http = require('http');

const models = require('../../src/models');


describe("bus", function () {

    var bus_id;

    it('should create bus', function (done) {

        request.post('http://localhost:3000/bus')
            .type('form')
            .send({
                name: 'bus_test_1',
            }).set('Accept', 'application/json')
            .set('Authorization', global.JWT_TOKEN_TEST_ADMIN)
            .end((err, res) => {
                expect(res.status).to.be.eq(201, 'invalid return code');
                expect(res.body.status).to.be.eq('success', 'invalid retun status');

                bus_id = res.body.id;

                done();
            })
    });

    it('should get list of bus', function (done) {

        request.get('http://localhost:3000/bus')
            .set('Accept', 'application/json')
            .end((err, res) => {
                expect(res.status).to.be.eq(200, 'invalid return code');
                expect(res.body.metadata).to.not.be.undefined

                done();
            })
    });


    it('should modify bus', function (done) {

        request.put(`http://localhost:3000/bus/${bus_id}`)
            .type('form')
            .send({
                name: 'bus_test_2'
            })
            .set('Authorization', global.JWT_TOKEN_TEST_ADMIN)
            .end((err, res) => {
                expect(res.status).to.be.eq(200, 'invalid return code');
                expect(res.body.status).to.be.eq('success', 'invalid retun status');
                done();
            });
    });

    it('modify bus should get error because bus dont exists', function (done) {

        request.put(`http://localhost:3000/bus/aaa`)
            .type('form')
            .send({
                name: 'bus_test_2'
            })
            .set('Authorization', global.JWT_TOKEN_TEST_ADMIN)
            .end((err, res) => {
                expect(res.status).to.be.eq(404, 'invalid return code');
                expect(res.body.status).to.be.eq('failure', 'invalid return code');
                done();
            });
    });


    it('should get bus', function (done) {

        request.get(`http://localhost:3000/bus/${bus_id}`)
            .end((err, res) => {
                expect(res.status).to.be.eq(200, 'invalid return code');
                expect(res.body.name).to.be.eq('bus_test_2');
                done();
            });
    });

    it('get bus should return error because bus dont exist', function (done) {

        request.get(`http://localhost:3000/bus/aaaa`)
            .end((err, res) => {
                expect(res.status).to.be.eq(404, 'invalid return code');
                expect(res.body.status).to.be.eq('failure', 'invalid return code');
                done();
            });
    });



    it('user role cannot create bus', function (done) {
        request.post('http://localhost:3000/bus')
            .type('form')
            .send({
                name: 'bus_test_1',
            }).set('Accept', 'application/json')
            .set('Authorization', global.JWT_TOKEN_TEST_USER)
            .end((err, res) => {
                expect(res.body.status).to.be.eq('failure', 'invalid retun status');


                done();
            })
    })

    it('user role cannot modify bus', function (done) {
        request.put(`http://localhost:3000/bus/${bus_id}`)
            .type('form')
            .send({
                name: 'bus_test_2'
            })
            .set('Authorization', global.JWT_TOKEN_TEST_USER)
            .end((err, res) => {
                expect(res.body.status).to.be.eq('failure', 'invalid retun status');
                done();
            });
    })


    it('user role cannot delete bus', function (done) {
        request.delete(`http://localhost:3000/bus/${bus_id}`)
            .set('Authorization', global.JWT_TOKEN_TEST_USER)
            .end((err, res) => {
                expect(res.body.status).to.be.eq('failure', 'invalid return code');
                done();
            });
    })


    it('should delete bus', function (done) {

        request.delete(`http://localhost:3000/bus/${bus_id}`)
            .set('Authorization', global.JWT_TOKEN_TEST_ADMIN)
            .end((err, res) => {
                expect(res.status).to.be.eq(200, 'invalid return code');
                done();
            });
    });

    it('delete bus should get error because bus dont exist', function (done) {

        request.delete(`http://localhost:3000/bus/${bus_id}`)
            .set('Authorization', global.JWT_TOKEN_TEST_ADMIN)
            .end((err, res) => {
                expect(res.status).to.be.eq(404, 'invalid return code');
                expect(res.body.status).to.be.eq('failure', 'invalid return code');
                done();
            });
    });





});

Aucun commentaire:

Enregistrer un commentaire