diff --git a/ring_doorbell/utils.py b/ring_doorbell/utils.py index 24797781..ade18a66 100644 --- a/ring_doorbell/utils.py +++ b/ring_doorbell/utils.py @@ -61,5 +61,3 @@ def _read_cache(filename): return data except EOFError: return _clean_cache(filename) - except: - raise diff --git a/tests/test_ring.py b/tests/test_ring.py index ac2912f3..3cc3b8b1 100644 --- a/tests/test_ring.py +++ b/tests/test_ring.py @@ -279,6 +279,15 @@ def test_chime_attributes(self, get_mock, post_mock): class TestRingDoorBell(unittest.TestCase): """Test the Ring DoorBell object.""" + def cleanup(self): + """Cleanup any data created from the tests.""" + if os.path.isfile(CACHE): + os.remove(CACHE) + + def tearDown(self): + """Stop everything started.""" + self.cleanup() + @mock.patch('requests.Session.get', side_effect=mocked_requests_get) @mock.patch('requests.Session.post', side_effect=mocked_requests_get) def test_doorbell_attributes(self, get_mock, post_mock): @@ -321,12 +330,19 @@ def test_shared_doorbell_attributes(self, get_mock, post_mock): self.assertEqual(5, dev.volume) self.assertEqual('Digital', dev.existing_doorbell_type) - os.remove(CACHE) - class TestRingDoorBellAlerts(unittest.TestCase): """Test the Ring DoorBell alerts.""" + def cleanup(self): + """Cleanup any data created from the tests.""" + if os.path.isfile(CACHE): + os.remove(CACHE) + + def tearDown(self): + """Stop everything started.""" + self.cleanup() + @mock.patch('requests.Session.get', side_effect=mocked_requests_get) @mock.patch('requests.Session.post', side_effect=mocked_requests_get) def test_doorbell_alerts(self, get_mock, post_mock): @@ -344,5 +360,3 @@ def test_doorbell_alerts(self, get_mock, post_mock): self.assertIsInstance(dev.alert_expires_at, datetime) self.assertTrue(datetime.now() <= dev.alert_expires_at) self.assertIsNotNone(dev._ring.cache_file) - - os.remove(CACHE) diff --git a/tests/test_ring_utils.py b/tests/test_ring_utils.py index 4dfe64d5..10e056ba 100644 --- a/tests/test_ring_utils.py +++ b/tests/test_ring_utils.py @@ -1,5 +1,6 @@ """The tests utils.py for the Ring platform.""" import os +import sys import unittest from ring_doorbell.utils import ( _locator, _clean_cache, _exists_cache, _save_cache, _read_cache) @@ -13,6 +14,15 @@ class TestUtils(unittest.TestCase): """Test utils.py.""" + def cleanup(self): + """Cleanup any data created from the tests.""" + if os.path.isfile(CACHE): + os.remove(CACHE) + + def tearDown(self): + """Stop everything started.""" + self.cleanup() + def test_locator(self): """Test _locator method.""" self.assertEquals(-1, _locator([DATA], 'key', 'bar')) @@ -22,28 +32,36 @@ def test_initiliaze_clean_cache(self): """Test _clean_cache method.""" self.assertTrue(_save_cache(DATA, CACHE)) self.assertIsInstance(_clean_cache(CACHE), dict) - os.remove(CACHE) + self.cleanup() def test_exists_cache(self): """Test _exists_cache method.""" self.assertTrue(_save_cache(DATA, CACHE)) self.assertTrue(_exists_cache(CACHE)) - os.remove(CACHE) + self.cleanup() def test_read_cache(self): """Test _read_cache method.""" self.assertTrue(_save_cache(DATA, CACHE)) self.assertIsInstance(_read_cache(CACHE), dict) - os.remove(CACHE) + self.cleanup() def test_read_cache_eoferror(self): """Test _read_cache method.""" open(CACHE, 'a').close() self.assertIsInstance(_read_cache(CACHE), dict) - os.remove(CACHE) + self.cleanup() def test_read_cache_dict(self): """Test _read_cache with expected dict.""" self.assertTrue(_save_cache(CACHE_ATTRS, CACHE)) self.assertIsInstance(_read_cache(CACHE), dict) - os.remove(CACHE) + self.cleanup() + + def test_general_exceptions(self): + """Test exception triggers on utils.py""" + self.assertRaises(TypeError, _clean_cache, True) + if sys.version_info.major == 2: + self.assertRaises(TypeError, _read_cache, True) + else: + self.assertRaises(OSError, _read_cache, True)