2008-01-09 Raphael Ackermann <raphael.ackermann@gmail.com> From PEP8 Always use 'self' for the first argument to instance methods.

svn: r9767
This commit is contained in:
Raphael Ackermann 2008-01-09 16:47:56 +00:00
parent c6a8810305
commit 2857943955
11 changed files with 181 additions and 168 deletions

View File

@ -1,3 +1,16 @@
2008-01-09 Raphael Ackermann <raphael.ackermann@gmail.com>
* src/gen/utils/callback.py
* src/ObjectSelector/_FamilyFilterFrame.py
* src/test/test/test_util_test.py
* src/test/test/gedread_util_test.py
* src/test/test_util.py
* src/test/gramps_cli_test.py
* src/test/ansel_utf8_test.py
* src/GrampsDbUtils/test/_GedcomChar_test.py
* src/GrampsDbUtils/test/_GrampsDbWRFactories_test.py
* src/GrampsDbUtils/test/GR_test.py
From PEP8 Always use 'self' for the first argument to instance methods.
2008-01-09 Raphael Ackermann <raphael.ackermann@gmail.com> 2008-01-09 Raphael Ackermann <raphael.ackermann@gmail.com>
* src/plugins/ExportVCard.py: in write_person use b_date instead of date * src/plugins/ExportVCard.py: in write_person use b_date instead of date
0001497: small typo and imports cleanup in ExportVCard.py 0001497: small typo and imports cleanup in ExportVCard.py

View File

