remove temp sqlite file after testing. improve offlinequeue and session_cache tests.

This commit is contained in:
Alan Hamlett 2015-09-27 03:17:54 -07:00
parent dd5678a919
commit db02b82d95
9 changed files with 121 additions and 55 deletions

View file

@ -28,9 +28,11 @@ class DependenciesTestCase(utils.TestCase):
'wakatime.packages.requests.adapters.HTTPAdapter.send', 'wakatime.packages.requests.adapters.HTTPAdapter.send',
'wakatime.offlinequeue.Queue.push', 'wakatime.offlinequeue.Queue.push',
['wakatime.offlinequeue.Queue.pop', None], ['wakatime.offlinequeue.Queue.pop', None],
['wakatime.offlinequeue.Queue.connect', None],
'wakatime.session_cache.SessionCache.save', 'wakatime.session_cache.SessionCache.save',
'wakatime.session_cache.SessionCache.delete', 'wakatime.session_cache.SessionCache.delete',
['wakatime.session_cache.SessionCache.get', requests.session], ['wakatime.session_cache.SessionCache.get', requests.session],
['wakatime.session_cache.SessionCache.connect', None],
] ]
def test_token_parser(self): def test_token_parser(self):

View file

@ -15,9 +15,11 @@ class LanguagesTestCase(utils.TestCase):
'wakatime.packages.requests.adapters.HTTPAdapter.send', 'wakatime.packages.requests.adapters.HTTPAdapter.send',
'wakatime.offlinequeue.Queue.push', 'wakatime.offlinequeue.Queue.push',
['wakatime.offlinequeue.Queue.pop', None], ['wakatime.offlinequeue.Queue.pop', None],
['wakatime.offlinequeue.Queue.connect', None],
'wakatime.session_cache.SessionCache.save', 'wakatime.session_cache.SessionCache.save',
'wakatime.session_cache.SessionCache.delete', 'wakatime.session_cache.SessionCache.delete',
['wakatime.session_cache.SessionCache.get', requests.session], ['wakatime.session_cache.SessionCache.get', requests.session],
['wakatime.session_cache.SessionCache.connect', None],
] ]
def test_language_detected_for_header_file(self): def test_language_detected_for_header_file(self):

View file

@ -20,9 +20,11 @@ class LanguagesTestCase(utils.TestCase):
'wakatime.packages.requests.adapters.HTTPAdapter.send', 'wakatime.packages.requests.adapters.HTTPAdapter.send',
'wakatime.offlinequeue.Queue.push', 'wakatime.offlinequeue.Queue.push',
['wakatime.offlinequeue.Queue.pop', None], ['wakatime.offlinequeue.Queue.pop', None],
['wakatime.offlinequeue.Queue.connect', None],
'wakatime.session_cache.SessionCache.save', 'wakatime.session_cache.SessionCache.save',
'wakatime.session_cache.SessionCache.delete', 'wakatime.session_cache.SessionCache.delete',
['wakatime.session_cache.SessionCache.get', requests.session], ['wakatime.session_cache.SessionCache.get', requests.session],
['wakatime.session_cache.SessionCache.connect', None],
] ]
@log_capture() @log_capture()

View file

@ -26,9 +26,11 @@ class BaseTestCase(utils.TestCase):
'wakatime.packages.requests.adapters.HTTPAdapter.send', 'wakatime.packages.requests.adapters.HTTPAdapter.send',
'wakatime.offlinequeue.Queue.push', 'wakatime.offlinequeue.Queue.push',
['wakatime.offlinequeue.Queue.pop', None], ['wakatime.offlinequeue.Queue.pop', None],
['wakatime.offlinequeue.Queue.connect', None],
'wakatime.session_cache.SessionCache.save', 'wakatime.session_cache.SessionCache.save',
'wakatime.session_cache.SessionCache.delete', 'wakatime.session_cache.SessionCache.delete',
['wakatime.session_cache.SessionCache.get', requests.session], ['wakatime.session_cache.SessionCache.get', requests.session],
['wakatime.session_cache.SessionCache.connect', None],
] ]
def test_help_contents(self): def test_help_contents(self):

