9f94cc97b7
- backend/testutil/setup.go: SQLite shops 表补充 business_hours 列 - stock_in_screen_test: 入库管理双 Tab 重构后,draft/pending 测试改为先切换到「入库审核」Tab - stock_out_insufficient_stock_flow_test: Inventory 构造参数 productUnit→unit,补充必填 id - inventory_repository_test: mock 数据改为平铺字段格式,补充必填 id - docs: 新增 NAS + Gitea 部署文档(Runner 设置、Shadowrocket 中继说明) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
272 lines
7.6 KiB
Dart
272 lines
7.6 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http_mock_adapter/http_mock_adapter.dart';
|
|
import 'package:jiu_client/core/api/api_client.dart';
|
|
import 'package:jiu_client/core/exceptions.dart';
|
|
import 'package:jiu_client/repositories/inventory_repository.dart';
|
|
|
|
const _baseUrl = 'http://localhost:8080/api/v1';
|
|
|
|
class _TestApiClient extends ApiClient {
|
|
final Dio _testDio;
|
|
_TestApiClient(this._testDio) : super(token: 'test-token');
|
|
|
|
@override
|
|
Future<Response> get(String path, {Map<String, dynamic>? params}) =>
|
|
_testDio.get(path, queryParameters: params);
|
|
|
|
@override
|
|
Future<Response> post(String path, {dynamic data}) =>
|
|
_testDio.post(path, data: data);
|
|
|
|
@override
|
|
Future<Response> put(String path, {dynamic data}) =>
|
|
_testDio.put(path, data: data);
|
|
|
|
@override
|
|
Future<Response> delete(String path) => _testDio.delete(path);
|
|
}
|
|
|
|
void main() {
|
|
late Dio dio;
|
|
late DioAdapter adapter;
|
|
late InventoryRepository repo;
|
|
|
|
setUp(() {
|
|
dio = Dio(BaseOptions(baseUrl: _baseUrl));
|
|
adapter = DioAdapter(dio: dio, matcher: const FullHttpRequestMatcher());
|
|
repo = InventoryRepository(_TestApiClient(dio));
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// listInventory()
|
|
// ---------------------------------------------------------------------------
|
|
group('InventoryRepository.listInventory()', () {
|
|
test('returns PageResult with inventory items on 200', () async {
|
|
adapter.onGet(
|
|
'/inventory',
|
|
(server) => server.reply(200, {
|
|
'data': [
|
|
{
|
|
'id': 1,
|
|
'warehouse_id': 1,
|
|
'warehouse_name': '主仓库',
|
|
'product_id': 1,
|
|
'product_name': '五粮液',
|
|
'product_code': 'P001',
|
|
'spec': '500ml',
|
|
'unit': '瓶',
|
|
'brand': '五粮液集团',
|
|
'min_stock': 10,
|
|
'quantity': 50.0,
|
|
}
|
|
],
|
|
'total': 1,
|
|
'page': 1,
|
|
'page_size': 50,
|
|
}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
final result = await repo.listInventory();
|
|
|
|
expect(result.total, 1);
|
|
expect(result.data.length, 1);
|
|
expect(result.data.first.productName, '五粮液');
|
|
expect(result.data.first.quantity, 50.0);
|
|
expect(result.data.first.warehouseId, 1);
|
|
});
|
|
|
|
test('returns empty list without error', () async {
|
|
adapter.onGet(
|
|
'/inventory',
|
|
(server) => server.reply(200, {
|
|
'data': [],
|
|
'total': 0,
|
|
'page': 1,
|
|
'page_size': 50,
|
|
}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
final result = await repo.listInventory();
|
|
expect(result.data, isEmpty);
|
|
expect(result.total, 0);
|
|
});
|
|
|
|
test('passes warehouseId and keyword params', () async {
|
|
adapter.onGet(
|
|
'/inventory',
|
|
(server) => server.reply(200, {
|
|
'data': [],
|
|
'total': 0,
|
|
'page': 1,
|
|
'page_size': 50,
|
|
}),
|
|
queryParameters: {
|
|
'page': 1,
|
|
'page_size': 50,
|
|
'warehouse_id': 1,
|
|
'keyword': '五粮液',
|
|
},
|
|
);
|
|
|
|
final result = await repo.listInventory(warehouseId: 1, keyword: '五粮液');
|
|
expect(result.data, isEmpty);
|
|
});
|
|
|
|
test('401 throws AppException', () async {
|
|
adapter.onGet(
|
|
'/inventory',
|
|
(server) => server.reply(401, {'error': 'unauthorized'}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
expect(
|
|
() => repo.listInventory(),
|
|
throwsA(predicate<AppException>((e) => e.statusCode == 401)),
|
|
);
|
|
});
|
|
|
|
test('400 throws AppException with server error message', () async {
|
|
adapter.onGet(
|
|
'/inventory',
|
|
(server) => server.reply(400, {'error': 'invalid warehouse_id'}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
expect(
|
|
() => repo.listInventory(),
|
|
throwsA(
|
|
predicate<AppException>(
|
|
(e) => e.message == 'invalid warehouse_id' && e.statusCode == 400,
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('network timeout throws AppException', () async {
|
|
final badDio = Dio(BaseOptions(
|
|
baseUrl: 'http://localhost:19999',
|
|
connectTimeout: const Duration(milliseconds: 100),
|
|
));
|
|
final badRepo = InventoryRepository(_TestApiClient(badDio));
|
|
|
|
try {
|
|
await badRepo.listInventory();
|
|
fail('Expected AppException');
|
|
} on AppException catch (e) {
|
|
expect(e.message, isNotEmpty);
|
|
}
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// listLogs()
|
|
// ---------------------------------------------------------------------------
|
|
group('InventoryRepository.listLogs()', () {
|
|
test('returns PageResult with inventory logs on 200', () async {
|
|
adapter.onGet(
|
|
'/inventory/logs',
|
|
(server) => server.reply(200, {
|
|
'data': [
|
|
{
|
|
'warehouse_id': 1,
|
|
'product_id': 1,
|
|
'warehouse': {'name': '主仓库'},
|
|
'product': {'name': '五粮液'},
|
|
'direction': 'in',
|
|
'quantity': 20.0,
|
|
'qty_before': 30.0,
|
|
'qty_after': 50.0,
|
|
'ref_type': 'stock_in',
|
|
'ref_id': 5,
|
|
'created_at': '2024-01-10T10:00:00Z',
|
|
}
|
|
],
|
|
'total': 1,
|
|
'page': 1,
|
|
'page_size': 50,
|
|
}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
final result = await repo.listLogs();
|
|
|
|
expect(result.total, 1);
|
|
expect(result.data.length, 1);
|
|
expect(result.data.first.direction, 'in');
|
|
expect(result.data.first.quantity, 20.0);
|
|
expect(result.data.first.qtyBefore, 30.0);
|
|
expect(result.data.first.qtyAfter, 50.0);
|
|
});
|
|
|
|
test('returns empty list without error', () async {
|
|
adapter.onGet(
|
|
'/inventory/logs',
|
|
(server) => server.reply(200, {
|
|
'data': [],
|
|
'total': 0,
|
|
'page': 1,
|
|
'page_size': 50,
|
|
}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
final result = await repo.listLogs();
|
|
expect(result.data, isEmpty);
|
|
});
|
|
|
|
test('passes warehouseId and productId params', () async {
|
|
adapter.onGet(
|
|
'/inventory/logs',
|
|
(server) => server.reply(200, {
|
|
'data': [],
|
|
'total': 0,
|
|
'page': 1,
|
|
'page_size': 50,
|
|
}),
|
|
queryParameters: {
|
|
'page': 1,
|
|
'page_size': 50,
|
|
'warehouse_id': 1,
|
|
'product_id': 1,
|
|
},
|
|
);
|
|
|
|
final result = await repo.listLogs(warehouseId: 1, productId: 1);
|
|
expect(result.data, isEmpty);
|
|
});
|
|
|
|
test('401 throws AppException', () async {
|
|
adapter.onGet(
|
|
'/inventory/logs',
|
|
(server) => server.reply(401, {'error': 'unauthorized'}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
expect(
|
|
() => repo.listLogs(),
|
|
throwsA(predicate<AppException>((e) => e.statusCode == 401)),
|
|
);
|
|
});
|
|
|
|
test('400 throws AppException with server error message', () async {
|
|
adapter.onGet(
|
|
'/inventory/logs',
|
|
(server) => server.reply(400, {'error': 'invalid product_id'}),
|
|
queryParameters: {'page': 1, 'page_size': 50},
|
|
);
|
|
|
|
expect(
|
|
() => repo.listLogs(),
|
|
throwsA(
|
|
predicate<AppException>(
|
|
(e) => e.message == 'invalid product_id' && e.statusCode == 400,
|
|
),
|
|
),
|
|
);
|
|
});
|
|
});
|
|
}
|