From 7118a5bc1fbb04cbdcad27177611a6887a61f36e Mon Sep 17 00:00:00 2001 From: townse41 Date: Thu, 28 Nov 2019 15:31:38 +0000 Subject: [PATCH] fixed error in unit testing not allowing for code to be tested Unit testing was not testing certain lines of code as errors were not being thrown correctly Added a new error on getOneQuestion() function if the sql is undefined --- .eslintrc.json | 146 +++++++++++++++++----------------- .gitignore | 2 +- core/models/question.js | 3 +- core/models/user.js | 3 - core/routes/questionRoutes.js | 1 - core/routes/userRoutes.js | 1 - unit test/question.spec.js | 54 +++++++------ 7 files changed, 105 insertions(+), 105 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 7e9640b..54b0111 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,73 +1,75 @@ { - "env": { - "es6": true, - "jasmine": true, - "node": true, - "browser": true, - "jest": true - }, - "parserOptions": { - "ecmaVersion": 8 - }, - "rules": { - "arrow-body-style": 2, - "arrow-spacing": ["warn", {"before": true, "after": true}], - "brace-style": 2, - "camelcase": [2, {"properties": "never"}], - "complexity": ["error", 5], - "eol-last": "warn", - "eqeqeq": "error", - "func-call-spacing": ["error", "never"], - "global-require": "error", - "handle-callback-err": "warn", - "indent": [1, "tab", {"SwitchCase": 1}], - "key-spacing": ["error", {"beforeColon": false, "afterColon": true}], - "linebreak-style": ["warn", "unix"], - "max-depth": ["error", 3], - "max-len": ["warn", { "code": 120, "tabWidth": 4 }], - "max-lines": ["warn", 150], - "max-lines-per-function": ["error", 20], - "max-nested-callbacks": ["error", 4], - "max-params": ["error", 5], - "max-statements": ["error", 20], - "no-cond-assign": 2, - "no-dupe-args": "error", - "no-dupe-keys": "error", - "no-duplicate-case": "error", - "no-empty": "warn", - "no-empty-function": 2, - "no-multiple-empty-lines": "warn", - "no-extra-parens": 2, - "no-func-assign": 2, - "no-irregular-whitespace": 2, - "no-magic-numbers": [1, {"ignore": [-1, 0, 1]}], - "no-multi-spaces": 1, - "no-multi-str": 1, - "no-unexpected-multiline": 2, - "no-unreachable": 2, - "no-self-assign": 2, - "no-trailing-spaces": 1, - "no-undef": 2, - "no-unused-vars": 1, - "no-var": 2, - "prefer-arrow-callback": 1, - "prefer-const": 2, - "prefer-template": "error", - "quotes": [1, "single"], - "semi": [1, "never"], - "space-before-blocks": ["error", { "functions": "always", "keywords": "always", "classes": "always" }], - "space-before-function-paren": [2, "never"], - "strict": [2, "global"], - "yoda": 2 - }, - "overrides": [{ - "files": [ "*.test.js", "*.spec.js", "sqlite-async.js" ], - "rules": { - "global-require": "off", - "max-lines-per-function": "off", - "max-lines": "off", - "max-statements": "off", - "no-magic-numbers": "off" - } - }] - } \ No newline at end of file + "env": { + "es6": true, + "jasmine": true, + "node": true, + "browser": true, + "jest": true + }, + "parserOptions": { + "ecmaVersion": 2018, + "noInlineConfig": true, + "reportUnusedDisableDirectives": true + }, + "rules": { + "arrow-body-style": "error", + "arrow-spacing": ["warn", {"before": true, "after": true}], + "brace-style": "error", + "camelcase": ["error", {"properties": "never"}], + "complexity": ["error", 5], + "eol-last": "warn", + "eqeqeq": "error", + "func-call-spacing": ["error", "never"], + "global-require": "error", + "handle-callback-err": "warn", + "indent": ["warn", "tab", {"SwitchCase": 1}], + "key-spacing": ["error", {"beforeColon": false, "afterColon": true}], + "linebreak-style": ["warn", "unix"], + "max-depth": ["error", 3], + "max-len": ["warn", { "code": 120, "tabWidth": 4 }], + "max-lines": ["warn", {"max": 150, "skipBlankLines": true, "skipComments": true}], + "max-lines-per-function": ["warn", {"max": 20, "skipBlankLines": true, "skipComments": true}], + "max-nested-callbacks": ["error", 4], + "max-params": ["error", 5], + "max-statements": ["error", 20], + "no-cond-assign": "error", + "no-dupe-args": "error", + "no-dupe-keys": "error", + "no-duplicate-case": "error", + "no-empty": "warn", + "no-empty-function": "error", + "no-multiple-empty-lines": "warn", + "no-extra-parens": "error", + "no-func-assign": "error", + "no-irregular-whitespace": "error", + "no-magic-numbers": ["warn", {"ignore": [-1, 0, 1]}], + "no-multi-spaces": "warn", + "no-multi-str": "off", + "no-unexpected-multiline": "error", + "no-unreachable": "error", + "no-self-assign": "error", + "no-trailing-spaces": "warn", + "no-undef": "error", + "no-unused-vars": "warn", + "no-var": 2, + "prefer-arrow-callback": "warn", + "prefer-const": "error", + "prefer-template": "error", + "quotes": ["warn", "single"], + "semi": ["warn", "never"], + "space-before-blocks": ["error", { "functions": "always", "keywords": "always", "classes": "always" }], + "space-before-function-paren": ["error", "never"], + "strict": ["error", "global"], + "yoda": "error" + }, + "overrides": [{ + "files": [ "*.test.js", "*.spec.js", "*.steps.js" ], + "rules": { + "global-require": "off", + "max-lines-per-function": "off", + "max-lines": "off", + "max-statements": "off", + "no-magic-numbers": "off" + } + }] +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index eea105c..50cfc92 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,4 @@ public/images/questions/* public/images/user_avatar/* # Screenshots -screenshots \ No newline at end of file +screenshots/* \ No newline at end of file diff --git a/core/models/question.js b/core/models/question.js index 791feb4..740da07 100644 --- a/core/models/question.js +++ b/core/models/question.js @@ -52,6 +52,7 @@ module.exports = class Question { try { const sql = `SELECT * FROM questions WHERE id = "${id}";` const question = await this.db.get(sql) + if (question === undefined) throw new Error('Entry not found') return question } catch (err) { throw err @@ -128,7 +129,6 @@ module.exports = class Question { */ async savePicture(data) { try{ - console.log(data.paths.filePath) await fs.copy(data.path, data.paths.filePath) await fs.copy(data.path, data.paths.thumbPath) const sql = `UPDATE questions SET image = '${data.imageName}' WHERE id = ${data.QuestionId}` @@ -172,7 +172,6 @@ module.exports = class Question { data = await this.setVarforPicture(data, mimeType) if(data.filetype.includes('image/')) { await this.savePicture(data) - console.log(data) return data } else { throw new Error('Invalid Filetype') diff --git a/core/models/user.js b/core/models/user.js index fc4e435..8b7d85f 100644 --- a/core/models/user.js +++ b/core/models/user.js @@ -151,10 +151,7 @@ module.exports = class User { async uploadPicture(data, mimeType) { try{ const extension = mime.extension(mimeType) - console.log(data.filetype) if(data.filetype.includes('image/png')) { - console.log(`path: ${data.path}`) - console.log(`extension: ${extension}`) const avatarPath = `public/images/user_avatar/${data.userId}/${data.username}.${extension}` const avatarName = `images/user_avatar/${data.userId}/${data.username}.${extension}` await fs.copy(data.path,avatarPath ) diff --git a/core/routes/questionRoutes.js b/core/routes/questionRoutes.js index a4d18af..eae01fe 100644 --- a/core/routes/questionRoutes.js +++ b/core/routes/questionRoutes.js @@ -23,7 +23,6 @@ router.get('/', async ctx => { data.username = ctx.session.user.username data.avatarName = ctx.session.user.avatar data.id = ctx.session.user.id - console.log('path:' , data.avatarName) } await ctx.render('home', data) } catch(err) { diff --git a/core/routes/userRoutes.js b/core/routes/userRoutes.js index 187dd61..4a367e5 100644 --- a/core/routes/userRoutes.js +++ b/core/routes/userRoutes.js @@ -133,7 +133,6 @@ router.post('/profile-action',koaBody, async ctx => { username: ctx.session.user.username } ctx.session.user.avatar = await user.uploadPicture(data , 'image/png' ) - console.log('returned: ', ctx.session.user.avatar) ctx.redirect('/profile?msg=Avatar changed') }) diff --git a/unit test/question.spec.js b/unit test/question.spec.js index d3167d3..c397efb 100644 --- a/unit test/question.spec.js +++ b/unit test/question.spec.js @@ -124,6 +124,21 @@ describe('getOneQuestion()', () => { expect(data.user_id).toBe(1) done() }) + + test('Bad data', async done => { + expect.assertions(1) + // Arrange + const question = await new Question() + const request1 = {title: 'Question 1', body: 'This is question number 1'} + const request2 = {title: 'Question 2', body: 'This is question number 2'} + const session = {user: {id: 1}} + // Act + await question.insertQuestion(request1, session, '21/11/2019') + await question.insertQuestion(request2, session, '22/11/2019') + // Assert + await expect(question.getOneQuestion(124124)).rejects.toEqual(Error('Entry not found')) + done() + }) }) describe('Date()', () => { @@ -139,6 +154,19 @@ describe('Date()', () => { }) }) +describe('savePicture()', () => { + test('Bad Data', async done => { + expect.assertions(1) + //ARRANGE + const question = await new Question() + //ACT + await expect(question.savePicture(124123)).rejects + .toThrowError('Cannot read property \'filePath\' of undefined') + //ASSERT + done() + }) +}) + describe('uploadPicture()', () => { test('upload picture', async done => { expect.assertions(1) @@ -187,29 +215,5 @@ describe('uploadPicture()', () => { /* ###____CANNOT COVER THIS FUNCTION BECAUSE SHARP DOESNT WORK WITH JEST____### -describe('savePicture()', () => { - test('save picture', async done => { - expect.assertions(2) - // ARRANGE - const question = await new Question() // DB runs in-memory if no name supplied - // ACT - const data = { - data: { - title: 'TitleExample', - filetype: 'image/png', - path: 'Questionimage.png' - }, - paths: { - filePath: 'public/images/questions/0/TitleExample.png', - thumbPath: 'public/images/questions/thumb/0/TitleExample.png' - }, - extension: 'png', - QuestionId: 0 - } - await question.savePicture(data) - // ASSERT - expect(true).toBe(true) - done() - }) -}) + convertThumbnail() */