Database Reference
In-Depth Information
If we send it a document without an
id
element, it adds an
id
element for us with
some sort of identifier.
We again modify our
id:insert
function to insert the additional test case (see the
file
id-2.xqm
), as shown in
Example 16-4
.
Example 16-4. id.xqm with two test cases (id-2.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))")
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 have added a second
%test:args
annotation to our function for our second
test case. Every
%test:args
or set of
%test:arg
annotations, delimited by one or
more
assert
annotations, forms a distinct test case.
For this test case we have two assertions—first that the result contains an element
id
, and second that the
id
has some child content. You may have as many asser‐
tions about the result of the function for each test case as you wish.
In this test case we are using
%test:assertXPath
instead of
%test:assertError
,
as before.
%test:assertXPath
allows us to evaluate an arbitrary XPath expres‐
sion against the
$result
of the function.
Finally, let's look at how we would add our final test case, which was:
If we send it a document without an
id
element, it adds an
id
element for us with
some sort of identifier; otherwise, the result document is indiscernible from the origi‐
nal.
We again modify our
id:insert
function to add the final test case (see the file
id-3.xqm
), as shown in
Example 16-5
.
Search WWH ::
Custom Search