Database Reference
In-Depth Information
ous and remove our test collection, as is good practice. Indeed, we can do such a
thing by providing custom setup and teardown functions that run before and after
our test functions, respectively. To declare a function as a setup or teardown func‐
tion, you can use the %test:setUp and %test:tearDown annotations. So let's look
briefly at a refactored id:generate function with test cases (see the file id-4.xqm ), as
shown in Example 16-6 .
Example 16-6. Refactored id.xqm to inject collection path (id-4.xqm)
declare
%test:setUp
function id:_test-setup() {
xmldb:create-collection("/db", "test-records"),
xmldb:store("/db/test-records", (), <record><id>12345678</id></record>),
xmldb:store("/db/test-records", (), <record><id>abcdefgh</id></record>)
};
declare
%test:tearDown
function id:_test-teardown() {
xmldb:remove("/db/test-records")
};
declare
%test:args("/db/test-records")
%test:assertXPath("$result ne '12345678'")
%test:assertXPath("$result ne 'abcdefgh'")
function id:generate($records-collection as xs:string) as xs:string {
let $id := id:random()
return
if(exists(collection($records-collection)/record/id[. eq $id]))then
id:generate($records-collection)
else
$id
};
The %test:setUp annotation will cause the id:_test-setup function to be
executed once before each function under test.
In this instance, we create the test data collection /db/test-records and place two
records in it containing the id s 12345678 and abcdefgh .
The %test:tearDown annotation will cause the id:_test-teardown function to
be executed once after each function under test.
In this instance, we clean up the test data we created in the before step by
removing the collection /db/test-records .
Search WWH ::




Custom Search