在使用嵌入式运行时进行测试时使用带有 ORDER BY 的查询

Using queries with ORDER BY when testing with the embedded runtime

我正在使用 TDD 方法开发我的作曲家应用程序,因此重要的是所有代码都可以 运行 在嵌入式 运行 测试中使用的时间。

我的绊脚石是我无法在测试中使用 ORDER BY 子句进行查询。

这是我的模型的一个片段:

asset AssetStatement identified by statementRevisionId {
  o String statementRevisionId
  o String statementId
  o DateTime createdAt
  o String effectiveDate regex=/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
  o Integer revision range=[0,]
  o Boolean actual
  o AssetStatementInfo info
}

这是查询:

query FindActualAssetStatement {
  description: "Finds actual asset statement with given (non-unique) id"
  statement:
    SELECT com.assetrisk.AssetStatement
    WHERE (actual == true AND statementId == _$id)
    ORDER BY revision DESC
    LIMIT 1
}

如果我删除了查询 运行s 的 ORDER BY 行,但是当它存在时我得到以下异常:

Error: Cannot sort on field(s) "revision" when using the default index
 at validateSort (node_modules/pouchdb-find/lib/index.js:472:13)
 at node_modules/pouchdb-find/lib/index.js:1138:5
 at <anonymous>
 at process._tickCallback (internal/process/next_tick.js:188:7)

即使我使用资产的唯一键字段进行排序,也会发生同样的错误。

这似乎是 PouchDB 的一个已知特性: https://github.com/pouchdb/pouchdb/issues/6399

但是,我似乎无法访问嵌入式 composer 环境中的基础数据库对象来为测试配置索引。

有什么方法可以让它发挥作用吗?

目前,我们通过修改测试命令以在测试前注释掉 ORDER BY 语句并在测试后取消注释来绕过此问题:

"test": "sed -i '' -e 's, ORDER BY,// ORDER BY,g' ./queries.qry && mocha -t 0 --recursive && sed -i '' -e 's,// ORDER BY, ORDER BY,g' ./queries.qry"

它有点 hacky,并没有解决 PouchDB 不处理索引的复杂问题,但它可能比根本不测试要好。