#!/bin/sh

set -eux

pg_virtualenv << EOF
 set -eux
 createdb pgwatch
 createdb pgwatch_metrics
 # start pgwatch
 pgwatch --sources postgresql:///pgwatch --sink=postgresql:///pgwatch_metrics --refresh=2 &
 PID="\$!"
 trap "kill \$PID" EXIT
 sleep 5
 # configure a source
 psql -c "insert into pgwatch.source (name, connstr) values ('local', 'postgresql://localhost/postgres');" pgwatch
 sleep 5
 # check if metrics were collected
 size_b=\$(psql -AtXc "select data->>'size_b' from db_size order by time desc;" pgwatch_metrics)
 [ "\$size_b" -ge 6000000 ] # DB should be at least 6MB
EOF
