Skip to content

Commit aa8c1c5

Browse files
authored
Merge pull request #40 from chartes/unit-tests
Unit tests
2 parents b04fe15 + 0808532 commit aa8c1c5

40 files changed

+1574
-183
lines changed

repo/backend/TEI_add_id.xqm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ declare default element namespace "https://github.com/chartes/dots/";
2525
: @return a sequence of update operations inserting `@xml:id` attributes and replacing `@ref` values.
2626
:)
2727
declare updating function dots.update:addXmlIdToFragment($dbName as xs:string) {
28-
for $fragments in db:get($dbName, $G:fragmentsRegister)//fragment
28+
let $fragmentsRegister := if (db:get($dbName, $G:fragmentsRegister)) then db:get($dbName, $G:fragmentsRegister) else db:get($dbName, concat("/", $G:fragmentsRegister))
29+
for $fragments in $fragmentsRegister//fragment
2930
let $node-id := $fragments/@node-id
3031
let $tei := db:get-id($dbName, $node-id)
3132
where not($tei/@xml:id)

repo/backend/db_switch_build.xqm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ declare %private function dots.build:switcher() {
5858
(:~
5959
: Creates the "dots_default_metadata_mapping.xml" document.
6060
: @return a <metadataMap/> element with XPath-based mappings for title, creator, and publisher metadata.
61+
: @todo vérifier que cette fonction fonctionne correctement. Et comment gérer l'ajout d'un nouveau projet ? Comment faire un update de ces paramètres ?
6162
:)
6263
declare %private function dots.build:metadataMap($rootId as xs:string := "", $rootTitle as xs:string := "", $rootDescription as xs:string := "", $linkXSL as xs:string := "", $defaultEngine as xs:string := "") {
6364
<metadataMap xmlns="https://github.com/chartes/dots/" xmlns:dc="http://purl.org/dc/elements/1.1/"
6465
xmlns:dct="http://purl.org/dc/terms/">{
6566
dots.build:headers("metadataMap"),
6667
<root>
67-
<id>{if ($rootId) then $rootId else "default"}</id>
68-
<title>{if ($rootTitle) then $rootTitle else "default title"}</title>
69-
{if ($rootDescription) then <description>{$rootDescription}</description>}
68+
<id>{if ($rootId != "") then $rootId else "default"}</id>
69+
<title>{if ($rootTitle != "") then $rootTitle else "default title"}</title>
70+
{if ($rootDescription != "") then <description>{$rootDescription}</description>}
7071
</root>,
7172
<mapping>
7273
<dc:title xpath="//titleStmt/title[@type = 'main' or position() = 1]" scope="document"/>

repo/backend/resources_register_builder.xqm

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,22 +269,29 @@ declare function resources:getCollectionMetadata(
269269
return
270270
if ($metadataMap)
271271
then
272-
for $metadata in $metadataMap/node()[@scope = "collection"]
273-
return
274-
if ($metadata/@resourceId = "all")
275-
then
276-
let $key := $metadata/name()
277-
return element {$key} {
278-
if ($metadata/@key) then attribute {"key"} {$metadata/@key},
279-
concat($metadata/@prefix, $metadata, $metadata/@suffix)
280-
}
281-
else
282-
let $source := functx:substring-after-last($metadata/@source, '/')
283-
let $csv-source := $csv-map($source)
284-
return
285-
for $record in $csv-source($collection)
272+
let $metadatas :=
273+
for $metadata in $metadataMap/node()[@scope = "collection"]
274+
return
275+
if ($metadata/@resourceId = "all")
276+
then
277+
let $key := $metadata/name()
278+
return element {$key} {
279+
if ($metadata/@key) then attribute {"key"} {$metadata/@key},
280+
concat($metadata/@prefix, $metadata, $metadata/@suffix)
281+
}
282+
else
283+
let $source := functx:substring-after-last($metadata/@source, '/')
284+
let $csv-source := $csv-map($source)
286285
return
287-
resources:createContent($metadata, $record)
286+
if (exists(map:get($csv-source, $collection)))
287+
then
288+
for $record in $csv-source($collection)
289+
return
290+
resources:createContent($metadata, $record)
291+
else
292+
()
293+
let $title := if ($metadatas/descendant-or-self::dc:title) then () else <dc:title>{$collection}</dc:title>
294+
return ($title, $metadatas)
288295
else <dc:title>{$collection}</dc:title>
289296
};
290297

repo/backend/update/add_collection.xqm

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,31 @@ declare namespace dc = "http://purl.org/dc/elements/1.1/";
1010

1111
declare namespace dots = "https://github.com/chartes/dots/";
1212

13+
(:~ Main function to handle the addition of a collection to the database.
14+
: It updates the resources register, the MaxCiteDepth of the parent collection, and the switcher dots.
15+
: @param $dbName Name of the database
16+
: @param $resourceId Identifier of the collection to add
17+
: @param $parentId Identifier of the parent collection (optional)
18+
:)
1319
declare updating function add_coll:handleAddition($dbName as xs:string, $resourceId as xs:string, $parentId as xs:string := "") {
14-
add_coll:addCollToResourcesReg($dbName, $resourceId, $parentId),
15-
add_coll:updateMaxCiteDepthCollection($dbName, $parentId),
16-
add_coll:addCollToSwitcherDots($dbName, $resourceId)
20+
let $parent := if ($parentId) then $parentId else utils_dots:getIdProject($dbName)
21+
return
22+
(
23+
add_coll:addCollToResourcesReg($dbName, $resourceId, $parent),
24+
add_coll:updateMaxCiteDepthCollection($dbName, $parent),
25+
add_coll:addCollToSwitcherDots($dbName, $resourceId)
26+
)
1727
};
1828

19-
declare updating function add_coll:addCollToResourcesReg($dbName as xs:string, $resourceId as xs:string, $parentId as xs:string := "") {
29+
(:~ Function to add a collection to the resources register.
30+
: It inserts a new <collection/> node with metadata and updates the parent relationship.
31+
: @param $dbName Name of the database
32+
: @param $resourceId Identifier of the collection to add
33+
: @param $parentId Identifier of the parent collection (optional)
34+
: @return Inserts a new <collection/> node into the resources register.
35+
:)
36+
declare updating function add_coll:addCollToResourcesReg($dbName as xs:string, $resourceId as xs:string, $parentId as xs:string) {
2037
let $csv := resources:getCSV-map($dbName, "collection")
21-
let $parent := if ($parentId) then $parentId else utils_dots:getIdProject($dbName)
2238
let $resources_register := db:get($dbName, $G:resourcesRegister)//dots:member
2339
let $metadata := resources:getCollectionMetadata($dbName, $resourceId, $csv)
2440
return
@@ -40,9 +56,8 @@ declare updating function add_coll:addCollToResourcesReg($dbName as xs:string, $
4056
: @param $parentIds identifier of the collection to update
4157
: @return updating the value of the @totalChildren attribute in a <collection/> node.
4258
:)
43-
declare updating %private function add_coll:updateMaxCiteDepthCollection($dbName as xs:string, $parentIds as xs:string) {
44-
let $idParentColl := if ($parentIds = "") then utils_dots:getIdProject($dbName) else $parentIds
45-
let $parent := db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $idParentColl]
59+
declare updating %private function add_coll:updateMaxCiteDepthCollection($dbName as xs:string, $parentId as xs:string) {
60+
let $parent := db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentId]
4661
let $totalChildren := $parent/@totalChildren
4762
return
4863
if ($parent != "")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
xquery version "4.0";
2+
3+
(:~
4+
: This module allows TODO
5+
: @author École nationale des chartes - Philippe Pons
6+
: @since 2025-10-20
7+
: @version 1.0
8+
:)
9+
10+
module namespace doc_to_coll = "backend/update/add_doc_to_collection";
11+
12+
import module namespace G = "globals";
13+
14+
declare namespace dots = "https://github.com/chartes/dots/";
15+
16+
declare updating function doc_to_coll:handleAddition($dbName as xs:string, $document as element(dots:document), $collection as element(dots:collection)) {
17+
doc_to_coll:updateDocumentElement($dbName, $document, $collection),
18+
doc_to_coll:incrementTotalChildren($dbName, $collection)
19+
};
20+
21+
declare updating function doc_to_coll:updateDocumentElement($dbName as xs:string, $document as element(dots:document), $collection as element(dots:collection)) {
22+
let $parentIds := $document/@parentIds
23+
return
24+
replace value of node $parentIds with concat($parentIds, " ", $collection/@dtsResourceId)
25+
};
26+
27+
declare updating function doc_to_coll:incrementTotalChildren($dbName as xs:string, $collection as element(dots:collection)) {
28+
let $totalChildren := $collection/@totalChildren
29+
return
30+
replace value of node $totalChildren with (xs:integer($totalChildren) + 1)
31+
};
32+

repo/backend/update/add_document.xqm

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ xquery version "3.1";
55
: @author École nationale des chartes - Philippe Pons
66
: @since 2025-01-10
77
: @version 1.0
8-
: @todo L'ajout d'un document dans une collection qui n'existe pas est possible. Comment remédier à cela ?
98
:)
109

1110
module namespace add_doc = "backend/update/add_document";
@@ -17,45 +16,59 @@ import module namespace resources = "backend/resources_register_builder";
1716
import module namespace fragments = "backend/fragments_register_builder";
1817
import module namespace dots.update = "backend/TEI_add_id";
1918
import module namespace script = "script";
19+
import module namespace utils_dots = "utils_dots";
2020

2121
declare namespace dots = "https://github.com/chartes/dots/";
2222
declare namespace dc = "http://purl.org/dc/elements/1.1/";
2323
declare namespace tei = "http://www.tei-c.org/ns/1.0";
2424

25-
declare updating function add_doc:handleAddition($dbName, $docPath) {
26-
let $csv := resources:getCSV-map($dbName, "document")
27-
let $csv-frag := resources:getCSV-map($dbName, "fragment")
25+
(:~ Update function to handle the addition of a new document and its fragments to the database and registers.
26+
: @param $dbName db name
27+
: @param $docPath absolute path to the document to add
28+
: @return updates the database and registers with the new document and its fragments
29+
:)
30+
declare updating function add_doc:handleAddition($dbName as xs:string, $docPath as xs:string, $parentId as xs:string) {
31+
let $resourceId := utils_dots:findDocId($docPath)
32+
let $docInRegister := utils_dots:getDocInRegister($dbName, $resourceId)
2833
return
29-
(
30-
add_doc:addDocToDB($dbName, $docPath),
31-
add_doc:addDocToResourcesReg($dbName, $docPath, $csv)
34+
if ($docInRegister)
35+
then script:error(dots_error:documentExists())
36+
else
37+
let $csv := resources:getCSV-map($dbName, "document")
38+
return
39+
(
40+
add_doc:addDocToDB($dbName, $docPath, $parentId),
41+
add_doc:addDocToResourcesReg($dbName, $docPath, $csv)
3242
)
3343
};
3444

35-
declare updating function add_doc:handleFragmentsAddition($dbName as xs:string, $docPath) {
36-
let $csv-frag := resources:getCSV-map($dbName, "fragment")
37-
return
38-
add_doc:addFragInReg($dbName, $docPath, $csv-frag)
39-
};
45+
(:~~~~~~~~~~~~~~~
46+
Update database
47+
~~~~~~~~~~~~~~~~~:)
4048

4149
(:~ Update function to add a new document with a specific path
4250
: @param $dbName db name
4351
: @param $docPath absolute path to the document to add
52+
: @param $parentId identifier of the parent collection. By default, the project identifier.
4453
: @return add the document at the path $docPath to the db $dbName
45-
: @todo the script using this function MUST check if contains($docPath, "data/").
4654
:)
47-
declare %private updating function add_doc:addDocToDB($dbName as xs:string, $docPath) {
48-
let $path := substring-after($docPath, "data/")
55+
declare %private updating function add_doc:addDocToDB($dbName as xs:string, $docPath as xs:string, $path as xs:string) {
56+
let $docName := functx:substring-after-last($docPath, "/")
4957
return
50-
db:put($dbName, $docPath, $path)
58+
db:put($dbName, $docPath, concat($path, "/", $docName))
5159
};
5260

61+
(:~~~~~~~~~~~~~~~~~~~~~~~~~~~
62+
Update resources_register.xml
63+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~:)
64+
5365
(:~ Update function to add a `<document/>` element to dots resources register (`dots/resources_register.xml`)
5466
: @param $dbName db name
5567
: @param $docPath absolute path to the document to add
68+
: @param $csv map of the csv metadata
5669
: @return a complete <document/> node
5770
:)
58-
declare updating %private function add_doc:addDocToResourcesReg($dbName as xs:string, $docPath, $csv) {
71+
declare updating %private function add_doc:addDocToResourcesReg($dbName as xs:string, $docPath as xs:string, $csv) {
5972
let $document := doc($docPath)/tei:TEI
6073
let $projectName := G:getTopCollectionId($dbName)
6174
let $dtsResourceId :=
@@ -75,44 +88,59 @@ declare updating %private function add_doc:addDocToResourcesReg($dbName as xs:st
7588
return $collId
7689
else $projectName
7790
return
78-
if ($document)
79-
then
80-
let $resources_register := db:get($dbName, $G:resourcesRegister)//dots:member
81-
let $doc_in_register := $resources_register/dots:document[@dtsResourceId = $dtsResourceId]
82-
return
83-
if ($doc_in_register)
84-
then
85-
(
86-
delete nodes $doc_in_register,
87-
insert node <document xmlns="https://github.com/chartes/dots/" dtsResourceId="{$dtsResourceId}" maxCiteDepth="{$maxCiteDepth}" parentIds="{$parentIds}">{
88-
resources:getDocumentMetadata($dbName, $document, $dtsResourceId, $csv),
89-
resources:getDotsProjectName($projectName)
90-
}</document> after $doc_in_register
91-
)
92-
else
93-
(
94-
insert node <document xmlns="https://github.com/chartes/dots/" dtsResourceId="{$dtsResourceId}" maxCiteDepth="{$maxCiteDepth}" parentIds="{$parentIds}">{
95-
resources:getDocumentMetadata($dbName, $document, $dtsResourceId, $csv),
96-
resources:getDotsProjectName($projectName)
91+
let $resources_register := db:get($dbName, $G:resourcesRegister)//dots:member
92+
return
93+
(
94+
insert node <document xmlns="https://github.com/chartes/dots/" dtsResourceId="{$dtsResourceId}" maxCiteDepth="{$maxCiteDepth}" parentIds="{$parentIds}">{
95+
resources:getDocumentMetadata($dbName, $document, $dtsResourceId, $csv),
96+
resources:getDotsProjectName($projectName)
9797
}</document> as last into $resources_register,
98-
add_doc:updateTotalChildrenCollection($dbName, $parentIds),
99-
add_doc:addDocToSwitcherDots($dbName, $dtsResourceId)
100-
)
101-
else ()
98+
add_doc:updateTotalChildrenCollection($dbName, $parentIds),
99+
add_doc:addDocToSwitcherDots($dbName, $dtsResourceId)
100+
)
101+
};
102+
103+
(:~ Update function to increment the number of documents in the parent collection
104+
: @param $dbName db name
105+
: @param $parentIds identifier of the collection to update
106+
: @return updating the value of the @totalChildren attribute in a <collection/> node.
107+
:)
108+
declare updating %private function add_doc:updateTotalChildrenCollection($dbName as xs:string, $parentIds as xs:string) {
109+
let $parent := db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
110+
let $totalChildren := $parent/@totalChildren
111+
return
112+
if ($parent != "")
113+
then replace value of node $totalChildren with xs:integer($parent/@totalChildren) + 1
114+
else
115+
update:output("La collection parente n'existe pas.")
116+
};
117+
118+
(:~~~~~~~~~~~~~~~~~~~~~~~~~~~
119+
Update fragments_register.xml
120+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~:)
121+
122+
(:~ Update function to handle the addition of fragments from a new document to the fragments register.
123+
: @param $dbName db name
124+
: @param $docPath absolute path to the document containing the fragments to add
125+
: @return updates the fragments register with the new fragments
126+
:)
127+
declare updating function add_doc:handleFragmentsAddition($dbName as xs:string, $docPath) {
128+
let $csv-frag := resources:getCSV-map($dbName, "fragment")
129+
return
130+
add_doc:addFragInReg($dbName, $docPath, $csv-frag)
102131
};
103132

104133
(:~ Update function to add `<fragment/>` nodes to the fragments register (`dots/fragments_register`).
105134
: @param $dbName db name
106135
: @param $docPath absolute path to the document to add
107136
: @return sequence of <fragment/> nodes
108137
:)
109-
declare updating %private function add_doc:addFragInReg($dbName as xs:string, $docPath, $csv) {
110-
let $pathToDoc := functx:substring-after-last($docPath, "data/")
111-
let $document := db:get($dbName, $pathToDoc)/tei:TEI
138+
declare updating %private function add_doc:addFragInReg($dbName as xs:string, $docPath, $csv) {
112139
let $resourceId :=
113-
if ($document/@xml:id)
114-
then normalize-space($document/@xml:id)
115-
else functx:substring-after-last($pathToDoc, "/")
140+
if (doc($docPath)/*:TEI/@xml:id)
141+
then normalize-space(doc($docPath)/*:TEI//@xml:id)
142+
else functx:substring-after-last($docPath, "/")
143+
let $document := utils_dots:getDocument($dbName, $resourceId)
116144
let $maxCiteDepth := fragments:getMaxCiteDepth($document//tei:refsDecl, 0)
117145
for $citeStructurePosition in $document//tei:refsDecl/tei:citeStructure
118146
return
@@ -129,21 +157,9 @@ declare updating %private function add_doc:addFragInReg($dbName as xs:string, $d
129157
else insert node $fragment as last into $fragments_register
130158
};
131159

132-
133-
(:~ Update function to increment the number of documents in a collection
134-
: @param $dbName db name
135-
: @param $parentIds identifier of the collection to update
136-
: @return updating the value of the @totalChildren attribute in a <collection/> node.
137-
:)
138-
declare updating %private function add_doc:updateTotalChildrenCollection($dbName as xs:string, $parentIds as xs:string) {
139-
let $parent := db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
140-
let $totalChildren := $parent/@totalChildren
141-
return
142-
if ($parent != "")
143-
then replace value of node $totalChildren with xs:integer($parent/@totalChildren) + 1
144-
else
145-
update:output("La collection parente n'existe pas.")
146-
};
160+
(:~~~~~~~~~~~~~~~~~~~~~~~~~~~
161+
Update switcher DoTS
162+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~:)
147163

148164
(:~ Updat function to add the documet to the switcher dots
149165
: @param $dbName db name

repo/backend/update/delete_collection.xqm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ declare updating function del_coll:handleDocInColl($dbName as xs:string, $collec
8080
(
8181
delete node $document,
8282
delete nodes db:get($dbName, $G:fragmentsRegister)//fragment[@resourceId = $resourceId],
83-
db:delete($dbName, $pathDoc)
83+
db:delete($dbName, $pathDoc),
84+
del_doc:updateSwitcherDots($dbName, $resourceId)
8485
)
8586
else
8687
(

0 commit comments

Comments
 (0)