Skip to content

Commit

Permalink
fix maxLengthBug with test (#61)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthijs van Henten <[email protected]>
  • Loading branch information
mvhenten and Matthijs van Henten authored Jul 8, 2024
1 parent 90d8a27 commit 658082f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import camelCase from 'lodash/camelCase';
import { faker } from '@faker-js/faker';
import { ConfigOptions } from './types';

const MAX_STRING_LENGTH = 42;

export interface ResponseMap {
code: string;
id: string;
Expand Down Expand Up @@ -33,6 +35,7 @@ export function transformToGenerateResultFunctions(
): string {
const context = {
faker,
MAX_STRING_LENGTH,
MAX_ARRAY_LENGTH: options?.maxArrayLength ?? 20,
baseURL: baseURL ?? '',
result: null,
Expand Down Expand Up @@ -195,11 +198,11 @@ function transformStringBasedOnFormat(schema: OpenAPIV3.NonArraySchemaObject, ke
return `faker.person.fullName()`;
} else {
if (minLength && maxLength) {
return `faker.string.alpha({ length: { min: ${minLength}, max: ${maxLength} })`;
return `faker.string.alpha({ length: { min: ${minLength}, max: ${maxLength} }})`;
} else if (minLength) {
return `faker.string.alpha({ length: { min: ${minLength} })`;
return `faker.string.alpha({ length: { min: ${minLength}, max: MAX_STRING_LENGTH }})`;
} else if (maxLength) {
return `faker.string.alpha({ length: { max: ${maxLength} })`;
return `faker.string.alpha({ length: { min: 0, max: ${maxLength} }})`;
} else {
return `faker.lorem.words()`;
}
Expand Down
33 changes: 33 additions & 0 deletions test/fixture/strings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.0
info:
title: (title)
version: 0.0.0
tags: []
paths:
/test:
get:
operationId: strings
parameters: []
responses:
'200':
description: The request has succeeded.
content:
application/json:
schema:
type: object
properties:
string5to10:
type: string
minLength: 5
maxLength: 10
atLeast3:
type: string
minLength: 3
max7:
type: string
maxLength: 7
required:
- string5to10
- atLeast3
- max7
components: {}
34 changes: 34 additions & 0 deletions test/transform.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, expect, it, vi } from 'vitest';

import { transformToGenerateResultFunctions } from '../src/transform';
import { generateOperationCollection } from '../src/generate';
import { getV3Doc } from '../src/swagger';

describe('transform:transformToGenerateResultFunctions', () => {
it('Generates a response function with epxected faker calls', async () => {
const apiDoc = await getV3Doc('./test/fixture/strings.yaml');
const schema = generateOperationCollection(apiDoc, { output: '' });

const transform = transformToGenerateResultFunctions(schema, '');
const src = transform.replace('export', '').replace('};', '}');

{
const faker = {
string: {
alpha: vi.fn(),
},
};

const MAX_STRING_LENGTH = 897;

// direct eval is safe here, we know the generated code
// and we need the local scope
const generateFunction = eval(`(${src})`);
const generatedResponse = generateFunction();

expect(faker.string.alpha).toHaveBeenNthCalledWith(1, { length: { min: 5, max: 10 } });
expect(faker.string.alpha).toHaveBeenNthCalledWith(2, { length: { min: 3, max: MAX_STRING_LENGTH } });
expect(faker.string.alpha).toHaveBeenNthCalledWith(3, { length: { min: 0, max: 7 } });
}
});
});

0 comments on commit 658082f

Please sign in to comment.