# test.py -- test script for Python interface to TeamTrack.
# Gareth Rees, Ravenbrook Limited, 2000-08-07.
# $Id: //info.ravenbrook.com/project/p4dti/version/0.5/code/python-teamtrack-interface/teamtrack_test.py#2 $
#
# See "Python interface to TeamTrack: design"
# <version/0.5/design/python-teamtrack-interface/> for the design being tested here.
#
# Copyright 2000 Ravenbrook Limited.  This document is provided "as is",
# without any express or implied warranty. In no event will the authors
# be held liable for any damages arising from the use of this document.
# You may make and distribute copies and derivative works of this
# document provided that (1) you do not charge a fee for this document or
# for its distribution, and (2) you retain as they appear all copyright
# and licence notices and document history entries, and (3) you append
# descriptions of your modifications to the document history.

import teamtrack
import socket

# The exception to raise when a test fails.
fail = ""

def connect():
    # Open a TeamTrack server on the local host.
    return teamtrack.connect('joe', '', socket.gethostname())

def check(name, expected, found):
    if expected != found:
        raise fail, "%s: expected %s but found %s" \
              % (name, str(expected), str(found))

# Create some records in the VCACTIONS table, verify that they are there as
# expected and then delete them.
def test_1():
    import sys, time
    code = 'test_1'
    s = connect()
    check("server refcount", sys.getrefcount(s), 2)
    vcactions = teamtrack.table['VCACTIONS']
    n = 10
    for i in range(n):
        r = s.new_record(vcactions)
        r['type'] = 999
        r['info1'] = i
        r['char1'] = str(i)
        r['char2'] = code
        r.add()
    rl = s.query(vcactions, "TS_TYPE = 999 AND TS_CHAR2 = '%s' "
                 "ORDER BY TS_INFO1" % code)
    for r in rl:
        s.delete_record(vcactions, r['id'])
    check("number of results", n, len(rl))
    for i in range(len(rl)):
        check("rl[%d]['char2']" % i, rl[i]['char2'], code)
        check("rl[%d]['info1']" % i, rl[i]['info1'], i)
        check("rl[%d]['char1']" % i, rl[i]['char1'], str(i))

# tests is a list of functions.  Each function takes no arguments, returns if
# successful and raises the "fail" exception together with an error message if
# it fails.
tests = [ test_1 ]

# run(quietly) runs all the tests and reports how many passed and which tests
# failed (if any).  If quietly is false or not supplied, failures are also
# reported as they happen, with error messages.
def run(quietly = 0):
    n = 0
    n_passed = 0
    passed_tests = []
    for test in tests:
        n = n + 1
        try:
            test()
            n_passed = n_passed + 1
            passed_tests = passed_tests + [n]
        except fail:
            if not quietly:
                import sys
                (type, value) = sys.exc_info()[:2]
                print "Test", n, "failed:", type, value
    if n == 0:
        p = 100.0
    else:
        p = 100.0 * n_passed / n
    print "%d tests passed out of %d (%d%%)" % (n_passed, n, p)
    if n_passed < n:
        failed_tests = []
        for i in range(n):
            if i+1 not in passed_tests:
                failed_tests = failed_tests + [i+1]
        print "Failed tests:", failed_tests

# Run the test suite when this module is loaded.
run()

# B. Document History
# 
# 2000-08-31 GDR Created.
# 
# 2000-09-07  GDR  Wrote test case that adds records to the TS_VCACTIONS table.
# 
# 2000-09-07 GDR Fixed typo.  It now runs and passes.
# 
# 2000-09-18 GDR Test records in VCACTIONS table have type = 999 to distinguish
# them from changes, fixes, filespecs etc.
