#!/bin/sh
#
help ()
{
	echo 2>&1 "run - execute tests

Commands:

  all   run all tests
  fund  run fundamental tests
  if    run interface unit tests
  trig  run trigger tests
  srf   run SRF tests
  gen   run generator tests

  help  this message
"
	exit 1
}
test "$1" = "help" -o -z "$1" && help

. db/conf
test "$1" = all -o -e "sql/$1" ||
	error "Unknown test section: $1"

tested=0
tested () { tested=$(($tested + 1)); }
passed=0
passed () { passed=$(($passed + 1)); }

testit ()
{
	bn="$(basename "$1")"
	
	tested
	if psql -E <"$1" | cat >"output/$bn" && 
		cmp "output/$bn" "expected/$bn"
	then
		message "Passed $bn..."
		passed
	else
		message "** Failed $bn..."
	fi
}

pgstart || exit
_EXIT ()
{
	pgstop
	message "

########################################
 SUMMARY: passed $passed of $tested tests
########################################
$passed/$tested
"
}
trap _EXIT EXIT

if test "$1" = all
then
	for t in sql/*
	do
		test -d "$t" && continue
		testit "$t"
	done
else
	testit "sql/$1"
fi
