Fix unit tests

svn: r22752
This commit is contained in:
Nick Hall 2013-07-28 14:57:22 +00:00
parent d6ff126d5a
commit 183e985747
9 changed files with 63 additions and 82 deletions

View File

View File

@ -18,25 +18,23 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# #
# gen/lib/test/grampstype_test.py
# $Id$ # $Id$
""" unittest for grampstype """ """ unittest for grampstype """
import unittest as U import unittest
from test.test_util import msg from ..grampstype import GrampsType
from ..grampstype import GrampsType, _init_map
# some simple map items to test with # some simple map items to test with
vals = "zz ab cd ef".split() vals = "zz ab cd ef".split()
keys = list(range(len(vals))) keys = list(range(len(vals)))
MAP = [ (k,v*2,v) for (k,v) in zip(keys, vals) ] MAP = [(k, v*2, v) for (k, v) in zip(keys, vals)]
BLIST= [1,3] BLIST= [1,3]
class GT0(GrampsType): class GT0(GrampsType):
_DEFAULT = 1 # just avoiding the pre-coded 0 _DEFAULT = 1 # just avoiding the pre-coded 0
_CUSTOM = 3 # just avoiding the pre-coded 0 _CUSTOM = 3 # just avoiding the pre-coded 0
_DATAMAP = MAP _DATAMAP = MAP
# NOTE: this type of code might be used in a migration utility # NOTE: this type of code might be used in a migration utility
@ -47,60 +45,55 @@ class GT1(GT0):
_BLACKLIST = BLIST _BLACKLIST = BLIST
class GT2(GT1): class GT2(GT1):
_BLACKLIST=None _BLACKLIST = None
class Test1(U.TestCase):
class Test1(unittest.TestCase):
# some basic tests # some basic tests
def test1a_basic(s): def test_basic(self):
s.gt=GT0() self.gt = GT0()
s.assertTrue(isinstance(s.gt, GrampsType)) self.assertTrue(isinstance(self.gt, GrampsType))
# spot-check that MAPs get built # spot-check that MAPs get built
e= len(keys) e = len(keys)
g= len(s.gt._E2IMAP) g = len(self.gt._E2IMAP)
s.assertEquals(g,e, msg(g,e, "expected length of E2IMAP")) self.assertEqual(g, e)
# init sets values for int, str, tuple # init sets values for int, str, tuple
# (we ignore instance here -- maybe SB tested, too?) # (we ignore instance here -- maybe SB tested, too?)
# this test depends on having _DEFAULT=1, _CUSTOM=3 # this test depends on having _DEFAULT=1, _CUSTOM=3
# NB: tuple tests w/ lengths < 2 fail before release 10403 # NB: tuple tests w/ lengths < 2 fail before release 10403
def test1b_init_value(s): def test_init_value(self):
for i,v,u in ( for i, v, u in (
(None, 1,''), # all DEFAULT (None, 1, 'abab'), # all DEFAULT
(0, 0,''), (0, 0, 'zzzz'),
(1, 1,''), (1, 1, 'abab'),
('efef', 3,'efef'), # matches CUSTOM ('efef', 3, 'efef'), # matches CUSTOM
('zzzz', 0,''), ('zzzz', 0, 'zzzz'),
('x', 3,'x'), # nomatch gives CUSTOM ('x', 3, 'x'), # nomatch gives CUSTOM
('', 3,''), # nomatch gives CUSTOM ('', 3, ''), # nomatch gives CUSTOM
((0,'zero'), 0,'zero'), # normal behavior ((0,'zero'), 0, 'zzzz'), # normal behavior
((2,), 2,''), # DEFAULT-string, just like int ((2,), 2, 'cdcd'), # DEFAULT-string, just like int
((), 1,''), # DEFAULT-pair ((), 1, 'abab'), # DEFAULT-pair
): ):
s.gt = GT0(i) self.gt = GT0(i)
g= s.gt.val g = self.gt.value
s.assertEquals(g,v, self.assertEqual(g, v)
msg(g,v, "initialization val from '%s'" % repr(i))) g = self.gt.string
g= s.gt.string self.assertEqual(g, u)
s.assertEquals(g,u,
msg(g,u, "initialization string from '%s'" % repr(i)))
# test blacklist functionality added to enable fix of bug #1680 # test blacklist functionality added to enable fix of bug #1680
class Test2(U.TestCase): class Test2(unittest.TestCase):
def test2a_blacklist(s): def test_blacklist(self):
s.gt=GT1() self.gt = GT1()
# check that MAPs have lengths reduced by blacklist # check that MAPs have lengths reduced by blacklist
e= len(keys) - len(BLIST) e = len(keys) - len(BLIST)
g= len(s.gt._E2IMAP) g = len(self.gt._E2IMAP)
s.assertEquals(g,e, msg(g,e, "expected length of blacklisted MAP")) self.assertEqual(g, e)
s.ub=GT2() self.ub=GT2()
# check that these MAPS are now un-blacklisted # check that these MAPS are now un-blacklisted
e= len(keys) e = len(keys)
g= len(s.ub._E2IMAP) g = len(self.ub._E2IMAP)
s.assertEquals(g,e, msg(g,e, "expected length of un-blacklisted MAP")) self.assertEqual(g, e)
if __name__ == "__main__": if __name__ == "__main__":
U.main() unittest.main()

