tests for session caching
This commit is contained in:
parent
0a56ffa8eb
commit
dfb8ad4502
3 changed files with 100 additions and 3 deletions
|
@ -418,3 +418,77 @@ class BaseTestCase(utils.TestCase):
|
||||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
||||||
|
|
||||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': 'localhost:1234'}, stream=False, timeout=30, verify=True)
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': 'localhost:1234'}, stream=False, timeout=30, verify=True)
|
||||||
|
|
||||||
|
def test_domain_heartbeat(self):
|
||||||
|
response = Response()
|
||||||
|
response.status_code = 0
|
||||||
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
|
entity = 'google.com'
|
||||||
|
config = 'tests/samples/configs/sample.cfg'
|
||||||
|
now = u(int(time.time()))
|
||||||
|
|
||||||
|
args = ['--entity', entity, '--entitytype', 'domain', '--config', config, '--time', now]
|
||||||
|
retval = execute(args)
|
||||||
|
|
||||||
|
self.assertEquals(retval, 102)
|
||||||
|
self.assertEquals(sys.stdout.getvalue(), '')
|
||||||
|
self.assertEquals(sys.stderr.getvalue(), '')
|
||||||
|
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
||||||
|
|
||||||
|
heartbeat = {
|
||||||
|
'entity': u(entity),
|
||||||
|
'time': float(now),
|
||||||
|
'type': 'domain',
|
||||||
|
}
|
||||||
|
stats = {
|
||||||
|
u('cursorpos'): None,
|
||||||
|
u('dependencies'): [],
|
||||||
|
u('language'): None,
|
||||||
|
u('lineno'): None,
|
||||||
|
u('lines'): None,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
||||||
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||||
|
|
||||||
|
def test_app_heartbeat(self):
|
||||||
|
response = Response()
|
||||||
|
response.status_code = 0
|
||||||
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
|
entity = 'Firefox'
|
||||||
|
config = 'tests/samples/configs/sample.cfg'
|
||||||
|
now = u(int(time.time()))
|
||||||
|
|
||||||
|
args = ['--entity', entity, '--entitytype', 'app', '--config', config, '--time', now]
|
||||||
|
retval = execute(args)
|
||||||
|
|
||||||
|
self.assertEquals(retval, 102)
|
||||||
|
self.assertEquals(sys.stdout.getvalue(), '')
|
||||||
|
self.assertEquals(sys.stderr.getvalue(), '')
|
||||||
|
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
||||||
|
|
||||||
|
heartbeat = {
|
||||||
|
'entity': u(entity),
|
||||||
|
'time': float(now),
|
||||||
|
'type': 'app',
|
||||||
|
}
|
||||||
|
stats = {
|
||||||
|
u('cursorpos'): None,
|
||||||
|
u('dependencies'): [],
|
||||||
|
u('language'): None,
|
||||||
|
u('lineno'): None,
|
||||||
|
u('lines'): None,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
||||||
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||||
|
|
23
tests/test_session_cache.py
Normal file
23
tests/test_session_cache.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
from wakatime.session_cache import SessionCache
|
||||||
|
from . import utils
|
||||||
|
|
||||||
|
|
||||||
|
class SessionCacheTestCase(utils.TestCase):
|
||||||
|
|
||||||
|
def test_can_connect(self):
|
||||||
|
|
||||||
|
db_file = None
|
||||||
|
with tempfile.NamedTemporaryFile() as fh:
|
||||||
|
db_file = fh.name
|
||||||
|
|
||||||
|
cache = SessionCache()
|
||||||
|
cache.DB_FILE = db_file
|
||||||
|
session = cache.get()
|
||||||
|
session.headers.update({'x-test': 'abc'})
|
||||||
|
cache.save(session)
|
||||||
|
cached_session = cache.get()
|
||||||
|
self.assertEquals(cached_session.headers.get('x-test'), 'abc')
|
|
@ -16,9 +16,9 @@ import sys
|
||||||
from .compat import u, open
|
from .compat import u, open
|
||||||
from .dependencies import DependencyParser
|
from .dependencies import DependencyParser
|
||||||
|
|
||||||
if sys.version_info[0] == 2:
|
if sys.version_info[0] == 2: # pragma: nocover
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
|
||||||
else:
|
else: # pragma: nocover
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
|
||||||
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
|
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
|
||||||
from pygments.modeline import get_filetype_from_buffer
|
from pygments.modeline import get_filetype_from_buffer
|
||||||
|
@ -75,7 +75,7 @@ def guess_lexer_using_filename(file_name, text):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
lexer = guess_lexer_for_filename(file_name, text)
|
lexer = guess_lexer_for_filename(file_name, text)
|
||||||
except:
|
except: # pragma: nocover
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if lexer is not None:
|
if lexer is not None:
|
||||||
|
|
Loading…
Reference in a new issue