1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-12 01:17:04 +02:00

Wip: refactor tests for updated schema name

This commit is contained in:
Thomas Heartman 2022-07-21 16:25:17 +02:00
parent 48e9bd4ead
commit f39faeaead
4 changed files with 84 additions and 43 deletions

View File

@ -1,6 +1,6 @@
import { FromSchema } from 'json-schema-to-ts'; import { FromSchema } from 'json-schema-to-ts';
export const exportParameters = { const exportParameters = {
format: { format: {
type: 'string', type: 'string',
enum: ['json', 'yaml'], enum: ['json', 'yaml'],

View File

@ -1,39 +1,76 @@
import { SchemaObject } from 'ajv'; import { SchemaObject } from 'ajv';
import fc from 'fast-check'; import fc, { Arbitrary } from 'fast-check';
import { createRequestParameters } from './request-parameters'; import {
createRequestParameters,
ParameterDescription,
ParameterDetails,
Parameters,
} from './request-parameters';
const paramDesc = <T, U>(
typeName: U,
gen: Arbitrary<T>,
): Arbitrary<ParameterDescription<U, T>> =>
fc.record(
{
type: fc.constant(typeName),
default: gen,
enum: fc.array(gen, { minLength: 1 }),
},
{ requiredKeys: ['type'] },
);
const parameterDescription = (): Arbitrary<ParameterDescription<any, any>> =>
fc.oneof(
paramDesc('boolean', fc.boolean()),
paramDesc('string', fc.string()),
paramDesc('number', fc.integer()),
);
const paramDetails = (): Arbitrary<ParameterDetails> =>
fc
.tuple(fc.string(), parameterDescription())
.map(([description, deets]) => ({
description,
...deets,
}));
const parameterDetails = (): Arbitrary<Parameters> =>
fc.dictionary(fc.string({ minLength: 1 }), paramDetails());
describe('request parameter utils', () => { describe('request parameter utils', () => {
it('turns an object of names and descriptions into a an expected parameter list', () => { it('turns an object of names and descriptions into a an expected parameter list', () => {
fc.assert( fc.assert(
fc.property( fc.property(parameterDetails(), fc.context(), (parameters, ctx) => {
fc.dictionary(fc.string({ minLength: 1 }), fc.string()), const result = createRequestParameters(parameters);
(parameters) => {
const result = createRequestParameters(parameters);
return result.every((paramsObject) => { ctx.log(JSON.stringify(parameters));
return ( // ctx.log(JSON.stringify(result));
paramsObject.description === return false;
parameters[paramsObject.name] // return result.every((paramsObject) => {
); // return false;
}); // });
}, }),
),
); );
}); });
it('says every parameter is of type string and goes in the query', () => { // it('assigns parameter descriptions correctly', () => {
fc.assert( // fc.assert(fc.property());
fc.property( // });
fc.dictionary(fc.string({ minLength: 1 }), fc.string()),
(parameters) => { // it('says every parameter is of type string and goes in the query', () => {
return createRequestParameters(parameters).every( // fc.assert(
(paramsObject) => // fc.property(
(paramsObject.schema as SchemaObject).type === // fc.dictionary(fc.string({ minLength: 1 }), fc.string()),
'string' && paramsObject.in === 'query',
); // (parameters) => {
}, // return createRequestParameters(parameters).every(
), // (paramsObject) =>
); // (paramsObject.schema as SchemaObject).type ===
}); // 'string' && paramsObject.in === 'query',
// );
// },
// ),
// );
// });
}); });

View File

@ -1,25 +1,29 @@
import { OpenAPIV3 } from 'openapi-types'; import { OpenAPIV3 } from 'openapi-types';
// type ParameterDetails = { export type ParameterDescription<TypeName, T> = {
// description: string; type: TypeName;
// } & ( default?: T;
// | { type: 'boolean'; default?: boolean; enum?: boolean[] } enum?: T[];
// | { type: 'string'; default?: string; enum?: string[] } };
// | { type: 'number'; default?: number; enum?: number[] }
// );
// { [parameterName: string]: Parameter }; export type ParameterDetails = {
// type Parameters = Record<ParameterName, ParameterDetails>; description: string;
} & (
| ParameterDescription<'boolean', boolean>
| ParameterDescription<'string', string>
| ParameterDescription<'number', number>
);
export type Parameters = Record<ParameterName, ParameterDetails>;
type ParameterName = string; type ParameterName = string;
type Description = string;
export const createRequestParameters = ( export const createRequestParameters = (
params: Record<ParameterName, Description>, params: Parameters,
): OpenAPIV3.ParameterObject[] => ): OpenAPIV3.ParameterObject[] =>
Object.entries(params).map(([name, description]) => ({ Object.entries(params).map(([name, deets]) => ({
name, name,
description, description: deets.description,
schema: { type: 'string' }, schema: { type: 'string' },
in: 'query', in: 'query',
})); }));

View File

@ -79,7 +79,7 @@ class StateController extends Controller {
responses: { responses: {
200: createResponseSchema('stateSchema'), 200: createResponseSchema('stateSchema'),
}, },
parameters: createRequestParameters(exportParametersSchema), // parameters: createRequestParameters(exportParametersSchema),
}), }),
], ],
}); });