Update VCard unittests to use XML import and command line config

svn: r16823
This commit is contained in:
Michiel Nauta 2011-03-13 22:07:16 +00:00
parent a75b8399b0
commit efbff4178a
2 changed files with 213 additions and 70 deletions

View File

@ -25,16 +25,18 @@ Unittest for export to VCard
To be called from src directory.
"""
# Uses vcf for input, would be better to use Gramps-XML, but import of
# Gramps-XML via stdin is hard.
import unittest
from cStringIO import StringIO
import sys
import os
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 libxml2
from libgrampsxml import GRAMPS_XML_VERSION
from const import VERSION
import Errors
import ExportVCard
@ -42,12 +44,43 @@ import ExportVCard
class VCardCheck(unittest.TestCase):
def setUp(self):
self.expect = ["BEGIN:VCARD", "VERSION:3.0", "PRODID:-//Gramps//NONSGML Gramps %s//EN" % VERSION, "FN:Lastname", "N:Lastname;;;;", "SORT-STRING:" + "Lastname".ljust(55), "END:VCARD"]
date = time.localtime(time.time())
self.input_list = ["BEGIN:VCARD", "VERSION:3.0", "FN:Lastname", "N:Lastname;;;;", "END:VCARD"]
strng = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE database PUBLIC "-//GRAMPS//DTD GRAMPS XML %s//EN"
"http://gramps-project.org/xml/%s/grampsxml.dtd">
<database xmlns="http://gramps-project.org/xml/%s/">
<header>
<created date="%04d-%02d-%02d" version="%s"/>
<researcher/>
</header>
<people>
<person id="I0000" handle="_0000">
<name type="Birth Name">
<surname>Lastname</surname>
</name>
</person>
</people>
</database>""" % \
(GRAMPS_XML_VERSION, GRAMPS_XML_VERSION, GRAMPS_XML_VERSION,
date[0], date[1], date[2], VERSION)
self.input_ = libxml2.readDoc(strng, '', None, libxml2.XML_PARSE_NONET)
self.database = self.input_.getRootElement()
self.people = self.database.firstElementChild().nextElementSibling()
self.person = self.people.firstElementChild()
self.name = self.person.firstElementChild()
self.lastname = self.name.firstElementChild()
def do_test(self, input_str, expect_str, debug=False):
process = subprocess.Popen('python gramps.py -i - -f vcf -e - -f vcf',
def do_test(self, input_doc, expect_str, debug=False):
input_strfile = StringIO()
buf = libxml2.createOutputBuffer(input_strfile, 'UTF-8')
input_doc.saveFormatFileTo(buf, 'UTF-8', 1)
if debug:
print input_strfile.getvalue()
process = subprocess.Popen('python gramps.py -i - -f gramps -e - -f vcf',
stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
result_str, err_str = process.communicate(input_str)
result_str, err_str = process.communicate(input_strfile.getvalue())
if err_str:
print err_str
if debug:
@ -56,7 +89,7 @@ class VCardCheck(unittest.TestCase):
self.assertEqual(result_str, expect_str)
def test_base(self):
self.do_test("\r\n".join(self.input_list),
self.do_test(self.input_,
"\r\n".join(self.expect) + '\r\n\r\n')
def test_esc_string_none(self):
@ -77,123 +110,234 @@ class VCardCheck(unittest.TestCase):
{"comma,":"semicolon;"})
def test_write_formatted_name_title(self):
self.input_list[3] = "N:Lastname;;;Sir.;"
self.name.newTextChild(None, 'title', 'Sir.')
self.expect[3] = "FN:Sir. Lastname"
self.expect[4] = "N:Lastname;;;Sir.;"
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_name_multiple_surname(self):
self.input_list[3] = "N:van Oranje,Nassau;;;;"
self.lastname.setContent("Oranje")
self.lastname.newProp("prefix", "van")
self.name.newTextChild(None, "surname", "Nassau")
self.expect[3] = "FN:van Oranje Nassau"
self.expect[4] = "N:van Oranje,Nassau;;;;"
self.expect[5] = "SORT-STRING:" + "Oranje".ljust(55)
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_name_callname(self):
self.input_list[2] = "FN:A B C Lastname"
self.input_list[3] = "N:Lastname;B;A,C;;"
# callname not in first names!
self.name.newTextChild(None, "first", "B C")
self.name.newTextChild(None, "call", "A")
self.expect[3] = "FN:B C Lastname"
self.expect[4] = "N:Lastname;A;B,C;;"
self.expect[5] = "SORT-STRING:" + "Lastname".ljust(25) + "B C".ljust(30)
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_name_callname_in_addnames(self):
self.name.newTextChild(None, "first", "A B C")
self.name.newTextChild(None, "call", "B")
self.expect[3] = "FN:A B C Lastname"
self.expect[4] = "N:Lastname;B;A,C;;"
self.expect[5] = "SORT-STRING:" + "Lastname".ljust(25) + "A B C".ljust(30)
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
#def test_write_name_callname_in_addnames(self):
# impossible to test with VCF input, need XML
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_name_no_callname(self):
self.input_list[2] = "FN:A B C Lastname"
self.input_list[3] = "N:Lastname;A;B,C;;"
self.name.newTextChild(None, "first", "A B C")
self.expect[3] = "FN:A B C Lastname"
self.expect[4] = "N:Lastname;A;B,C;;"
self.expect[5] = "SORT-STRING:" + "Lastname".ljust(25) + "A B C".ljust(30)
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_name_no_additional_names(self):
self.input_list[2] = "FN:A Lastname"
self.input_list[3] = "N:Lastname;A;;;"
self.name.newTextChild(None, "first", "A")
self.expect[3] = "FN:A Lastname"
self.expect[4] = "N:Lastname;A;;;"
self.expect[5] = "SORT-STRING:" + "Lastname".ljust(25) + "A".ljust(30)
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_name_honprefix(self):
self.input_list[3] = "N:Lastname;;;Sir;"
self.name.newTextChild(None, 'title', 'Sir')
self.expect[3] = "FN:Sir Lastname"
self.expect[4] = "N:Lastname;;;Sir;"
self.expect[5] = "SORT-STRING:" + "Lastname".ljust(55)
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_name_honsuffix(self):
self.input_list[3] = "N:Lastname;;;;Jr."
self.name.newTextChild(None, 'suffix', 'Jr.')
self.expect[3] = "FN:Lastname\, Jr."
self.expect[4] = "N:Lastname;;;;Jr."
self.expect[5] = "SORT-STRING:" + "Lastname".ljust(55)+ "Jr."
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_nicknames_regular(self):
self.input_list.insert(4, "NICKNAME:Nick,N.")
name = self.person.newChild(None, 'name', '')
name.newProp('type', 'Birth Name')
name.newProp('alt', '1')
name.newTextChild(None, 'nick', 'Nick')
name = self.person.newChild(None, 'name', '')
name.newProp('type', 'Birth Name')
name.newProp('alt', '1')
name.newTextChild(None, 'nick', 'N.')
self.expect.insert(6, "NICKNAME:Nick,N.")
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
#def test_nicknames_primary_nick(self)
# impossible to test with VCF input, need XML
def test_nicknames_primary_nick(self):
self.name.newTextChild(None, 'nick', 'Nick')
name = self.person.newChild(None, 'name', '')
name.newProp('type', 'Birth Name')
name.newProp('alt', '1')
name.newTextChild(None, 'nick', 'N.')
self.expect.insert(6, "NICKNAME:Nick,N.")
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_birthdate_regular(self):
self.input_list.insert(4, "BDAY:2001-02-28")
events = self.database.newChild(None, 'events', None)
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0000')
event.newProp('id', 'E0000')
event.newTextChild(None, 'type', 'Birth')
dateval = event.newChild(None, 'dateval', None)
dateval.newProp('val', '2001-02-28')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0000')
evtref.newProp('role', 'Primary')
self.people.addPrevSibling(events)
self.expect.insert(6, "BDAY:2001-02-28")
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
#def test_write_birthdate_empty(self):
#def test_write_birhtdate_textonly(self):
#def test_write_birthdate_span(self):
#def test_write_birthdate_range(self):
# impossible to test with VCF input, need XML
def test_write_birthdate_empty(self):
events = self.database.newChild(None, 'events', None)
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0000')
event.newProp('id', 'E0000')
event.newTextChild(None, 'type', 'Birth')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0000')
evtref.newProp('role', 'Primary')
self.people.addPrevSibling(events)
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_birhtdate_textonly(self):
events = self.database.newChild(None, 'events', None)
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0000')
event.newProp('id', 'E0000')
event.newTextChild(None, 'type', 'Birth')
datestr = event.newChild(None, 'datestr', None)
datestr.newProp('val', 'Christmas 2001')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0000')
evtref.newProp('role', 'Primary')
self.people.addPrevSibling(events)
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_birthdate_span(self):
events = self.database.newChild(None, 'events', None)
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0000')
event.newProp('id', 'E0000')
event.newTextChild(None, 'type', 'Birth')
datespan = event.newChild(None, 'datespan', None)
datespan.newProp('start', '2001-02-28')
datespan.newProp('stop', '2002-02-28')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0000')
evtref.newProp('role', 'Primary')
self.people.addPrevSibling(events)
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_birthdate_range(self):
events = self.database.newChild(None, 'events', None)
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0000')
event.newProp('id', 'E0000')
event.newTextChild(None, 'type', 'Birth')
daterange = event.newChild(None, 'daterange', None)
daterange.newProp('start', '2001-02-28')
daterange.newProp('stop', '2002-02-28')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0000')
evtref.newProp('role', 'Primary')
self.people.addPrevSibling(events)
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_addresses_regular(self):
self.input_list.insert(4, "ADR:pobox;bis;street;place;province;zip;country")
address = self.person.newChild(None, 'address', None)
address.newChild(None, 'street', 'pobox bis street')
address.newChild(None, 'city', 'place')
address.newChild(None, 'country', 'country')
address.newChild(None, 'state', 'province')
address.newChild(None, 'postal', 'zip')
self.expect.insert(6, "ADR:;;pobox bis street;place;province;zip;country")
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_addresses_phone(self):
self.input_list.insert(4, "TEL:01234-56789")
address = self.person.newChild(None, 'address', None)
address.newChild(None, 'phone', '01234-56789')
self.expect.insert(6, "TEL:01234-56789")
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_urls_email(self):
self.input_list.insert(4, "EMAIL:me@example.com")
url = self.person.newChild(None, 'url', None)
url.newProp('type', 'E-mail')
url.newProp('href', 'me@example.com')
self.expect.insert(6, "EMAIL:me@example.com")
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
#def test_write_urls_emial_mailto(self):
# impossible to test with VCF input, need XML
def test_write_urls_emial_mailto(self):
url = self.person.newChild(None, 'url', None)
url.newProp('type', 'E-mail')
url.newProp('href', 'mailto:me@example.com')
self.expect.insert(6, "EMAIL:me@example.com")
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_urls_url(self):
self.input_list.insert(4, "URL:http://www.example.org")
url = self.person.newChild(None, 'url', None)
url.newProp('type', 'Web Home')
url.newProp('href', 'http://www.example.org')
self.expect.insert(6, "URL:http://www.example.org")
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
def test_write_occupation_regular(self):
self.input_list.insert(4, "ROLE:carpenter")
events = self.database.newChild(None, 'events', None)
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0000')
event.newProp('id', 'E0000')
event.newChild(None, 'type', 'Occupation')
event.newChild(None, 'description', 'carpenter')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0000')
evtref.newProp('role', 'Primary')
self.people.addPrevSibling(events)
self.expect.insert(6, "ROLE:carpenter")
self.do_test("\r\n".join(self.input_list),
"\r\n".join(self.expect) + '\r\n\r\n')
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
#def test_write_occupation_lastdate(self):
# impossible to test with VCF input, need XML
def test_write_occupation_lastdate(self):
events = self.database.newChild(None, 'events', None)
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0000')
event.newProp('id', 'E0000')
event.newChild(None, 'type', 'Occupation')
dateval = event.newChild(None, 'dateval', None)
dateval.newProp('val', '2011-02-28')
event.newChild(None, 'description', 'foreman')
event = events.newChild(None, 'event', None)
event.newProp('handle', '_e0001')
event.newProp('id', 'E0001')
event.newChild(None, 'type', 'Occupation')
dateval = event.newChild(None, 'dateval', None)
dateval.newProp('val', '2000-09-21')
event.newChild(None, 'description', 'carpenter')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0000')
evtref.newProp('role', 'Primary')
evtref = self.person.newChild(None, 'eventref', None)
evtref.newProp('hlink', '_e0001')
evtref.newProp('role', 'Primary')
self.people.addPrevSibling(events)
self.expect.insert(6, "ROLE:foreman")
self.do_test(self.input_, "\r\n".join(self.expect) + '\r\n\r\n')
if __name__ == "__main__":
unittest.main()

View File

@ -25,8 +25,6 @@ Unittest of import of VCard
To be called from src directory.
"""
#TODO id number depend on user format.
# in case of a failing test, add True as last parameter to do_test to see the output.
from cStringIO import StringIO
@ -98,8 +96,9 @@ class VCardCheck(unittest.TestCase):
buf = libxml2.createOutputBuffer(expect_canonical_strfile, 'UTF-8')
self.string2canonicalxml(expect_str, buf)
process = subprocess.Popen('python gramps.py -i - -f vcf -e - -f gramps',
stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
process = subprocess.Popen('python gramps.py '
'--config=preferences.eprefix:DEFAULT -i - -f vcf -e - -f gramps',
stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
result_str, err_str = process.communicate(input_str)
if err_str:
print err_str