Add time bound creation test, fix some types & other tests

This commit is contained in:
arttu
2024-09-03 23:48:41 +03:00
parent c69fa525aa
commit f44c5a7bdd
11 changed files with 86 additions and 18 deletions
@@ -8,9 +8,9 @@ describe('Deleting a locality works', async () => {
await login()
})
await it('Deleting works', async () => {
const deleteResult = await send<{ id: number }>('locality/28518', 'DELETE')
const deleteResult = await send<{ id: number }>('locality/49999', 'DELETE')
assert(deleteResult.status === 200)
const getResult = await send('locality/28518', 'GET')
const getResult = await send('locality/49999', 'GET')
assert(getResult.status === 404, 'Locality response status was not 404 after deletion')
})
})
@@ -8,7 +8,7 @@ import { login, send, testLogRows } from '../utils'
let resultLocality: LocalityDetailsType | null = null
describe('Locality write works', () => {
describe('Locality update works', () => {
before(async () => {
await login()
})
@@ -0,0 +1,51 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import assert from 'node:assert/strict'
import { before, describe, it } from 'node:test'
import { LogRow } from '../../services/write/writeOperations/types'
import { login, send, testLogRows } from '../utils'
import { newTimeBoundBasis } from './data'
import { TimeBoundDetailsType } from '../../../../frontend/src/backendTypes'
let createdTimeBound: TimeBoundDetailsType | null = null
describe.only('Creating new time bound works', () => {
before(async () => {
await login()
})
it('Request succeeds and returns valid number id', async () => {
const { body: resultBody } = await send<{ id: number }>('time-bound', 'PUT', {
timeBound: { ...newTimeBoundBasis },
})
const { id: createdId } = resultBody
assert(typeof createdId === 'number', `Invalid result returned on write: ${createdId}`)
const { body } = await send<TimeBoundDetailsType>(`time-bound/${createdId}`, 'GET')
createdTimeBound = body
})
it('Contains correct data', () => {
const { b_name, b_comment, age } = createdTimeBound!
assert(b_name === newTimeBoundBasis.b_name, 'Name is different than expected')
assert(age === newTimeBoundBasis.age, 'Age differs')
assert(b_comment === newTimeBoundBasis.b_comment, 'Comment differs')
})
it('Update logs are correct', () => {
const lastUpdate = createdTimeBound!.now_bau[createdTimeBound!.now_bau.length - 1]
assert(lastUpdate.bau_comment === newTimeBoundBasis.comment, 'Comment is correct')
const logRows = lastUpdate.updates
const expectedLogRows: Partial<LogRow>[] = [
{
table: 'now_tu_bound',
column: 'b_name',
oldValue: null,
value: newTimeBoundBasis.b_name!,
type: 'add',
},
]
testLogRows(logRows, expectedLogRows, 3)
})
})
+20
View File
@@ -0,0 +1,20 @@
import { EditDataType, EditMetaData, Reference, TimeBoundDetailsType } from '../../../../frontend/src/backendTypes'
export const newTimeBoundBasis: EditDataType<TimeBoundDetailsType & EditMetaData> = {
b_name: 'C2N-y',
age: 1.778,
b_comment: null,
references: [{ rid: 24188 } as Reference],
comment: 'test time bound',
now_bau: [],
}
export const editedTimeBound: EditDataType<TimeBoundDetailsType & EditMetaData> = {
bid: 11,
b_name: 'C6N-y',
age: 1.778,
b_comment: 'test comment',
comment: 'test updating time unit',
references: [{ rid: 24188 } as Reference],
now_bau: [],
}
@@ -8,7 +8,7 @@ import { newTimeUnitBasis } from './data'
let createdTimeUnit: TimeUnitDetailsType | null = null
describe('Creating new species works', () => {
describe('Creating new time unit works', () => {
before(async () => {
await login()
})
+6 -5
View File
@@ -1,10 +1,10 @@
import { Request, Router } from 'express'
import { getAllTimeBounds, getTimeBoundDetails, getTimeBoundTimeUnits } from '../services/timeBound'
import { fixBigInt } from '../utils/common'
import { EditDataType, EditMetaData, TimeBoundDetailsType } from '../../../frontend/src/backendTypes'
import { deleteTimeUnit, writeTimeBound } from '../services/write/timeBound'
import { requireOneOf } from '../middlewares/authorizer'
import { Role } from '../../../frontend/src/types'
import { requireOneOf } from '../middlewares/authorizer'
import { getAllTimeBounds, getTimeBoundDetails, getTimeBoundTimeUnits } from '../services/timeBound'
import { deleteTimeBound, writeTimeBound } from '../services/write/timeBound'
import { fixBigInt } from '../utils/common'
const router = Router()
@@ -32,13 +32,14 @@ router.put(
async (req: Request<object, object, { timeBound: EditDataType<TimeBoundDetailsType> & EditMetaData }>, res) => {
const { comment, references, ...editedTimeBound } = req.body.timeBound
const result = await writeTimeBound(editedTimeBound, comment, references, req.user!.initials)
console.log({ result })
return res.status(200).send({ id: result })
}
)
router.delete('/:id', requireOneOf([Role.Admin, Role.EditUnrestricted]), async (req, res) => {
const id = parseInt(req.params.id)
await deleteTimeUnit(id, req.user!)
await deleteTimeBound(id, req.user!)
res.status(200).send()
})
+2 -5
View File
@@ -25,20 +25,17 @@ export const writeTimeBound = async (
try {
await writeHandler.start()
const result = await writeHandler.upsertObject('now_tu_bound', timeBound, ['bid'])
writeHandler.idValue = result!.bid as number
await writeHandler.logUpdatesAndComplete(authorizer, comment ?? '', references ?? [])
await writeHandler.commit()
return timeBound.bid
return result!.bid as number
} catch (e) {
await writeHandler.end()
throw e
}
}
export const deleteTimeUnit = async (id: number, user: User) => {
export const deleteTimeBound = async (id: number, user: User) => {
const timeBound = await getTimeBoundDetails(id)
if (!timeBound) throw new Error('Time bound not found')
const writeHandler = getTimeBoundWriteHandler('delete')
@@ -1,5 +1,4 @@
import { PoolConnection } from 'mariadb'
import { logger } from '../../../utils/logger'
import { pool } from '../../../utils/db'
import { DbValue, AllowedTables } from './types'
@@ -90,7 +89,7 @@ export class DatabaseHandler {
async executeQuery<T>(query: string, values?: Array<DbValue>) {
if (!this.connection) throw new Error('DB connection not initialized')
logger.info(`Executing SQL query: ${query} \nWith values ${JSON.stringify(values)}\n`)
// logger.info(`Executing SQL query: ${query} \nWith values ${JSON.stringify(values)}\n`)
let returnValue
if (values) {
returnValue = await this.connection.query<T>(query, values)
+2 -2
View File
@@ -114,9 +114,9 @@ export type TimeBound = {
b_comment: string
}
export type TimeBoundDetailsType = Prisma.now_tu_bound & { now_bau: Prisma.now_bau }
export type TimeBoundDetailsType = Prisma.now_tu_bound & { now_bau: TimeBoundUpdate[] }
export type TimeBoundUpdate = Prisma.now_bau & { now_br: Prisma.now_br }
export type TimeBoundUpdate = Prisma.now_bau & { now_br: Prisma.now_br } & { updates: UpdateLog[] }
/* Time Unit */
export type TimeUnitSequence = Prisma.now_tu_sequence