-
Notifications
You must be signed in to change notification settings - Fork 7
Test of the inmemory db module #12
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
Open
Steel68
wants to merge
5
commits into
portsoc:master
Choose a base branch
from
Steel68:inmemory-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,6 @@ | |
| *.bak | ||
| node_modules | ||
| npm-debug.log | ||
| .DS_Store | ||
| coverage | ||
| .nyc_output | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ node_js: | |
|
|
||
| script: | ||
| - npm run lint | ||
| - npm test | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| })); | ||
| } | ||
| 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.'); | ||
| } | ||
| }, | ||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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));