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; otherwise, the result document is indiscernible from the
original.
That is to say, it has the same
descendant nodes
(we check this as the function is
really creating a copy of the input and modifying it).
Arguably, the second and third test cases just described have some
overlap and could be merged into one. However, having two sepa‐
rate tests gives us more granularity in understanding the problems
if or when tests fail. For instance, it is entirely possible that the sec‐
ond test case could pass while the third test case fails, which tells us
the issue is with copying the descendant nodes and not with the
generation of the
id
element. Writing fine-grained tests that allow
you to quickly discover where bugs or regressions occur can help
expedite bug fixes.
Let's now look at how we might write our first test case, which was:
If we send it a document that already has an
id
element, it raises the error
id:ERR-
PRESENT
.
We will begin by modifying the
id:insert
function by adding some XQSuite anno‐
tations (see the file
id-1.xqm
), as shown in
Example 16-2
.
Example 16-2. id.xqm with first test case (id-1.xqm)
xquery version "3.0";
module namespace id = "http://example.com/record/id";
import module namespace util = "http://exist-db.org/xquery/util";
declare namespace test = "http://exist-db.org/xquery/xqsuite";
declare variable $id:ERR-PRESENT := xs:QName("id:ERR-PRESENT");
declare
%test:args("<record><id>existing</id></record>")
%test:assertError("id:ERR-PRESENT")
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/@*,
Search WWH ::
Custom Search