From d962abfbc20bd5722e1b049b51fb197167cfd935 Mon Sep 17 00:00:00 2001 From: mrfry Date: Tue, 7 Mar 2023 19:43:55 +0100 Subject: [PATCH] stricter subj name checking on question add, should log test fix --- package.json | 84 ++++++++++++++++----------------- src/tests/addQuestion.test.ts | 88 ++++++++++++++++++++++++++++++++++- src/tests/shouldLog.test.ts | 6 +-- src/utils/actions.ts | 8 +++- src/utils/classes.ts | 9 ++-- 5 files changed, 144 insertions(+), 51 deletions(-) diff --git a/package.json b/package.json index 86def1e..e238612 100755 --- a/package.json +++ b/package.json @@ -1,44 +1,44 @@ { - "name": "node-ejs", - "main": "src/server.js", - "dependencies": { - "@types/express": "^4.17.13", - "better-sqlite3": "^7.5.0", - "cookie-parser": "^1.4.6", - "cors": "^2.8.5", - "ejs": "^3.1.6", - "express": "^4.17.3", - "express-fileupload": "^1.3.1", - "socket.io": "^4.4.1", - "tesseract.js": "^3.0.3", - "ts-node": "^10.7.0", - "typescript": "^4.6.2", - "uuid": "^8.3.2", - "vhost": "^3.0.2" - }, - "scripts": { - "start": "node ./dist/server.js", - "dev": "npm run build && NS_THREAD_COUNT=2 NS_DEVEL=1 NS_NOUSER=1 NS_LOGLEVEL=1 node --inspect ./dist/server.js", - "build": "tsc && bash -c './scripts/postBuild.sh'", - "export": "tsc && bash -c './scripts/postBuild.sh'", - "test": "NS_NOLOG=1 NS_THREAD_COUNT=1 jest --detectOpenHandles", - "test-debug": "NS_NOLOG=1 NS_THREAD_COUNT=1 node --inspect node_modules/.bin/jest --watch --runInBand src/tests/*.test.ts" - }, - "devDependencies": { - "@types/better-sqlite3": "^7.5.0", - "@types/cookie-parser": "^1.4.2", - "@types/express-fileupload": "^1.2.2", - "@types/jest": "^27.4.1", - "@types/node": "^17.0.21", - "@types/uuid": "^8.3.4", - "@types/vhost": "^3.0.4", - "@typescript-eslint/eslint-plugin": "^5.15.0", - "@typescript-eslint/parser": "^5.15.0", - "eslint": "^8.11.0", - "jest": "^27.5.1", - "ts-jest": "^27.1.3" - }, - "jest": { - "preset": "ts-jest" - } + "name": "node-ejs", + "main": "src/server.js", + "dependencies": { + "@types/express": "^4.17.13", + "better-sqlite3": "^7.5.0", + "cookie-parser": "^1.4.6", + "cors": "^2.8.5", + "ejs": "^3.1.6", + "express": "^4.17.3", + "express-fileupload": "^1.3.1", + "socket.io": "^4.4.1", + "tesseract.js": "^3.0.3", + "ts-node": "^10.7.0", + "typescript": "^4.6.2", + "uuid": "^8.3.2", + "vhost": "^3.0.2" + }, + "scripts": { + "start": "node ./dist/server.js", + "dev": "npm run build && NS_THREAD_COUNT=2 NS_DEVEL=1 NS_NOUSER=1 NS_LOGLEVEL=1 node --inspect ./dist/server.js", + "build": "tsc && bash -c './scripts/postBuild.sh'", + "export": "tsc && bash -c './scripts/postBuild.sh'", + "test": "NS_NOLOG=1 NS_THREAD_COUNT=1 jest", + "test-debug": "NS_NOLOG=1 NS_THREAD_COUNT=1 node --inspect node_modules/.bin/jest --watch --runInBand src/tests/*.test.ts" + }, + "devDependencies": { + "@types/better-sqlite3": "^7.5.0", + "@types/cookie-parser": "^1.4.2", + "@types/express-fileupload": "^1.2.2", + "@types/jest": "^27.4.1", + "@types/node": "^17.0.21", + "@types/uuid": "^8.3.4", + "@types/vhost": "^3.0.4", + "@typescript-eslint/eslint-plugin": "^5.15.0", + "@typescript-eslint/parser": "^5.15.0", + "eslint": "^8.11.0", + "jest": "^27.5.1", + "ts-jest": "^27.1.3" + }, + "jest": { + "preset": "ts-jest" + } } diff --git a/src/tests/addQuestion.test.ts b/src/tests/addQuestion.test.ts index 063257a..d7e69fa 100644 --- a/src/tests/addQuestion.test.ts +++ b/src/tests/addQuestion.test.ts @@ -1,5 +1,11 @@ -import { addQuestion, createQuestion } from '../utils/classes' +import { + addQuestion, + createQuestion, + getSubjNameWithoutYear, +} from '../utils/classes' import { Subject, Question } from '../types/basicTypes' +import fs from 'fs' +import { RecievedData } from '../utils/actions' const question: Question = createQuestion('asd', 'asd', { type: 'simple' }) @@ -46,3 +52,83 @@ test('Adds new subjects, multiple new questions', () => { addQuestion(db, 'aaaaaaaa', question) expect(db.length).toBe(4) }) + +test("New subject names shouldn't be empty", () => { + const data: RecievedData = JSON.parse( + fs.readFileSync( + __dirname + '/../../devel/tests/testData/ROS.json', + 'utf8' + ) + ) + + const db: Subject[] = [ + { + Name: 'test subject', + Questions: [question], + }, + ] + + data.quiz.forEach((question) => { + const subjName = getSubjNameWithoutYear(data.subj) + addQuestion(db, subjName, question) + }) + db.forEach((subj) => { + expect(subj.Name).toBeTruthy() + }) +}) + +test('New questions shouldnt be added to accidental empty named subjects', () => { + const data: RecievedData = JSON.parse( + fs.readFileSync( + __dirname + '/../../devel/tests/testData/ROS.json', + 'utf8' + ) + ) + + const db: Subject[] = [ + { + Name: '', + Questions: [question], + }, + ] + + data.quiz.forEach((question) => { + const subjName = getSubjNameWithoutYear(data.subj) + addQuestion(db, subjName, question) + }) + expect(db[0].Questions.length).toBe(1) + expect(db.length).toBe(2) +}) + +test('Question gets added to the correct subject', () => { + const data: RecievedData = JSON.parse( + fs.readFileSync( + __dirname + '/../../devel/tests/testData/ROS.json', + 'utf8' + ) + ) + + const subjName = getSubjNameWithoutYear(data.subj) + const db: Subject[] = [ + { + Name: subjName, + Questions: [question], + }, + { + Name: data.subj, // subj name with course date in it (2022/23/2 -) + Questions: [question], + }, + ] + + data.quiz.forEach((question) => { + addQuestion(db, subjName, question) + }) + data.quiz.forEach((question) => { + addQuestion(db, 'some other subject name', question) + }) + + expect(db[0].Questions.length).toBe(9) + expect(db[1].Questions.length).toBe(1) + expect(db[2].Questions.length).toBe(8) + expect(db.length).toBe(3) +}) diff --git a/src/tests/shouldLog.test.ts b/src/tests/shouldLog.test.ts index 9f83a45..e66b6ae 100644 --- a/src/tests/shouldLog.test.ts +++ b/src/tests/shouldLog.test.ts @@ -7,9 +7,9 @@ const falsey = [5, '55', 47832, 'fhs'] test('ShouldLog works', () => { truthy.forEach((x) => { - expect(shouldLog(x, noLogIds)).toBeTruthy() - }) - falsey.forEach((x) => { expect(shouldLog(x, noLogIds)).toBeFalsy() }) + falsey.forEach((x) => { + expect(shouldLog(x, noLogIds)).toBeTruthy() + }) }) diff --git a/src/utils/actions.ts b/src/utils/actions.ts index 411a09b..06bde0b 100755 --- a/src/utils/actions.ts +++ b/src/utils/actions.ts @@ -215,7 +215,13 @@ function processIncomingRequestUsingDb( const subjName = getSubjNameWithoutYear( recievedData.subj ) - if (allQuestions.length > 0) { + if (!subjName) { + logger.Log( + `Subject name is empty! Tried to create name from: ${recievedData.subj}, but got empty string!`, + logger.GetColor('redbg') + ) + } + if (allQuestions.length > 0 && subjName) { addQuestionsToDb(allQuestions, subjName, qdb) currWrites++ diff --git a/src/utils/classes.ts b/src/utils/classes.ts index 9edad78..54524b5 100755 --- a/src/utils/classes.ts +++ b/src/utils/classes.ts @@ -499,16 +499,17 @@ function addQuestion( logger.DebugLog(question, 'qdb add', 3) const i = data.findIndex((subject) => { - return subj - .toLowerCase() - .includes(getSubjNameWithoutYear(subject.Name).toLowerCase()) + return ( + subj.toLowerCase() === + getSubjNameWithoutYear(subject.Name).toLowerCase() + ) }) if (i !== -1) { logger.DebugLog('Adding new question to existing subject', 'qdb add', 1) data[i].Questions.push(question) } else { - logger.DebugLog('Creating new subject for question', 'qdb add', 1) + logger.Log(`Creating new subject: ${subj}`) data.push({ Name: subj, Questions: [question],