XQuery Unit testing in eXist-1.4

[UPDATE 2011-01-19]: As of revisions 13587 and 13589, the XQuery Unit Testing framework has been ported back from eXist-trunk to the eXist-1.4.x branch. While obsolescing the need for the XSLT stylesheet presented in this blog post, I’ll leave the latter here for the sake of documentation. eXist users who want to test XQueries in eXist-1.4 now are encouraged to use its built-in XQuery Unit Testing framework instead.

[UPDATE 2011-01-05]: The XSLT stylesheet has been extended with missing features:

  • [feature]: added @trace handling
  • [feature]: added <xpath> handling
  • [feature]: added <store-files> handling
  • [feature]: added context handling for util:eval()
  • [fix]: <![CDATA[ ]]> in output: spaces required…

[UPDATE 2010-12-09]: The XSLT stylesheet has been substantially reworked, to produce

  • more legible XQuery code
  • more reliable XQuery code, taking into account serialization options, and deriving the most sensible highlight-matches settings where necessary

Currently, I’m heavily porting old XQuery code to the latest version of the eXist XML database’s new Lucene FT index and search capabilities. In doing so, I’m hitting a couple of bugs in this area, that I’m trying to isolate, test and report as clearly as possible. This post discusses a means to use the same test files for both eXist-1.4 and eXist-trunk.

The addition of the XQuery Unit testing framework in eXist-trunk code has proven to be a tremendously great help in developing test cases. On the other hand, at the time of writing this post, this functionality was not yet available in eXist-1.4, making it cumbersome to (manually) set up a testing environment, executing different tests and compare their results with expected results. Still, I wanted to be able to compare the same test queries in both versions, and see where differences are (and in my case, help deciding on which version to continue with, as my development schedule is tightening up).

In order to do so, I’ve created an XSLT stylesheet UnitTest2XQuery.xsl that converts XQuery Unit test XML files to XQuery code which is natively executable in eXist-1.4. It should generate identical reports to those of the XQuery Unit testing framework in eXist-trunk, making it easy to quickly compare results of both eXist versions for identical queries.

I’m not going into detail on the stylesheet here (nothing fancy, really), but will add some remarks on how to use it. Ideally, this stylesheet could be stored in the db (say, as ‘/db/UnitTest2XQuery.xsl’) and put to work as follows:

let $test :=
<TestSet>
    <testName>really simple test</testName>
    <description>
        <p>just a simple test, really</p>
        <author>Ron Van den Branden</author>
    </description>
    <setup>
        <create-collection parent="/db" name="coll"/>
        <store collection="/db/coll" name="test.xml">
            <p att1="val1" att2="val2">this is a test document</p>
        </store>
    </setup>
    <functions><![CDATA[
        declare function local:echo($node) {
        <echo>{$node/node()}</echo>
        };
        ]]></functions>
    <tearDown>
        <remove-collection collection="/db/coll"/>
    </tearDown>
    <test output="text">
        <task>really simple test, text</task>
        <code><![CDATA[
            local:echo(collection('/db/coll')//p)/string()
            ]]></code>
        <expected>this is a test document</expected>
    </test>
    <test output="xml">
        <task>really simple test, xml</task>
        <code><![CDATA[
            local:echo(collection('/db/coll')//p)
            ]]></code>
        <expected><echo>this is a test document</echo></expected>
    </test>
</TestSet>

return util:eval(transform:transform($test, doc('/db/UnitTest2XQuery.xsl'), ()))

Unfortunately, eXist’s transform:transform() function does ugly things to text that has been processed with disable-output-escaping. Instead of properly escaped text, the unescaped text is copied, wrapped in processing instructions:

<?javax.xml.transform.disable-output-escaping?>

        declare function local:echo($node) {
        &lt;echo&gt;{$node/node()}&lt;/echo&gt;
        };

<?javax.xml.transform.enable-output-escaping?>

Clearly, this can’t be interpreted as XQuery. Hence, the best way to use the stylesheet is by batch-transforming XQuery Unit test files with an XSLT processor, and then execute the resulting XQuery code with eXist.

Conclusion

I guess the most important conclusion is: grab the XQueryUnit2XQuery.xsl stylesheet and see if it helps you in developing XQuery code for eXist!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: