Add a basic unit test
authorJohn Hodge <[email protected]>
Sun, 19 Feb 2017 09:32:19 +0000 (17:32 +0800)
committerJohn Hodge <[email protected]>
Sun, 19 Feb 2017 09:32:19 +0000 (17:32 +0800)
.gitignore
tests/TEST_basic.sh [new file with mode: 0755]
tests/_common.sh [new file with mode: 0644]

index 6e59002..fdff355 100644 (file)
@@ -5,3 +5,5 @@
 dispsrv
 dispense
 cokebank.log
+
+/tests/rundir
diff --git a/tests/TEST_basic.sh b/tests/TEST_basic.sh
new file mode 100755 (executable)
index 0000000..f2f1301
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/bash
+set -eux
+TESTNAME=basic
+
+. _common.sh
+
+if $DISPENSE acct tpg; then
+       FAIL "Database contains '$USER'"
+fi
+
+sqlite3 "${BASEDIR}cokebank.db" "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('${USER}',1,1);"
+
+TRY_COMMAND "$DISPENSE user add unittest_user0"
+
+LOG "Checking for test user"
+TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $    0.00'
+
+TRY_COMMAND $DISPENSE acct unittest_user0 +100 Unit_test
+TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $    1.00'
+TRY_COMMAND $DISPENSE acct unittest_user0 -100 Unit_test
+TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $    0.00'
+LOG "Success"
diff --git a/tests/_common.sh b/tests/_common.sh
new file mode 100644 (file)
index 0000000..0019b99
--- /dev/null
@@ -0,0 +1,52 @@
+
+BASEDIR=rundir/${TESTNAME}/
+PORT=22222
+
+mkdir -p ${BASEDIR}
+rm -f ${BASEDIR}cokebank.db
+
+cat << EOF > ${BASEDIR}cfg_server.conf
+# AUTOGENERATED Test ${TESTNAME}
+server_port ${PORT}
+cokebank_database ${BASEDIR}cokebank.db
+items_file ${BASEDIR}cfg_items.conf
+door_serial_port /dev/null
+
+coke_modbus_address 0.0.0.0
+coke_modbus_port 502
+test_mode no
+
+disable_syslog yes
+coke_dummy_mode yes
+EOF
+
+echo "# AUTOGENERATED Test ${TESTNAME}" > ${BASEDIR}cfg_items.conf
+
+LOG() {
+       echo "TEST ${TESTNAME}: "$*
+}
+FAIL() {
+       echo "TEST ${TESTNAME} FAIL: "$*
+       exit 1
+}
+TRY_COMMAND() {
+       cmd="$*"
+       if ! $cmd ; then
+               FAIL "Command \`$cmd\` failed"
+       fi
+}
+
+DISPENSE="../dispense -H localhost -P ${PORT}"
+
+LD_LIBRARY_PATH=.. ../dispsrv -f ${BASEDIR}cfg_server.conf --dont-daemonise > ${BASEDIR}server.log 2>&1 &
+server_pid=$!
+
+cleanup() {
+       LOG "Killing ${server_pid}"
+       kill ${server_pid}
+}
+trap cleanup EXIT
+
+LOG "Server running on PID ${server_pid}"
+sleep 1
+echo "" | nc localhost ${PORT}

UCC git Repository :: git.ucc.asn.au