pref-matrix

Web interface to coordinate preferences
git clone https://git.instinctive.eu/pref-matrix.git
Log | Files | Refs | README | LICENSE

commit 10cebf385b4af8e422175d0c5f2a54bed2397048
parent 54c4b83ae7a866bc679e3f5aac8aee382316cab2
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sat, 20 Jan 2024 15:46:45 +0000

First test suite
Diffstat:
Atest/run.sh | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/test-1.json | 2++
Atest/test-1.scm | 26++++++++++++++++++++++++++
Atest/test-2-01.json | 2++
Atest/test-2-02.json | 2++
Atest/test-2-03.json | 2++
Atest/test-2-04.json | 2++
Atest/test-2-05.json | 2++
Atest/test-2-06.json | 2++
Atest/test-2-07.json | 2++
Atest/test-2-08.json | 2++
Atest/test-2-09.json | 2++
Atest/test-2-10.json | 2++
Atest/test-2-dump.sql | 27+++++++++++++++++++++++++++
Atest/test-2.scm | 2++
15 files changed, 171 insertions(+), 0 deletions(-)

diff --git a/test/run.sh b/test/run.sh @@ -0,0 +1,94 @@ +#!/bin/sh + +# Copyright (c) 2024, Natacha Porté +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +set -Cue + +if test "$#" -lt 1; then + echo "Usage: $0 [start-cmd]" >&2 + exit 1 +fi + +: "${TEST_DIR:=$(dirname "$0")}" +: "${TMP_DIR:=/tmp}" + +TO_CLEAN="test-all.json" + +trap 'rm -f ${TO_CLEAN}' EXIT + +if test -z "${TEST_DB-}"; then + TEST_DB="$(mktemp "${TMP_DIR}/pref-matrix-test.XXXXXXXX")" + TO_CLEAN="${TO_CLEAN} ${TEST_DB}*" +fi + +if test -z "${TEST_TRACE-}"; then + TEST_TRACE="$(mktemp "${TMP_DIR}/pref-matrix-test.XXXXXXXX")" + TO_CLEAN="${TO_CLEAN} ${TEST_TRACE}" +fi + +################### +## Test 1: replay + +echo -n "" >|"${TEST_TRACE}" +"$@" :memory: "${TEST_TRACE}" "${TEST_DIR}/test-1.scm" +sed '/; 2[0-9][0-9][0-9]-/d;$s/$/\n(generate-json)\n(exit)/' "${TEST_TRACE}" \ + | diff -u "${TEST_DIR}/test-1.scm" - +diff -u "${TEST_DIR}/test-1.json" test-all.json + +################# +## Test 2: HTTP + +"$@" "${TEST_DB}" "${TEST_TRACE}" "${TEST_DIR}/test-2.scm" & +SRV_PID=$! + +trap 'rm -f ${TO_CLEAN}; kill ${SRV_PID}' EXIT + +sleep 1 + +curl -s -d 'name=foo' 'http://localhost:9090/new-subject' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-01.json" test-all.json +curl -s -d 'name=01' 'http://localhost:9090/new-object' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-02.json" test-all.json +curl -s -d 'name=03' 'http://localhost:9090/new-object' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-03.json" test-all.json +curl -s -d 'name=bar' 'http://localhost:9090/new-subject' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-04.json" test-all.json +curl -s -d 'sub=bar' -d '01=3' -d '04=4' \ + 'http://localhost:9090/bin/set-pref' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-05.json" test-all.json +curl -s -d 'name=02' 'http://localhost:9090/do/new-object' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-06.json" test-all.json +curl -s -d 'name=bar' 'http://localhost:9090/new-subject' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-06.json" test-all.json +curl -s -d 'name=04' 'http://localhost:9090/new-object' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-07.json" test-all.json +curl -s -d 'sub=meow' -d '04=2' -d '01=4' \ + 'http://localhost:9090/set-pref' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-07.json" test-all.json +curl -s -d 'name=meow' 'http://localhost:9090/new-subject' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-08.json" test-all.json +curl -s -d 'sub=foo' -d '01=1' -d '04=2' -d '01=4' \ + 'http://localhost:9090/set-pref' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-09.json" test-all.json +curl -s -d 'sub=bar' -d '01=0' \ + 'http://localhost:9090/set-pref' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-10.json" test-all.json +curl -s -d 'name=04' 'http://localhost:9090/new-object' >>"${TEST_TRACE}" +diff -u "${TEST_DIR}/test-2-10.json" test-all.json + +kill "${SRV_PID}" +trap 'rm -f ${TO_CLEAN}' EXIT + +sqlite3 "${TEST_DB}" .dump | diff -u "${TEST_DIR}/test-2-dump.sql" - diff --git a/test/test-1.json b/test/test-1.json @@ -0,0 +1 @@ +[["01","02","03","04","05"],{"bar":{"01":4},"foo":{"01":2,"02":4,"03":1,"04":3,"05":5},"meow":{}}] +\ No newline at end of file diff --git a/test/test-1.scm b/test/test-1.scm @@ -0,0 +1,26 @@ +(new-subject "foo") +; -> 1 +(new-subject "bar") +; -> 2 +(new-subject "meow") +; -> 3 +(new-object "01") +; -> 1 +(new-object "02") +; -> 2 +(new-object "03") +; -> 3 +(new-object "04") +; -> 4 +(new-object "05") +; -> 5 +(set-subject-pref "bar" '(("01" . "4"))) +; -> (1) +(set-subject-pref "foo" '(("01" . "2") ("02" . "4") ("03" . "1") ("04" . "3") ("05" . "5"))) +; -> (2 3 4 5 6) +(set-subject-pref "meow" '()) +; -> () +(set-config "json-prefix" "test-") +(set-config "server-port" 9090) +(generate-json) +(exit) diff --git a/test/test-2-01.json b/test/test-2-01.json @@ -0,0 +1 @@ +[[""],{"foo":{}}] +\ No newline at end of file diff --git a/test/test-2-02.json b/test/test-2-02.json @@ -0,0 +1 @@ +[["01"],{"foo":{}}] +\ No newline at end of file diff --git a/test/test-2-03.json b/test/test-2-03.json @@ -0,0 +1 @@ +[["01","03"],{"foo":{}}] +\ No newline at end of file diff --git a/test/test-2-04.json b/test/test-2-04.json @@ -0,0 +1 @@ +[["01","03"],{"bar":{},"foo":{}}] +\ No newline at end of file diff --git a/test/test-2-05.json b/test/test-2-05.json @@ -0,0 +1 @@ +[["01","03"],{"bar":{"01":3},"foo":{}}] +\ No newline at end of file diff --git a/test/test-2-06.json b/test/test-2-06.json @@ -0,0 +1 @@ +[["01","02","03"],{"bar":{"01":3},"foo":{}}] +\ No newline at end of file diff --git a/test/test-2-07.json b/test/test-2-07.json @@ -0,0 +1 @@ +[["01","02","03","04"],{"bar":{"01":3},"foo":{}}] +\ No newline at end of file diff --git a/test/test-2-08.json b/test/test-2-08.json @@ -0,0 +1 @@ +[["01","02","03","04"],{"bar":{"01":3},"foo":{},"meow":{}}] +\ No newline at end of file diff --git a/test/test-2-09.json b/test/test-2-09.json @@ -0,0 +1 @@ +[["01","02","03","04"],{"bar":{"01":3},"foo":{"01":4,"04":2},"meow":{}}] +\ No newline at end of file diff --git a/test/test-2-10.json b/test/test-2-10.json @@ -0,0 +1 @@ +[["01","02","03","04"],{"bar":{"01":0},"foo":{"01":4,"04":2},"meow":{}}] +\ No newline at end of file diff --git a/test/test-2-dump.sql b/test/test-2-dump.sql @@ -0,0 +1,27 @@ +PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE config (key TEXT PRIMARY KEY, val); +INSERT INTO config VALUES('json-prefix','test-'); +INSERT INTO config VALUES('server-port',9090); +CREATE TABLE subject (id INTEGER PRIMARY KEY, name TEXT NOT NULL); +INSERT INTO subject VALUES(1,'foo'); +INSERT INTO subject VALUES(2,'bar'); +INSERT INTO subject VALUES(3,'meow'); +CREATE TABLE object (id INTEGER PRIMARY KEY, name TEXT NOT NULL); +INSERT INTO object VALUES(1,'01'); +INSERT INTO object VALUES(2,'03'); +INSERT INTO object VALUES(3,'02'); +INSERT INTO object VALUES(4,'04'); +CREATE TABLE pref (id INTEGER PRIMARY KEY, + sub_id REFERENCES subject(id) + ON UPDATE CASCADE ON DELETE CASCADE, + obj_id REFERENCES object(id) + ON UPDATE CASCADE ON DELETE CASCADE, + val INTEGER NOT NULL DEFAULT 0); +INSERT INTO pref VALUES(3,1,4,2); +INSERT INTO pref VALUES(4,1,1,4); +INSERT INTO pref VALUES(5,2,1,0); +CREATE UNIQUE INDEX sub_name ON subject(name); +CREATE UNIQUE INDEX obj_name ON object(name); +CREATE UNIQUE INDEX sub_obj ON pref(sub_id,obj_id); +COMMIT; diff --git a/test/test-2.scm b/test/test-2.scm @@ -0,0 +1,2 @@ +(set-config "json-prefix" "test-") +(set-config "server-port" 9090)