pep257 and simple client state test

This commit is contained in:
Simeon Warner 2016-03-28 12:14:26 -04:00
parent 8d81455251
commit 9ff696ba4e
2 changed files with 35 additions and 5 deletions

View File

@ -1,4 +1,4 @@
"""ResourceSync client state class
"""ResourceSync client state class.
The client requires memory of state to support incremental
synchronization. At minimum it must store the source timestamp
@ -19,14 +19,14 @@ except ImportError: #python2
class ClientState(object):
"""
"""
"""Read and store client state on disk."""
def __init__(self):
"""Initialize ClientState object with default status file name."""
self.status_file = '.resync-client-status.cfg'
def set_state(self,site,timestamp=None):
"""Write status dict to client status file
"""Write status dict to client status file.
FIXME - should have some file lock to avoid race
"""
@ -44,7 +44,7 @@ class ClientState(object):
configfile.close()
def get_state(self,site):
"""Read client status file and return dict"""
"""Read client status file and return dict."""
parser = ConfigParser.SafeConfigParser()
status_section = 'incremental'
parser.read(self.status_file)
@ -58,4 +58,10 @@ class ClientState(object):
return(timestamp)
def config_site_to_name(self, name):
"""Convert site name to safe string for config.
Simply replaces any non-word chars with underscore. This could
lead to multiple site names mapping to one config name but
probably not too likely.
"""
return( re.sub(r"[^\w]",'_',name) )

View File

@ -0,0 +1,24 @@
from tests.testcase_with_tmpdir import TestCase
import os.path
import unittest
from resync.client_state import ClientState
class TestClientState(TestCase):
def test01_set_and_get(self):
cs = ClientState()
self.assertEqual( cs.status_file, '.resync-client-status.cfg')
cs.status_file = os.path.join(self.tmpdir,cs.status_file)
# Get state
site = 'https://this.site/'
self.assertEqual( cs.get_state(site), None )
cs.set_state(site, 123)
self.assertEqual( cs.get_state(site), 123 )
cs.set_state(site, 456)
self.assertEqual( cs.get_state(site), 456 )
cs.set_state(site)
self.assertEqual( cs.get_state(site), None )
if __name__ == '__main__':
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestClientUtils)
unittest.TextTestRunner(verbosity=2).run(suite)