diff --git a/tests/test_wakatime_offlinequeue.py b/tests/test_wakatime_offlinequeue.py new file mode 100644 index 0000000..31e8da5 --- /dev/null +++ b/tests/test_wakatime_offlinequeue.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + + +from wakatime.main import execute +from wakatime.offlinequeue import Queue +from wakatime.packages import requests + +import os +import time +from wakatime.compat import u +from wakatime.packages.requests.models import Response +from . import utils + + +class OfflineQueueTestCase(utils.TestCase): + patch_these = [ + 'wakatime.packages.requests.adapters.HTTPAdapter.send', + 'wakatime.session_cache.SessionCache.save', + 'wakatime.session_cache.SessionCache.delete', + ['wakatime.session_cache.SessionCache.get', requests.session], + ] + + def test_heartbeat_saved_from_error_response(self): + response = Response() + response.status_code = 500 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + now = u(int(time.time())) + entity = 'tests/samples/twolinefile.txt' + config = 'tests/samples/sample.cfg' + + args = ['--file', entity, '--alternate-project', 'xyz', '--config', config, '--time', now] + execute(args) + + queue = Queue() + saved_heartbeat = queue.pop() + self.assertEquals(os.path.abspath(entity), saved_heartbeat['entity']) + + def test_heartbeat_discarded_from_400_response(self): + response = Response() + response.status_code = 400 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + now = u(int(time.time())) + entity = 'tests/samples/twolinefile.txt' + config = 'tests/samples/sample.cfg' + + args = ['--file', entity, '--alternate-project', 'xyz', '--config', config, '--time', now] + execute(args) + + queue = Queue() + saved_heartbeat = queue.pop() + self.assertEquals(None, saved_heartbeat) + + def test_offline_heartbeat_sent_after_success_response(self): + response = Response() + response.status_code = 500 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + now = u(int(time.time())) + entity = 'tests/samples/twolinefile.txt' + config = 'tests/samples/sample.cfg' + + args = ['--file', entity, '--alternate-project', 'xyz', '--config', config, '--time', now] + execute(args) + + response.status_code = 201 + execute(args) + + queue = Queue() + saved_heartbeat = queue.pop() + self.assertEquals(None, saved_heartbeat) diff --git a/tests/utils.py b/tests/utils.py index 9b8142e..862e3bd 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -3,9 +3,9 @@ import logging try: - from mock import patch + import mock except ImportError: - from unittest.mock import patch + import unittest.mock as mock try: # Python 2.6 import unittest2 as unittest @@ -25,7 +25,7 @@ class TestCase(unittest.TestCase): for patch_this in self.patch_these: namespace = patch_this[0] if isinstance(patch_this, (list, set)) else patch_this - patcher = patch(namespace) + patcher = mock.patch(namespace) mocked = patcher.start() self.patched[namespace] = mocked @@ -36,4 +36,4 @@ class TestCase(unittest.TestCase): mocked.return_value = retval def tearDown(self): - patch.stopall() + mock.patch.stopall() diff --git a/wakatime/offlinequeue.py b/wakatime/offlinequeue.py index 09efe41..8870e26 100644 --- a/wakatime/offlinequeue.py +++ b/wakatime/offlinequeue.py @@ -18,7 +18,7 @@ from time import sleep try: import sqlite3 HAS_SQL = True -except ImportError: +except ImportError: # pragma: nocover HAS_SQL = False from .compat import u @@ -117,12 +117,12 @@ class Queue(object): 'plugin': row[8], } loop = False - except sqlite3.Error: + except sqlite3.Error: # pragma: nocover log.debug(traceback.format_exc()) sleep(wait) tries -= 1 try: conn.close() - except sqlite3.Error: + except sqlite3.Error: # pragma: nocover log.debug(traceback.format_exc()) return heartbeat