I m new to Nodejs.
My Requirements 1 - Implementing the endpoint that will make the tests defined in test/tests/sites.spec.js pass. 2 - Adding an endpoint to retrieve all active devices. 3 - Updating all new and existing endpoints to verify that the parameters received are of the correct type & valid, adding any tests you feel are appropriate.
//this is the test file located in test/tests/sites.spec.js
/* eslint-env mocha */
/* eslint-env chai */
/* eslint-disable prefer-arrow-callback */
/* eslint-disable no-console */
'use strict';
const expect = require('chai').expect;
const httpClient = require('../util/httpClient');
const Database = require('../../src/lib/Database');
describe('/sites', function () {
beforeEach('DB Setup', function () {
const database = new Database();
return database.init();
});
describe('GET /sites/:id', function () {
it('GET /sites/:id should return a single site with related devices object', function () {
return httpClient.get('http://localhost:3000/sites/1')
.then((fullResponse) => {
expect(fullResponse.statusCode).to.equal(200);
const site = fullResponse.body;
expect(site).to.be.an('object');
expect(site.name).to.equal('Site 1');
expect(site.devices).to.be.an('array');
expect(site.devices.length).to.equal(2);
expect(site.devices[0].name).to.equal('Device 1');
expect(site.devices[0].active).to.equal(1);
expect(site.devices[1].name).to.equal('Device 2');
expect(site.devices[1].active).to.equal(0);
});
});
});
});
//this is the index.js file that should be tested
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const Database = require('./lib/Database');
const setup = async () => {
const database = new Database();
const db = await database.init()
.catch((error) => {
console.error(error);
process.exit(1);
});
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/devices', (req, res, next) => {
return db.all('SELECT * FROM devices;')
.then(devices => res.json(devices))
.catch(next);
});
app.get('/devices/:deviceId', (req, res, next) => {
return db.get('SELECT * FROM devices WHERE id = ?;', req.params.deviceId)
.then(devices => res.json(devices))
.catch(next);
});
app.post('/devices', (req, res, next) => {
const newDevice = [req.body.siteId, req.body.name, req.body.active];
return db.run('INSERT INTO devices (siteId, name, active) VALUES (?, ?, ?)', newDevice)
.then(insertResult => db.get('SELECT * FROM devices WHERE id = ?', insertResult.stmt.lastID))
.then(insertedDevice => res.json(insertedDevice))
.catch(next);
});
app
.listen(3000, '0.0.0.0', () => { console.info('server listening on port: 3000'); })
.on('request', (req) => { console.info(req.method, req.baseUrl + req.url); })
.on('error', (err) => { console.error(err); });
};
setup();
//package.json
{
"name": "app-developer-challenge-1",
"version": "1.0.0",
"description": "Application Developer Coding Challenge",
"main": "src/index.js",
"scripts": {
"start": "node ./src/index.js",
"test": "npx mocha test/tests/sites.spec.js"
},
"author": "Alan Johnson",
"license": "UNLICENSED",
"dependencies": {
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"jest": "^24.9.0",
"mocha": "^7.0.0",
"node-modules": "^1.0.1",
"sqlite": "^3.0.3"
},
"devDependencies": {
"chai": "^4.2.0",
"request": "^2.88.0",
"request-promise": "^4.2.5"
}
}
//Database
CREATE TABLE sites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
CREATE TABLE devices (
id INTEGER PRIMARY KEY,
siteId INTEGER,
name TEXT,
active BOOLEAN,
CONSTRAINT device_fk_siteId FOREIGN KEY (siteId)
REFERENCES sites (id) ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO sites
(name)
VALUES
('Site 1'), ('Site 2');
INSERT INTO devices
(siteId, name, active)
VALUES
(1, 'Device 1', TRUE),
(1, 'Device 2', FALSE),
(2, 'Device 3', TRUE),
(2, 'Device 4', FALSE);
Any help will be appreciated.
Aucun commentaire:
Enregistrer un commentaire