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

时间:2017-12-25 12:46:18

标签: hyperledger-composer

我正在使用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
}

如果我删除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

但是,我似乎无法访问嵌入式编辑器环境中的底层数据库对象,以配置测试工作的索引。

有没有办法让它起作用?

1 个答案:

答案 0 :(得分:0)

当前,我们通过修改测试命令以在测试之前注释掉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"

这有点棘手,不能解决PouchDB不处理索引的复杂问题,但总比不进行测试要好。