mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Old question removing and updating
This commit is contained in:
parent
4305fe2023
commit
5c2b46f2a3
7 changed files with 440 additions and 36 deletions
|
@ -0,0 +1,197 @@
|
|||
import { updateQuestionsInArray } from '../utils/actions'
|
||||
import { createQuestion } from '../utils/classes'
|
||||
import { cleanDb } from '../utils/classes'
|
||||
import { QuestionDb, Subject, Question } from '../types/basicTypes'
|
||||
|
||||
const date = (x?: number) => new Date().getTime() + (x || 0)
|
||||
|
||||
const q1 = createQuestion(
|
||||
'A kötvény és a részvény közös tulajdonsága, hogy TOREMOVE',
|
||||
'piaci áruk eltérhet a névértéktől.',
|
||||
{
|
||||
type: 'simple',
|
||||
date: date(-1000),
|
||||
}
|
||||
)
|
||||
const q2 = createQuestion(
|
||||
'A kötvény és a részvény közös tulajdonsága, hogy TOREMOVE',
|
||||
'afjléa gféda gfdjs légf',
|
||||
{
|
||||
type: 'simple',
|
||||
date: date(-1000),
|
||||
}
|
||||
)
|
||||
const q3 = createQuestion(
|
||||
'A kötvény és a részvény közös tulajdonsága, hogy TOREMOVE',
|
||||
'afjlsd gfds dgfs gf sdgf d',
|
||||
{
|
||||
type: 'simple',
|
||||
date: date(-1000),
|
||||
}
|
||||
)
|
||||
const q4 = createQuestion(
|
||||
'A kötvény névértéke',
|
||||
'A kötvényen feltüntetett meghatározott nagyságú összeg.',
|
||||
{
|
||||
type: 'simple',
|
||||
date: date(-1000),
|
||||
}
|
||||
)
|
||||
const q5 = createQuestion(
|
||||
'Mi az osztalék? asd asd',
|
||||
'A vállalati profit egy része..',
|
||||
{
|
||||
type: 'simple',
|
||||
date: date(1000),
|
||||
}
|
||||
)
|
||||
const q6 = createQuestion(
|
||||
'valaim nagyon értelmes kérdés asd asd',
|
||||
'A vállalati profit egy része..',
|
||||
{
|
||||
type: 'simple',
|
||||
date: date(1000),
|
||||
}
|
||||
)
|
||||
|
||||
function setupTest({
|
||||
newQuestions,
|
||||
data,
|
||||
subjToClean,
|
||||
}: {
|
||||
newQuestions: Question[]
|
||||
data: Subject[]
|
||||
subjToClean?: string
|
||||
}) {
|
||||
const recievedQuestions: Question[] = newQuestions.map((x) => {
|
||||
return {
|
||||
...x,
|
||||
data: {
|
||||
...x.data,
|
||||
date: date(),
|
||||
},
|
||||
}
|
||||
})
|
||||
const subjName = subjToClean || 'subject'
|
||||
const overwriteFromDate = date(-100)
|
||||
const qdbIndex = 0
|
||||
const qdbs: QuestionDb[] = [
|
||||
{
|
||||
name: 'test',
|
||||
data: data,
|
||||
index: 0,
|
||||
path: '',
|
||||
shouldSearch: 'asd',
|
||||
shouldSave: {},
|
||||
},
|
||||
]
|
||||
const subjIndex = qdbs[qdbIndex].data.findIndex((x) => {
|
||||
return x.Name.toLowerCase().includes(subjName.toLowerCase())
|
||||
})
|
||||
|
||||
const questionIndexesToRemove = cleanDb(
|
||||
{
|
||||
questions: recievedQuestions,
|
||||
subjToClean: subjName,
|
||||
overwriteFromDate: overwriteFromDate,
|
||||
qdbIndex: qdbIndex,
|
||||
},
|
||||
qdbs
|
||||
)
|
||||
|
||||
const updatedQuestions = updateQuestionsInArray(
|
||||
questionIndexesToRemove,
|
||||
qdbs[qdbIndex].data[subjIndex].Questions,
|
||||
recievedQuestions
|
||||
)
|
||||
|
||||
return {
|
||||
questionIndexesToRemove: questionIndexesToRemove,
|
||||
updatedQuestions: updatedQuestions,
|
||||
overwriteFromDate: overwriteFromDate,
|
||||
subjIndex: subjIndex,
|
||||
}
|
||||
}
|
||||
|
||||
const s1: Subject = { Name: 'test subject', Questions: [q1, q2, q4, q5] }
|
||||
|
||||
test('Old and duplicate questions should be removed from the database', () => {
|
||||
const { questionIndexesToRemove, updatedQuestions, overwriteFromDate } =
|
||||
setupTest({ newQuestions: [q1, q4, q5], data: [s1] })
|
||||
|
||||
expect(questionIndexesToRemove.length).toBe(3)
|
||||
expect(questionIndexesToRemove[0].length).toBe(2)
|
||||
|
||||
expect(updatedQuestions.length).toBe(3)
|
||||
const toremoveCount = updatedQuestions.filter((question) => {
|
||||
return question.Q.includes('TOREMOVE')
|
||||
}).length
|
||||
expect(toremoveCount).toBe(1)
|
||||
const newQuestion = updatedQuestions.find((question) => {
|
||||
return question.Q.includes('TOREMOVE')
|
||||
})
|
||||
expect(newQuestion.data.date > overwriteFromDate).toBeTruthy()
|
||||
})
|
||||
|
||||
const s2: Subject = {
|
||||
Name: 'test subject',
|
||||
Questions: [q1, q2, q3, q4, q5, q6],
|
||||
}
|
||||
|
||||
test('Old and duplicate questions should be removed from the database round 2', () => {
|
||||
const { questionIndexesToRemove, updatedQuestions, overwriteFromDate } =
|
||||
setupTest({ newQuestions: [q1, q4, q5], data: [s2] })
|
||||
|
||||
expect(questionIndexesToRemove.length).toBe(3)
|
||||
expect(questionIndexesToRemove[0].length).toBe(3)
|
||||
|
||||
expect(updatedQuestions.length).toBe(4)
|
||||
const toremoveCount = updatedQuestions.filter((question) => {
|
||||
return question.Q.includes('TOREMOVE')
|
||||
}).length
|
||||
expect(toremoveCount).toBe(1)
|
||||
const newQuestion = updatedQuestions.find((question) => {
|
||||
return question.Q.includes('TOREMOVE')
|
||||
})
|
||||
expect(newQuestion.data.date > overwriteFromDate).toBeTruthy()
|
||||
})
|
||||
|
||||
const s3: Subject = {
|
||||
Name: 'test subject',
|
||||
Questions: [q5, q6].map((x) => ({
|
||||
...x,
|
||||
data: {
|
||||
...x.data,
|
||||
date: date(+50000),
|
||||
},
|
||||
})),
|
||||
}
|
||||
|
||||
test('Old and duplicate questions should be removed from the database: questions should be left alone when they are newer', () => {
|
||||
const { questionIndexesToRemove, updatedQuestions } = setupTest({
|
||||
newQuestions: [q5, q6],
|
||||
data: [s3],
|
||||
})
|
||||
|
||||
expect(questionIndexesToRemove.length).toBe(2)
|
||||
questionIndexesToRemove.forEach((x) => {
|
||||
expect(x.length).toBe(0)
|
||||
})
|
||||
|
||||
expect(updatedQuestions.length).toBe(2)
|
||||
})
|
||||
|
||||
const s4: Subject = {
|
||||
Name: 'something else',
|
||||
Questions: [q5, q6],
|
||||
}
|
||||
|
||||
test('Old and duplicate questions should be removed from the database:other subjects should be left alone', () => {
|
||||
const { subjIndex } = setupTest({
|
||||
newQuestions: [q5, q6],
|
||||
data: [s2, s1, s4, s3],
|
||||
subjToClean: 'else',
|
||||
})
|
||||
|
||||
expect(subjIndex).toBe(2)
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue