[tor-commits] [bridgedb/develop] Add additional unittests for uncovered portions of bridgedb.persistent.

isis at torproject.org isis at torproject.org
Sun Mar 1 06:04:16 UTC 2015


commit 09350c2af6c3248f5543740796f3e73d23b3f7aa
Author: Isis Lovecruft <isis at torproject.org>
Date:   Tue Feb 24 20:47:18 2015 +0000

    Add additional unittests for uncovered portions of bridgedb.persistent.
---
 lib/bridgedb/persistent.py                      |   13 ++---
 lib/bridgedb/test/test_persistentSaveAndLoad.py |   68 ++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 8 deletions(-)

diff --git a/lib/bridgedb/persistent.py b/lib/bridgedb/persistent.py
index 8c65e78..f728bdd 100644
--- a/lib/bridgedb/persistent.py
+++ b/lib/bridgedb/persistent.py
@@ -17,7 +17,7 @@ import os.path
 
 try:
     import cPickle as pickle
-except (ImportError, NameError):
+except (ImportError, NameError):  # pragma: no cover
     import pickle
 
 from twisted.python.reflect import safe_repr
@@ -148,7 +148,7 @@ class State(jelly.Jellyable):
                 fh.close()
             os.unlink(self._statefile)
             self._statefile = None
-        except (IOError, OSError) as error:
+        except (IOError, OSError) as error:  # pragma: no cover
             logging.error("There was an error deleting the statefile: '%s'"
                           % self._statefile)
 
@@ -178,7 +178,7 @@ class State(jelly.Jellyable):
                 fh = statefile
             else:
                 raise TypeError("Nothing worked.")
-        except (IOError, OSError) as error:
+        except (IOError, OSError) as error:  # pragma: no cover
             err += "There was an error reading statefile "
             err += "'{0}':\n{1}".format(statefile, error)
         except (AttributeError, TypeError) as error:
@@ -208,10 +208,9 @@ class State(jelly.Jellyable):
 
         try:
             fh = open(statefile, 'w')
-        except MissingState as error:
-            err += error.message
-        except (IOError, OSError) as error:
-            err += "Error writing state file to '%s': %s" % (statefile, error)
+        except (IOError, OSError) as error:  # pragma: no cover
+            logging.warn("Error writing state file to '%s': %s"
+                         % (statefile, error))
         else:
             try:
                 pickle.dump(jelly.jelly(self), fh)
diff --git a/lib/bridgedb/test/test_persistentSaveAndLoad.py b/lib/bridgedb/test/test_persistentSaveAndLoad.py
index 7919df3..67585f6 100644
--- a/lib/bridgedb/test/test_persistentSaveAndLoad.py
+++ b/lib/bridgedb/test/test_persistentSaveAndLoad.py
@@ -67,6 +67,45 @@ class StateSaveAndLoadTests(unittest.TestCase):
         if savedStatefile:
             self.assertTrue(os.path.isfile(str(savedStatefile)))
 
+    def test_init_with_STATEFILE(self):
+        config = self.config
+        setattr(config, 'STATEFILE', '~/foo.state')
+        state = persistent.State(**config.__dict__)
+        self.loadedStateAssertions(state)
+        statefile = state.statefile
+        self.assertTrue(statefile.endswith('foo.state'))
+
+    def test_init_without_config(self):
+        state = persistent.State(None)
+        self.loadedStateAssertions(state)
+
+    def test_init_with_config(self):
+        state = persistent.State(self.config)
+        self.loadedStateAssertions(state)
+
+    def test_get_statefile(self):
+        statefile = self.state._get_statefile()
+        self.assertIsInstance(statefile, basestring)
+
+    def test_set_statefile(self):
+        self.state._set_statefile('~/bar.state')
+        statefile = self.state._get_statefile()
+        self.assertIsInstance(statefile, basestring)
+
+    def test_set_statefile_new_dir(self):
+        config = self.config
+        setattr(config, 'STATEFILE', 'statefiles/foo.state')
+        state = persistent.State(**config.__dict__)
+        self.loadedStateAssertions(state)
+        statefile = state.statefile
+        self.assertTrue(statefile.endswith('foo.state'))
+
+    def test_del_statefile(self):
+        self.state._set_statefile('baz.state')
+        self.state._del_statefile()
+        statefile = self.state._get_statefile()
+        self.assertIsNone(statefile)
+
     def test_save(self):
         self.state.save()
         self.savedStateAssertions()
@@ -93,5 +132,32 @@ class StateSaveAndLoadTests(unittest.TestCase):
 
     def test_load(self):
         self.state.save()
-        loadedState = persistent.load()     
+        loadedState = persistent.load()
+        self.loadedStateAssertions(loadedState)
+
+    def test_load_with_state(self):
+        loadedState = persistent.load(self.state)
         self.loadedStateAssertions(loadedState)
+
+    def test_load_with_None(self):
+        persistent._setState(None)
+        self.assertRaises(persistent.MissingState,
+                          persistent.load, None)
+
+    def test_load_with_statefile(self):
+        self.assertRaises(persistent.MissingState,
+                          self.state.load, 'quux.state')
+
+    def test_load_with_statefile_opened(self):
+        fh = open('quux.state', 'w+')
+        self.assertRaises(persistent.MissingState, self.state.load, fh)
+        fh.close()
+
+    def test_load_with_statefile_object(self):
+        self.assertRaises(persistent.MissingState, self.state.load, object)
+
+    def test_load_without_statefile(self):
+        persistent._setState(None)
+        self.state.statefile = None
+        self.assertRaises(persistent.MissingState,
+                          persistent.load)





More information about the tor-commits mailing list