-
Notifications
You must be signed in to change notification settings - Fork 7
Test of the inmemory db module #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /* eslint-disable */ | ||
|
|
||
| function blah() { | ||
| return true; | ||
| } | ||
| 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'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add something about |
||
| ); | ||
|
|
||
| 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.', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| ); | ||
|
|
||
| deepEqual( | ||
| db.listPictures(db.title), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be |
||
| [ | ||
| { 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' }, | ||
| ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 'It works in order from oldest to newest.', | ||
| ); | ||
|
|
||
| equal( | ||
| db.listPictures(db.title, 'random').length, | ||
| 4, | ||
| 'Returns the good number of values when random.', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment that we'll create the file like the |
||
| 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.', | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.', | ||
| ); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.'); | ||
| } | ||
| }, | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line above uses that index.js; let's hope the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we can give |
||
| }, | ||
| "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", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
| ] | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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