remove temp sqlite file after testing. improve offlinequeue and session_cache tests.
This commit is contained in:
parent
dd5678a919
commit
db02b82d95
9 changed files with 121 additions and 55 deletions
|
@ -28,9 +28,11 @@ class DependenciesTestCase(utils.TestCase):
|
|||
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
||||
'wakatime.offlinequeue.Queue.push',
|
||||
['wakatime.offlinequeue.Queue.pop', None],
|
||||
['wakatime.offlinequeue.Queue.connect', None],
|
||||
'wakatime.session_cache.SessionCache.save',
|
||||
'wakatime.session_cache.SessionCache.delete',
|
||||
['wakatime.session_cache.SessionCache.get', requests.session],
|
||||
['wakatime.session_cache.SessionCache.connect', None],
|
||||
]
|
||||
|
||||
def test_token_parser(self):
|
||||
|
|
|
@ -15,9 +15,11 @@ class LanguagesTestCase(utils.TestCase):
|
|||
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
||||
'wakatime.offlinequeue.Queue.push',
|
||||
['wakatime.offlinequeue.Queue.pop', None],
|
||||
['wakatime.offlinequeue.Queue.connect', None],
|
||||
'wakatime.session_cache.SessionCache.save',
|
||||
'wakatime.session_cache.SessionCache.delete',
|
||||
['wakatime.session_cache.SessionCache.get', requests.session],
|
||||
['wakatime.session_cache.SessionCache.connect', None],
|
||||
]
|
||||
|
||||
def test_language_detected_for_header_file(self):
|
||||
|
|
|
@ -20,9 +20,11 @@ class LanguagesTestCase(utils.TestCase):
|
|||
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
||||
'wakatime.offlinequeue.Queue.push',
|
||||
['wakatime.offlinequeue.Queue.pop', None],
|
||||
['wakatime.offlinequeue.Queue.connect', None],
|
||||
'wakatime.session_cache.SessionCache.save',
|
||||
'wakatime.session_cache.SessionCache.delete',
|
||||
['wakatime.session_cache.SessionCache.get', requests.session],
|
||||
['wakatime.session_cache.SessionCache.connect', None],
|
||||
]
|
||||
|
||||
@log_capture()
|
||||
|
|
|
@ -26,9 +26,11 @@ class BaseTestCase(utils.TestCase):
|
|||
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
||||
'wakatime.offlinequeue.Queue.push',
|
||||
['wakatime.offlinequeue.Queue.pop', None],
|
||||
['wakatime.offlinequeue.Queue.connect', None],
|
||||
'wakatime.session_cache.SessionCache.save',
|
||||
'wakatime.session_cache.SessionCache.delete',
|
||||
['wakatime.session_cache.SessionCache.get', requests.session],
|
||||
['wakatime.session_cache.SessionCache.connect', None],
|
||||
]
|
||||
|
||||
def test_help_contents(self):
|
||||
|
|
|
@ -6,6 +6,8 @@ from wakatime.offlinequeue import Queue
|
|||
from wakatime.packages import requests
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
from wakatime.compat import u
|
||||
|
@ -19,9 +21,14 @@ class OfflineQueueTestCase(utils.TestCase):
|
|||
'wakatime.session_cache.SessionCache.save',
|
||||
'wakatime.session_cache.SessionCache.delete',
|
||||
['wakatime.session_cache.SessionCache.get', requests.session],
|
||||
['wakatime.session_cache.SessionCache.connect', None],
|
||||
]
|
||||
|
||||
def test_heartbeat_saved_from_error_response(self):
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||
mock_db_file.return_value = fh.name
|
||||
|
||||
response = Response()
|
||||
response.status_code = 500
|
||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||
|
@ -38,6 +45,10 @@ class OfflineQueueTestCase(utils.TestCase):
|
|||
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
||||
|
||||
def test_heartbeat_discarded_from_400_response(self):
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||
mock_db_file.return_value = fh.name
|
||||
|
||||
response = Response()
|
||||
response.status_code = 400
|
||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||
|
@ -54,6 +65,10 @@ class OfflineQueueTestCase(utils.TestCase):
|
|||
self.assertEquals(None, saved_heartbeat)
|
||||
|
||||
def test_offline_heartbeat_sent_after_success_response(self):
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||
mock_db_file.return_value = fh.name
|
||||
|
||||
response = Response()
|
||||
response.status_code = 500
|
||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||
|
@ -73,19 +88,53 @@ class OfflineQueueTestCase(utils.TestCase):
|
|||
self.assertEquals(None, saved_heartbeat)
|
||||
|
||||
def test_empty_project_can_be_saved(self):
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||
mock_db_file.return_value = fh.name
|
||||
|
||||
response = Response()
|
||||
response.status_code = 500
|
||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix='.txt') as fh:
|
||||
|
||||
now = u(int(time.time()))
|
||||
entity = fh.name
|
||||
entity = 'tests/samples/codefiles/emptyfile.txt'
|
||||
config = 'tests/samples/configs/sample.cfg'
|
||||
|
||||
args = ['--file', entity, '--config', config, '--time', now]
|
||||
execute(args)
|
||||
|
||||
queue = Queue()
|
||||
saved_heartbeat = queue.pop()
|
||||
self.assertEquals(sys.stdout.getvalue(), '')
|
||||
self.assertEquals(sys.stderr.getvalue(), '')
|
||||
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
||||
|
||||
def test_handles_connection_exception(self):
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||
mock_db_file.return_value = fh.name
|
||||
|
||||
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/codefiles/twolinefile.txt'
|
||||
config = 'tests/samples/configs/sample.cfg'
|
||||
|
||||
args = ['--file', entity, '--config', config, '--time', now]
|
||||
execute(args)
|
||||
|
||||
with utils.mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
||||
mock_connect.side_effect = sqlite3.Error('')
|
||||
|
||||
response.status_code = 201
|
||||
execute(args)
|
||||
|
||||
queue = Queue()
|
||||
saved_heartbeat = queue.pop()
|
||||
self.assertEquals(None, saved_heartbeat)
|
||||
|
||||
queue = Queue()
|
||||
saved_heartbeat = queue.pop()
|
||||
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
||||
|
|
|
@ -20,9 +20,11 @@ class LanguagesTestCase(utils.TestCase):
|
|||
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
||||
'wakatime.offlinequeue.Queue.push',
|
||||
['wakatime.offlinequeue.Queue.pop', None],
|
||||
['wakatime.offlinequeue.Queue.connect', None],
|
||||
'wakatime.session_cache.SessionCache.save',
|
||||
'wakatime.session_cache.SessionCache.delete',
|
||||
['wakatime.session_cache.SessionCache.get', requests.session],
|
||||
['wakatime.session_cache.SessionCache.connect', None],
|
||||
]
|
||||
|
||||
def test_project_base(self):
|
||||
|
|
|
@ -7,6 +7,12 @@ from . import utils
|
|||
|
||||
|
||||
class SessionCacheTestCase(utils.TestCase):
|
||||
patch_these = [
|
||||
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
||||
'wakatime.offlinequeue.Queue.push',
|
||||
['wakatime.offlinequeue.Queue.pop', None],
|
||||
['wakatime.offlinequeue.Queue.connect', None],
|
||||
]
|
||||
|
||||
def test_can_crud_session(self):
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
|
@ -22,7 +28,7 @@ class SessionCacheTestCase(utils.TestCase):
|
|||
session = cache.get()
|
||||
self.assertEquals(session.headers.get('x-test'), None)
|
||||
|
||||
def test_get_handles_connection_error(self):
|
||||
def test_handles_connection_exception(self):
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
cache = SessionCache()
|
||||
cache.DB_FILE = fh.name
|
||||
|
|
|
@ -31,8 +31,11 @@ class Queue(object):
|
|||
db_file = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
||||
table_name = 'heartbeat_1'
|
||||
|
||||
def get_db_file(self):
|
||||
return self.db_file
|
||||
|
||||
def connect(self):
|
||||
conn = sqlite3.connect(self.db_file)
|
||||
conn = sqlite3.connect(self.get_db_file())
|
||||
c = conn.cursor()
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS {0} (
|
||||
entity text,
|
||||
|
@ -47,9 +50,8 @@ class Queue(object):
|
|||
'''.format(self.table_name))
|
||||
return (conn, c)
|
||||
|
||||
|
||||
def push(self, data, stats, plugin, misc=None):
|
||||
if not HAS_SQL:
|
||||
if not HAS_SQL: # pragma: nocover
|
||||
return
|
||||
try:
|
||||
conn, c = self.connect()
|
||||
|
@ -70,9 +72,8 @@ class Queue(object):
|
|||
except sqlite3.Error:
|
||||
log.error(traceback.format_exc())
|
||||
|
||||
|
||||
def pop(self):
|
||||
if not HAS_SQL:
|
||||
if not HAS_SQL: # pragma: nocover
|
||||
return None
|
||||
tries = 3
|
||||
wait = 0.1
|
||||
|
|
|
@ -83,12 +83,12 @@ class SessionCache(object):
|
|||
row = c.fetchone()
|
||||
if row is not None:
|
||||
session = pickle.loads(row[0])
|
||||
except:
|
||||
except: # pragma: nocover
|
||||
log.error(traceback.format_exc())
|
||||
|
||||
try:
|
||||
conn.close()
|
||||
except:
|
||||
except: # pragma: nocover
|
||||
log.error(traceback.format_exc())
|
||||
|
||||
return session if session is not None else requests.session()
|
||||
|
|
Loading…
Reference in a new issue