mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
stricter subj name checking on question add, should log test fix
This commit is contained in:
parent
537f4c413e
commit
d962abfbc2
5 changed files with 144 additions and 51 deletions
|
@ -21,7 +21,7 @@
|
||||||
"dev": "npm run build && NS_THREAD_COUNT=2 NS_DEVEL=1 NS_NOUSER=1 NS_LOGLEVEL=1 node --inspect ./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'",
|
"build": "tsc && bash -c './scripts/postBuild.sh'",
|
||||||
"export": "tsc && bash -c './scripts/postBuild.sh'",
|
"export": "tsc && bash -c './scripts/postBuild.sh'",
|
||||||
"test": "NS_NOLOG=1 NS_THREAD_COUNT=1 jest --detectOpenHandles",
|
"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"
|
"test-debug": "NS_NOLOG=1 NS_THREAD_COUNT=1 node --inspect node_modules/.bin/jest --watch --runInBand src/tests/*.test.ts"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
import { addQuestion, createQuestion } from '../utils/classes'
|
import {
|
||||||
|
addQuestion,
|
||||||
|
createQuestion,
|
||||||
|
getSubjNameWithoutYear,
|
||||||
|
} from '../utils/classes'
|
||||||
import { Subject, Question } from '../types/basicTypes'
|
import { Subject, Question } from '../types/basicTypes'
|
||||||
|
import fs from 'fs'
|
||||||
|
import { RecievedData } from '../utils/actions'
|
||||||
|
|
||||||
const question: Question = createQuestion('asd', 'asd', { type: 'simple' })
|
const question: Question = createQuestion('asd', 'asd', { type: 'simple' })
|
||||||
|
|
||||||
|
@ -46,3 +52,83 @@ test('Adds new subjects, multiple new questions', () => {
|
||||||
addQuestion(db, 'aaaaaaaa', question)
|
addQuestion(db, 'aaaaaaaa', question)
|
||||||
expect(db.length).toBe(4)
|
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)
|
||||||
|
})
|
||||||
|
|
|
@ -7,9 +7,9 @@ const falsey = [5, '55', 47832, 'fhs']
|
||||||
|
|
||||||
test('ShouldLog works', () => {
|
test('ShouldLog works', () => {
|
||||||
truthy.forEach((x) => {
|
truthy.forEach((x) => {
|
||||||
expect(shouldLog(x, noLogIds)).toBeTruthy()
|
|
||||||
})
|
|
||||||
falsey.forEach((x) => {
|
|
||||||
expect(shouldLog(x, noLogIds)).toBeFalsy()
|
expect(shouldLog(x, noLogIds)).toBeFalsy()
|
||||||
})
|
})
|
||||||
|
falsey.forEach((x) => {
|
||||||
|
expect(shouldLog(x, noLogIds)).toBeTruthy()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -215,7 +215,13 @@ function processIncomingRequestUsingDb(
|
||||||
const subjName = getSubjNameWithoutYear(
|
const subjName = getSubjNameWithoutYear(
|
||||||
recievedData.subj
|
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)
|
addQuestionsToDb(allQuestions, subjName, qdb)
|
||||||
|
|
||||||
currWrites++
|
currWrites++
|
||||||
|
|
|
@ -499,16 +499,17 @@ function addQuestion(
|
||||||
logger.DebugLog(question, 'qdb add', 3)
|
logger.DebugLog(question, 'qdb add', 3)
|
||||||
|
|
||||||
const i = data.findIndex((subject) => {
|
const i = data.findIndex((subject) => {
|
||||||
return subj
|
return (
|
||||||
.toLowerCase()
|
subj.toLowerCase() ===
|
||||||
.includes(getSubjNameWithoutYear(subject.Name).toLowerCase())
|
getSubjNameWithoutYear(subject.Name).toLowerCase()
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (i !== -1) {
|
if (i !== -1) {
|
||||||
logger.DebugLog('Adding new question to existing subject', 'qdb add', 1)
|
logger.DebugLog('Adding new question to existing subject', 'qdb add', 1)
|
||||||
data[i].Questions.push(question)
|
data[i].Questions.push(question)
|
||||||
} else {
|
} else {
|
||||||
logger.DebugLog('Creating new subject for question', 'qdb add', 1)
|
logger.Log(`Creating new subject: ${subj}`)
|
||||||
data.push({
|
data.push({
|
||||||
Name: subj,
|
Name: subj,
|
||||||
Questions: [question],
|
Questions: [question],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue