Database Reference
In-Depth Information
Example 16-5. id.xqm with three test cases (id-3.xqm)
declare
%test:args("<record><id>existing</id></record>")
%test:assertError("id:ERR-PRESENT")
%test:args("<record/>")
%test:assertXPath("$result/exists(id)")
%test:assertXPath("not($result/empty(id))")
%test:args("<record a='1'><child1>text1</child1></record>")
%test:assertXPath("$result/exists(id)")
%test:assertXPath("$result/@a eq '1'")
%test:assertXPath("local-name(($result/child::element())[1]) eq 'id'")
%test:assertXPath("local-name(($result/child::element())[2]) eq 'child1'")
%test:assertXPath("$result/child1/text() eq 'text1'")
function id:insert($record as element(record)) as element(record) {
if($record/id)then
fn:error($id:ERR-PRESENT, "<id> is already present in record!", $record/id)
else
<record>
{
$record/@*,
<id>{id:generate()}</id>,
$record/node()
}
</record>
};
We set the argument for the function for our test case to be an XML element
that contains both attributes and descendant nodes, as we want to make sure
the result is properly constructed.
This is the same assertion as from our last test case, to ensure that an
id
ele‐
ment is added to the record.
These are several assertions to ensure that the
record
element returned by
the function contains all of the nodes in the same order that the
record
ele‐
ment provided them as the argument to the function.
Executing our test runner again (
test-runner.xq
) now produces an xUnit result docu‐
ment similar to the following:
<testsuites>
<testsuite
package=
"http://example.com/record/id"
timestamp=
"2014-07-02T18:26:29.784+01:00"
failures=
"0"
tests=
"3"
time=
"PT0.064S"
>
<testcase
name=
"insert"
class=
"id:insert"
/>
<testcase
name=
"insert"
class=
"id:insert"
/>
<testcase
name=
"insert"
class=
"id:insert"
/>
Search WWH ::
Custom Search