View file

@ -6,6 +6,8 @@ from wakatime.offlinequeue import Queue
from wakatime.packages import requests from wakatime.packages import requests
import os import os
import sqlite3
import sys
import tempfile import tempfile
import time import time
from wakatime.compat import u from wakatime.compat import u
@ -19,73 +21,120 @@ class OfflineQueueTestCase(utils.TestCase):
'wakatime.session_cache.SessionCache.save', 'wakatime.session_cache.SessionCache.save',
'wakatime.session_cache.SessionCache.delete', 'wakatime.session_cache.SessionCache.delete',
['wakatime.session_cache.SessionCache.get', requests.session], ['wakatime.session_cache.SessionCache.get', requests.session],
['wakatime.session_cache.SessionCache.connect', None],
] ]
def test_heartbeat_saved_from_error_response(self): def test_heartbeat_saved_from_error_response(self):
response = Response() with tempfile.NamedTemporaryFile() as fh:
response.status_code = 500 with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response mock_db_file.return_value = fh.name
now = u(int(time.time())) response = Response()
entity = 'tests/samples/codefiles/twolinefile.txt' response.status_code = 500
config = 'tests/samples/configs/sample.cfg' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
args = ['--file', entity, '--config', config, '--time', now] now = u(int(time.time()))
execute(args) entity = 'tests/samples/codefiles/twolinefile.txt'
config = 'tests/samples/configs/sample.cfg'
queue = Queue() args = ['--file', entity, '--config', config, '--time', now]
saved_heartbeat = queue.pop() execute(args)
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
queue = Queue()
saved_heartbeat = queue.pop()
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
def test_heartbeat_discarded_from_400_response(self): def test_heartbeat_discarded_from_400_response(self):
response = Response() with tempfile.NamedTemporaryFile() as fh:
response.status_code = 400 with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response mock_db_file.return_value = fh.name
now = u(int(time.time())) response = Response()
entity = 'tests/samples/codefiles/twolinefile.txt' response.status_code = 400
config = 'tests/samples/configs/sample.cfg' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
args = ['--file', entity, '--config', config, '--time', now] now = u(int(time.time()))
execute(args) entity = 'tests/samples/codefiles/twolinefile.txt'
config = 'tests/samples/configs/sample.cfg'
queue = Queue() args = ['--file', entity, '--config', config, '--time', now]
saved_heartbeat = queue.pop() execute(args)
self.assertEquals(None, saved_heartbeat)
queue = Queue()
saved_heartbeat = queue.pop()
self.assertEquals(None, saved_heartbeat)
def test_offline_heartbeat_sent_after_success_response(self): def test_offline_heartbeat_sent_after_success_response(self):
response = Response() with tempfile.NamedTemporaryFile() as fh:
response.status_code = 500 with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response mock_db_file.return_value = fh.name
now = u(int(time.time())) response = Response()
entity = 'tests/samples/codefiles/twolinefile.txt' response.status_code = 500
config = 'tests/samples/configs/sample.cfg' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
args = ['--file', entity, '--config', config, '--time', now] now = u(int(time.time()))
execute(args) entity = 'tests/samples/codefiles/twolinefile.txt'
config = 'tests/samples/configs/sample.cfg'
response.status_code = 201 args = ['--file', entity, '--config', config, '--time', now]
execute(args) execute(args)
queue = Queue() response.status_code = 201
saved_heartbeat = queue.pop() execute(args)
self.assertEquals(None, saved_heartbeat)
queue = Queue()
saved_heartbeat = queue.pop()
self.assertEquals(None, saved_heartbeat)
def test_empty_project_can_be_saved(self): def test_empty_project_can_be_saved(self):
response = Response() with tempfile.NamedTemporaryFile() as fh:
response.status_code = 500 with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response mock_db_file.return_value = fh.name
with tempfile.NamedTemporaryFile(suffix='.txt') as fh: response = Response()
response.status_code = 500
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time())) now = u(int(time.time()))
entity = fh.name entity = 'tests/samples/codefiles/emptyfile.txt'
config = 'tests/samples/configs/sample.cfg' config = 'tests/samples/configs/sample.cfg'
args = ['--file', entity, '--config', config, '--time', now] args = ['--file', entity, '--config', config, '--time', now]
execute(args) execute(args)
queue = Queue() queue = Queue()
saved_heartbeat = queue.pop() saved_heartbeat = queue.pop()
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity']) 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'])

