Commit 7e7fb5bd authored by Sriram Karra's avatar Sriram Karra

Add a test suite for parsing .vcf files in our code

- Add a few sample vcf files for testing purposes
- Some changes to the CDContact class to ensure it can be
  instantiated standalone - without a folder or db instance
  tied to it.
parent 44c969a9
......@@ -74,13 +74,16 @@ class CDContact(Contact):
CREATED = 'X-ASYNK-CREATED'
SYNC_TAG_PREFIX = 'X-ASYNK-SYNCTAG-'
def __init__ (self, folder, con=None, vco=None, itemid=None):
def __init__ (self, folder, con=None, vco=None, itemid=None,
debug_vcf=False):
"""vco, if not None, should be a valid vCard object (i.e. the contents
of a vCard file, for e.g. When vco is not None, itemid should also be
not None"""
Contact.__init__(self, folder, con)
self.debug_vcf = debug_vcf
self.set_etag(None)
self.set_uid(None)
self.set_vco(vco)
......@@ -104,7 +107,8 @@ class CDContact(Contact):
elif vco:
self.init_props_from_vco(vco)
assert(itemid)
if not self.debug_vcf:
assert(itemid)
self.set_itemid(itemid)
self.in_init(False)
......@@ -403,7 +407,10 @@ class CDContact(Contact):
def _snarf_sync_tags_from_vco (self, vco):
conf = self.get_config()
pname_re = conf.get_profile_name_re() # '([0-9a-zA-Z]+)'
if self.debug_vcf:
pname_re = '([0-9a-zA-Z]+)'
else:
pname_re = conf.get_profile_name_re()
pname_re = "^" + l(self.SYNC_TAG_PREFIX) + pname_re + "-"
for label, val in vco.contents.iteritems():
......
......@@ -78,9 +78,9 @@ class Item:
# like any other object attributes
self.set_folder(folder)
self.set_db(folder.get_db())
self.set_dbid(folder.get_dbid())
self.set_config(folder.get_config())
self.set_db(folder.get_db() if folder else None)
self.set_dbid(folder.get_dbid() if folder else None)
self.set_config(folder.get_config() if folder else None)
##
## First the abstract methods
......
BEGIN:VCARD
VERSION:3.0
PRODID:-//ASynK v2.0.0-rc2+//EN
UID:7aecd057-3cf9-11e4-acf2-20c9d0851eb3
FN:Own\, the Cloud Test
N:Test;Own\, the Cloud;;;
REV:20140915T165813Z
X-ASYNK-CREATED:20130804T102215Z
X-ASYNK-SYNCTAG-TESTBBCD-BB:bc1d00c7-fcef-11e2-b4c3-3c07541b9945
END:VCARD
BEGIN:VCARD
VERSION:3.0
PRODID:-//ownCloud//NONSGML Contacts 0.3.0.17//EN
UID:eac0a14e-38cd-11e4-a3af-52540054ecf0
EMAIL;TYPE=INTERNET;TYPE=pref;TYPE=HOME:foo@gmail.com
EMAIL;TYPE=INTERNET;TYPE=pref;TYPE=WORK:me@foo.com
FN:Foo Doe
N:Doe;Foo;;;
REV:2014-09-10T09:41:10+00:00
TEL;TYPE=VOICE;TYPE=CELL;TYPE=pref:(925) 123-4567
X-ASYNK-CREATED:20140414T220503Z
X-ASYNK-SYNCTAG-BBCD-BB:3de93042-38c5-11e4-a9a2-52540054ecf0
TEL;TYPE=PREF;TYPE=HOME:(925) 234-5678
END:VCARD
##
## Created : Wed Sep 17 08:45:41 IST 2014
##
## Copyright (C) 2014 Sriram Karra <karra.etc@gmail.com>
##
## This file is part of ASynK
##
## ASynK is free software: you can redistribute it and/or modify it under
## the terms of the GNU Affero GPL (GNU AGPL) as published by the
## Free Software Foundation, version 3 of the License
##
## ASynK is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
## License for more details.
##
## You should have a copy of the license in the doc/ directory of ASynK. If
## not, see <http://www.gnu.org/licenses/>.
##
#####
##
## This unit test file is used to test vCard file parsing and processing
## functionality in ASynK
##
## Usage is: python test_cd.py <VCF file>
import glob, logging, os, re, shutil, sys, traceback, unittest
## Being able to fix the sys.path thusly makes is easy to execute this
## script standalone from IDLE. Hack it is, but what the hell.
DIR_PATH = os.path.abspath(os.path.join(
os.path.dirname(os.path.abspath('__file__')), '../..'))
EXTRA_PATHS = [os.path.join(DIR_PATH, 'lib'), os.path.join(DIR_PATH, 'asynk')]
sys.path = EXTRA_PATHS + sys.path
print sys.path
from state import Config
from pimdb_cd import CDPIMDB
from contact_cd import CDContact
from vobject import vobject
asynk_base_dir = os.path.abspath(os.path.join("..", ".."))
user_dir = os.path.abspath('user_dir')
state_src = os.path.join('..', '..', 'state.init.json')
state_dest = os.path.join(user_dir, 'state.json')
confn_src = os.path.join('..', '..', 'config',
Config.get_latest_config_filen(asynk_base_dir))
confn_dest = os.path.join(user_dir, 'config.json')
def usage ():
print 'Usage: python test_cd.py'
def main (argv=None):
print 'Command line: ', sys.argv
print confn_src
exit
if os.path.exists(user_dir):
logging.debug('Clearing user directory: %s', user_dir)
shutil.rmtree(user_dir)
else:
logging.debug('Creating user directory: %s', user_dir)
os.makedirs(user_dir)
shutil.copyfile(state_src, state_dest)
shutil.copyfile(confn_src, confn_dest)
global config
config = Config(asynk_base_dir=asynk_base_dir, user_dir=user_dir)
data = None
with open(sys.argv[1], "r") as f:
data = f.read()
print data
vco = vobject.readOne(data)
print vco
con = CDContact(None, vco=vobject.readOne(data), debug_vcf=True)
print con
if __name__ == '__main__':
logging.getLogger().setLevel(logging.ERROR)
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment