124 lines
3.8 KiB
Dart
124 lines
3.8 KiB
Dart
// 测试 BasePageResult 包裹逻辑
|
|
// 验证包含 total 和 items 的分页响应模型是否正确转换为 BasePageResult
|
|
|
|
import 'package:swagger_generator_flutter/core/models.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
group('BasePageResult 包裹逻辑测试', () {
|
|
test('应该识别包含 total 和 items 的分页响应模型', () {
|
|
// 创建一个包含 total 和 items 的模型
|
|
const paginationModel = ApiModel(
|
|
name: 'SuperiorTaskListResultPageResponse',
|
|
description: '分页响应实体类',
|
|
properties: {
|
|
'total': ApiProperty(
|
|
name: 'total',
|
|
type: PropertyType.integer,
|
|
description: '总记录条数',
|
|
required: true,
|
|
),
|
|
'items': ApiProperty(
|
|
name: 'items',
|
|
type: PropertyType.array,
|
|
description: '响应数据',
|
|
required: true,
|
|
items: ApiModel(
|
|
name: 'SuperiorTaskListResult',
|
|
description: 'Item type',
|
|
properties: {},
|
|
required: [],
|
|
),
|
|
),
|
|
},
|
|
required: ['total', 'items'],
|
|
);
|
|
|
|
// 验证模型是否被识别为分页模型
|
|
expect(paginationModel.properties.containsKey('total'), isTrue);
|
|
expect(paginationModel.properties.containsKey('items'), isTrue);
|
|
expect(paginationModel.properties['total']!.type, PropertyType.integer);
|
|
expect(paginationModel.properties['items']!.type, PropertyType.array);
|
|
|
|
// 验证 total 是数值类型
|
|
final totalProp = paginationModel.properties['total']!;
|
|
expect(
|
|
totalProp.type == PropertyType.integer ||
|
|
totalProp.type == PropertyType.number,
|
|
isTrue,
|
|
reason: 'total 字段应该是数值类型',
|
|
);
|
|
|
|
// 验证 items 是数组类型
|
|
final itemsProp = paginationModel.properties['items']!;
|
|
expect(
|
|
itemsProp.type == PropertyType.array,
|
|
isTrue,
|
|
reason: 'items 字段应该是数组类型',
|
|
);
|
|
});
|
|
|
|
test('分页模型的 items 类型应该被正确提取', () {
|
|
const itemsProperty = ApiProperty(
|
|
name: 'items',
|
|
type: PropertyType.array,
|
|
description: '数据列表',
|
|
required: true,
|
|
items: ApiModel(
|
|
name: 'SuperiorTaskListResult',
|
|
description: 'Task result',
|
|
properties: {},
|
|
required: [],
|
|
),
|
|
);
|
|
|
|
expect(itemsProperty.items, isNotNull);
|
|
expect(itemsProperty.items!.name, 'SuperiorTaskListResult');
|
|
});
|
|
|
|
test('非分页模型不应该被识别为分页模型', () {
|
|
const normalModel = ApiModel(
|
|
name: 'NormalResult',
|
|
description: '普通响应',
|
|
properties: {
|
|
'id': ApiProperty(
|
|
name: 'id',
|
|
type: PropertyType.integer,
|
|
description: 'ID',
|
|
required: true,
|
|
),
|
|
'name': ApiProperty(
|
|
name: 'name',
|
|
type: PropertyType.string,
|
|
description: '名称',
|
|
required: true,
|
|
),
|
|
},
|
|
required: ['id', 'name'],
|
|
);
|
|
|
|
// 验证不包含 total 和 items
|
|
expect(normalModel.properties.containsKey('total'), isFalse);
|
|
expect(normalModel.properties.containsKey('items'), isFalse);
|
|
});
|
|
|
|
test('验证分页响应模型的命名模式', () {
|
|
// 典型的分页响应模型命名
|
|
final pageResponseNames = [
|
|
'SuperiorTaskListResultPageResponse',
|
|
'FeedBackInfoPageResponse',
|
|
'ManagerDataCollectionResultPageResponse',
|
|
'ClassesTaskListResultPageResponse',
|
|
];
|
|
|
|
for (final name in pageResponseNames) {
|
|
expect(
|
|
name.endsWith('PageResponse'),
|
|
isTrue,
|
|
reason: '$name 应该以 PageResponse 结尾',
|
|
);
|
|
}
|
|
});
|
|
});
|
|
}
|