# test.py -- test script for Python interface to TeamTrack. # Gareth Rees, Ravenbrook Limited, 2000-08-07. # $Id: //info.ravenbrook.com/project/p4dti/branch/2000-09-13/demo-debugging/code/python-teamtrack-interface/teamtrack_test.py#1 $ import teamtrack import socket 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['info1'] = i r['char1'] = str(i) r['char2'] = code r.add() rl = s.query(vcactions, "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'], `i`) # The exception to raise when a test fails. fail = "" # 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()