View File

@ -23,12 +23,9 @@
""" Unittest that tests the code involved in merging """ """ Unittest that tests the code involved in merging """
import unittest import unittest
import sys
import os
sys.path.append(os.curdir)
from .. import (Person, Surname, Name, NameType, Family, FamilyRelType, from .. import (Person, Surname, Name, NameType, Family, FamilyRelType,
Event, EventType, Source, Place, Citation, Event, EventType, Source, Place, Citation, Date,
Repository, RepositoryType, MediaObject, Note, NoteType, Repository, RepositoryType, MediaObject, Note, NoteType,
StyledText, StyledTextTag, StyledTextTagType, Tag, StyledText, StyledTextTag, StyledTextTagType, Tag,
ChildRef, ChildRefType, Attribute, MediaRef, AttributeType, ChildRef, ChildRefType, Attribute, MediaRef, AttributeType,

View File

View File

@ -23,9 +23,6 @@
import unittest import unittest
from test import test_util as tu
tu.path_append_parent()
from ..callback import Callback from ..callback import Callback
try: try:

View File

View File

@ -25,23 +25,20 @@ Unittest for export to VCard
To be called from src directory. To be called from src directory.
""" """
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import sys import sys
import os
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
from StringIO import StringIO from StringIO import StringIO
else: else:
from io import StringIO from io import StringIO
import time import time
sys.path.append(os.curdir)
sys.path.append(os.path.join(os.curdir, 'plugins', 'export'))
sys.path.append(os.path.join(os.curdir, 'plugins', 'lib'))
import subprocess import subprocess
import libxml2 import libxml2
from gramps.plugins.lib.libgrampsxml import GRAMPS_XML_VERSION from gramps.plugins.lib.libgrampsxml import GRAMPS_XML_VERSION
from gramps.version import VERSION from gramps.version import VERSION
import exportvcard from gramps.plugins.export.exportvcard import VCardWriter
class VCardCheck(unittest.TestCase): class VCardCheck(unittest.TestCase):
def setUp(self): def setUp(self):
@ -80,7 +77,7 @@ class VCardCheck(unittest.TestCase):
if debug: if debug:
print(input_strfile.getvalue()) print(input_strfile.getvalue())
process = subprocess.Popen('python gramps.py -i - -f gramps -e - -f vcf', process = subprocess.Popen('python Gramps.py -i - -f gramps -e - -f vcf',
stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
result_str, err_str = process.communicate(input_strfile.getvalue()) result_str, err_str = process.communicate(input_strfile.getvalue())
if err_str: if err_str:
@ -95,20 +92,20 @@ class VCardCheck(unittest.TestCase):
"\r\n".join(self.expect) + '\r\n\r\n') "\r\n".join(self.expect) + '\r\n\r\n')
def test_esc_string_none(self): def test_esc_string_none(self):
self.assertEqual(ExportVCard.VCardWriter.esc("nothing"), "nothing") self.assertEqual(VCardWriter.esc("nothing"), "nothing")
def test_esc_string_all(self): def test_esc_string_all(self):
self.assertEqual(ExportVCard.VCardWriter.esc("backslash\\_comma,_semicolon;"), self.assertEqual(VCardWriter.esc("backslash\\_comma,_semicolon;"),
"backslash\\\\_comma\\,_semicolon\\;") "backslash\\\\_comma\\,_semicolon\\;")
def test_esc_string_list(self): def test_esc_string_list(self):
self.assertEqual(ExportVCard.VCardWriter.esc(["comma,", "semicolon;"]),["comma\\,", "semicolon\\;"]) self.assertEqual(VCardWriter.esc(["comma,", "semicolon;"]),["comma\\,", "semicolon\\;"])
def test_esc_string_tuple(self): def test_esc_string_tuple(self):
self.assertEqual(ExportVCard.VCardWriter.esc(("comma,", "semicolon;")),("comma\\,", "semicolon\\;")) self.assertEqual(VCardWriter.esc(("comma,", "semicolon;")),("comma\\,", "semicolon\\;"))
def test_esc_string_wrongtype(self): def test_esc_string_wrongtype(self):
self.assertRaises(TypeError, ExportVCard.VCardWriter.esc, self.assertRaises(TypeError, VCardWriter.esc,
{"comma,":"semicolon;"}) {"comma,":"semicolon;"})
def test_write_formatted_name_title(self): def test_write_formatted_name_title(self):

View File

View File

@ -37,18 +37,15 @@ else:
from io import StringIO from io import StringIO
import time import time
import unittest import unittest
sys.path.append(os.curdir)
sys.path.append(os.path.join(os.curdir, 'plugins','import'))
sys.path.append(os.path.join(os.curdir, 'plugins', 'lib'))
import subprocess import subprocess
import libxml2 import libxml2
import libxslt import libxslt
from gramps.plugins.lib.libgrampsxml import GRAMPS_XML_VERSION from gramps.plugins.lib.libgrampsxml import GRAMPS_XML_VERSION
from gramps.gen.const import ROOT_DIR
from gramps.gen.const import ROOT_DIR, VERSION from gramps.version import VERSION
import importvcard from gramps.plugins.importer.importvcard import (VCardParser, fitin,
from importvcard import VCardParser splitof_nameprefix)
class VCardCheck(unittest.TestCase): class VCardCheck(unittest.TestCase):
def setUp(self): def setUp(self):
@ -63,7 +60,7 @@ class VCardCheck(unittest.TestCase):
<database xmlns="http://gramps-project.org/xml/%s/"> <database xmlns="http://gramps-project.org/xml/%s/">
<header> <header>
<created date="%04d-%02d-%02d" version="%s"/> <created date="%04d-%02d-%02d" version="%s"/>
<researcher/> <researcher>\n </researcher>
</header>""" % \ </header>""" % \
(GRAMPS_XML_VERSION, GRAMPS_XML_VERSION, GRAMPS_XML_VERSION, (GRAMPS_XML_VERSION, GRAMPS_XML_VERSION, GRAMPS_XML_VERSION,
date[0], date[1], date[2], VERSION) date[0], date[1], date[2], VERSION)
@ -100,7 +97,7 @@ class VCardCheck(unittest.TestCase):
buf = libxml2.createOutputBuffer(expect_canonical_strfile, 'UTF-8') buf = libxml2.createOutputBuffer(expect_canonical_strfile, 'UTF-8')
self.string2canonicalxml(expect_str, buf) self.string2canonicalxml(expect_str, buf)
process = subprocess.Popen('python gramps.py ' process = subprocess.Popen('python Gramps.py '
'--config=preferences.eprefix:DEFAULT -i - -f vcf -e - -f gramps', '--config=preferences.eprefix:DEFAULT -i - -f vcf -e - -f gramps',
stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
result_str, err_str = process.communicate(input_str) result_str, err_str = process.communicate(input_str)
@ -122,26 +119,26 @@ class VCardCheck(unittest.TestCase):
self.do_test("\r\n".join(self.vcard), self.gramps) self.do_test("\r\n".join(self.vcard), self.gramps)
def test_splitof_nameprefix_noprefix(self): def test_splitof_nameprefix_noprefix(self):
self.assertEqual(ImportVCard.splitof_nameprefix("Noprefix"), ('',"Noprefix")) self.assertEqual(splitof_nameprefix("Noprefix"), ('',"Noprefix"))
def test_splitof_nameprefix_prefix(self): def test_splitof_nameprefix_prefix(self):
self.assertEqual(ImportVCard.splitof_nameprefix("van Prefix"), ('van',"Prefix")) self.assertEqual(splitof_nameprefix("van Prefix"), ('van',"Prefix"))
def test_splitof_nameprefix_doublespace(self): def test_splitof_nameprefix_doublespace(self):
self.assertEqual(ImportVCard.splitof_nameprefix("van Prefix"), ('van',"Prefix")) self.assertEqual(splitof_nameprefix("van Prefix"), ('van',"Prefix"))
def test_fitin_regular(self): def test_fitin_regular(self):
self.assertEqual(ImportVCard.fitin("Mr. Gaius Julius Caesar", self.assertEqual(fitin("Mr. Gaius Julius Caesar",
"Gaius Caesar", "Julius"), 6) "Gaius Caesar", "Julius"), 6)
def test_fitin_wrong_receiver(self): def test_fitin_wrong_receiver(self):
self.assertEqual(ImportVCard.fitin("A B C", "A D", "B"), -1) self.assertEqual(fitin("A B C", "A D", "B"), -1)
def test_fitin_wrong_element(self): def test_fitin_wrong_element(self):
self.assertRaises(ValueError, ImportVCard.fitin, "A B C", "A C", "D") self.assertRaises(ValueError, fitin, "A B C", "A C", "D")
def test_fitin_last_element(self): def test_fitin_last_element(self):
self.assertRaises(IndexError, ImportVCard.fitin, "A B C", "A B", "C") self.assertRaises(IndexError, fitin, "A B C", "A B", "C")
def test_name_value_split_begin_colon(self): def test_name_value_split_begin_colon(self):
self.vcard.insert(4, ":email@example.com") self.vcard.insert(4, ":email@example.com")