Skip to content

Commit

Permalink
test: generate the JSON Schema beforehand to optimize test execution
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff committed May 5, 2024
1 parent e1e27be commit ad28745
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
11 changes: 5 additions & 6 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { MatcherFunction } from 'expect'
import type { Schema } from 'ajv'
import Ajv from 'ajv'
import type { Config } from 'ts-json-schema-generator'
import { createGenerator } from 'ts-json-schema-generator'
Expand All @@ -12,10 +11,10 @@ const ajvConfig: Config = {
tsconfig: 'tsconfig.json'
}

export const createSchema = (type: string): Schema => createGenerator({ ...ajvConfig, type }).createSchema(type)
const schemaGenerator = createGenerator({ ...ajvConfig, type: '*' })

const toMatchSchema: MatcherFunction<[type: string, schema: Schema]> = (actual: any, type: string, schema: Schema) => {
const result = ajv.validate(schema, actual)
const toMatchSchema: MatcherFunction<[type: string]> = (actual: any, type: string) => {
const result = ajv.validate(schemaGenerator.createSchema(type), actual)

return {
message: () => `expected ${type + (result ? ' not ' : ' ')}to match schema: ${inspect(ajv.errors)}`,
Expand All @@ -27,9 +26,9 @@ expect.extend({ toMatchSchema })

declare module 'expect' {
interface AsymmetricMatchers {
toMatchSchema: (type: string, schema: Schema) => void
toMatchSchema: (type: string) => void
}
interface Matchers<R> {
toMatchSchema: (type: string, schema: Schema) => R
toMatchSchema: (type: string) => R
}
}
8 changes: 3 additions & 5 deletions test/static.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { describe, expect } from '@jest/globals'
import { Skolengo } from '../src/index'
import { createSchema } from './common'

const schema = createSchema('*')
import './common'

/**
* Tests d'intégration des endpoints ne nécessitant pas d'authentification
Expand All @@ -11,14 +9,14 @@ describe('Test Skolengo API types - Public endpoints', () => {
it('should getAppCurrentConfig return AppConfig type', async () => {
const response = await Skolengo.getAppCurrentConfig()

expect(response).toMatchSchema('AppCurrentConfig', schema)
expect(response).toMatchSchema('AppCurrentConfig')
})

it('should searchSchool return School type', async () => {
const response = await Skolengo.searchSchool({ text: 'Lycée' }, 50)

for (const school of response) {
expect(school).toMatchSchema('School', schema)
expect(school).toMatchSchema('School')
}
})
})
36 changes: 17 additions & 19 deletions test/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import type { Agenda, HomeworkAssignment } from '../src/models/Calendar'
import type { AbsenceFile } from '../src/models/SchoolLife'
import type { SchoolInfo } from '../src/models/School'
import { Skolengo } from '../src/index'
import { createSchema } from './common'
import './common'

const SKOLENGO_TOKENSET = process.env.SKOLENGO_TOKENSET
const describeAuthenticated = SKOLENGO_TOKENSET !== undefined ? describe : describe.skip

const schema = createSchema('*')

/**
* Tests d'intégration des endpoints qui nécessitent une authentification
*/
Expand All @@ -24,12 +22,12 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', ()

it('should match type User', async () => {
const response = await user.getUserInfo()
expect(response).toMatchSchema('User', schema)
expect(response).toMatchSchema('User')
})

it('should match type AbsenceReason[]', async () => {
const reasons = await user.getAbsenceReasons()
for (const reason of reasons) expect(reason).toMatchSchema('AbsenceReason', schema)
for (const reason of reasons) expect(reason).toMatchSchema('AbsenceReason')
})

/**
Expand All @@ -43,13 +41,13 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', ()
})

it('should match type SchoolInfo[]', () => {
for (const info of schoolInfoList) expect(info).toMatchSchema('SchoolInfo', schema)
for (const info of schoolInfoList) expect(info).toMatchSchema('SchoolInfo')
})

it('should match the SchoolInfo', async () => {
for (const info of schoolInfoList.slice(0, 2)) {
const infoDetail = await user.getSchoolInfo(info.id)
expect(infoDetail).toMatchSchema('SchoolInfo', schema)
expect(infoDetail).toMatchSchema('SchoolInfo')
}
})
})
Expand All @@ -68,23 +66,23 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', ()
})

it('should match type EvaluationSettings', () => {
for (const evaluationSettings of evaluationSettingsList) expect(evaluationSettings).toMatchSchema('EvaluationSettings', schema)
for (const evaluationSettings of evaluationSettingsList) expect(evaluationSettings).toMatchSchema('EvaluationSettings')
})

it('should match type Evaluation[]', () => {
for (const evaluation of evaluationList) expect(evaluation).toMatchSchema('Evaluation', schema)
for (const evaluation of evaluationList) expect(evaluation).toMatchSchema('Evaluation')
})

it('should match type EvaluationDetail[]', async () => {
for (const evaluation of evaluationList) {
const evaluationDetail = await user.getEvaluationDetail(undefined, evaluation.evaluations[0].id)
expect(evaluationDetail).toMatchSchema('EvaluationDetail', schema)
expect(evaluationDetail).toMatchSchema('EvaluationDetail')
}
})

it('should match type Attachment[]', async () => {
const response = await user.getPeriodicReportsFiles(undefined, 2)
for (const attachment of response) expect(attachment).toMatchSchema('Attachment', schema)
for (const attachment of response) expect(attachment).toMatchSchema('Attachment')
})
})

Expand All @@ -94,7 +92,7 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', ()
describe('Test of the MSG module', () => {
it('should match type UsersMailSettings', async () => {
const response = await user.getUsersMailSettings()
expect(response).toMatchSchema('UsersMailSettings', schema)
expect(response).toMatchSchema('UsersMailSettings')
})
})

Expand All @@ -113,30 +111,30 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', ()
})

it('should match type Agenda[]', () => {
for (const agenda of agendaList) expect(agenda).toMatchSchema('Agenda', schema)
for (const agenda of agendaList) expect(agenda).toMatchSchema('Agenda')
})

it('should match type Lesson[]', () => {
for (const lesson of agendaList[0].lessons) {
expect(lesson).toMatchSchema('Lesson', schema)
expect(lesson).toMatchSchema('Lesson')
}
})

it('should match type Lesson', async () => {
for (const lesson of agendaList[0].lessons) {
const lessonDetail = await user.getLesson(undefined, lesson.id)
expect(lessonDetail).toMatchSchema('Lesson', schema)
expect(lessonDetail).toMatchSchema('Lesson')
}
})

it('should match type HomeWorkAssignment[]', () => {
for (const homework of homeworkList) expect(homework).toMatchSchema('HomeworkAssignment', schema)
for (const homework of homeworkList) expect(homework).toMatchSchema('HomeworkAssignment')
})

it('should match type HomeWorkAssignment', async () => {
for (const homework of homeworkList) {
const homeworkDetail = await user.getHomeworkAssignment(undefined, homework.id)
expect(homeworkDetail).toMatchSchema('HomeworkAssignment', schema)
expect(homeworkDetail).toMatchSchema('HomeworkAssignment')
}
})
})
Expand All @@ -152,13 +150,13 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', ()
})

it('should match type AbsenceFile[]', () => {
for (const file of absenceFiles) expect(file).toMatchSchema('AbsenceFile', schema)
for (const file of absenceFiles) expect(file).toMatchSchema('AbsenceFile')
})

it('should match type AbsenceFile', async () => {
for (const file of absenceFiles) {
const fileDetail = await user.getAbsenceFile(file.id)
expect(fileDetail).toMatchSchema('AbsenceFile', schema)
expect(fileDetail).toMatchSchema('AbsenceFile')
}
})
})
Expand Down

0 comments on commit ad28745

Please sign in to comment.