Tests: added a generic cli test framework; testing gedcom imports

This commit is contained in:
Doug Blank 2016-04-28 11:38:44 -04:00
parent e9065f3b21
commit aa69ffb174

View File

@ -28,12 +28,14 @@ from gramps.test.test_util import Gramps
ddir = os.path.dirname(__file__) ddir = os.path.dirname(__file__)
example = os.path.join(ddir, "..", "..", "..", example = os.path.join(ddir, "..", "..", "..",
"example", "gramps", "data.gramps") "example", "gramps", "data.gramps")
sample = os.path.join(ddir, "..", "..", "..",
"example", "gedcom", "sample.ged")
TREE_NAME = "Test_reporttest" TREE_NAME = "Test_reporttest"
class ReportControl(object): class ReportControl(object):
def tearDown(self): def tearDown(self):
out, err = self.call("-y", "--remove", TREE_NAME) out, err = self.call("-y", "--remove", TREE_NAME + ".*")
def call(self, *args): def call(self, *args):
print("call:", args) print("call:", args)
@ -56,22 +58,10 @@ class ReportControl(object):
report_name, description = line.split("- ", 1) report_name, description = line.split("- ", 1)
self.reports.append(report_name.strip()) self.reports.append(report_name.strip())
def testall(self, class_): def addreport(self, class_, report_name, test_function,
count = 0 files, **options):
print(self.reports)
for report_name in self.reports:
print("add attr:", report_name)
setattr(class_, "test_%s" % count, dynamic_method(
"--force",
"-O", TREE_NAME,
"--action", "report",
"--options", "name=%s" % report_name))
count += 1
def addtest(self, class_, report_name, test_function,
files, **options):
test_name = report_name.replace("-", "_") test_name = report_name.replace("-", "_")
setattr(class_, test_name, dynamic_method( setattr(class_, test_name, dynamic_report_method(
report_name, report_name,
test_function, test_function,
files, files,
@ -81,8 +71,18 @@ class ReportControl(object):
"--options", "name=%s" % report_name, "--options", "name=%s" % report_name,
**options)) **options))
def dynamic_method(report_name, test_function, def addcli(self, class_, report_name, test_function,
files, *args, **options): files, *args, **options):
test_name = report_name.replace("-", "_")
setattr(class_, test_name,
dynamic_cli_method(
report_name,
test_function,
files,
*args))
def dynamic_report_method(report_name, test_function,
files, *args, **options):
args = list(args) args = list(args)
args[-1] += "," + (",".join(["%s=%s" % (k, v) for (k,v) in options.items()])) args[-1] += "," + (",".join(["%s=%s" % (k, v) for (k,v) in options.items()]))
options["files"] = files options["files"] = files
@ -92,6 +92,15 @@ def dynamic_method(report_name, test_function,
self.assertTrue(test_function(out, err, report_name, **options)) self.assertTrue(test_function(out, err, report_name, **options))
return test_method return test_method
def dynamic_cli_method(report_name, test_function,
files, *args, **options):
options["files"] = files
# This needs to have "test" in name:
def test_method(self):
out, err = self.call(*args)
self.assertTrue(test_function(out, err, report_name, **options))
return test_method
class TestDynamic(unittest.TestCase): class TestDynamic(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@ -110,7 +119,7 @@ class TestDynamic(unittest.TestCase):
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
out, err = cls.call("-y", "--remove", TREE_NAME) out, err = cls.call("-y", "--remove", TREE_NAME + ".*")
reports = ReportControl() reports = ReportControl()
@ -122,43 +131,58 @@ def report_contains(text):
print(contents) print(contents)
if options.get("files", []): if options.get("files", []):
for filename in options.get("files", []): for filename in options.get("files", []):
if os.path.isdir(filename): if filename is None:
pass
elif os.path.isdir(filename):
shutil.rmtree(filename) shutil.rmtree(filename)
elif os.path.isfile(filename): elif os.path.isfile(filename):
os.remove(filename) os.remove(filename)
else: else:
raise Exception("can't find '%s' in order to delete it" % filename) raise Exception("can't find '%s' in order to delete it" % filename)
else: elif os.path.isfile(report_name + "." + ext):
os.remove(report_name + "." + ext) os.remove(report_name + "." + ext)
else:
raise Exception("can't find '%s' in order to delete it" % (report_name + "." + ext))
return text in contents return text in contents
return test_output_file return test_output_file
def err_does_not_contain(text): def err_does_not_contain(text):
def test_output_file(out, err, report_name, **options): def test_output_file(out, err, report_name, **options):
ext = options["off"]
print(err)
if options.get("files", []): if options.get("files", []):
for filename in options.get("files", []): for filename in options.get("files", []):
if os.path.isdir(filename): if filename is None:
pass
elif os.path.isdir(filename):
shutil.rmtree(filename) shutil.rmtree(filename)
elif os.path.isfile(filename): elif os.path.isfile(filename):
os.remove(filename) os.remove(filename)
else: else:
raise Exception("can't find '%s' in order to delete it" % filename) raise Exception("can't find '%s' in order to delete it" % filename)
else: else:
os.remove(report_name + "." + ext) ext = options["off"]
if os.path.isfile(report_name + "." + ext):
os.remove(report_name + "." + ext)
else:
raise Exception("can't find '%s' in order to delete it" % (report_name + "." + ext))
return text not in err return text not in err
return test_output_file return test_output_file
reports.addtest(TestDynamic, "tag_report", reports.addreport(TestDynamic, "tag_report",
report_contains("I0037 Smith, Edwin Michael"), report_contains("I0037 Smith, Edwin Michael"),
[], [],
off="txt", tag="tag1") off="txt", tag="tag1")
reports.addtest(TestDynamic, "navwebpage", reports.addreport(TestDynamic, "navwebpage",
err_does_not_contain("Failed to write report."), err_does_not_contain("Failed to write report."),
["/tmp/NAVWEB"], ["/tmp/NAVWEB"],
off="html", target="/tmp/NAVWEB") off="html", target="/tmp/NAVWEB")
reports.addcli(TestDynamic, "import_gedcom",
err_does_not_contain("Failed to write report."),
[None],
"--force",
"-C", TREE_NAME + "_import_gedcom",
"--import", sample)
### Three hashes: capture out/err seems to conflict with Travis/nose proxy: ### Three hashes: capture out/err seems to conflict with Travis/nose proxy:
@ -215,10 +239,10 @@ report_list = [
] ]
for (report_name, off, files) in report_list: for (report_name, off, files) in report_list:
reports.addtest(TestDynamic, report_name, reports.addreport(TestDynamic, report_name,
err_does_not_contain("Failed to write report."), err_does_not_contain("Failed to write report."),
files=files, files=files,
off=off) off=off)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()