0

我正在创建一个只读 PostgreSQL 数据库。由于填充后它不会被修改,我想运行一些测试以确保数据是好的。带有 ORM mixin 的 TestHarness 为每个测试创建一个空数据库。我可以从测试中访问真实的数据库吗?

我正在从 Aqueduct Slack 频道移出一个问答,以供公众参考。

4

1 回答 1

0

减少的答案:

您可以启动一个服务器并获取它的 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);
  });

}
于 2020-02-03T01:14:48.587 回答