924 lines
27 KiB
Dart
924 lines
27 KiB
Dart
import 'package:test/test.dart';
|
|
|
|
import 'package:swagger_generator_flutter/core/models.dart';
|
|
|
|
void main() {
|
|
group('ApiPath', () {
|
|
test('creates ApiPath with required fields', () {
|
|
const path = ApiPath(
|
|
path: '/api/users',
|
|
method: HttpMethod.get,
|
|
summary: 'Get users',
|
|
description: 'Retrieve all users',
|
|
operationId: 'getUsers',
|
|
tags: ['User'],
|
|
parameters: [],
|
|
responses: {},
|
|
requestBody: null,
|
|
);
|
|
|
|
expect(path.path, '/api/users');
|
|
expect(path.method, HttpMethod.get);
|
|
expect(path.summary, 'Get users');
|
|
expect(path.description, 'Retrieve all users');
|
|
expect(path.tags, ['User']);
|
|
expect(path.parameters, isEmpty);
|
|
expect(path.responses, isEmpty);
|
|
expect(path.requestBody, isNull);
|
|
});
|
|
|
|
test('creates ApiPath with all fields', () {
|
|
final parameters = [
|
|
const ApiParameter(
|
|
name: 'id',
|
|
location: ParameterLocation.path,
|
|
required: true,
|
|
type: PropertyType.integer,
|
|
description: 'User ID',
|
|
),
|
|
];
|
|
|
|
final responses = {
|
|
'200': const ApiResponse(
|
|
code: '200',
|
|
description: 'Success',
|
|
schema: {'type': 'object'},
|
|
content: null,
|
|
),
|
|
};
|
|
|
|
const requestBody = ApiRequestBody(
|
|
description: 'User data',
|
|
required: true,
|
|
content: {
|
|
'application/json': {
|
|
'schema': {'type': 'object'}
|
|
}
|
|
},
|
|
);
|
|
|
|
final path = ApiPath(
|
|
path: '/api/users/{id}',
|
|
method: HttpMethod.put,
|
|
summary: 'Update user',
|
|
description: 'Update user by ID',
|
|
operationId: 'updateUser',
|
|
tags: ['User'],
|
|
parameters: parameters,
|
|
responses: responses,
|
|
requestBody: requestBody,
|
|
);
|
|
|
|
expect(path.parameters, hasLength(1));
|
|
expect(path.responses, hasLength(1));
|
|
expect(path.requestBody, isNotNull);
|
|
});
|
|
});
|
|
|
|
group('ApiParameter', () {
|
|
test('creates ApiParameter with required fields', () {
|
|
const param = ApiParameter(
|
|
name: 'id',
|
|
location: ParameterLocation.path,
|
|
required: true,
|
|
type: PropertyType.integer,
|
|
description: 'User ID',
|
|
);
|
|
|
|
expect(param.name, 'id');
|
|
expect(param.location, ParameterLocation.path);
|
|
expect(param.required, true);
|
|
expect(param.type, PropertyType.integer);
|
|
expect(param.description, 'User ID');
|
|
});
|
|
|
|
test('creates ApiParameter with optional fields', () {
|
|
const param = ApiParameter(
|
|
name: 'page',
|
|
location: ParameterLocation.query,
|
|
required: false,
|
|
type: PropertyType.integer,
|
|
description: 'Page number',
|
|
format: 'int32',
|
|
example: 1,
|
|
defaultValue: 1,
|
|
);
|
|
|
|
expect(param.format, 'int32');
|
|
expect(param.example, 1);
|
|
expect(param.defaultValue, 1);
|
|
});
|
|
|
|
test('creates ApiParameter from JSON', () {
|
|
final json = {
|
|
'name': 'id',
|
|
'in': 'path',
|
|
'required': true,
|
|
'type': 'integer',
|
|
'description': 'User ID',
|
|
'format': 'int64',
|
|
'example': 123,
|
|
'default': 1,
|
|
};
|
|
|
|
final param = ApiParameter.fromJson(json);
|
|
|
|
expect(param.name, 'id');
|
|
expect(param.location, ParameterLocation.path);
|
|
expect(param.required, true);
|
|
expect(param.type, PropertyType.integer);
|
|
expect(param.description, 'User ID');
|
|
expect(param.format, 'int64');
|
|
expect(param.example, 123);
|
|
expect(param.defaultValue, 1);
|
|
});
|
|
});
|
|
|
|
group('ApiResponse', () {
|
|
test('creates ApiResponse with required fields', () {
|
|
const response = ApiResponse(
|
|
code: '200',
|
|
description: 'Success',
|
|
schema: null,
|
|
content: null,
|
|
);
|
|
|
|
expect(response.code, '200');
|
|
expect(response.description, 'Success');
|
|
expect(response.schema, isNull);
|
|
expect(response.content, isNull);
|
|
});
|
|
|
|
test('creates ApiResponse with schema', () {
|
|
final schema = {
|
|
'type': 'object',
|
|
'properties': {
|
|
'id': {'type': 'integer'},
|
|
'name': {'type': 'string'},
|
|
},
|
|
};
|
|
|
|
final response = ApiResponse(
|
|
code: '200',
|
|
description: 'Success',
|
|
schema: schema,
|
|
content: null,
|
|
);
|
|
|
|
expect(response.schema, equals(schema));
|
|
});
|
|
|
|
test('creates ApiResponse from JSON', () {
|
|
final json = {
|
|
'description': 'Success',
|
|
'schema': {'type': 'object'},
|
|
'content': {
|
|
'application/json': {
|
|
'schema': {'type': 'object'},
|
|
},
|
|
},
|
|
};
|
|
|
|
final response = ApiResponse.fromJson('200', json);
|
|
|
|
expect(response.code, '200');
|
|
expect(response.description, 'Success');
|
|
expect(response.schema, isNotNull);
|
|
expect(response.content, isNotNull);
|
|
});
|
|
});
|
|
|
|
group('ApiRequestBody', () {
|
|
test('creates ApiRequestBody with required fields', () {
|
|
const requestBody = ApiRequestBody(
|
|
description: 'User data',
|
|
required: true,
|
|
content: null,
|
|
);
|
|
|
|
expect(requestBody.description, 'User data');
|
|
expect(requestBody.required, true);
|
|
expect(requestBody.content, isNull);
|
|
});
|
|
|
|
test('creates ApiRequestBody with content', () {
|
|
final content = {
|
|
'application/json': {
|
|
'schema': {
|
|
'type': 'object',
|
|
'properties': {
|
|
'name': {'type': 'string'},
|
|
'email': {'type': 'string'},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
final requestBody = ApiRequestBody(
|
|
description: 'User data',
|
|
required: true,
|
|
content: content,
|
|
);
|
|
|
|
expect(requestBody.content, equals(content));
|
|
});
|
|
|
|
test('creates ApiRequestBody from JSON', () {
|
|
final json = {
|
|
'description': 'User data',
|
|
'required': true,
|
|
'content': {
|
|
'application/json': {
|
|
'schema': {'type': 'object'},
|
|
},
|
|
},
|
|
};
|
|
|
|
final requestBody = ApiRequestBody.fromJson(json);
|
|
|
|
expect(requestBody.description, 'User data');
|
|
expect(requestBody.required, true);
|
|
expect(requestBody.content, isNotNull);
|
|
});
|
|
});
|
|
|
|
group('ApiModel', () {
|
|
test('creates ApiModel with required fields', () {
|
|
const model = ApiModel(
|
|
name: 'User',
|
|
description: 'User model',
|
|
properties: {},
|
|
required: [],
|
|
);
|
|
|
|
expect(model.name, 'User');
|
|
expect(model.description, 'User model');
|
|
expect(model.properties, isEmpty);
|
|
expect(model.required, isEmpty);
|
|
expect(model.isEnum, false);
|
|
expect(model.enumValues, isEmpty);
|
|
expect(model.enumType, isNull);
|
|
});
|
|
|
|
test('creates ApiModel with properties', () {
|
|
final properties = {
|
|
'id': const ApiProperty(
|
|
name: 'id',
|
|
type: PropertyType.integer,
|
|
description: 'User ID',
|
|
required: true,
|
|
),
|
|
'name': const ApiProperty(
|
|
name: 'name',
|
|
type: PropertyType.string,
|
|
description: 'User name',
|
|
required: true,
|
|
),
|
|
};
|
|
|
|
final model = ApiModel(
|
|
name: 'User',
|
|
description: 'User model',
|
|
properties: properties,
|
|
required: ['id', 'name'],
|
|
);
|
|
|
|
expect(model.properties, hasLength(2));
|
|
expect(model.required, ['id', 'name']);
|
|
});
|
|
|
|
test('creates enum ApiModel', () {
|
|
const model = ApiModel(
|
|
name: 'UserStatus',
|
|
description: 'User status enum',
|
|
properties: {},
|
|
required: [],
|
|
isEnum: true,
|
|
enumValues: ['active', 'inactive', 'pending'],
|
|
enumType: PropertyType.string,
|
|
);
|
|
|
|
expect(model.isEnum, true);
|
|
expect(model.enumValues, ['active', 'inactive', 'pending']);
|
|
expect(model.enumType, PropertyType.string);
|
|
});
|
|
|
|
test('creates ApiModel from JSON', () {
|
|
final json = {
|
|
'description': 'User model',
|
|
'properties': {
|
|
'id': {
|
|
'type': 'integer',
|
|
'description': 'User ID',
|
|
},
|
|
'name': {
|
|
'type': 'string',
|
|
'description': 'User name',
|
|
},
|
|
},
|
|
'required': ['id', 'name'],
|
|
};
|
|
|
|
final model = ApiModel.fromJson('User', json);
|
|
|
|
expect(model.name, 'User');
|
|
expect(model.description, 'User model');
|
|
expect(model.properties, hasLength(2));
|
|
expect(model.required, ['id', 'name']);
|
|
});
|
|
|
|
test('creates enum ApiModel from JSON', () {
|
|
final json = {
|
|
'description': 'User status enum',
|
|
'enum': ['active', 'inactive', 'pending'],
|
|
'type': 'string',
|
|
};
|
|
|
|
final model = ApiModel.fromJson('UserStatus', json);
|
|
|
|
expect(model.isEnum, true);
|
|
expect(model.enumValues, ['active', 'inactive', 'pending']);
|
|
expect(model.enumType, PropertyType.string);
|
|
});
|
|
});
|
|
|
|
group('ApiProperty', () {
|
|
test('creates ApiProperty with required fields', () {
|
|
const property = ApiProperty(
|
|
name: 'id',
|
|
type: PropertyType.integer,
|
|
description: 'User ID',
|
|
required: true,
|
|
);
|
|
|
|
expect(property.name, 'id');
|
|
expect(property.type, PropertyType.integer);
|
|
expect(property.description, 'User ID');
|
|
expect(property.required, true);
|
|
expect(property.nullable, false);
|
|
});
|
|
|
|
test('creates ApiProperty with optional fields', () {
|
|
const property = ApiProperty(
|
|
name: 'name',
|
|
type: PropertyType.string,
|
|
description: 'User name',
|
|
required: false,
|
|
nullable: true,
|
|
format: 'string',
|
|
example: 'John Doe',
|
|
defaultValue: 'Unknown',
|
|
reference: 'User',
|
|
);
|
|
|
|
expect(property.nullable, true);
|
|
expect(property.format, 'string');
|
|
expect(property.example, 'John Doe');
|
|
expect(property.defaultValue, 'Unknown');
|
|
expect(property.reference, 'User');
|
|
});
|
|
|
|
test('creates ApiProperty from JSON', () {
|
|
final json = {
|
|
'type': 'string',
|
|
'description': 'User name',
|
|
'nullable': true,
|
|
'format': 'string',
|
|
'example': 'John Doe',
|
|
'default': 'Unknown',
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('name', json, []);
|
|
|
|
expect(property.name, 'name');
|
|
expect(property.type, PropertyType.string);
|
|
expect(property.description, 'User name');
|
|
expect(property.nullable, true);
|
|
expect(property.format, 'string');
|
|
expect(property.example, 'John Doe');
|
|
expect(property.defaultValue, 'Unknown');
|
|
});
|
|
|
|
test('creates ApiProperty with reference', () {
|
|
final json = {
|
|
'type': 'object',
|
|
'description': 'User object',
|
|
'\$ref': '#/components/schemas/User',
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('user', json, []);
|
|
|
|
expect(property.type, PropertyType.reference);
|
|
expect(property.reference, 'User');
|
|
});
|
|
|
|
test('creates ApiProperty with array items', () {
|
|
final json = {
|
|
'type': 'array',
|
|
'description': 'User list',
|
|
'items': {
|
|
'type': 'object',
|
|
'\$ref': '#/components/schemas/User',
|
|
},
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('users', json, []);
|
|
|
|
expect(property.type, PropertyType.array);
|
|
expect(property.items, isNotNull);
|
|
expect(property.items!.name, 'User');
|
|
});
|
|
});
|
|
|
|
group('PropertyType', () {
|
|
test('converts string to PropertyType', () {
|
|
expect(PropertyType.fromString('string'), PropertyType.string);
|
|
expect(PropertyType.fromString('integer'), PropertyType.integer);
|
|
expect(PropertyType.fromString('number'), PropertyType.number);
|
|
expect(PropertyType.fromString('boolean'), PropertyType.boolean);
|
|
expect(PropertyType.fromString('array'), PropertyType.array);
|
|
expect(PropertyType.fromString('object'), PropertyType.object);
|
|
expect(PropertyType.fromString('unknown'), PropertyType.string);
|
|
});
|
|
|
|
test('gets PropertyType value', () {
|
|
expect(PropertyType.string.value, 'string');
|
|
expect(PropertyType.integer.value, 'integer');
|
|
expect(PropertyType.number.value, 'number');
|
|
expect(PropertyType.boolean.value, 'boolean');
|
|
expect(PropertyType.array.value, 'array');
|
|
expect(PropertyType.object.value, 'object');
|
|
expect(PropertyType.reference.value, 'reference');
|
|
});
|
|
});
|
|
|
|
group('ParameterLocation', () {
|
|
test('converts string to ParameterLocation', () {
|
|
expect(ParameterLocation.fromString('query'), ParameterLocation.query);
|
|
expect(ParameterLocation.fromString('path'), ParameterLocation.path);
|
|
expect(ParameterLocation.fromString('header'), ParameterLocation.header);
|
|
expect(ParameterLocation.fromString('cookie'), ParameterLocation.cookie);
|
|
expect(ParameterLocation.fromString('unknown'), ParameterLocation.query);
|
|
});
|
|
});
|
|
|
|
group('HttpMethod', () {
|
|
test('converts string to HttpMethod', () {
|
|
expect(HttpMethod.fromString('GET'), HttpMethod.get);
|
|
expect(HttpMethod.fromString('POST'), HttpMethod.post);
|
|
expect(HttpMethod.fromString('PUT'), HttpMethod.put);
|
|
expect(HttpMethod.fromString('DELETE'), HttpMethod.delete);
|
|
expect(HttpMethod.fromString('PATCH'), HttpMethod.patch);
|
|
expect(HttpMethod.fromString('HEAD'), HttpMethod.head);
|
|
expect(HttpMethod.fromString('OPTIONS'), HttpMethod.options);
|
|
});
|
|
|
|
test('handles case insensitive input', () {
|
|
expect(HttpMethod.fromString('get'), HttpMethod.get);
|
|
expect(HttpMethod.fromString('Post'), HttpMethod.post);
|
|
expect(HttpMethod.fromString('put'), HttpMethod.put);
|
|
});
|
|
|
|
test('returns default for unknown method', () {
|
|
expect(HttpMethod.fromString('UNKNOWN'), HttpMethod.get);
|
|
expect(HttpMethod.fromString(''), HttpMethod.get);
|
|
});
|
|
|
|
test('gets HttpMethod value', () {
|
|
expect(HttpMethod.get.value, 'GET');
|
|
expect(HttpMethod.post.value, 'POST');
|
|
expect(HttpMethod.put.value, 'PUT');
|
|
expect(HttpMethod.delete.value, 'DELETE');
|
|
expect(HttpMethod.patch.value, 'PATCH');
|
|
expect(HttpMethod.head.value, 'HEAD');
|
|
expect(HttpMethod.options.value, 'OPTIONS');
|
|
});
|
|
});
|
|
|
|
group('SwaggerDocument', () {
|
|
test('creates SwaggerDocument with required fields', () {
|
|
const document = SwaggerDocument(
|
|
title: 'Test API',
|
|
version: '1.0.0',
|
|
description: 'Test API description',
|
|
host: 'api.example.com',
|
|
basePath: '/api',
|
|
schemes: ['https'],
|
|
consumes: ['application/json'],
|
|
produces: ['application/json'],
|
|
paths: {},
|
|
models: {},
|
|
controllers: {},
|
|
);
|
|
|
|
expect(document.title, 'Test API');
|
|
expect(document.version, '1.0.0');
|
|
expect(document.description, 'Test API description');
|
|
expect(document.host, 'api.example.com');
|
|
expect(document.basePath, '/api');
|
|
expect(document.schemes, ['https']);
|
|
expect(document.consumes, ['application/json']);
|
|
expect(document.produces, ['application/json']);
|
|
expect(document.paths, isEmpty);
|
|
expect(document.models, isEmpty);
|
|
expect(document.controllers, isEmpty);
|
|
});
|
|
|
|
test('creates SwaggerDocument from JSON with all fields', () {
|
|
final json = {
|
|
'info': {
|
|
'title': 'Test API',
|
|
'version': '1.0.0',
|
|
'description': 'Test API description',
|
|
},
|
|
'host': 'api.example.com',
|
|
'basePath': '/api',
|
|
'schemes': ['https', 'http'],
|
|
'consumes': ['application/json', 'application/xml'],
|
|
'produces': ['application/json', 'text/plain'],
|
|
};
|
|
|
|
final document = SwaggerDocument.fromJson(json);
|
|
|
|
expect(document.title, 'Test API');
|
|
expect(document.version, '1.0.0');
|
|
expect(document.description, 'Test API description');
|
|
expect(document.host, 'api.example.com');
|
|
expect(document.basePath, '/api');
|
|
expect(document.schemes, ['https', 'http']);
|
|
expect(document.consumes, ['application/json', 'application/xml']);
|
|
expect(document.produces, ['application/json', 'text/plain']);
|
|
});
|
|
|
|
test('creates SwaggerDocument from JSON with minimal fields', () {
|
|
final json = <String, dynamic>{};
|
|
|
|
final document = SwaggerDocument.fromJson(json);
|
|
|
|
expect(document.title, 'API');
|
|
expect(document.version, '1.0.0');
|
|
expect(document.description, '');
|
|
expect(document.host, '');
|
|
expect(document.basePath, '');
|
|
expect(document.schemes, ['https']);
|
|
expect(document.consumes, ['application/json']);
|
|
expect(document.produces, ['application/json']);
|
|
});
|
|
|
|
test('creates SwaggerDocument from JSON with null info', () {
|
|
final json = {'info': null};
|
|
|
|
final document = SwaggerDocument.fromJson(json);
|
|
|
|
expect(document.title, 'API');
|
|
expect(document.version, '1.0.0');
|
|
expect(document.description, '');
|
|
});
|
|
});
|
|
|
|
group('ApiController', () {
|
|
test('creates ApiController with required fields', () {
|
|
const controller = ApiController(
|
|
name: 'UserController',
|
|
description: 'User management controller',
|
|
paths: [],
|
|
);
|
|
|
|
expect(controller.name, 'UserController');
|
|
expect(controller.description, 'User management controller');
|
|
expect(controller.paths, isEmpty);
|
|
});
|
|
|
|
test('creates ApiController with paths', () {
|
|
final paths = [
|
|
const ApiPath(
|
|
path: '/api/users',
|
|
method: HttpMethod.get,
|
|
summary: 'Get users',
|
|
description: 'Retrieve all users',
|
|
operationId: 'getUsers',
|
|
tags: ['User'],
|
|
parameters: [],
|
|
responses: {},
|
|
requestBody: null,
|
|
),
|
|
const ApiPath(
|
|
path: '/api/users/{id}',
|
|
method: HttpMethod.post,
|
|
summary: 'Create user',
|
|
description: 'Create a new user',
|
|
operationId: 'createUser',
|
|
tags: ['User'],
|
|
parameters: [],
|
|
responses: {},
|
|
requestBody: null,
|
|
),
|
|
];
|
|
|
|
final controller = ApiController(
|
|
name: 'UserController',
|
|
description: 'User management controller',
|
|
paths: paths,
|
|
);
|
|
|
|
expect(controller.paths, hasLength(2));
|
|
expect(controller.paths[0].path, '/api/users');
|
|
expect(controller.paths[1].path, '/api/users/{id}');
|
|
});
|
|
|
|
test('creates ApiController from paths', () {
|
|
final paths = [
|
|
const ApiPath(
|
|
path: '/api/users',
|
|
method: HttpMethod.get,
|
|
summary: 'Get users',
|
|
description: 'Retrieve all users',
|
|
operationId: 'getUsers',
|
|
tags: ['User'],
|
|
parameters: [],
|
|
responses: {},
|
|
requestBody: null,
|
|
),
|
|
];
|
|
|
|
final controller = ApiController.fromPaths('UserController', paths);
|
|
|
|
expect(controller.name, 'UserController');
|
|
expect(controller.description, 'UserController');
|
|
expect(controller.paths, hasLength(1));
|
|
});
|
|
});
|
|
|
|
group('ApiPath fromJson', () {
|
|
test('creates ApiPath from JSON with all fields', () {
|
|
final json = {
|
|
'summary': 'Get users',
|
|
'description': 'Retrieve all users',
|
|
'operationId': 'getUsers',
|
|
'tags': ['User'],
|
|
'parameters': [
|
|
<String, dynamic>{
|
|
'name': 'id',
|
|
'in': 'path',
|
|
'required': true,
|
|
'type': 'integer',
|
|
'description': 'User ID',
|
|
}
|
|
],
|
|
'responses': <String, dynamic>{
|
|
'200': <String, dynamic>{
|
|
'description': 'Success',
|
|
'schema': <String, dynamic>{'type': 'object'},
|
|
}
|
|
},
|
|
'requestBody': <String, dynamic>{
|
|
'description': 'User data',
|
|
'required': true,
|
|
'content': <String, dynamic>{
|
|
'application/json': <String, dynamic>{
|
|
'schema': <String, dynamic>{'type': 'object'},
|
|
},
|
|
},
|
|
},
|
|
'deprecated': true,
|
|
};
|
|
|
|
final path = ApiPath.fromJson('/api/users/{id}', 'PUT', json);
|
|
|
|
expect(path.path, '/api/users/{id}');
|
|
expect(path.method, HttpMethod.put);
|
|
expect(path.summary, 'Get users');
|
|
expect(path.description, 'Retrieve all users');
|
|
expect(path.operationId, 'getUsers');
|
|
expect(path.tags, ['User']);
|
|
expect(path.parameters, hasLength(1));
|
|
expect(path.responses, hasLength(1));
|
|
expect(path.requestBody, isNotNull);
|
|
expect(path.deprecated, true);
|
|
});
|
|
|
|
test('creates ApiPath from JSON with minimal fields', () {
|
|
final json = <String, dynamic>{};
|
|
|
|
final path = ApiPath.fromJson('/api/users', 'GET', json);
|
|
|
|
expect(path.path, '/api/users');
|
|
expect(path.method, HttpMethod.get);
|
|
expect(path.summary, '');
|
|
expect(path.description, '');
|
|
expect(path.operationId, '');
|
|
expect(path.tags, isEmpty);
|
|
expect(path.parameters, isEmpty);
|
|
expect(path.responses, isEmpty);
|
|
expect(path.requestBody, isNull);
|
|
expect(path.deprecated, false);
|
|
});
|
|
});
|
|
|
|
group('ApiModel fromJson edge cases', () {
|
|
test('creates ApiModel with nullable properties', () {
|
|
final json = {
|
|
'description': 'User model',
|
|
'properties': {
|
|
'id': {
|
|
'type': 'integer',
|
|
'description': 'User ID',
|
|
'nullable': true,
|
|
},
|
|
'name': {
|
|
'type': 'string',
|
|
'description': 'User name',
|
|
'nullable': false,
|
|
},
|
|
},
|
|
};
|
|
|
|
final model = ApiModel.fromJson('User', json);
|
|
|
|
expect(model.properties['id']!.nullable, true);
|
|
expect(model.properties['id']!.required, false);
|
|
expect(model.properties['name']!.nullable, false);
|
|
expect(model.properties['name']!.required, true);
|
|
expect(model.required, ['name']);
|
|
});
|
|
|
|
test('creates ApiModel with explicit required fields', () {
|
|
final json = {
|
|
'description': 'User model',
|
|
'properties': {
|
|
'id': {
|
|
'type': 'integer',
|
|
'description': 'User ID',
|
|
'nullable': true,
|
|
},
|
|
'name': {
|
|
'type': 'string',
|
|
'description': 'User name',
|
|
'nullable': false,
|
|
},
|
|
},
|
|
'required': ['id', 'name'],
|
|
};
|
|
|
|
final model = ApiModel.fromJson('User', json);
|
|
|
|
expect(model.properties['id']!.required, true);
|
|
expect(model.properties['name']!.required, true);
|
|
expect(model.required, ['id', 'name']);
|
|
});
|
|
});
|
|
|
|
group('ApiProperty complex types', () {
|
|
test('creates ApiProperty with nested object', () {
|
|
final json = {
|
|
'type': 'object',
|
|
'description': 'User address',
|
|
'properties': {
|
|
'street': {'type': 'string'},
|
|
'city': {'type': 'string'},
|
|
},
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('address', json, []);
|
|
|
|
expect(property.type, PropertyType.object);
|
|
expect(property.description, 'User address');
|
|
expect(property.required, false);
|
|
});
|
|
|
|
test('creates ApiProperty with array of primitives', () {
|
|
final json = {
|
|
'type': 'array',
|
|
'description': 'User tags',
|
|
'items': {
|
|
'type': 'string',
|
|
},
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('tags', json, []);
|
|
|
|
expect(property.type, PropertyType.array);
|
|
expect(property.description, 'User tags');
|
|
expect(property.items, isNotNull);
|
|
expect(property.items!.name, 'string');
|
|
});
|
|
|
|
test('creates ApiProperty with array of objects', () {
|
|
final json = {
|
|
'type': 'array',
|
|
'description': 'User list',
|
|
'items': {
|
|
'type': 'object',
|
|
'\$ref': '#/components/schemas/User',
|
|
},
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('users', json, []);
|
|
|
|
expect(property.type, PropertyType.array);
|
|
expect(property.description, 'User list');
|
|
expect(property.items, isNotNull);
|
|
expect(property.items!.name, 'User');
|
|
});
|
|
|
|
test('creates ApiProperty with file type', () {
|
|
final json = {
|
|
'type': 'file',
|
|
'description': 'User avatar',
|
|
'format': 'binary',
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('avatar', json, []);
|
|
|
|
expect(property.type, PropertyType.file);
|
|
expect(property.description, 'User avatar');
|
|
expect(property.format, 'binary');
|
|
});
|
|
|
|
test('creates ApiProperty with date type', () {
|
|
final json = {
|
|
'type': 'string',
|
|
'format': 'date',
|
|
'description': 'User birth date',
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('birthDate', json, []);
|
|
|
|
expect(property.type, PropertyType.string);
|
|
expect(property.format, 'date');
|
|
expect(property.description, 'User birth date');
|
|
});
|
|
|
|
test('creates ApiProperty with date-time type', () {
|
|
final json = {
|
|
'type': 'string',
|
|
'format': 'date-time',
|
|
'description': 'User created at',
|
|
};
|
|
|
|
final property = ApiProperty.fromJson('createdAt', json, []);
|
|
|
|
expect(property.type, PropertyType.string);
|
|
expect(property.format, 'date-time');
|
|
expect(property.description, 'User created at');
|
|
});
|
|
});
|
|
|
|
group('Error handling and edge cases', () {
|
|
test('ApiParameter fromJson handles missing fields gracefully', () {
|
|
final json = <String, dynamic>{};
|
|
|
|
final param = ApiParameter.fromJson(json);
|
|
|
|
expect(param.name, '');
|
|
expect(param.location, ParameterLocation.query);
|
|
expect(param.required, false);
|
|
expect(param.type, PropertyType.string);
|
|
expect(param.description, '');
|
|
expect(param.format, isNull);
|
|
expect(param.example, isNull);
|
|
expect(param.defaultValue, isNull);
|
|
});
|
|
|
|
test('ApiResponse fromJson handles missing fields gracefully', () {
|
|
final json = <String, dynamic>{};
|
|
|
|
final response = ApiResponse.fromJson('200', json);
|
|
|
|
expect(response.code, '200');
|
|
expect(response.description, '');
|
|
expect(response.schema, isNull);
|
|
expect(response.content, isNull);
|
|
});
|
|
|
|
test('ApiRequestBody fromJson handles missing fields gracefully', () {
|
|
final json = <String, dynamic>{};
|
|
|
|
final requestBody = ApiRequestBody.fromJson(json);
|
|
|
|
expect(requestBody.description, '');
|
|
expect(requestBody.required, false);
|
|
expect(requestBody.content, isNull);
|
|
});
|
|
|
|
test('PropertyType fromString handles unknown types', () {
|
|
expect(PropertyType.fromString('unknown'), PropertyType.string);
|
|
expect(PropertyType.fromString(''), PropertyType.string);
|
|
expect(PropertyType.fromString('CUSTOM_TYPE'), PropertyType.string);
|
|
});
|
|
|
|
test('ParameterLocation fromString handles unknown locations', () {
|
|
expect(ParameterLocation.fromString('unknown'), ParameterLocation.query);
|
|
expect(ParameterLocation.fromString(''), ParameterLocation.query);
|
|
expect(ParameterLocation.fromString('CUSTOM_LOCATION'),
|
|
ParameterLocation.query);
|
|
});
|
|
|
|
test('HttpMethod fromString handles unknown methods', () {
|
|
expect(HttpMethod.fromString('unknown'), HttpMethod.get);
|
|
expect(HttpMethod.fromString(''), HttpMethod.get);
|
|
expect(HttpMethod.fromString('CUSTOM_METHOD'), HttpMethod.get);
|
|
});
|
|
});
|
|
}
|