tests for offline queue

This commit is contained in:
Alan Hamlett 2015-09-06 18:22:05 -07:00
parent db8f30ac2a
commit 859017daef
3 changed files with 79 additions and 7 deletions

View file

@ -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)

View file

@ -3,9 +3,9 @@
import logging import logging
try: try:
from mock import patch import mock
except ImportError: except ImportError:
from unittest.mock import patch import unittest.mock as mock
try: try:
# Python 2.6 # Python 2.6
import unittest2 as unittest import unittest2 as unittest
@ -25,7 +25,7 @@ class TestCase(unittest.TestCase):
for patch_this in self.patch_these: for patch_this in self.patch_these:
namespace = patch_this[0] if isinstance(patch_this, (list, set)) else patch_this namespace = patch_this[0] if isinstance(patch_this, (list, set)) else patch_this
patcher = patch(namespace) patcher = mock.patch(namespace)
mocked = patcher.start() mocked = patcher.start()
self.patched[namespace] = mocked self.patched[namespace] = mocked
@ -36,4 +36,4 @@ class TestCase(unittest.TestCase):
mocked.return_value = retval mocked.return_value = retval
def tearDown(self): def tearDown(self):
patch.stopall() mock.patch.stopall()

View file

@ -18,7 +18,7 @@ from time import sleep
try: try:
import sqlite3 import sqlite3
HAS_SQL = True HAS_SQL = True
except ImportError: except ImportError: # pragma: nocover
HAS_SQL = False HAS_SQL = False
from .compat import u from .compat import u
@ -117,12 +117,12 @@ class Queue(object):
'plugin': row[8], 'plugin': row[8],
} }
loop = False loop = False
except sqlite3.Error: except sqlite3.Error: # pragma: nocover
log.debug(traceback.format_exc()) log.debug(traceback.format_exc())
sleep(wait) sleep(wait)
tries -= 1 tries -= 1
try: try:
conn.close() conn.close()
except sqlite3.Error: except sqlite3.Error: # pragma: nocover
log.debug(traceback.format_exc()) log.debug(traceback.format_exc())
return heartbeat return heartbeat