From ce13c93cf14cb71894481b0303a7058a72a6f4ce Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sat, 21 Nov 2026 11:23:41 +0100 Subject: Load values from the database --- lib/query/tests/insert.t | 387 ++++++----------------------------------------- lib/query/tests/select.t | 48 ++++++ 2 files changed, 92 insertions(+), 343 deletions(-) create mode 100644 lib/query/tests/select.t (limited to 'lib/query/tests') diff --git a/lib/query/tests/insert.t b/lib/query/tests/insert.t index e060ad4..3dd2c94 100644 --- a/lib/query/tests/insert.t +++ b/lib/query/tests/insert.t @@ -1,31 +1,32 @@ -Test the capacity to insert elements in the database according a specific -schema +Test the capacity to insert elements in the database according to a specific +schema. -The data to insert is given in the json format. This format is a subset from the -configuration (for example we can’t have a property with the same name as a -subcontent) +The configuration is read on the standard input, and the name of the content to +populate is given as argument. Two rows are inserted per run, with a value +generated for each field from its type and the row number (see +[Common.provider_for]): text and integer fields get a value derived from the +field name, monovalued references point to the row of id 1, and multivalued +references get two values for the first row and one for the second. -Minimal exemple, a content without any field does not have any value: +A content without any field gets an empty row: - $ tests --insert < { > "content": [ > { - > "id": "entity", - > "values": [ {} ] + > "id": "entity" > } > ] > } > EOF entity:1 {} + entity:2 + {} -Ensure an error is raised if the value does not match the expected type. - -Here, the year is declared as an integer, but the value is provided as string. -This error is expected to be checked and reported. +Text and integer fields are filled with a generated value: - $ tests --insert < { > "content": [ > { @@ -33,366 +34,66 @@ This error is expected to be checked and reported. > "section": [ > { > "field": [ - > { - > "name": "year", - > "label": {}, - > "type": "integer" - > } + > { "name": "name", "label": {}, "type": "text" }, + > { "name": "year", "label": {}, "type": "integer" } > ] > } - > ], - > "values": [ { "year": "1234", } ] - > } - > ] - > } - > EOF - Fatal error: exception Query__Encoder.TypeMismatch - [2] - - $ tests --insert < { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "name", - > "label": {}, - > "type": "text", - > "required": true - > }, - > { - > "name": "year", - > "label": {}, - > "type": "integer" - > } - > ] - > } - > ], - > "values": [ - > { - > "name": "name", - > "year": 1234, - > } > ] > } > ] > } > EOF entity:1 - {"name":"name","year":1234} - -Any value can be omitted and will be replaced by the default value: - $ tests --insert < { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "year", - > "label": {}, - > "type": "integer", - > } - > ] - > } - > ], - > "values": [ {} ] - > } - > ] - > } - > EOF - entity:1 - {"year":null} - -If the field is "required" the value is still inserted: the requirement feature -is only functionnal; this shall not block the creation of the content in the -database. - - $ tests --insert < { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "year", - > "label": {}, - > "type": "integer", - > "required": true, - > } - > ] - > } - > ], - > "values": [ {} ] - > } - > ] - > } - > EOF - entity:1 - {"year":null} + {"name":"name-1","year":1} + entity:2 + {"name":"name-2","year":2} -A subcontent is linked to the parent: +References are declared in the field type. A monovalued reference stores the +id of the target row in the content table, and each value of a multivalued +reference gets its own line, read back from the dedicated table: - $ tests --insert < { - > "content": [ + > "list": [ > { - > "id": "referential", - > }, - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "idx", - > "label": {}, - > "type": "integer" - > }, - > ] - > } - > ], - > "subcontent": [ - > { - > "id": "details", - > "section": [ - > { - > "field": [ - > { - > "name": "comment", - > "label": {}, - > "type": "text" - > }, - > ] - > } - > ], - > } - > ], - > "values": [ - > { - > "idx": 1, - > "details": [ - > {"comment": "lorem"}, - > {"comment": "ipsum"} - > ] - > }, - > { - > "idx": 2, - > "details": [ - > {"comment": "lorem 2"}, - > ] - > } + > "id": "country_list", + > "field": [ + > { "name": "code", "label": {}, "type": "text" } > ] > } - > ] - > } - > EOF - entity:1 - {"idx":1} - details:1 - {"comment":"lorem","parent":1} - details:2 - {"comment":"ipsum","parent":1} - entity:2 - {"idx":2} - details:3 - {"comment":"lorem 2","parent":2} - -In this syntax, external references are declared inside the feature referencing -them. - - $ tests --insert < { + > ], > "content": [ > { - > "id": "referential", + > "id": "entity", > "section": [ > { > "field": [ + > { "name": "name", "label": {}, "type": "text" }, + > { "name": "year", "label": {}, "type": "integer" }, > { - > "name": "description", + > "name": "country", > "label": {}, - > "type": "text" + > "type": { "content": "country_list" } > }, - > ] - > } - > ], - > "values": [ - > {"description": "id2"}, - > ] - > }, - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "idx", - > "label": {}, - > "type": "integer" - > }, - > { - > "name": "comment", - > "label": {}, - > "type": { - > "content": "referential", - > "multivaluated": false - > } - > } - > ] - > } - > ], - > "values": [ - > { - > "idx": 1, - > "comment": {"description": "id1"}, - > - > }, - > ] - > } - > ] - > } - > EOF - referential:2 - {"description":"id2"} - entity:1 - {"idx":1,"comment":1} - -Four contents are created for the type referential here: -- Two as contents linked referenced by the content "entity" (ids 1 and 2). -- Two as contents not referenced at all (ids 3 and 4). - - $ tests --insert < { - > "content": [ - > { - > "id": "referential", - > "section": [ - > { - > "field": [ > { - > "name": "description", + > "name": "aliases", > "label": {}, - > "type": "text" - > }, + > "type": { "content": "country_list", "multivaluated": true } + > } > ] > } - > ], - > "values": [ - > {"description": "id3"}, - > {"description": "id4"} - > ] - > }, - > { - > "id": "entity", - > "section": [ - > { - > "field": [ - > { - > "name": "idx", - > "label": {}, - > "type": "integer" - > }, - > { - > "name": "comment", - > "label": {}, - > "type": { - > "content": "referential", - > "multivaluated": true - > } - > } - > ] - > } - > ], - > "values": [ - > { - > "idx": 1, - > "comment": [ - > {"description": "id1"}, - > {"description": "id2"} - > ] - > }, > ] > } > ] > } > EOF - referential:3 - {"description":"id3"} - referential:4 - {"description":"id4"} - entity:1 - {"idx":1} entity:1 - {"comment":1} + {"name":"name-1","year":1,"country":1} entity:1 - {"comment":2} - -Subcontent are also managed in the same way. - - $ tests --insert < { - > "content": [ - > { - > "id": "entity", - > "section": [ - > { - > "field": [ ] - > } - > ], - > "subcontent": [ - > { - > "id": "details", - > "section": [ - > { - > "field": [ - > { - > "name": "reference", - > "label": {}, - > "type": { - > "content": "referential", - > "multivaluated": true - > } - > } - > ] - > } - > ], - > } - > ], - > "values": [ - > { - > "idx": 1, - > "details": [ - > { - > // Create two references here, the counter for the non-linked - > // reference is raised to 3 - > "reference": [ {}, {} ] - > } - > ] - > }, - > ] - > }, - > { - > "id": "referential", - > "values": [ {} ] - > }, - > ] - > } - > EOF + {"aliases":1} entity:1 - {} - details:1 - {"parent":1} - details:1 - {"reference":1} - details:1 - {"reference":2} - referential:3 - {} + {"aliases":2} + entity:2 + {"name":"name-2","year":2,"country":1} + entity:2 + {"aliases":2} diff --git a/lib/query/tests/select.t b/lib/query/tests/select.t new file mode 100644 index 0000000..f4a4740 --- /dev/null +++ b/lib/query/tests/select.t @@ -0,0 +1,48 @@ +Low level test of [Query.select]: two rows are inserted in the table of the +content given as argument (the configuration is read on the standard input), +then each row is read back with [Query.select]. One line is printed per field, +as received by the callback. The [id] of the row is the argument of [select]: +it is not reported by the callback. + + $ tests --select "entity" < { + > "list": [ + > { + > "id": "country_list", + > "field": [ + > { "name": "code", "label": {}, "type": "text" } + > ] + > } + > ], + > "content": [ + > { + > "id": "entity", + > "section": [ + > { + > "field": [ + > { "name": "name", "label": {}, "type": "text" }, + > { "name": "year", "label": {}, "type": "integer" }, + > { "name": "country", "label": {}, "type": { "content": "country_list" } }, + > { + > "name": "aliases", + > "label": {}, + > "type": { "content": "country_list", "multivaluated": true } + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + row 1: + name = "name-1" + year = 1 + country -> 1 + aliases -> [1,2] + row 2: + name = "name-2" + year = 2 + country -> 1 + aliases -> [2] + -- cgit v1.2.3