Tests and tweaks for client_utils

This commit is contained in:
Simeon Warner 2016-04-04 12:47:23 -04:00
parent f3377fa18b
commit c839be7ba0
3 changed files with 51 additions and 28 deletions

View File

@ -20,12 +20,22 @@ Copyright 2012,2013 Simeon Warner
import logging
import logging.config
import optparse
import sys
from datetime import datetime
from resync.client import ClientFatalError
from resync.explorer import Explorer
from resync.utils import UTCFormatter
class UTCFormatter(logging.Formatter):
"""Format datetime values as ISO8601 UTC Z form.
Based on http://bit.ly/T2n3Xk
"""
def formatTime(self, record, datefmt=None):
"""Format datetime of record.created as ISO8601 UTC Z form."""
timestamp = record.created
return datetime.utcfromtimestamp(timestamp).isoformat() + 'Z'
def init_logging(to_file=False, logfile=None, default_logfile='/tmp/resync.log',
human=True, verbose=False, eval_mode=False,
@ -88,8 +98,8 @@ def parse_links(args_link):
for link_str in args_link:
try:
links.append(parse_link(link_str))
except ValueError as e:
raise ClientFatalError("Bad --link option '%s' (%s)"%(link_str,str(e)))
except ClientFatalError as e:
raise ClientFatalError("Bad --link option '%s' (%s)" % (link_str,str(e)))
return(links)
def parse_link(link_str):

View File

@ -1,19 +1,5 @@
"""util.py: A collection of common util functions used in source and/or client."""
from logging import Formatter
from datetime import datetime
class UTCFormatter(Formatter):
"""Format datetime values as ISO8601 UTC Z form.
Based on http://bit.ly/T2n3Xk
"""
def formatTime(self, record):
"""Format datetime of record.created as ISO8601 UTC Z form."""
timestamp = record.created
return datetime.utcfromtimestamp(timestamp).isoformat() + 'Z'
"""Compute digests for ResourceSync.
These are all base64 encoded according to the rules of

View File

@ -1,16 +1,37 @@
from tests.testcase_with_tmpdir import TestCase
import logging
import os.path
import re
import unittest
from resync.client_utils import count_true_args,parse_links,parse_link,parse_capabilities,parse_capability_lists
from resync.client_utils import init_logging,count_true_args,parse_links,parse_link,parse_capabilities,parse_capability_lists
from resync.client import ClientFatalError
class TestClientUtils(unittest.TestCase):
class TestClientUtils(TestCase):
def test01_count_true_args(self):
def test01_init_logging(self):
#to_file=False, logfile=None, default_logfile='/tmp/resync.log',
# human=True, verbose=False, eval_mode=False,
# default_logger='client', extra_loggers=None):
tmplog = os.path.join(self.tmpdir,'tmp.log')
init_logging( to_file=True, default_logfile=tmplog, extra_loggers=['x1','x2'] )
# check x1 and x2 set, not x3 (can tell by level)
self.assertTrue( logging.getLogger('x1').level, logging.DEBUG )
self.assertTrue( logging.getLogger('x2').level, logging.DEBUG )
self.assertEqual( logging.getLogger('x3').level, 0 )
# write something, check goes to file
log = logging.getLogger('resync')
log.warning('PIGS MIGHT FLY')
logtxt = open(tmplog,'r').read()
self.assertTrue( re.search(r'WARNING \| PIGS MIGHT FLY', logtxt) )
def test02_count_true_args(self):
self.assertEqual( count_true_args(), 0 )
self.assertEqual( count_true_args(True), 1 )
self.assertEqual( count_true_args(False), 0 )
self.assertEqual( count_true_args(0,1,2,3), 3 )
def test02_parse_links(self):
def test03_parse_links(self):
self.assertEqual( parse_links( [] ), [] )
self.assertEqual( parse_links( ['u,h'] ), [{'href': 'h', 'rel': 'u'}] )
self.assertEqual( parse_links( ['u,h','v,i'] ), [{'href': 'h', 'rel': 'u'},{'href': 'i', 'rel': 'v'}] )
@ -18,7 +39,7 @@ class TestClientUtils(unittest.TestCase):
self.assertRaises( ClientFatalError, parse_links, ['u'] )
self.assertRaises( ClientFatalError, parse_links, ['u,h','u'] )
def test03_parse_link(self):
def test04_parse_link(self):
# Input string of the form: rel,href,att1=val1,att2=val2
self.assertEqual( parse_link('u,h'), {'href': 'h', 'rel': 'u'} )
self.assertEqual( parse_link('u,h,a=b'), {'a': 'b', 'href': 'h', 'rel': 'u'} )
@ -32,14 +53,20 @@ class TestClientUtils(unittest.TestCase):
self.assertRaises( ClientFatalError, parse_link, 'u,h,a=' )
self.assertRaises( ClientFatalError, parse_link, 'u,h,a=b,=c' )
def test04_parse_capabilities(self):
def test05_parse_capabilities(self):
# Input string of the form: cap_name=uri,cap_name=uri
# good
c = parse_capabilities( 'a=' )
self.assertEqual( len(c), 1 )
self.assertEqual( c['a'], '' )
c = parse_capabilities( 'a=b,c=' )
self.assertEqual( len(c), 2 )
self.assertEqual( c['a'], 'b' )
# bad
self.assertRaises( ClientFatalError, parse_capabilities, 'a' )
#self.assertRaises( ClientFatalError, parse_capabilities, 'a=' )
self.assertRaises( ClientFatalError, parse_capabilities, 'a=b,' )
#self.assertRaises( ClientFatalError, parse_capabilities, 'a=b,c=' )
def test05_parse_capability_lists(self):
def test06_parse_capability_lists(self):
# Input string of the form: uri,uri
self.assertEqual( parse_capability_lists('a,b'), ['a','b'] )