From f39faeaead8c16373bb705d359bc267b7f66b577 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Thu, 21 Jul 2022 16:25:17 +0200 Subject: [PATCH] Wip: refactor tests for updated schema name --- .../openapi/spec/export-parameters-schema.ts | 2 +- .../openapi/util/request-parameters.test.ts | 93 +++++++++++++------ src/lib/openapi/util/request-parameters.ts | 30 +++--- src/lib/routes/admin-api/state.ts | 2 +- 4 files changed, 84 insertions(+), 43 deletions(-) diff --git a/src/lib/openapi/spec/export-parameters-schema.ts b/src/lib/openapi/spec/export-parameters-schema.ts index ef82acf2ae..a9fa7bbe11 100644 --- a/src/lib/openapi/spec/export-parameters-schema.ts +++ b/src/lib/openapi/spec/export-parameters-schema.ts @@ -1,6 +1,6 @@ import { FromSchema } from 'json-schema-to-ts'; -export const exportParameters = { +const exportParameters = { format: { type: 'string', enum: ['json', 'yaml'], diff --git a/src/lib/openapi/util/request-parameters.test.ts b/src/lib/openapi/util/request-parameters.test.ts index 5ac64c076c..bb5fc198dc 100644 --- a/src/lib/openapi/util/request-parameters.test.ts +++ b/src/lib/openapi/util/request-parameters.test.ts @@ -1,39 +1,76 @@ import { SchemaObject } from 'ajv'; -import fc from 'fast-check'; -import { createRequestParameters } from './request-parameters'; +import fc, { Arbitrary } from 'fast-check'; +import { + createRequestParameters, + ParameterDescription, + ParameterDetails, + Parameters, +} from './request-parameters'; + +const paramDesc = ( + typeName: U, + gen: Arbitrary, +): Arbitrary> => + fc.record( + { + type: fc.constant(typeName), + default: gen, + enum: fc.array(gen, { minLength: 1 }), + }, + { requiredKeys: ['type'] }, + ); + +const parameterDescription = (): Arbitrary> => + fc.oneof( + paramDesc('boolean', fc.boolean()), + paramDesc('string', fc.string()), + paramDesc('number', fc.integer()), + ); + +const paramDetails = (): Arbitrary => + fc + .tuple(fc.string(), parameterDescription()) + .map(([description, deets]) => ({ + description, + ...deets, + })); + +const parameterDetails = (): Arbitrary => + fc.dictionary(fc.string({ minLength: 1 }), paramDetails()); describe('request parameter utils', () => { it('turns an object of names and descriptions into a an expected parameter list', () => { fc.assert( - fc.property( - fc.dictionary(fc.string({ minLength: 1 }), fc.string()), - (parameters) => { - const result = createRequestParameters(parameters); + fc.property(parameterDetails(), fc.context(), (parameters, ctx) => { + const result = createRequestParameters(parameters); - return result.every((paramsObject) => { - return ( - paramsObject.description === - parameters[paramsObject.name] - ); - }); - }, - ), + ctx.log(JSON.stringify(parameters)); + // ctx.log(JSON.stringify(result)); + return false; + // return result.every((paramsObject) => { + // return false; + // }); + }), ); }); - it('says every parameter is of type string and goes in the query', () => { - fc.assert( - fc.property( - fc.dictionary(fc.string({ minLength: 1 }), fc.string()), + // it('assigns parameter descriptions correctly', () => { + // fc.assert(fc.property()); + // }); - (parameters) => { - return createRequestParameters(parameters).every( - (paramsObject) => - (paramsObject.schema as SchemaObject).type === - 'string' && paramsObject.in === 'query', - ); - }, - ), - ); - }); + // it('says every parameter is of type string and goes in the query', () => { + // fc.assert( + // fc.property( + // fc.dictionary(fc.string({ minLength: 1 }), fc.string()), + + // (parameters) => { + // return createRequestParameters(parameters).every( + // (paramsObject) => + // (paramsObject.schema as SchemaObject).type === + // 'string' && paramsObject.in === 'query', + // ); + // }, + // ), + // ); + // }); }); diff --git a/src/lib/openapi/util/request-parameters.ts b/src/lib/openapi/util/request-parameters.ts index fa571ea7bb..b4442b1170 100644 --- a/src/lib/openapi/util/request-parameters.ts +++ b/src/lib/openapi/util/request-parameters.ts @@ -1,25 +1,29 @@ import { OpenAPIV3 } from 'openapi-types'; -// type ParameterDetails = { -// description: string; -// } & ( -// | { type: 'boolean'; default?: boolean; enum?: boolean[] } -// | { type: 'string'; default?: string; enum?: string[] } -// | { type: 'number'; default?: number; enum?: number[] } -// ); +export type ParameterDescription = { + type: TypeName; + default?: T; + enum?: T[]; +}; -// { [parameterName: string]: Parameter }; -// type Parameters = Record; +export type ParameterDetails = { + description: string; +} & ( + | ParameterDescription<'boolean', boolean> + | ParameterDescription<'string', string> + | ParameterDescription<'number', number> +); + +export type Parameters = Record; type ParameterName = string; -type Description = string; export const createRequestParameters = ( - params: Record, + params: Parameters, ): OpenAPIV3.ParameterObject[] => - Object.entries(params).map(([name, description]) => ({ + Object.entries(params).map(([name, deets]) => ({ name, - description, + description: deets.description, schema: { type: 'string' }, in: 'query', })); diff --git a/src/lib/routes/admin-api/state.ts b/src/lib/routes/admin-api/state.ts index dbd45f9daa..95936a4bf3 100644 --- a/src/lib/routes/admin-api/state.ts +++ b/src/lib/routes/admin-api/state.ts @@ -79,7 +79,7 @@ class StateController extends Controller { responses: { 200: createResponseSchema('stateSchema'), }, - parameters: createRequestParameters(exportParametersSchema), + // parameters: createRequestParameters(exportParametersSchema), }), ], });