postgres (#14)
Make codebase compatible with Postgres. Add test scripts for it and a CI pipeline test. Reviewed-on: #14pull/16/head
parent
7b1b8e0fee
commit
440a03396c
16 changed files with 200 additions and 70 deletions
@ -0,0 +1,8 @@ |
||||
# Note: this Dockerfile is written to be executed with the whole source |
||||
# as its context. |
||||
|
||||
FROM postgres:12 |
||||
|
||||
# Install node.js |
||||
RUN apt update -y && apt install -y npm |
||||
|
@ -0,0 +1,4 @@ |
||||
export default function asJson(s: Object | string) { |
||||
return (typeof s === 'string') ? |
||||
JSON.parse(s) : s; |
||||
} |
@ -1,24 +0,0 @@ |
||||
function Player() { |
||||
} |
||||
Player.prototype.play = function(song) { |
||||
this.currentlyPlayingSong = song; |
||||
this.isPlaying = true; |
||||
}; |
||||
|
||||
Player.prototype.pause = function() { |
||||
this.isPlaying = false; |
||||
}; |
||||
|
||||
Player.prototype.resume = function() { |
||||
if (this.isPlaying) { |
||||
throw new Error("song is already playing"); |
||||
} |
||||
|
||||
this.isPlaying = true; |
||||
}; |
||||
|
||||
Player.prototype.makeFavorite = function() { |
||||
this.currentlyPlayingSong.persistFavoriteStatus(true); |
||||
}; |
||||
|
||||
module.exports = Player; |
@ -1,9 +0,0 @@ |
||||
function Song() { |
||||
} |
||||
|
||||
Song.prototype.persistFavoriteStatus = function(value) { |
||||
// something complicated
|
||||
throw new Error("not yet implemented"); |
||||
}; |
||||
|
||||
module.exports = Song; |
@ -0,0 +1,117 @@ |
||||
#!/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":5432,"user":"mudbase","password":"mudbase","database":"mudbase"}}' |
||||
|
||||
DO_SQLITE= |
||||
DO_POSTGRES= |
||||
START_POSTGRES= |
||||
START_POSTGRES_TESTCONTAINER= |
||||
|
||||
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. |
||||
-sp,--start-postgres Start its own Postgres Docker container for testing against. |
||||
-spt,--start-postgres-testcontainer Assume running in the sandervocke/postgres_node container. Spin up a PG process. |
||||
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 |
||||
;; |
||||
-sp|--start-postgres) |
||||
START_POSTGRES=1 |
||||
shift |
||||
;; |
||||
-spt|--start-postgres-testcontainer) |
||||
START_POSTGRES_TESTCONTAINER=1 |
||||
shift |
||||
;; |
||||
*) # preserve positional arguments |
||||
echo "Preserving params: $1" |
||||
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 |
||||
if [ ! -z "${START_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 5432:5432 \ |
||||
postgres:12) |
||||
docker start ${CONTAINERID} |
||||
trap "docker stop ${CONTAINERID}" EXIT |
||||
# Wait for postgres to be ready. |
||||
while true; do |
||||
pg_isready -d mudbase -h localhost -p 5432 -U mudbase |
||||
[ "$(echo $?)" == "0" ] && break |
||||
sleep 1 |
||||
done |
||||
elif [ ! -z "${START_POSTGRES_TESTCONTAINER}" ]; then |
||||
# Fire up a test Postgres process. |
||||
POSTGRES_USER=mudbase POSTGRES_PASSWORD=mudbase POSTGRES_DATABASE=mudbase /docker-entrypoint.sh postgres & |
||||
PID=$(echo $!) |
||||
trap "kill $PID" EXIT |
||||
# Wait for postgres to be ready. |
||||
while true; do |
||||
/usr/bin/pg_isready -d mudbase -h localhost -p 5432 -U mudbase |
||||
[ "$(echo $?)" == "0" ] && break |
||||
sleep 1 |
||||
done |
||||
fi |
||||
|
||||
MUDBASE_DB_CONFIG="$POSTGRES_CONFIG" ./node_modules/.bin/ts-node node_modules/jasmine/bin/jasmine --config=test/jasmine.json "$@" |
||||
POSTGRES_RESULT=$(echo $?) |
||||
|
||||
if [ ! -z "${START_POSTGRES}" ]; then |
||||
docker stop ${CONTAINERID} |
||||
trap - EXIT |
||||
fi |
||||
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 |
||||
|
||||
[ "${SQLITE_RESULT}" == '1' -o "${POSTGRES_RESULT}" == '1' ] && exit 1 |
||||
exit 0 |
||||
|
Loading…
Reference in new issue