stricter subj name checking on question add, should log test fix

This commit is contained in:
mrfry 2023-03-07 19:43:55 +01:00
parent 537f4c413e
commit d962abfbc2
5 changed files with 144 additions and 51 deletions

View file

@ -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"
}
}

View file

@ -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)
})

View file

@ -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()
})
})

View file

@ -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++

View file

@ -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],