def test_query_commit(self, get): expected = {'files': []} get.return_value = utils.ResponseMock(200, expected) self.assertEqual(expected, self.github.query_commit('commitCCC')) get.assert_called_with( 'https://api.github.com/repos/ownerA/repoB/commits/commitCCC', auth=('authusernameC', 'authacesstokenD')) get.return_value = utils.ResponseMock(400) self.assertRaises(exceptions.HTTPError, self.github.query_commit, 'commitCCC')
def test_query_changed_files_in_commit(self, get): data = { 'files': [{ 'filename': 'scripts/google/covid_mobility/README.md', 'status': 'modified', "size": 39 }, { 'filename': 'scripts/google/covid_mobility/TestCovidMobility.py', 'status': 'removed', "size": 12 }, { 'filename': 'scripts/google/covid_mobility/covidmobility.py', 'status': 'added', "size": 4, }, { 'filename': 'scripts/google/covid_mobility/input/data.csv', 'status': 'removed', }] } expected = [ ('scripts/google/covid_mobility/README.md', 'modified'), ('scripts/google/covid_mobility/TestCovidMobility.py', 'removed'), ('scripts/google/covid_mobility/covidmobility.py', 'added'), ('scripts/google/covid_mobility/input/data.csv', 'removed') ] get.return_value = utils.ResponseMock(200, data) self.assertEqual( expected, self.github._query_changed_files_in_commit('commitCCC')) get.assert_called_with( 'https://api.github.com/repos/ownerA/repoB/commits/commitCCC', auth=('authusernameC', 'authacesstokenD'))
def test_download_repo(self, get): tar_path = 'test/data/treasury_constant_maturity_rates.tar.gz' with open(tar_path, 'rb') as tar: headers = {'Content-Disposition': 'attachment; filename=abc'} get.return_value = utils.ResponseMock(200, raw=tar, headers=headers) with tempfile.TemporaryDirectory() as dir_path: downloaded = self.github.download_repo(dir_path, 'commit-sha') self.assertEqual( f'{dir_path}/treasury_constant_maturity_rates', downloaded) file = os.path.join(downloaded, 'treasury_constant_maturity_rates.csv') assert test.utils.compare_lines( 'test/data/treasury_constant_maturity_rates.csv', file, integration_test.NUM_LINES_TO_CHECK) file = os.path.join(downloaded, 'treasury_constant_maturity_rates.mcf') assert test.utils.compare_lines( 'test/data/treasury_constant_maturity_rates.mcf', file, integration_test.NUM_LINES_TO_CHECK) file = os.path.join(downloaded, 'treasury_constant_maturity_rates.tmcf') assert test.utils.compare_lines( 'test/data/treasury_constant_maturity_rates.tmcf', file, integration_test.NUM_LINES_TO_CHECK)
def test_update_attempt(self, patch): attempt = {'import_name': 'treasury'} patch.return_value = utils.ResponseMock(200, {}) self.assertEqual({}, self.dashboard.update_attempt(attempt, 'id')) patch.assert_called_with( 'https://datcom-data.uc.r.appspot.com/import_attempts/id', json=attempt)
def test_dir_exists_other_error(self, get): get.return_value = utils.ResponseMock(500) self.assertRaises(exceptions.HTTPError, self.github._dir_exists, 'c', 'a/b/c') get.assert_called_with( 'https://api.github.com/repos/ownerA/repoB/contents/a/b/c?ref=c', auth=('authusernameC', 'authacesstokenD'))
def test_download_repo_http_error(self, get): tar_path = 'test/data/treasury_constant_maturity_rates.tar.gz' with open(tar_path, 'rb') as tar: get.return_value = utils.ResponseMock(400, raw=tar) with tempfile.TemporaryDirectory() as dir_path: self.assertRaises(exceptions.HTTPError, self.github.download_repo, dir_path, 'commit-sha')
def test_download_repo_empty(self, get): with open('test/data/empty.tar.gz', 'rb') as tar: headers = {'Content-Disposition': 'attachment; filename=abc'} get.return_value = utils.ResponseMock(200, raw=tar, headers=headers) with tempfile.TemporaryDirectory() as dir_path: self.assertRaises(FileNotFoundError, self.github.download_repo, dir_path, 'commit-sha')
def test_log_helper_time(self, post): """Tests that time_logged is generated if not supplied.""" args = {'message': 'message', 'level': 'level', 'run_id': 'run'} expected = { 'message': 'message', 'level': 'level', 'run_id': 'run', 'time_logged': '2020-07-15T12:07:17.365264+00:00', 'log_id': 'random-id' } post.return_value = utils.ResponseMock(200, expected) self.assertEqual(expected, self.dashboard._log_helper(**args))
def test_download_repo_timeout(self, get): tar_path = 'test/data/treasury_constant_maturity_rates.tar.gz' with open(tar_path, 'rb') as tar: headers = {'Content-Disposition': 'attachment; filename=abc'} get.return_value = utils.ResponseMock(200, raw=tar, headers=headers) with tempfile.TemporaryDirectory() as dir_path: self.assertRaises(exceptions.Timeout, self.github.download_repo, dir_path, 'commit-sha', 0.000001)
def test_log_helper(self, post): args = { 'message': 'message', 'level': 'level', 'run_id': 'run', 'attempt_id': 'attempt', 'time_logged': 'time' } expected = { 'message': 'message', 'level': 'level', 'run_id': 'run', 'attempt_id': 'attempt', 'time_logged': 'time', 'log_id': 'random-id' } post.return_value = utils.ResponseMock(200, expected) self.assertEqual(expected, self.dashboard._log_helper(**args)) post.assert_called_once_with( 'https://datcom-data.uc.r.appspot.com/logs', json=args)
def test_query_files_in_dir(self, get): data = [{ "name": "go.sum", "type": "file", 'foo': 'bar' }, { "name": "import-automation", "type": "dir", 'bar': 'foo' }, { "name": "requirements.txt", "type": "file", }, { "name": "schema", "type": "dir" }] expected = ['go.sum', 'requirements.txt'] get.return_value = utils.ResponseMock(200, data) self.assertEqual(expected, self.github._query_files_in_dir('committt', 'd')) get.assert_called_with( 'https://api.github.com/repos/ownerA/repoB/contents/d?ref=committt', auth=('authusernameC', 'authacesstokenD'))
def test_log_helper_http(self, post): """Tests that an exception is thrown is the HTTP request fails.""" post.return_value = utils.ResponseMock(400) self.assertRaises(exceptions.HTTPError, self.dashboard._log_helper, 'message', 'level', 'attempt')
class DashboardAPITest(unittest.TestCase): def setUp(self): self.dashboard = dashboard_api.DashboardAPI('client-id') @mock.patch('app.service.iap_request.IAPRequest.post') def test_log_helper(self, post): args = { 'message': 'message', 'level': 'level', 'run_id': 'run', 'attempt_id': 'attempt', 'time_logged': 'time' } expected = { 'message': 'message', 'level': 'level', 'run_id': 'run', 'attempt_id': 'attempt', 'time_logged': 'time', 'log_id': 'random-id' } post.return_value = utils.ResponseMock(200, expected) self.assertEqual(expected, self.dashboard._log_helper(**args)) post.assert_called_once_with( 'https://datcom-data.uc.r.appspot.com/logs', json=args) @mock.patch('app.service.iap_request.IAPRequest.post') def test_log_helper_time(self, post): """Tests that time_logged is generated if not supplied.""" args = {'message': 'message', 'level': 'level', 'run_id': 'run'} expected = { 'message': 'message', 'level': 'level', 'run_id': 'run', 'time_logged': '2020-07-15T12:07:17.365264+00:00', 'log_id': 'random-id' } post.return_value = utils.ResponseMock(200, expected) self.assertEqual(expected, self.dashboard._log_helper(**args)) def test_log_helper_id(self): """Tests that at least one of run_id and attempt_id need to be specified.""" self.assertRaises(ValueError, self.dashboard._log_helper, 'message', 'level') @mock.patch('app.service.iap_request.IAPRequest.post') def test_log_helper_http(self, post): """Tests that an exception is thrown is the HTTP request fails.""" post.return_value = utils.ResponseMock(400) self.assertRaises(exceptions.HTTPError, self.dashboard._log_helper, 'message', 'level', 'attempt') @mock.patch('app.service.iap_request.IAPRequest.patch') def test_update_attempt(self, patch): attempt = {'import_name': 'treasury'} patch.return_value = utils.ResponseMock(200, {}) self.assertEqual({}, self.dashboard.update_attempt(attempt, 'id')) patch.assert_called_with( 'https://datcom-data.uc.r.appspot.com/import_attempts/id', json=attempt) @mock.patch('app.service.iap_request.IAPRequest.patch') def test_update_run(self, patch): run = {'commit_sha': 'commit-sha'} patch.return_value = utils.ResponseMock(200, {}) self.assertEqual({}, self.dashboard.update_run(run, 'id')) patch.assert_called_with( 'https://datcom-data.uc.r.appspot.com/system_runs/id', json=run) @mock.patch('app.service.iap_request.IAPRequest.post', lambda self, url, json=None: utils.ResponseMock(200, json)) def test_levels(self): """Tests that the convenient logging functions set the right logging levels. """ args = {'message': 'message', 'time_logged': 'time', 'run_id': 'run'} funcs = [(self.dashboard.critical, 'critical'), (self.dashboard.error, 'error'), (self.dashboard.warning, 'warning'), (self.dashboard.info, 'info'), (self.dashboard.debug, 'debug')] for func, level in funcs: self.assertEqual(level, func(**args)['level'])
def test_dir_exists_not(self, get): get.return_value = utils.ResponseMock(404) self.assertFalse(self.github._dir_exists('c', 'a/b/c')) get.assert_called_with( 'https://api.github.com/repos/ownerA/repoB/contents/a/b/c?ref=c', auth=('authusernameC', 'authacesstokenD'))
def test_query_files_in_dir_raise(self, get): get.return_value = utils.ResponseMock(400) self.assertRaises(exceptions.HTTPError, self.github._query_files_in_dir, 'committt', 'dirrr')
def test_update_run(self, patch): run = {'commit_sha': 'commit-sha'} patch.return_value = utils.ResponseMock(200, {}) self.assertEqual({}, self.dashboard.update_run(run, 'id')) patch.assert_called_with( 'https://datcom-data.uc.r.appspot.com/system_runs/id', json=run)
def test_query_changed_files_in_commit_raise(self, get): get.return_value = utils.ResponseMock(400) self.assertRaises(exceptions.HTTPError, self.github.query_commit, 'commitCCC')