2015-09-07 01:22:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
from wakatime.main import execute
|
|
|
|
from wakatime.offlinequeue import Queue
|
|
|
|
from wakatime.packages import requests
|
|
|
|
|
|
|
|
import os
|
2015-09-07 01:47:17 +00:00
|
|
|
import tempfile
|
2015-09-07 01:22:05 +00:00
|
|
|
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()))
|
2015-09-07 03:06:36 +00:00
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-08 22:48:28 +00:00
|
|
|
config = 'tests/samples/configs/sample.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-07 01:47:17 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
2015-09-07 01:22:05 +00:00
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
2015-09-07 01:47:17 +00:00
|
|
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
2015-09-07 01:22:05 +00:00
|
|
|
|
|
|
|
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()))
|
2015-09-07 03:06:36 +00:00
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-08 22:48:28 +00:00
|
|
|
config = 'tests/samples/configs/sample.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-07 01:47:17 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
2015-09-07 01:22:05 +00:00
|
|
|
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()))
|
2015-09-07 03:06:36 +00:00
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-08 22:48:28 +00:00
|
|
|
config = 'tests/samples/configs/sample.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-07 01:47:17 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
2015-09-07 01:22:05 +00:00
|
|
|
execute(args)
|
|
|
|
|
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
|
|
|
def test_empty_project_can_be_saved(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2015-09-08 22:56:06 +00:00
|
|
|
with tempfile.NamedTemporaryFile(suffix='.txt') as fh:
|
2015-09-07 01:47:17 +00:00
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = fh.name
|
2015-09-08 22:48:28 +00:00
|
|
|
config = 'tests/samples/configs/sample.cfg'
|
2015-09-07 01:47:17 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue()
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|