# REQUIRES: postgres
# RUN: rm -rf "%t.install"
# RUN: %{shared_inputs}/postgres_wrapper.sh "%t.install" /bin/sh %s "%t.install" postgresql://pgtest@localhost:9100 "%{shared_inputs}"
set -eux

TESTDIR="$1"
PGURL="$2"
SHARED_INPUTS="$3"
dropdb --if-exists --maintenance-db="${PGURL}/postgres" lnt_regr_test_PostgresDB
createdb --maintenance-db="${PGURL}/postgres" lnt_regr_test_PostgresDB

lnt create "${TESTDIR}/instance" --db-dir ${PGURL} --default-db lnt_regr_test_PostgresDB

# Import a test set.
lnt import "${TESTDIR}/instance" "${SHARED_INPUTS}/sample-a-small.plist" --show-sample-count

# Import a test set.
lnt import "${TESTDIR}/instance" "${SHARED_INPUTS}/sample-b-small.plist" --show-sample-count

# Check that we remove both the sample and the run, and that we don't commit by
# default.
#
lnt updatedb "${TESTDIR}/instance" --testsuite nts --delete-run 1 \
	--show-sql >& "${TESTDIR}/runrm.out"
# RUN: filecheck --check-prefix CHECK-RUNRM %s < "%t.install/runrm.out"

# CHECK-RUNRM: DELETE FROM "NT_Sample" WHERE "NT_Sample"."ID" = %(ID)s
# CHECK-RUNRM-NEXT: ({'ID': 1}, {'ID': 2})
# CHECK-RUNRM: DELETE FROM "NT_Run" WHERE "NT_Run"."ID" = %(ID)s
# CHECK-RUNRM-NEXT: {'ID': 1}
# CHECK-RUNRM: COMMIT

# Import a test set.
lnt import "${TESTDIR}/instance" "${SHARED_INPUTS}/sample-a-small.plist" --show-sample-count

# Check that we remove runs when we remove a machine.
#
lnt updatedb "${TESTDIR}/instance" --testsuite nts \
	--delete-machine "LNT SAMPLE MACHINE" \
	--show-sql >& "${TESTDIR}/machinerm.out"
# RUN: filecheck --check-prefix CHECK-MACHINERM %s < "%t.install/machinerm.out"

# CHECK-MACHINERM: DELETE FROM "NT_Sample" WHERE "NT_Sample"."ID" = %(ID)s
# CHECK-MACHINERM-NEXT: ({'ID': 3}, {'ID': 4}, {'ID': 5})
# CHECK-MACHINERM: DELETE FROM "NT_Run" WHERE "NT_Run"."ID" = %(ID)s
# CHECK-MACHINERM-NEXT: {'ID': 2}
# CHECK-MACHINERM: DELETE FROM "NT_Machine" WHERE "NT_Machine"."ID" = %(ID)s
# CHECK-MACHINERM-NEXT: {'ID': 1}
# CHECK-MACHINERM: COMMIT
