2016-03-15 30 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' **都不起作用。 –