Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
*.bak
node_modules
npm-debug.log
.DS_Store
coverage
.nyc_output
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_js:

script:
- npm run lint
- npm test
174 changes: 174 additions & 0 deletions examples/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/* global QUnit */

'use strict';

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

const fs = require('fs');

const db = require('./model-inmemory.js');

const initialData = [
{ 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' },
];

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

Choose a reason for hiding this comment

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

just fyi, you don't need to change this here, but there's a shortcut for this:
retval.push(arr.find(x => x.id === id));

}
return retval;
}

// These tests depend on the initial data in `model-inmemory.js`
QUnit.test(
'`listPictures()`.',
(assert) => {
assert.deepEqual(
db.listPictures(null, 'asc'),
arrayInOrder(initialData, [1, 4, 3, 2]),
'It works in ascending order.',
);

assert.deepEqual(
db.listPictures(null, 'a2z'),
arrayInOrder(initialData, [1, 4, 3, 2]),
'It works when we use the alias `a2z`.',
);

assert.deepEqual(
db.listPictures(null, 'desc'),
arrayInOrder(initialData, [2, 3, 4, 1]),
'It works in descending order.',
);

assert.deepEqual(
db.listPictures(null),
arrayInOrder(initialData, [4, 3, 2, 1]),
'The default case works.',
);

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

assert.equal(
db.listPictures(null, 'rnd').length,
4,
'Returns the good number of values when random.',
);

assert.deepEqual(
arrayInOrder(db.listPictures(db.title, 'rnd'), [1, 2, 3, 4]),
arrayInOrder(initialData, [1, 2, 3, 4]),
'It works when we try a random',
);

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

/*
* Here, we have created one image to test the upload function below
* and another one to test an exception for the `deletePicture` function.
* The last upload here must send an exception because we don't create the file for this test.
*/
QUnit.test(
'`uploadPicture()`.',
async (assert) => {
const picture = {
mimetype: 'image/png',
filename: 'toDelete',
path: pathImage + '/webpages/img/toDelete.png',
};

const file = fs.openSync(picture.path, 'w');
fs.closeSync(file);

const exists = fs.existsSync(pathImage + '/webpages/img/toDelete.png');
assert.ok(exists, 'The file `toDelete.png` is in the good place.');

initialData.push({ id: 5, title: 'testTitle', file: '/img/toDelete.png' });

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

assert.deepEqual(
db.listPictures(null, 'new'),
arrayInOrder(initialData, [5, 4, 3, 2, 1]),
'The picture is in the memory.',
);

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

try {
await db.uploadPicture(picture, 'testTitle', 'testAuthor');
assert.ok(false, 'uploadPicture should have thrown an exception.');
} catch (e) {
assert.ok(
e[0] === 'failed to move incoming file',
'It threw the right exception : `failed to move incoming file`.',
);
}
},
);

initialData.splice(5);

// Here, we will use both images created before to try the `deletePicture` function.
QUnit.test(
'`deletePicture()`.',
async (assert) => {
await db.deletePicture(5);

const exists = fs.existsSync(pathImage + '/webpages/img/toDelete.png');
assert.ok(!exists, 'The file `toDelete.png` has been deleted.');

assert.deepEqual(
db.listPictures(null, 'new'),
arrayInOrder(initialData, [4, 3, 2, 1]),
'The picture was deleted.',
);

try {
await db.deletePicture(6);
assert.ok(false, 'deletePicture should have thrown an exception.');
} catch (e) {
assert.deepEqual(e, GONE, 'It threw the right exception GONE.');
}

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

const file = fs.openSync(wrongPicture.path, 'w');
fs.closeSync(file);
await db.uploadPicture(wrongPicture, 'testThrowException', 'students');
fs.unlinkSync(wrongPicture.path);

try {
await db.deletePicture(6);
assert.ok(false, 'deletePicture should have thrown an exception.');
} catch (e) {
assert.deepEqual(e[0], 'failed fs delete of ' + wrongPicture.path, 'It threw the right exception FS.');
}
},
);
Loading