@ -58,28 +58,28 @@ class nc():
NB: name _must_ match the X names in db get_number_of_X NB: name _must_ match the X names in db get_number_of_X
""" """
def dbncheck(s, dbcall): def dbncheck(self, dbcall):
err = None err = None
got = dbcall() got = dbcall()
if not got == s.num: if not got == self.num:
err = "%s: got %d, expected %d" % (s.name, got, s.num) err = "%s: got %d, expected %d" % (self.name, got, self.num)
return err return err
def __init__(s, name, num): def __init__(self, name, num):
s.name = name self.name = name
s.num = num self.num = num
s.getname = "get_number_of_" + name self.getname = "get_number_of_" + name
def __call__(s, db): def __call__(self, db):
dbcall = getattr(db,s.getname) dbcall = getattr(db,self.getname)
s.dbncheck(dbcall) self.dbncheck(dbcall)
class fnci(): class fnci():
"""fnci (frag-numcheckset item) is a data container for: """fnci (frag-numcheckset item) is a data container for:
a fragment of gedcom a fragment of gedcom
a sequence of nc items to check a sequence of nc items to check
""" """
def __init__(s, frag, ncset): def __init__(self, frag, ncset):
s.frag = frag self.frag = frag
s.ncset = ncset self.ncset = ncset
# test data table for Test1.test1a_numchecks # test data table for Test1.test1a_numchecks
fnumchecks = ( fnumchecks = (
@ -119,28 +119,28 @@ def _checklog(tlogger, pat=None):
class Test1(U.TestCase): class Test1(U.TestCase):
def setUp(s): def setUp(self):
# make a test subdir and compose some pathnames # make a test subdir and compose some pathnames
s.tdir = tu.make_subdir("RG_test") self.tdir = tu.make_subdir("RG_test")
s.tdb = op.join(s.tdir,"test_db") self.tdb = op.join(self.tdir,"test_db")
s.ifil = op.join(s.tdir,"test_in.ged") self.ifil = op.join(self.tdir,"test_in.ged")
s.lfil = op.join(s.tdir,"test.log") self.lfil = op.join(self.tdir,"test.log")
def test1a_numchecks(s): def test1a_numchecks(self):
tl = tu.TestLogger() tl = tu.TestLogger()
for i,f in enumerate(fnumchecks): for i,f in enumerate(fnumchecks):
gr.make_gedcom_input(s.ifil, f.frag) gr.make_gedcom_input(self.ifil, f.frag)
db = gr.create_empty_db(s.tdb) db = gr.create_empty_db(self.tdb)
tl.logfile_init(s.lfil) tl.logfile_init(self.lfil)
gr.gread(db,s.ifil) gr.gread(db,self.ifil)
errs = _checklog(tl, r"Line \d+") errs = _checklog(tl, r"Line \d+")
s.assertEquals(errs, 0, self.assertEquals(errs, 0,
"ncset(%d): got %d unexpected log messages" % "ncset(%d): got %d unexpected log messages" %
(i,errs)) (i,errs))
# ok, no log error message, check db counts # ok, no log error message, check db counts
for call in f.ncset: for call in f.ncset:
err = call(db) err = call(db)
s.assertFalse(err, err) self.assertFalse(err, err)
if __name__ == "__main__": if __name__ == "__main__":
U.main() U.main()

View File

@ -40,22 +40,22 @@ class Test1_ansi(unittest.TestCase):
fil = os.path.join(cdir,enc) fil = os.path.join(cdir,enc)
exp = utest_chars exp = utest_chars
def setUp(s): def setUp(self):
gen_chars(s.fil, s.enc) gen_chars(self.fil, self.enc)
def test1a_read_ansi(s): def test1a_read_ansi(self):
f = open(s.fil) f = open(self.fil)
ra= G.AnsiReader(f) ra= G.AnsiReader(f)
got = ra.readline() got = ra.readline()
s.assertEquals(got,s.exp, m(got,s.exp, "AnsiReader")) self.assertEquals(got,self.exp, m(got,self.exp, "AnsiReader"))
def test1b_read_codec_latin1(s): def test1b_read_codec_latin1(self):
got=codecs.open(s.fil, encoding=s.enc).read() got=codecs.open(self.fil, encoding=self.enc).read()
s.assertEquals(got,s.exp, m(got,s.exp, "using codec %s" % s.enc)) self.assertEquals(got,self.exp, m(got,self.exp, "using codec %s" % self.enc))
def test1c_read_codec_cp1252(s): def test1c_read_codec_cp1252(self):
got=codecs.open(s.fil, encoding=s.cp).read() got=codecs.open(self.fil, encoding=self.cp).read()
s.assertEquals(got,s.exp, m(got,s.exp, "using codec %s" % s.cp)) self.assertEquals(got,self.exp, m(got,self.exp, "using codec %s" % self.cp))
### ###
class Test2_ansel(unittest.TestCase): class Test2_ansel(unittest.TestCase):
@ -64,14 +64,14 @@ class Test2_ansel(unittest.TestCase):
afil = os.path.join(cdir,enc) afil = os.path.join(cdir,enc)
exp = a2u exp = a2u
def setUp(s): def setUp(self):
open(s.afil, "wb").write(atest_bytes) open(self.afil, "wb").write(atest_bytes)
def test2a_read_ansel(s): def test2a_read_ansel(self):
f = open(s.afil) f = open(self.afil)
ra = G.AnselReader(f) ra = G.AnselReader(f)
got = ra.readline() got = ra.readline()
s.assertEquals(got,s.exp, m(got,s.exp, "AnselReader")) self.assertEquals(got,self.exp, m(got,self.exp, "AnselReader"))
### ###
class Test3(unittest.TestCase): class Test3(unittest.TestCase):
@ -86,35 +86,35 @@ class Test3(unittest.TestCase):
f1byte = os.path.join(cdir, "1byte") f1byte = os.path.join(cdir, "1byte")
exp = utest_chars exp = utest_chars
def setUp(s): def setUp(self):
gen_chars(s.ufil, s.enc) gen_chars(self.ufil, self.enc)
if not os.path.exists(s.f1byte): if not os.path.exists(self.f1byte):
open(s.f1byte, "wb").write("1") open(self.f1byte, "wb").write("1")
def test3a_u8_UTF8Reader_NO_BOM_sig(s): def test3a_u8_UTF8Reader_NO_BOM_sig(self):
f=open(s.ufil) f=open(self.ufil)
ra=G.UTF8Reader(f) ra=G.UTF8Reader(f)
g = ra.readline() g = ra.readline()
s.assertEquals(g,s.exp, m(g,s.exp, "orig UTF8Reader")) self.assertEquals(g,self.exp, m(g,self.exp, "orig UTF8Reader"))
r2 = G.UTF8Reader(open(s.f1byte)) r2 = G.UTF8Reader(open(self.f1byte))
g = r2.readline() g = r2.readline()
s.assertEquals(g,"1", self.assertEquals(g,"1",
m(g,"1", "read 1-byte file")) m(g,"1", "read 1-byte file"))
# NB: utf_8 reads data and never expects a BOM-sig # NB: utf_8 reads data and never expects a BOM-sig
def test3b_utf8_codec_NO_BOM_sig_as_expected(s): def test3b_utf8_codec_NO_BOM_sig_as_expected(self):
g=codecs.open(s.ufil, encoding=s.enc).read() g=codecs.open(self.ufil, encoding=self.enc).read()
s.assertEquals(g,s.exp, m(g,s.exp, "codec utf8")) self.assertEquals(g,self.exp, m(g,self.exp, "codec utf8"))
g=codecs.open(s.f1byte, encoding=s.enc).read() g=codecs.open(self.f1byte, encoding=self.enc).read()
s.assertEquals(g,"1", m(g,"1", "codec utf8")) self.assertEquals(g,"1", m(g,"1", "codec utf8"))
# NB: utf_8_sig reads data even absent a BOM-sig (GOOD!) # NB: utf_8_sig reads data even absent a BOM-sig (GOOD!)
def test3c_utf8_sig_codec_NO_BOM_sig_tolerated_GOOD(s): def test3c_utf8_sig_codec_NO_BOM_sig_tolerated_GOOD(self):
g=codecs.open(s.ufil, encoding=s.enc_sig).read() g=codecs.open(self.ufil, encoding=self.enc_sig).read()
s.assertEquals(g,s.exp, self.assertEquals(g,self.exp,
m(g,s.exp, "codec utf_8_sig NO sig input")) m(g,self.exp, "codec utf_8_sig NO sig input"))
g=codecs.open(s.f1byte, encoding=s.enc_sig).read() g=codecs.open(self.f1byte, encoding=self.enc_sig).read()
s.assertEquals(g,"1", self.assertEquals(g,"1",
m(g,"1", "codec utf_8_sig NO sig input")) m(g,"1", "codec utf_8_sig NO sig input"))
### ###
@ -127,32 +127,32 @@ class Test4(unittest.TestCase):
ufil = os.path.join(cdir, "chars.utf8_sig") ufil = os.path.join(cdir, "chars.utf8_sig")
exp = utest_chars exp = utest_chars
def setUp(s): def setUp(self):
gen_chars(s.ufil, s.enc_sig) gen_chars(self.ufil, self.enc_sig)
def test4a_u8_UTF8Reader_WITH_BOM_sig(s): def test4a_u8_UTF8Reader_WITH_BOM_sig(self):
f=open(s.ufil) f=open(self.ufil)
ra=G.UTF8Reader(f) ra=G.UTF8Reader(f)
g = ra.readline() g = ra.readline()
s.assertEquals(g,s.exp, m(g,s.exp, "orig UTF8Reader")) self.assertEquals(g,self.exp, m(g,self.exp, "orig UTF8Reader"))
# utf_8 reads an initial BOM-sig as data -- oops, pity # utf_8 reads an initial BOM-sig as data -- oops, pity
# write the test to verify this known codec behavior # write the test to verify this known codec behavior
# ==> Recommend: do not use utf8 as input codec (use utf_8_sig) # ==> Recommend: do not use utf8 as input codec (use utf_8_sig)
def test4b_utf8_codec_WITH_BOM_sig_reads_as_data_PITY(s): def test4b_utf8_codec_WITH_BOM_sig_reads_as_data_PITY(self):
g=codecs.open(s.ufil, encoding=s.enc).read() g=codecs.open(self.ufil, encoding=self.enc).read()
e0=u'\ufeff' e0=u'\ufeff'
s.assertEquals(g[0], e0, self.assertEquals(g[0], e0,
m(g[0],e0, "codec utf8 reads 'BOM'-sig as data" )) m(g[0],e0, "codec utf8 reads 'BOM'-sig as data" ))
g = g[1:] g = g[1:]
s.assertEquals(g,s.exp, self.assertEquals(g,self.exp,
m(g,s.exp, "codec utf8 reads rest of data ok")) m(g,self.exp, "codec utf8 reads rest of data ok"))
# utf_8_sig reads and ignores the BOM-sig # utf_8_sig reads and ignores the BOM-sig
def test4c_utf8_sig_codec_WITH_BOM_sig_as_expected(s): def test4c_utf8_sig_codec_WITH_BOM_sig_as_expected(self):
g=codecs.open(s.ufil, encoding=s.enc_sig).read() g=codecs.open(self.ufil, encoding=self.enc_sig).read()
s.assertEquals(g,s.exp, self.assertEquals(g,self.exp,
m(g,s.exp, "codec utf_8_sig NO sig input")) m(g,self.exp, "codec utf_8_sig NO sig input"))
### ###

View File

@ -12,9 +12,9 @@ class Test1(unittest.TestCase):
NB: if any test fails, check imports within that module NB: if any test fails, check imports within that module
""" """
def test1a_buried_imports(s): def test1a_buried_imports(self):
import sys import sys
s.assertTrue(par in sys.path, self.assertTrue(par in sys.path,
"par %r has to be in path!" % par) "par %r has to be in path!" % par)
ilist = ( ilist = (
"_WriteGrdb", "_WriteGrdb",
@ -28,7 +28,7 @@ class Test1(unittest.TestCase):
mod = __import__(m) mod = __import__(m)
except ImportError: except ImportError:
mod = None mod = None
s.assertTrue(mod, "try import of module %r" % m) self.assertTrue(mod, "try import of module %r" % m)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -72,7 +72,7 @@ class FamilyFilterFrame(FilterFrameBase):
self._set_filter(filter_spec) self._set_filter(filter_spec)
def _set_filter(filter_spec): def _set_filter(self):
pass pass
def on_apply(self,button): def on_apply(self,button):

View File

@ -454,12 +454,12 @@ class GrampsDBCallback(object):
# Class methods # Class methods
# #
def __disable_all_signals(cls): def __disable_all_signals(self):
GrampsDBCallback.__BLOCK_ALL_SIGNALS = True GrampsDBCallback.__BLOCK_ALL_SIGNALS = True
disable_all_signals = classmethod(__disable_all_signals) disable_all_signals = classmethod(__disable_all_signals)
def __enable_all_signals(cls): def __enable_all_signals(self):
GrampsDBCallback.__BLOCK_ALL_SIGNALS = False GrampsDBCallback.__BLOCK_ALL_SIGNALS = False
enable_all_signals = classmethod(__enable_all_signals) enable_all_signals = classmethod(__enable_all_signals)

View File

@ -169,7 +169,7 @@ class Test4(unittest.TestCase):
self.assertEquals(A.ansel_to_utf8(""), u"", "empty a2u") self.assertEquals(A.ansel_to_utf8(""), u"", "empty a2u")
self.assertEquals(A.utf8_to_ansel(u""), "", "empty u2a") self.assertEquals(A.utf8_to_ansel(u""), "", "empty u2a")
def test4b_unmapped_combos(s): def test4b_unmapped_combos(self):
""" 4b: (sample) unmapped (non-precomposed) combinations """ """ 4b: (sample) unmapped (non-precomposed) combinations """
samples = ( samples = (
# ansel, unicode, failure-report-message .. see function msg() # ansel, unicode, failure-report-message .. see function msg()
@ -181,17 +181,17 @@ class Test4(unittest.TestCase):
for a,u,m in samples: for a,u,m in samples:
# ansel to unicode and inverse # ansel to unicode and inverse
a2u=A.ansel_to_utf8(a) a2u=A.ansel_to_utf8(a)
s.assertEquals(a2u, u, msg(a2u, u, m, "a2u")) self.assertEquals(a2u, u, msg(a2u, u, m, "a2u"))
a2u2a = A.utf8_to_ansel(a2u) a2u2a = A.utf8_to_ansel(a2u)
s.assertEquals(a2u2a, a, msg(a2u2a, a, m, "a2u2a")) self.assertEquals(a2u2a, a, msg(a2u2a, a, m, "a2u2a"))
# unicode to ansel and inverse # unicode to ansel and inverse
u2a = A.utf8_to_ansel(u) u2a = A.utf8_to_ansel(u)
s.assertEquals(u2a, a, msg(u2a, a, m, "u2a")) self.assertEquals(u2a, a, msg(u2a, a, m, "u2a"))
u2a2u = A.ansel_to_utf8(u2a) u2a2u = A.ansel_to_utf8(u2a)
s.assertEquals(u2a2u, u, msg(u2a2u, u, m, "u2a2u")) self.assertEquals(u2a2u, u, msg(u2a2u, u, m, "u2a2u"))
def test4c_multiple_combos(s): def test4c_multiple_combos(self):
""" 4c: (a2u) ignore multiple combinations (include precomposed) """ """ 4c: (a2u) ignore multiple combinations (include precomposed) """
samples = ( samples = (
("b\xF0\xE5Ze", u"bZ\u0304e", "b <cedilla> Z+macron e"), ("b\xF0\xE5Ze", u"bZ\u0304e", "b <cedilla> Z+macron e"),
@ -203,9 +203,9 @@ class Test4(unittest.TestCase):
) )
for a,u,m in samples: for a,u,m in samples:
a2u=A.ansel_to_utf8(a) a2u=A.ansel_to_utf8(a)
s.assertEquals(a2u, u, msg(a2u,u,m, "a2u drop extra <combiners>")) self.assertEquals(a2u, u, msg(a2u,u,m, "a2u drop extra <combiners>"))
def test4d_multiple_combos(s): def test4d_multiple_combos(self):
""" 4c: (u2a) ignore multiple combinations (include precomposed) """ """ 4c: (u2a) ignore multiple combinations (include precomposed) """
samples = ( samples = (
("b\xE5Ze", u"bZ\u0304\u0327e", "b Z+macron <cedilla> e"), ("b\xE5Ze", u"bZ\u0304\u0327e", "b Z+macron <cedilla> e"),
@ -214,23 +214,23 @@ class Test4(unittest.TestCase):
) )
for a,u,m in samples: for a,u,m in samples:
u2a=A.utf8_to_ansel(u) u2a=A.utf8_to_ansel(u)
s.assertEquals(u2a, a, msg(u2a,a,m, "u2a drop extra <combiners>")) self.assertEquals(u2a, a, msg(u2a,a,m, "u2a drop extra <combiners>"))
class Test99(unittest.TestCase): class Test99(unittest.TestCase):
""" test regression cases """ """ test regression cases """
def test_99a(s): def test_99a(self):
""" 99a: sanity check on counts """ """ 99a: sanity check on counts """
n1B= len(A._onebyte) n1B= len(A._onebyte)
n2B= len(A._twobyte) n2B= len(A._twobyte)
na = n1B+n2B na = n1B+n2B
nu = len(A._utoa) nu = len(A._utoa)
s.assertEquals(na, nu, msg(na, nu, "basic counts: a2u=u2a")) self.assertEquals(na, nu, msg(na, nu, "basic counts: a2u=u2a"))
nac = len(A._acombiners) nac = len(A._acombiners)
nuc = len(A._ucombiners) nuc = len(A._ucombiners)
s.assertEquals(nac, nuc, msg(nac, nuc, "combiner counts: a2u=u2a")) self.assertEquals(nac, nuc, msg(nac, nuc, "combiner counts: a2u=u2a"))
def test_99b(s): def test_99b(self):
""" 99b: fix incorrect mapping for ansel 0xAE """ 99b: fix incorrect mapping for ansel 0xAE
It used-to-be U+02be but was changed March 2005 to U+02bc It used-to-be U+02be but was changed March 2005 to U+02bc
@ -248,7 +248,7 @@ class Test99(unittest.TestCase):
) )
for a, u, m in revs: for a, u, m in revs:
g = A.ansel_to_utf8(a) g = A.ansel_to_utf8(a)
s.assertEquals(g,u, self.assertEquals(g,u,
msg(g, u, m, "spec change")) msg(g, u, m, "spec change"))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -24,37 +24,37 @@ out_ged = os.path.join(ddir,"test_out.ged")
class Test(unittest.TestCase): class Test(unittest.TestCase):
def setUp(s): def setUp(self):
if not os.path.exists(min1r): if not os.path.exists(min1r):
open(min1r,"wb").write(test_ged) open(min1r,"wb").write(test_ged)
if os.path.exists(out_ged): if os.path.exists(out_ged):
os.remove(out_ged) os.remove(out_ged)
# silly test just to illustrate unittest setUp behavior # silly test just to illustrate unittest setUp behavior
def test1_setup_works(s): def test1_setup_works(self):
s.assertTrue(os.path.exists(ddir), "data dir %r exists" % ddir) self.assertTrue(os.path.exists(ddir), "data dir %r exists" % ddir)
s.assertTrue(os.path.exists(min1r), "data file %r exists" % min1r) self.assertTrue(os.path.exists(min1r), "data file %r exists" % min1r)
s.assertFalse(os.path.exists(out_ged), self.assertFalse(os.path.exists(out_ged),
"NO out file %r yet" % out_ged) "NO out file %r yet" % out_ged)
# This tests the fix for bug #1331-1334 # This tests the fix for bug #1331-1334
# read trivial gedcom input, write gedcom output # read trivial gedcom input, write gedcom output
def test2_exec_CLI(s): def test2_exec_CLI(self):
ifile = min1r ifile = min1r
ofile = out_ged ofile = out_ged
gcmd = "./gramps.py -i%s -o%s" % (ifile, ofile) gcmd = "./gramps.py -i%s -o%s" % (ifile, ofile)
rc = os.system("cd %s && python %s" % (pdir, gcmd)) rc = os.system("cd %s && python %s" % (pdir, gcmd))
s.assertEquals(rc,0, tu.msg(rc,0, "executed CLI cmmand %r" % gcmd)) self.assertEquals(rc,0, tu.msg(rc,0, "executed CLI cmmand %r" % gcmd))
# simple validation o output # simple validation o output
s.assertTrue(os.path.isfile(ofile), "output file created") self.assertTrue(os.path.isfile(ofile), "output file created")
content = open(ofile).read() content = open(ofile).read()
g = re.search("INDI", content) g = re.search("INDI", content)
s.assertTrue(g, "found 'INDI' in output file") self.assertTrue(g, "found 'INDI' in output file")
# this verifies that files in the temporary "import dir" # this verifies that files in the temporary "import dir"
# get cleaned before (and after) running a CLI # get cleaned before (and after) running a CLI
# (eg cleanout stale files from prior crash-runs) # (eg cleanout stale files from prior crash-runs)
def test3_files_in_import_dir(s): def test3_files_in_import_dir(self):
import const import const
ddir = os.path.join(const.TEMP_DIR,"import_dbdir") ddir = os.path.join(const.TEMP_DIR,"import_dbdir")
os.makedirs(ddir) os.makedirs(ddir)
@ -68,10 +68,10 @@ class Test(unittest.TestCase):
ofile = out_ged ofile = out_ged
gcmd = "./gramps.py -i%s -o%s" % (ifile, ofile) gcmd = "./gramps.py -i%s -o%s" % (ifile, ofile)
rc = os.system("cd %s && python %s" % (pdir, gcmd)) rc = os.system("cd %s && python %s" % (pdir, gcmd))
s.assertEquals(rc,0, tu.msg(rc,0, "executed CLI cmmand %r" % gcmd)) self.assertEquals(rc,0, tu.msg(rc,0, "executed CLI cmmand %r" % gcmd))
for fn in bogofiles: for fn in bogofiles:
s.assertFalse(os.path.exists(fn)) self.assertFalse(os.path.exists(fn))
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -12,11 +12,11 @@ from test import gedread_util as gr
class Test(U.TestCase): class Test(U.TestCase):
def setUp(s): def setUp(self):
# make a dir to hold an input gedcom file # make a dir to hold an input gedcom file
s.tdir = tu.make_subdir("gr_test") self.tdir = tu.make_subdir("gr_test")
def test1(s): def test1(self):
prec=""" prec="""
0 @I1@ INDI 0 @I1@ INDI
1 NAME GedRead TEST /Person/ 1 NAME GedRead TEST /Person/
@ -24,31 +24,31 @@ class Test(U.TestCase):
# create a gedcom input file # create a gedcom input file
# from canned head/tail -- see gedread_util # from canned head/tail -- see gedread_util
infil = os.path.join(s.tdir,"test_in.ged") infil = os.path.join(self.tdir,"test_in.ged")
gr.make_gedcom_input(infil, prec) gr.make_gedcom_input(infil, prec)
s.assertTrue(os.path.isfile(infil), self.assertTrue(os.path.isfile(infil),
"create input file %s" % infil) "create input file %s" % infil)
# create an empty database # create an empty database
dbpath = os.path.join(s.tdir,"test_db") dbpath = os.path.join(self.tdir,"test_db")
db = gr.create_empty_db(dbpath) db = gr.create_empty_db(dbpath)
s.assertTrue(os.path.isdir(dbpath), self.assertTrue(os.path.isdir(dbpath),
"create database (dir) %s" % dbpath) "create database (dir) %s" % dbpath)
# create logfile to test for read log-messages # create logfile to test for read log-messages
# (note: uses recently added test_util # (note: uses recently added test_util
log = os.path.join(s.tdir, "test_log") log = os.path.join(self.tdir, "test_log")
tl = tu.TestLogger() tl = tu.TestLogger()
tl.logfile_init(log) tl.logfile_init(log)
# now read the gedcom # now read the gedcom
gr.gread(db, infil) gr.gread(db, infil)
logging.warn("nothing here") logging.warn("nothing here")
loglines = tl.logfile_getlines() loglines = tl.logfile_getlines()
s.assertEquals(len(loglines),1, self.assertEquals(len(loglines),1,
"log has no unexpected content") "log has no unexpected content")
# verify one person in database # verify one person in database
np = db.get_number_of_people() np = db.get_number_of_people()
s.assertEquals(np,1, self.assertEquals(np,1,
tu.msg(np,1, "db has exactly one person")) tu.msg(np,1, "db has exactly one person"))
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -49,39 +49,39 @@ except ImportError:
# some enabling infrastructure features # some enabling infrastructure features
class Test1(U.TestCase): class Test1(U.TestCase):
def test1a_custom_exception(s): def test1a_custom_exception(self):
tmsg = "testing" tmsg = "testing"
try: try:
err = None err = None
raise tu.TestError(tmsg) raise tu.TestError(tmsg)
except tu.TestError,e: except tu.TestError,e:
emsg = e.value emsg = e.value
s.assertEqual(emsg, tmsg, self.assertEqual(emsg, tmsg,
"raising TestError: g=%r e=%r" % (emsg, tmsg)) "raising TestError: g=%r e=%r" % (emsg, tmsg))
def test1b_msg_reporting_utility(s): def test1b_msg_reporting_utility(self):
g,e = "got this", "expected that" g,e = "got this", "expected that"
m,p = "your message here", "pfx" m,p = "your message here", "pfx"
tmsg0 = m + "\n .....got:'" + g + \ tmsg0 = m + "\n .....got:'" + g + \
"'\n expected:'" + e +"'" "'\n expected:'" + e +"'"
tmsg1 = p + ": " + tmsg0 tmsg1 = p + ": " + tmsg0
s.assertEqual(tu.msg(g,e,m), tmsg0, "non-prefix message") self.assertEqual(tu.msg(g,e,m), tmsg0, "non-prefix message")
s.assertEqual(tu.msg(g,e,m,p), tmsg1, "prefix message") self.assertEqual(tu.msg(g,e,m,p), tmsg1, "prefix message")
# path-related features (note use of tu.msg tested above) # path-related features (note use of tu.msg tested above)
class Test2(U.TestCase): class Test2(U.TestCase):
def test2a_context_via_traceback(s): def test2a_context_via_traceback(self):
e = os.path.basename(__file__).rstrip(".co") # eg in *.py[co] e = os.path.basename(__file__).rstrip(".co") # eg in *.py[co]
g = os.path.basename(tu._caller_context()[0]).rstrip('co') g = os.path.basename(tu._caller_context()[0]).rstrip('co')
s.assertEqual(g,e, tu.msg(g,e, "_caller_context")) self.assertEqual(g,e, tu.msg(g,e, "_caller_context"))
def test2b_absdir(s): def test2b_absdir(self):
here = tu.absdir(); here = tu.absdir();
g=tu.absdir(__file__) g=tu.absdir(__file__)
s.assertEqual(g,here, tu.msg(g,here, "absdir")) self.assertEqual(g,here, tu.msg(g,here, "absdir"))
def test2c_path_append_parent(s): def test2c_path_append_parent(self):
here = tu.absdir(); here = tu.absdir();
par = os.path.dirname(here) par = os.path.dirname(here)
was_there = par in sys.path was_there = par in sys.path
@ -91,16 +91,16 @@ class Test2(U.TestCase):
np = len(sys.path) np = len(sys.path)
for p in (None, __file__): for p in (None, __file__):
s.assertFalse(par in sys.path, "par not in initial path") self.assertFalse(par in sys.path, "par not in initial path")
if not p: if not p:
g = tu.path_append_parent() g = tu.path_append_parent()
else: else:
g = tu.path_append_parent(p) g = tu.path_append_parent(p)
s.assertEqual(g,par, tu.msg(g,par, "path_append_parent return")) self.assertEqual(g,par, tu.msg(g,par, "path_append_parent return"))
s.assertTrue(par in sys.path, "actually appends") self.assertTrue(par in sys.path, "actually appends")
sys.path.remove(par) sys.path.remove(par)
l= len(sys.path) l= len(sys.path)
s.assertEqual(l,np, tu.msg(l,np,"numpaths")) self.assertEqual(l,np, tu.msg(l,np,"numpaths"))
if was_there: if was_there:
# restore entry state (but no multiples needed!) # restore entry state (but no multiples needed!)
sys.path.append(par) sys.path.append(par)
@ -113,59 +113,59 @@ class Test3(U.TestCase):
home= os.environ["HOME"] home= os.environ["HOME"]
if home: if home:
home_junk = os.path.join(home,"test_junk") home_junk = os.path.join(home,"test_junk")
def _rmsubs(s): def _rmsubs(self):
import shutil import shutil
for sub in s.asubs: for sub in self.asubs:
if os.path.isdir(sub): if os.path.isdir(sub):
shutil.rmtree(sub) shutil.rmtree(sub)
def setUp(s): def setUp(self):
s._rmsubs() self._rmsubs()
if s.home and not os.path.isdir(s.home_junk): if self.home and not os.path.isdir(self.home_junk):
os.mkdir(s.home_junk) os.mkdir(self.home_junk)
def tearDown(s): def tearDown(self):
s._rmsubs() self._rmsubs()
if s.home and os.path.isdir(s.home_junk): if self.home and os.path.isdir(self.home_junk):
os.rmdir(s.home_junk) os.rmdir(self.home_junk)
def test3a_subdir(s): def test3a_subdir(self):
for sub in s.asubs: for sub in self.asubs:
s.assertFalse(os.path.isdir(sub), "init: no dir %r" % sub) self.assertFalse(os.path.isdir(sub), "init: no dir %r" % sub)
b,d = os.path.dirname(sub), os.path.basename(sub) b,d = os.path.dirname(sub), os.path.basename(sub)
md = tu.make_subdir(d, b) md = tu.make_subdir(d, b)
s.assertTrue(os.path.isdir(sub), "made dir %r" % sub) self.assertTrue(os.path.isdir(sub), "made dir %r" % sub)
s.assertEqual(md,sub, tu.msg(md,sub, self.assertEqual(md,sub, tu.msg(md,sub,
"make_subdir returns path")) "make_subdir returns path"))
s2 = os.path.join(sub,"sub2") s2 = os.path.join(sub,"sub2")
tu.make_subdir("sub2", sub) tu.make_subdir("sub2", sub)
s.assertTrue(os.path.isdir(s2), "made dir %r" % s2) self.assertTrue(os.path.isdir(s2), "made dir %r" % s2)
f = os.path.join(s2,"test_file") f = os.path.join(s2,"test_file")
open(f,"w").write("testing..") open(f,"w").write("testing..")
s.assertTrue(os.path.isfile(f), "file %r exists" % f) self.assertTrue(os.path.isfile(f), "file %r exists" % f)
tu.delete_tree(sub) tu.delete_tree(sub)
s.assertFalse(os.path.isdir(sub), self.assertFalse(os.path.isdir(sub),
"delete_tree removes subdir %r" % sub ) "delete_tree removes subdir %r" % sub )
def test3b_delete_tree_constraint(s): def test3b_delete_tree_constraint(self):
if s.home: if self.home:
err = None err = None
try: try:
tu.delete_tree(s.home_junk) tu.delete_tree(self.home_junk)
except tu.TestError, e: except tu.TestError, e:
err = e.value err = e.value
s.assertFalse(err is None, self.assertFalse(err is None,
"deltree on %r raises TestError" % (s.home_junk)) "deltree on %r raises TestError" % (self.home_junk))
else: else:
s.fail("Skip deltree constraint test, no '$HOME' var") self.fail("Skip deltree constraint test, no '$HOME' var")
# logging (& misc?) # logging (& misc?)
class Test4(U.TestCase): class Test4(U.TestCase):
logf = "/tmp/__tu__log__" logf = "/tmp/__tu__log__"
def test4a(s): def test4a(self):
wmsg = "a warning message" wmsg = "a warning message"
emsg = "an error message" emsg = "an error message"
import logging import logging
@ -173,13 +173,13 @@ class Test4(U.TestCase):
tl = tu.TestLogger() tl = tu.TestLogger()
for i in (1,2): for i in (1,2):
# 2 passes to test clearing old file # 2 passes to test clearing old file
tl.logfile_init(s.logf) tl.logfile_init(self.logf)
logging.warn(wmsg) logging.warn(wmsg)
logging.info("nada") logging.info("nada")
logging.error(emsg) logging.error(emsg)
ll = tl.logfile_getlines() ll = tl.logfile_getlines()
nl = len(ll) nl = len(ll)
s.assertEquals(nl,2, self.assertEquals(nl,2,
tu.msg(nl,2, "pass %d: expected line count" % i)) tu.msg(nl,2, "pass %d: expected line count" % i))

View File

@ -166,24 +166,24 @@ class TestLogger():
Note that existing logging will still occur, possibly Note that existing logging will still occur, possibly
resulting in console messages and popup dialogs resulting in console messages and popup dialogs
""" """
def __init__(s, lvl=logging.WARN): def __init__(self, lvl=logging.WARN):
logging.basicConfig(level=lvl) logging.basicConfig(level=lvl)
def logfile_init(s, lfname): def logfile_init(self, lfname):
"""init or re-init a logfile""" """init or re-init a logfile"""
if getattr(s, "lfh", None): if getattr(self, "lfh", None):
logging.getLogger().handlers.remove(s.lfh) logging.getLogger().handlers.remove(self.lfh)
if os.path.isfile(lfname): if os.path.isfile(lfname):
os.unlink(lfname) os.unlink(lfname)
s.lfh = logging.FileHandler(lfname) self.lfh = logging.FileHandler(lfname)
logging.getLogger().addHandler(s.lfh) logging.getLogger().addHandler(self.lfh)
s.lfname = lfname self.lfname = lfname
def logfile_getlines(s): def logfile_getlines(self):
"""get current content of logfile as list of lines""" """get current content of logfile as list of lines"""
txt = [] txt = []
if s.lfname and os.path.isfile(s.lfname): if self.lfname and os.path.isfile(self.lfname):
txt = open(s.lfname).readlines() txt = open(self.lfname).readlines()
return txt return txt
#===eof=== #===eof===