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 a47b9d8 commit e1e27be
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
10 changes: 6 additions & 4 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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 @@ -11,8 +12,9 @@ const ajvConfig: Config = {
tsconfig: 'tsconfig.json'
}

const toMatchSchema: MatcherFunction<[type: string]> = (actual: any, type: string) => {
const schema = createGenerator({ ...ajvConfig, type }).createSchema(type)
export const createSchema = (type: string): Schema => createGenerator({ ...ajvConfig, type }).createSchema(type)

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

return {
Expand All @@ -25,9 +27,9 @@ expect.extend({ toMatchSchema })

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

const schema = createSchema('*')

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

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

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')
expect(school).toMatchSchema('School', schema)
}
})
})
36 changes: 19 additions & 17 deletions test/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ 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 './common'
import { createSchema } from './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 @@ -22,12 +24,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')
expect(response).toMatchSchema('User', schema)
})

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

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

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

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')
expect(infoDetail).toMatchSchema('SchoolInfo', schema)
}
})
})
Expand All @@ -66,23 +68,23 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', ()
})

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

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

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')
expect(evaluationDetail).toMatchSchema('EvaluationDetail', schema)
}
})

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

Expand All @@ -92,7 +94,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')
expect(response).toMatchSchema('UsersMailSettings', schema)
})
})

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

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

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

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')
expect(lessonDetail).toMatchSchema('Lesson', schema)
}
})

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

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

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

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

0 comments on commit e1e27be

Please sign in to comment.