def test_get_handles_connection_exception(self): 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 = 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/good_config.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'])
def test_get_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'])
def test_heartbeat_saved_from_result_index_error(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name response = CustomResponse() response.response_text = '{"responses": [[{"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)
def test_push_handles_exception_on_close(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with 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/good_config.cfg' mock_conn = mock.MagicMock() mock_conn.close.side_effect = sqlite3.Error('') mock_c = mock.MagicMock() mock_c.fetchone.return_value = None with mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect: mock_connect.return_value = (mock_conn, mock_c) args = ['--file', entity, '--config', config, '--time', now] execute(args) response.status_code = 201 execute(args) queue = Queue(None, None) saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat) self.assertNothingPrinted()
def test_heartbeat_saved_when_requests_raises_exception(self, logs): logging.disable(logging.NOTSET) 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 exception_msg = u("Oops, requests raised an exception. You're move.") self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].side_effect = AttributeError(exception_msg) 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() saved_heartbeat = queue.pop() self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity']) self.assertEquals(sys.stdout.getvalue(), '') self.assertEquals(sys.stderr.getvalue(), '') output = [u(' ').join(x) for x in logs.actual()] self.assertIn(exception_msg, output[0]) self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() self.patched['wakatime.session_cache.SessionCache.delete'].assert_has_calls([call(), call()]) self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
def test_offline_heartbeat_sent_after_success_response(self): with NamedTemporaryFile() as fh: with 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/good_config.cfg' args = ['--file', entity, '--config', config, '--time', now] execute(args) response = CustomResponse() response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}' self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response execute(args) queue = Queue(None, None) saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat)
def test_heartbeat_saved_when_requests_raises_exception(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name exception_msg = u("Oops, requests raised an exception. This is a test.") self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].side_effect = AttributeError(exception_msg) 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() output = [u(' ').join(x) for x in logs.actual()] self.assertIn(exception_msg, output[0]) 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()
def test_heartbeat_saved_from_result_index_error(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file: mock_db_file.return_value = fh.name response = CustomResponse() response.response_text = '{"responses": [[{"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.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity']) self.assertNothingPrinted() expected = 'IndexError' actual = self.getLogOutput(logs) self.assertIn(expected, actual)
def test_nonascii_heartbeat_saved(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with 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 TemporaryDirectory() as tempdir: filename = list(filter(lambda x: x.endswith('.txt'), os.listdir(u('tests/samples/codefiles/unicode'))))[0] entity = os.path.join('tests/samples/codefiles/unicode', filename) shutil.copy(entity, os.path.join(tempdir, filename)) entity = os.path.realpath(os.path.join(tempdir, filename)) now = u(int(time.time())) config = 'tests/samples/configs/good_config.cfg' key = str(uuid.uuid4()) language = 'lang汉语' if self.isPy35OrNewer else 'lang\xe6\xb1\x89\xe8\xaf\xad' project = 'proj汉语' if self.isPy35OrNewer else 'proj\xe6\xb1\x89\xe8\xaf\xad' branch = 'branch汉语' if self.isPy35OrNewer else 'branch\xe6\xb1\x89\xe8\xaf\xad' heartbeat = { 'language': u(language), 'lines': 0, 'entity': os.path.realpath(entity), 'project': u(project), 'branch': u(branch), 'time': float(now), 'type': 'file', 'is_write': False, 'user_agent': ANY, 'dependencies': [], } args = ['--file', entity, '--key', key, '--config', config, '--time', now] with mock.patch('wakatime.stats.standardize_language') as mock_language: mock_language.return_value = language with mock.patch('wakatime.heartbeat.get_project_info') as mock_project: mock_project.return_value = (project, branch) execute(args) self.assertNothingPrinted() self.assertNothingLogged(logs) self.assertHeartbeatSent(heartbeat) queue = Queue(None, None) saved_heartbeat = queue.pop() self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity']) self.assertEquals(u(language), saved_heartbeat['language']) self.assertEquals(u(project), saved_heartbeat['project']) self.assertEquals(u(branch), saved_heartbeat['branch'])
def test_nonascii_heartbeat_saved(self, logs): logging.disable(logging.NOTSET) with NamedTemporaryFile() as fh: with 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 TemporaryDirectory() as tempdir: filename = list(filter(lambda x: x.endswith('.txt'), os.listdir(u('tests/samples/codefiles/unicode'))))[0] entity = os.path.join('tests/samples/codefiles/unicode', filename) shutil.copy(entity, os.path.join(tempdir, filename)) entity = os.path.realpath(os.path.join(tempdir, filename)) now = u(int(time.time())) config = 'tests/samples/configs/good_config.cfg' key = str(uuid.uuid4()) language = 'lang汉语' if self.isPy35OrNewer else 'lang\xe6\xb1\x89\xe8\xaf\xad' project = 'proj汉语' if self.isPy35OrNewer else 'proj\xe6\xb1\x89\xe8\xaf\xad' branch = 'branch汉语' if self.isPy35OrNewer else 'branch\xe6\xb1\x89\xe8\xaf\xad' heartbeat = { 'language': u(language), 'lines': 0, 'entity': os.path.realpath(entity), 'project': u(project), 'branch': u(branch), 'time': float(now), 'type': 'file', 'is_write': False, 'user_agent': ANY, 'dependencies': [], } args = ['--file', entity, '--key', key, '--config', config, '--time', now] with mock.patch('wakatime.stats.standardize_language') as mock_language: mock_language.return_value = (language, None) with mock.patch('wakatime.heartbeat.get_project_info') as mock_project: mock_project.return_value = (project, branch) execute(args) self.assertNothingPrinted() self.assertNothingLogged(logs) self.assertHeartbeatSent(heartbeat) queue = Queue(None, None) saved_heartbeat = queue.pop() self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity']) self.assertEquals(u(language), saved_heartbeat['language']) self.assertEquals(u(project), saved_heartbeat['project']) self.assertEquals(u(branch), saved_heartbeat['branch'])
def test_500_error_when_sending_offline_heartbeats(self): 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 = Response() response.status_code = 500 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response config = 'tests/samples/configs/good_config.cfg' now1 = u(int(time.time())) entity1 = 'tests/samples/codefiles/emptyfile.txt' project1 = 'proj1' args = ['--file', entity1, '--config', config, '--time', now1, '--project', project1] execute(args) now2 = u(int(time.time())) entity2 = 'tests/samples/codefiles/twolinefile.txt' project2 = 'proj2' args = ['--file', entity2, '--config', config, '--time', now2, '--project', project2] execute(args) # send heartbeats from offline queue after 201 response now3 = u(int(time.time())) entity3 = 'tests/samples/codefiles/python.py' project3 = 'proj3' args = ['--file', entity3, '--config', config, '--time', now3, '--project', project3] class CustomResponse(Response): count = 0 @property def status_code(self): if self.count > 2: return 500 self.count += 1 return 201 @status_code.setter def status_code(self, value): pass response = CustomResponse() self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response retval = execute(args) self.assertEquals(retval, SUCCESS) # offline queue should be empty queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(sys.stdout.getvalue(), '') self.assertEquals(sys.stderr.getvalue(), '') self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity'])
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())) entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/configs/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] execute(args) queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat)
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())) entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/configs/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] execute(args) queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat)
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 with tempfile.NamedTemporaryFile(suffix='.txt') as fh: now = u(int(time.time())) entity = fh.name config = 'tests/samples/configs/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] execute(args) queue = Queue() saved_heartbeat = queue.pop() 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 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) queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat)
def test_heartbeat_saved_from_error_response(self): with NamedTemporaryFile() as fh: with 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/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'])
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 with tempfile.NamedTemporaryFile(suffix='.txt') as fh: now = u(int(time.time())) entity = fh.name config = 'tests/samples/configs/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] execute(args) queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
def test_heartbeat_saved_from_error_response(self): with NamedTemporaryFile() as fh: with 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/good_config.cfg' args = ['--file', entity, '--config', config, '--time', now] execute(args) queue = Queue(None, None) saved_heartbeat = queue.pop() self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
def test_heartbeat_saved_when_requests_raises_exception(self, logs): logging.disable(logging.NOTSET) 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 self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].side_effect = AttributeError( "'Retry' object has no attribute 'history'") 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() saved_heartbeat = queue.pop() self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity']) self.assertEquals(sys.stdout.getvalue(), '') self.assertEquals(sys.stderr.getvalue(), '') output = [u(' ').join(x) for x in logs.actual()] expected = u( "AttributeError: \\'Retry\\' object has no attribute \\'history\\'" ) self.assertIn(expected, output[0]) self.patched[ 'wakatime.session_cache.SessionCache.get'].assert_called_once_with( ) self.patched[ 'wakatime.session_cache.SessionCache.delete'].assert_has_calls( [call(), call()]) self.patched[ 'wakatime.session_cache.SessionCache.save'].assert_not_called( )
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 now = u(int(time.time())) 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_empty_project_can_be_saved(self): 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 = 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/emptyfile.txt' config = 'tests/samples/configs/good_config.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_offline_heartbeat_sent_after_success_response(self): with NamedTemporaryFile() as fh: with 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/good_config.cfg' args = ['--file', entity, '--config', config, '--time', now] execute(args) response = CustomResponse() response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}' self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response execute(args) queue = Queue(None, None) saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat)
def test_all_offline_heartbeats_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 config = 'tests/samples/configs/good_config.cfg' now1 = u(int(time.time())) entity1 = 'tests/samples/codefiles/emptyfile.txt' project1 = 'proj1' args = ['--file', entity1, '--config', config, '--time', now1, '--project', project1] execute(args) now2 = u(int(time.time())) entity2 = 'tests/samples/codefiles/twolinefile.txt' project2 = 'proj2' args = ['--file', entity2, '--config', config, '--time', now2, '--project', project2] execute(args) # send heartbeats from offline queue after 201 response now3 = u(int(time.time())) entity3 = 'tests/samples/codefiles/python.py' project3 = 'proj3' args = ['--file', entity3, '--config', config, '--time', now3, '--project', project3] response.status_code = 201 execute(args) # offline queue should be empty queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat) calls = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args_list body = calls[0][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity1)) self.assertEquals(data.get('project'), project1) self.assertEquals(u(int(data.get('time'))), now1) body = calls[1][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity2)) self.assertEquals(data.get('project'), project2) self.assertEquals(u(int(data.get('time'))), now2) body = calls[2][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity3)) self.assertEquals(data.get('project'), project3) self.assertEquals(u(int(data.get('time'))), now3) body = calls[3][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity1)) self.assertEquals(data.get('project'), project1) self.assertEquals(u(int(data.get('time'))), now1) body = calls[4][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity2)) self.assertEquals(data.get('project'), project2) self.assertEquals(u(int(data.get('time'))), now2)
def test_all_offline_heartbeats_sent_after_success_response(self): 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 = Response() response.status_code = 500 self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response config = 'tests/samples/configs/good_config.cfg' now1 = u(int(time.time())) entity1 = 'tests/samples/codefiles/emptyfile.txt' project1 = 'proj1' args = [ '--file', entity1, '--config', config, '--time', now1, '--project', project1 ] execute(args) now2 = u(int(time.time())) entity2 = 'tests/samples/codefiles/twolinefile.txt' project2 = 'proj2' args = [ '--file', entity2, '--config', config, '--time', now2, '--project', project2 ] execute(args) # send heartbeats from offline queue after 201 response now3 = u(int(time.time())) entity3 = 'tests/samples/codefiles/python.py' project3 = 'proj3' args = [ '--file', entity3, '--config', config, '--time', now3, '--project', project3 ] response.status_code = 201 execute(args) # offline queue should be empty queue = Queue() saved_heartbeat = queue.pop() self.assertEquals(None, saved_heartbeat) calls = self.patched[ 'wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args_list body = calls[0][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity1)) self.assertEquals(data.get('project'), project1) self.assertEquals(u(int(data.get('time'))), now1) body = calls[1][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity2)) self.assertEquals(data.get('project'), project2) self.assertEquals(u(int(data.get('time'))), now2) body = calls[2][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity3)) self.assertEquals(data.get('project'), project3) self.assertEquals(u(int(data.get('time'))), now3) body = calls[3][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity1)) self.assertEquals(data.get('project'), project1) self.assertEquals(u(int(data.get('time'))), now1) body = calls[4][0][0].body data = json.loads(body) self.assertEquals(data.get('entity'), os.path.abspath(entity2)) self.assertEquals(data.get('project'), project2) self.assertEquals(u(int(data.get('time'))), now2)