summaryrefslogtreecommitdiff
path: root/lib/query/tests
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-03-27 08:48:05 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-03-27 08:48:05 +0100
commit830e7c5d27d161b91402414e6ecd49f1ed818bfb (patch)
tree448864635d29b1ea66424cbb9e0816fbcf1e1907 /lib/query/tests
Initial commit
Diffstat (limited to 'lib/query/tests')
-rw-r--r--lib/query/tests/create.t150
-rw-r--r--lib/query/tests/dune11
-rw-r--r--lib/query/tests/insert.t213
3 files changed, 374 insertions, 0 deletions
diff --git a/lib/query/tests/create.t b/lib/query/tests/create.t
new file mode 100644
index 0000000..565235a
--- /dev/null
+++ b/lib/query/tests/create.t
@@ -0,0 +1,150 @@
+Test the schema generated in the database. The tests create the database
+in memory according the configuration given, then display the schema for a
+specfic table. (The schema is extracted from the table, and requires the table
+to be succesfully created).
+
+Test a single content with no field:
+
+ $ tests -schema "entity" <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > }
+ > ]
+ > }
+ > EOF
+ CREATE TABLE "entity"
+ ( "id" INTEGER PRIMARY KEY )
+
+Text fields or numeric fields are created as columns directly in the table:
+
+ $ tests -schema "entity" <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "name",
+ > "label": {},
+ > "type": "text",
+ > "required": true
+ > },
+ > {
+ > "name": "year",
+ > "label": {},
+ > "type": "integer"
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+ CREATE TABLE "entity"
+ ( "id" INTEGER PRIMARY KEY
+ , "year" INTEGER
+ , "name" VARCHAR )
+
+A subcontent is linked to the parent:
+
+ $ tests -schema "details" <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "subcontent": [
+ > {
+ > "id": "details"
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+ CREATE TABLE "details"
+ ( "parent" INTEGER NOT NULL
+ , "id" INTEGER PRIMARY KEY
+ , FOREIGN KEY("parent") REFERENCES entity("id") )
+
+A reference field is declared as a foreign key to the linked element
+
+ $ tests -schema "entity" <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "name",
+ > "label": {},
+ > "type": "text",
+ > "required": true
+ > },
+ > {
+ > "name": "user_type",
+ > "label": {},
+ > "type": {
+ > "content": "user_type_list",
+ > "multivaluated": false
+ > }
+ > }
+ > ]
+ > }
+ > ]
+ > },
+ > {
+ > "id": "user_type_list"
+ > }
+ > ]
+ > }
+ > EOF
+ CREATE TABLE "entity"
+ ( "id" INTEGER PRIMARY KEY
+ , "user_type" INTEGER
+ , "name" VARCHAR
+ , FOREIGN KEY("user_type") REFERENCES user_type_list("id") )
+
+When the field is multivaluated, there is no column for this property in the
+table but a dedicated table is created:
+
+ $ tests -schema "entity" "entity.user_type" <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "user_type",
+ > "label": {},
+ > "type": {
+ > "content": "user_type_list",
+ > "multivaluated": true
+ > }
+ > }
+ > ]
+ > }
+ > ]
+ > },
+ > {
+ > "id": "user_type_list"
+ > }
+ > ]
+ > }
+ > EOF
+ CREATE TABLE "entity"
+ ( "id" INTEGER PRIMARY KEY )
+ CREATE TABLE "entity.user_type"
+ ( "id" INTEGER PRIMARY KEY
+ , "parent" INTEGER NOT NULL
+ , "target" INTEGER NOT NULL
+ , FOREIGN KEY("parent") REFERENCES entity("id")
+ , FOREIGN KEY("target") REFERENCES user_type_list("id") )
diff --git a/lib/query/tests/dune b/lib/query/tests/dune
new file mode 100644
index 0000000..c5913da
--- /dev/null
+++ b/lib/query/tests/dune
@@ -0,0 +1,11 @@
+; Add the test library in the path
+
+(env
+ (_
+ (binaries %{project_root}/test/tests.exe)))
+
+; And depend of it. The binary will be available in the
+; cram tests under the name "tests"
+
+(cram
+ (deps %{bin:tests}))
diff --git a/lib/query/tests/insert.t b/lib/query/tests/insert.t
new file mode 100644
index 0000000..236bb97
--- /dev/null
+++ b/lib/query/tests/insert.t
@@ -0,0 +1,213 @@
+Test the capacity to insert elements in the database according 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)
+
+Minimal exemple, a content without any field does not have any value:
+
+ $ tests -insert <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "values": [ {} ]
+ > }
+ > ]
+ > }
+ > EOF
+ entity:1
+ {}
+
+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.
+
+ $ tests -insert <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "year",
+ > "label": {},
+ > "type": "integer"
+ > }
+ > ]
+ > }
+ > ],
+ > "values": [ { "year": "1234", } ]
+ > }
+ > ]
+ > }
+ > EOF
+ Fatal error: exception Query__Insert.TypeMismatch
+ [2]
+
+ $ tests -insert <<EOF
+ > {
+ > "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 <<EOF
+ > {
+ > "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.
+
+ $ tests -insert <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "year",
+ > "label": {},
+ > "type": "integer",
+ > "required": true,
+ > }
+ > ]
+ > }
+ > ],
+ > "values": [ {} ]
+ > }
+ > ]
+ > }
+ > EOF
+ entity:1
+ {"year":null}
+
+A subcontent is linked to the parent:
+
+ $ tests -insert <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "referential",
+ > },
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "idx",
+ > "label": {},
+ > "type": "integer"
+ > },
+ > {
+ > "name": "ref",
+ > "label": {},
+ > "type": {
+ > "content": "referential",
+ > "multivaluated": true
+ > }
+ > }
+ > ]
+ > }
+ > ],
+ > "subcontent": [
+ > {
+ > "id": "details",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "comment",
+ > "label": {},
+ > "type": "text"
+ > },
+ > ]
+ > }
+ > ],
+ > }
+ > ],
+ > "values": [
+ > {
+ > "idx": 1,
+ > "details": [
+ > {"comment": "lorem"},
+ > {"comment": "ipsum"}
+ > ]
+ > },
+ > {
+ > "idx": 2,
+ > "details": [
+ > {"comment": "lorem 2"},
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > 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}