diff --git a/server/test/integration/flows/helpers.js b/server/test/integration/flows/helpers.js index 354a16a..f6f3251 100644 --- a/server/test/integration/flows/helpers.js +++ b/server/test/integration/flows/helpers.js @@ -1,7 +1,12 @@ import { expect } from "chai"; export async function initTestDB() { - const knex = await require('knex')({ client: 'sqlite3', connection: ':memory:'}) + // Allow different database configs - but fall back to SQLite in memory if necessary. + const config = process.env.MUDBASE_DB_CONFIG ? + JSON.parse(process.env.MUDBASE_DB_CONFIG) : + { client: 'sqlite3', connection: ':memory:' }; + console.log("Running tests with DB config: ", config); + const knex = await require('knex')(config); await knex.migrate.latest(); return knex; } diff --git a/server/test/test.sh b/server/test/test.sh new file mode 100755 index 0000000..b4196eb --- /dev/null +++ b/server/test/test.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +# Wrapper around the Jasmine test scripts. +# Can test with different types of databases. + +SCRIPT=`realpath $0` +SCRIPTPATH=`dirname $SCRIPT` +SOURCEPATH="$SCRIPTPATH/.." + +SQLITE_CONFIG='{"client": "sqlite3", "connection": ":memory:"}' +POSTGRES_CONFIG='{"client":"pg","connection":{"host":"localhost","port":9432,"user":"mudbase","password":"mudbase","database":"mudbase"}}' + +DO_SQLITE= +DO_POSTGRES= + +usage() { + cat << EOF | echo +This tool runs MuDBase's Jasmine tests. Different database back-ends can be selected (multiple is allowed too). +Options: + -s,--sqlite Use SQLite in memory for testing. + -p,--postgres Use Postgres for testing. Spins up a temporary Postgres container on localhos, port 9432. + any other option is passed on to Jasmine. +EOF +} + +# Argument parsing +PARAMS="" +while (( "$#" )); do + case "$1" in + -s|--sqlite) + DO_SQLITE=1 + shift + ;; + -p|--postgres) + DO_POSTGRES=1 + shift + ;; + *) # preserve positional arguments + PARAMS="$PARAMS $1" + shift + ;; + esac +done +# Set positional arguments in their proper place +eval set -- "$PARAMS" + +pushd $SOURCEPATH +if [ ! -z "${DO_SQLITE}" ]; then + MUDBASE_DB_CONFIG="$SQLITE_CONFIG" ./node_modules/.bin/ts-node node_modules/jasmine/bin/jasmine --config=test/jasmine.json $@ + SQLITE_RESULT=$(echo $?) +fi +if [ ! -z "${DO_POSTGRES}" ]; then + # Fire up a test Postgres. + docker pull postgres:12 + CONTAINERID=$(docker create --rm \ + --env POSTGRES_USER=mudbase \ + --env POSTGRES_PASSWORD=mudbase \ + --env POSTGRES_DB=mudbase \ + -p 9432:5432 \ + postgres:12) + docker start ${CONTAINERID} + trap "docker stop ${CONTAINERID}" EXIT + + # Wait for postgres to be ready. + while [ -z "$(docker logs ${CONTAINERID} | grep 'ready to accept connections')" ]; do + sleep 1; + done + + MUDBASE_DB_CONFIG="$POSTGRES_CONFIG" ./node_modules/.bin/ts-node node_modules/jasmine/bin/jasmine --config=test/jasmine.json $@ 2>&1 + POSTGRES_RESULT=$(echo $?) + + docker stop ${CONTAINERID} + trap - EXIT +fi + +printf "Tests finished. Results:\n\n" +if [ ! -z "${DO_SQLITE}" ]; then + echo "SQLite: $([ ${SQLITE_RESULT} == '1' ] && echo Fail || echo Success)" +fi +if [ ! -z "${DO_POSTGRES}" ]; then + echo "Postgres: $([ ${POSTGRES_RESULT} == '1' ] && echo Fail || echo Success)" +fi + +popd \ No newline at end of file