2016-03-15 26 views
1

我想为我的应用使用测试。我已经检查了文档以及A. Retter在书中的特定章节。但是,我不知道如何使用其他类型的参数(特别是节点)。我的功能通常比转换数字或字符串的功能要高得多。例如,我想测试一个以文档(节点)作为参数的函数,并返回a)一个HTML文件; b)一个pdf文件。如何使用XQSuite测试eXist-db中的复杂功能

测试功能(在这里,我尝试测试上面提到的第一个功能):

xquery version "3.0"; 

module namespace cust = 'http://46.28.111.241:8081/exist/db/apps/karolinum-x/modules/cust'; 

declare namespace tei = 'http://www.tei-c.org/ns/1.0'; 
declare namespace test = 'http://exist-db.org/xquery/xqsuite'; 
declare 
    %test:args('<TEI xmlns="http://www.tei-c.org/ns/1.0"> 
        <text> 
         <body> 
          <div n="1"> 
           <head>Heading</head> 
           <p>paragraph</p> 
          </div> 
         </body> 
        </text>') 
    %test:assertXPath('$result/html') 
function cust:transform($doc as node()*) as node() { 
    let $styles := doc('/db/apps/karolinum-x/resources/xslt/style-web.xsl') 
    let $document := 
     (
      <book n='1'>{($doc//tei:div[@n='1'])[1]}</book> 
     ) 
    let $finale := transform:transform($document, $styles,()) 
    return $finale 
}; 

测试运行:

xquery version "3.0"; 

import module namespace test = 'http://exist-db.org/xquery/xqsuite' at 'resource:org/exist/xquery/lib/xqsuite/xqsuite.xql'; 

test:suite(inspect:module-functions(xs:anyURI('test-test.xqm'))) 

结果:

<testsuites> 
    <testsuite package="http://46.28.111.241:8081/exist/db/apps/karolinum-x/modules/cust" timestamp="2016-03-15T12:53:16.5+01:00" failures="0" pending="0" tests="1" time="PT0.002S"> 
     <testcase name="transform" class="cust:transform"> 
      <error type="err:XPTY0004" message="It is a type error if, 
       during the static analysis phase, an expression is found to have a 
       static type that is not appropriate for the context in which the 
       expression occurs, or during the dynamic evaluation phase, the dynamic 
       type of a value does not match a required type as specified by the 
       matching rules in 2.5.4 SequenceType Matching."/> 
     </testcase> 
    </testsuite> 
</testsuites> 

回答

2

好了,所以两件事情:

  1. 您错过了您通过%test:args注释注入的XML中的关闭</TEI>

  2. 您似乎在XQSuite中发现了一个错误。我可以确认在您的%test:args中使用带有没有明确类型或明确类型为node()的函数参数的XML不起作用。你能否请在https://github.com/exist-db/exist/issues上为此打开一个问题。作为一种解决方法,如果你将$doc as node()*更改为$doc as element()*那么这似乎工作。

p.s.您的测试看起来有点脆弱,因为您在测试中硬编码数据库文档的路径,更好地注入路径,或者更好,以避免函数内的副作用函数(如doc()和collection()正在测试中。

+0

非常感谢提示,它真的帮了大忙!当然,我会解决这个问题。 –

+0

剩下的唯一问题是我不能让'assertXPath'通过:)我会认为'($ result/html)'应该可以工作,但它不会。 –

+0

哇,真的不明白为什么assert不起作用。 '$ result // @ *'工作,'$ result // *'工作,'$ result // * [@ class =“chapter”]'工作,'$ result/html **不**。我在这里错过了什么?这里不可能获取任何元素,即使是'$ result // html'或'$ result // body' **都不起作用。 –