我正在创建一个只读 PostgreSQL 数据库。由于填充后它不会被修改,我想运行一些测试以确保数据是好的。带有 ORM mixin 的 TestHarness 为每个测试创建一个空数据库。我可以从测试中访问真实的数据库吗?
我正在从 Aqueduct Slack 频道移出一个问答,以供公众参考。
减少的答案:
您可以启动一个服务器并获取它的 mangedContext.:
final app = Application<YourChannel>() ..options.configurationFilePath = theConfigString ..options.port = somePort; await app.startOnCurrentIsolate(); final context = app.channel.context; // Now you can use the context to access the data base
这就是我在上下文中实现它的方式:
import "package:test/test.dart";
import 'package:my_server/model/text_line.dart';
import '../harness/app.dart';
Future main() async {
// setup
final app = Application<MyServerChannel>()
..options.configurationFilePath = 'config.yaml';
await app.startOnCurrentIsolate();
final context = app.channel.context;
test('Database has the right number of rows', () async {
final query = Query<TextLine>(context);
final numRows = await query.reduce.count();
expect(numRows, 43845);
});
}