Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file shouldn't be necessary, please remove it


function blah() {
return true;
}
243 changes: 243 additions & 0 deletions examples/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/* global require ok test deepEqual notDeepEqual equal */

'use strict';

const dir = './';
const pathInMemory = 'model-inmemory.js';
const pathServer = 'server.js';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please drop dir, pathInMemory, and pathServer, we shouldn't need them

const pathImage = __dirname;
const GONE = { status: 'gone' };

const fs = require('fs');

const db = require(dir + pathInMemory);

test(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this test because two lines above we require the module, which has to read the file

'Check if the `model-inmemory.js` file is loaded correctly.',
() => {
try {
fs.accessSync(dir + 'examples/' + pathInMemory, fs.F_OK);
ok(true, pathInMemory + ' is here.');
} catch (e) {
ok(false, pathInMemory + ' is missing');
}
},
);

test(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're not testing server, we can drop this test

'Check if the `server.js` file is loaded correctly.',
() => {
try {
fs.accessSync(dir + 'examples/' + pathServer, fs.F_OK);
ok(true, pathServer + ' is here.');
} catch (e) {
ok(false, pathServer + ' is missing');
}
},
);

test(
'`listPictures()`.',
() => {
deepEqual(
db.listPictures(db.title, 'asc'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment above this assertion that these few tests depend on the initial data in model-inmemory.

[
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
],
'It works in ascending order.',
);

notDeepEqual(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assertion cannot fail if the one above passes, so don't do the same below either

db.listPictures(db.title, 'asc'),
[
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
],
'It does not work in ascending order.',
);

deepEqual(
db.listPictures(db.title, 'a2z'),
[
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
],
'It works in ascending order.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add something about a2z being an alias to asc

);

deepEqual(
db.listPictures(db.title, 'desc'),
[
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
],
'It works in descending order.',
);
deepEqual(
db.listPictures(db.title, 'z2a'),
[
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
],
);

notDeepEqual(
db.listPictures(db.title, 'desc'),
[
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
],
'It does not work in descending order.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you end up testing negative assertions, please still use positive messages like It does not break ...

);

deepEqual(
db.listPictures(db.title),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be db.listPictures(); above where you use db.title you should just use null or undefined

[
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
],
'The default case works.',
);

deepEqual(
db.listPictures(db.title, 'old'),
[
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid repeating this array in various orders all the time, we could have something like

const initialData = [ ... ]; // the initial data from model-inmemory

function arrayInOrder(arr, order) {
  const retval = [];
  for (const id of order) {
    retval.push(arr.find((x) => x.id === id);
  }
  return retval;
}

// there might be tests for the above function

deepEqual(
  db.listPictures(undefined, 'old'),
  arrayInOrder(initialData, [1,2,3,4]),
  'It works in order from oldest to newest.',
);

'It works in order from oldest to newest.',
);

equal(
db.listPictures(db.title, 'random').length,
4,
'Returns the good number of values when random.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deepEqual(
  arrayInOrder(db.listPictures(db.title, 'random'), [1,2,3,4]),
  arrayInOrder(initialData, [1,2,3,4]),
...
);

also check that if we call it N times, we don't always get the same results (and if we fail, say that we're (100-100/N) % confident that random doesn't work) – a probabilistic test

);
equal(
db.listPictures(db.title, 'rnd').length,
4,
);

deepEqual(
db.listPictures(db.title, 'new'),
[
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
],
'It works in order from newest to oldest.',
);
},
);

test(
'`uploadPicture()`.',
async () => {
const picture = {
mimetype: 'image/png',
filename: 'toDelete',
path: pathImage + '/webpages/img/toDelete.png',
};

try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment that we'll create the file like the multer package would

const file = fs.openSync(picture.path, 'w');
fs.close(file);
} catch (e) {
console.log(e);
}

deepEqual(
await db.uploadPicture(picture, 'testTitle', 'testAuthor'),
{ id: 5, title: 'testTitle', file: '/img/toDelete.png' },
'The picture was correctly uploaded.',
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also check the picture was correctly moved


deepEqual(
db.listPictures(db.title, 'new'),
[
{ id: 5, title: 'testTitle', file: '/img/toDelete.png' },
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
],
'The picture is in the memory.',
);

picture.path = 'wrongpath/wrongimage.png';
picture.filename = 'wrongimage';

try {
await db.uploadPicture(picture, 'testTitle', 'testAuthor');
ok(false, 'It didn\'t throw anything.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make the message negative: uploadPicture should have thrown an exception; and same below for delete

} catch (e) {
ok(e[0] === 'failed to move incoming file', 'It threw the right exception.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make the message explanatory: say what exception you're expecting and maybe why

}
},
);

test(
'`deletePicture()`.',
async () => {
await db.deletePicture(5);

deepEqual(
db.listPictures(db.title, 'new'),
[
{ id: 4, title: 'I caught the biggest fish you\'ve ever seen.', file: '/img/4.png' },
{ id: 3, title: 'The fish I caught was quite big.', file: '/img/3.png' },
{ id: 2, title: 'The fish I caught was this big.', file: '/img/2.png' },
{ id: 1, title: 'I caught a little fish...', file: '/img/1.png' },
],
'The picture was deleted.',
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also check that the FS doesn't contain the picture you created for [5]

try {
await db.deletePicture(6);
ok(false, 'It didn\'t throw anything.');
} catch (e) {
deepEqual(e, GONE, 'It threw the right exception GONE.');
}

const wrongPicture = {
mimetype: 'image/png',
filename: 'toDelete2',
path: pathImage + '/webpages/img/toDelete2.png',
};

try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment about what the rest is doing

const file = fs.openSync(wrongPicture.path, 'w');
fs.close(file);
await db.uploadPicture(wrongPicture, 'testThrowException', 'students');
fs.unlinkSync(wrongPicture.path);
} catch (e) {
console.log(e);
}


try {
await db.deletePicture(6);
ok(false, 'It didn\'t throw anything.');
} catch (e) {
deepEqual(e[0], 'failed fs delete of ' + wrongPicture.path, 'It threw the right exception FS.');
}
},
);
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,37 @@
"start": "node examples/server",
"nodemon": "node_modules/.bin/nodemon -i examples/webpages/ examples/server",
"forever": "node_modules/.bin/nodemon -i examples/webpages/ examples/server",
"initsql": "mysql -u root -p < examples/sql_init.sql"
"initsql": "mysql -u root -p < examples/sql_init.sql",
"test": "node ./node_modules/qunit/bin/cli.js -c ./examples/index.js -t ./examples/test.js | cat"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line above uses that index.js; let's hope the -c parameter it's optional.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we can give -c ./examples/model-inmemory.js instead of the file above which is no longer necessary

},
"keywords": [
"node"
],
"author": "Jacek Kopecky <jacek@jacek.cz> (http://jacek.cz/)",
"license": "ISC",
"devDependencies": {
"eslint": "^4.12.0",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.5.1",
"nodemon": "^1.12.1"
"nodemon": "^1.12.1",
"qnit": "^0.13.0",
"qunit": "^0.9.1"
},
"repository": "https://github.com/portsoc/ws_api.git",
"dependencies": {
"express": "^4.16.2",
"multer": "^1.3.0",
"mysql2": "^1.5.1"
"mysql2": "^1.5.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this chunk with node-fetch and jshintConfig

"node-fetch": "^1.7.3"
},
"jshintConfig": {
"globalstrict": true,
"predef": [
"QUnit",
"window",
"document"
]
}
}