summaryrefslogtreecommitdiff
path: root/lib/query/tests/create.t
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/create.t
Initial commit
Diffstat (limited to 'lib/query/tests/create.t')
-rw-r--r--lib/query/tests/create.t150
1 files changed, 150 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") )