improve test coverage for parsing api bulk results

This commit is contained in:
Alan Hamlett 2017-11-22 18:54:47 -08:00
parent 9ffb677f9d
commit 8269196c88
2 changed files with 88 additions and 5 deletions

View file

@ -632,3 +632,87 @@ class OfflineQueueTestCase(utils.TestCase):
self.assertEquals(u(language), saved_heartbeat['language'])
self.assertEquals(u(project), saved_heartbeat['project'])
self.assertEquals(u(branch), saved_heartbeat['branch'])
@log_capture()
def test_heartbeat_saved_when_bulk_result_json_decode_error(self, logs):
logging.disable(logging.NOTSET)
with utils.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 = CustomResponse()
response.response_text = '[[{id":1}]]'
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/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
execute(args)
queue = Queue(None, None)
saved_heartbeat = queue.pop()
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
self.assertNothingPrinted()
expected = 'JSONDecodeError'
actual = self.getLogOutput(logs)
self.assertIn(expected, actual)
@log_capture()
def test_heartbeat_saved_from_result_type_error(self, logs):
logging.disable(logging.NOTSET)
with utils.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 = CustomResponse()
response.response_text = '[0]'
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/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
execute(args)
queue = Queue(None, None)
saved_heartbeat = queue.pop()
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
self.assertNothingPrinted()
expected = 'TypeError'
actual = self.getLogOutput(logs)
self.assertIn(expected, actual)
@log_capture()
def test_heartbeat_saved_from_result_index_error(self, logs):
logging.disable(logging.NOTSET)
with utils.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 = CustomResponse()
response.response_text = '[[{"id":1}]]'
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/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
execute(args)
queue = Queue(None, None)
saved_heartbeat = queue.pop()
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
self.assertNothingPrinted()
expected = 'IndexError'
actual = self.getLogOutput(logs)
self.assertIn(expected, actual)

View file

@ -30,7 +30,7 @@ log = logging.getLogger('WakaTime')
try:
from .packages import requests
except ImportError:
except ImportError: # pragma: nocover
log.traceback(logging.ERROR)
print(traceback.format_exc())
log.error('Please upgrade Python to the latest version.')
@ -167,11 +167,11 @@ def _process_server_results(heartbeats, code, content, results, args, configs):
try:
c = results[i][1]
except:
log.traceback(logging.WARNING)
c = 0
try:
text = json.dumps(results[i][0])
except:
if log.isEnabledFor(logging.DEBUG):
log.traceback(logging.WARNING)
text = ''
if not _success(c):
@ -212,7 +212,6 @@ def _get_results(response):
try:
results = response.json()
except:
if log.isEnabledFor(logging.DEBUG):
log.traceback(logging.WARNING)
return results