View file

@ -20,9 +20,11 @@ class LanguagesTestCase(utils.TestCase):
'wakatime.packages.requests.adapters.HTTPAdapter.send', 'wakatime.packages.requests.adapters.HTTPAdapter.send',
'wakatime.offlinequeue.Queue.push', 'wakatime.offlinequeue.Queue.push',
['wakatime.offlinequeue.Queue.pop', None], ['wakatime.offlinequeue.Queue.pop', None],
['wakatime.offlinequeue.Queue.connect', None],
'wakatime.session_cache.SessionCache.save', 'wakatime.session_cache.SessionCache.save',
'wakatime.session_cache.SessionCache.delete', 'wakatime.session_cache.SessionCache.delete',
['wakatime.session_cache.SessionCache.get', requests.session], ['wakatime.session_cache.SessionCache.get', requests.session],
['wakatime.session_cache.SessionCache.connect', None],
] ]
def test_project_base(self): def test_project_base(self):

View file

@ -7,6 +7,12 @@ from . import utils
class SessionCacheTestCase(utils.TestCase): 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): def test_can_crud_session(self):
with tempfile.NamedTemporaryFile() as fh: with tempfile.NamedTemporaryFile() as fh:
@ -22,7 +28,7 @@ class SessionCacheTestCase(utils.TestCase):
session = cache.get() session = cache.get()
self.assertEquals(session.headers.get('x-test'), None) 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: with tempfile.NamedTemporaryFile() as fh:
cache = SessionCache() cache = SessionCache()
cache.DB_FILE = fh.name cache.DB_FILE = fh.name

View file

@ -31,8 +31,11 @@ class Queue(object):
db_file = os.path.join(os.path.expanduser('~'), '.wakatime.db') db_file = os.path.join(os.path.expanduser('~'), '.wakatime.db')
table_name = 'heartbeat_1' table_name = 'heartbeat_1'
def get_db_file(self):
return self.db_file
def connect(self): def connect(self):
conn = sqlite3.connect(self.db_file) conn = sqlite3.connect(self.get_db_file())
c = conn.cursor() c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS {0} ( c.execute('''CREATE TABLE IF NOT EXISTS {0} (
entity text, entity text,
@ -47,9 +50,8 @@ class Queue(object):
'''.format(self.table_name)) '''.format(self.table_name))
return (conn, c) return (conn, c)
def push(self, data, stats, plugin, misc=None): def push(self, data, stats, plugin, misc=None):
if not HAS_SQL: if not HAS_SQL: # pragma: nocover
return return
try: try:
conn, c = self.connect() conn, c = self.connect()
@ -70,9 +72,8 @@ class Queue(object):
except sqlite3.Error: except sqlite3.Error:
log.error(traceback.format_exc()) log.error(traceback.format_exc())
def pop(self): def pop(self):
if not HAS_SQL: if not HAS_SQL: # pragma: nocover
return None return None
tries = 3 tries = 3
wait = 0.1 wait = 0.1

View file

@ -83,12 +83,12 @@ class SessionCache(object):
row = c.fetchone() row = c.fetchone()
if row is not None: if row is not None:
session = pickle.loads(row[0]) session = pickle.loads(row[0])
except: except: # pragma: nocover
log.error(traceback.format_exc()) log.error(traceback.format_exc())
try: try:
conn.close() conn.close()
except: except: # pragma: nocover
log.error(traceback.format_exc()) log.error(traceback.format_exc())
return session if session is not None else requests.session() return session if session is not None else requests.session()