From 3e95d48678a9ac0c1da06a0f3405cb574ce3144e Mon Sep 17 00:00:00 2001 From: Martin Weise <martin.weise@tuwien.ac.at> Date: Mon, 17 Feb 2025 14:49:24 +0100 Subject: [PATCH] Improved coverage Signed-off-by: Martin Weise <martin.weise@tuwien.ac.at> --- .gitlab-ci.yml | 6 +- .../{test => tests}/conftest.py | 0 .../{test => tests}/s3_config.json | 0 .../{test => tests}/test_determine_dt.py | 0 .../{test => tests}/test_determine_pk.py | 0 .../{test => tests}/test_s3_client.py | 0 .../init/{test => tests}/conftest.py | 0 .../{test => tests}/test_integration_app.py | 0 .../init/{test => tests}/test_unit_app.py | 0 lib/python/.coveragerc | 3 - lib/python/dbrepo/api/dto.py | 2 + lib/python/tests/test_unit_rest_client.py | 121 +++++++++++++----- lib/python/tests/test_unit_view.py | 22 +--- 13 files changed, 99 insertions(+), 55 deletions(-) rename dbrepo-analyse-service/{test => tests}/conftest.py (100%) rename dbrepo-analyse-service/{test => tests}/s3_config.json (100%) rename dbrepo-analyse-service/{test => tests}/test_determine_dt.py (100%) rename dbrepo-analyse-service/{test => tests}/test_determine_pk.py (100%) rename dbrepo-analyse-service/{test => tests}/test_s3_client.py (100%) rename dbrepo-auth-service/init/{test => tests}/conftest.py (100%) rename dbrepo-auth-service/init/{test => tests}/test_integration_app.py (100%) rename dbrepo-auth-service/init/{test => tests}/test_unit_app.py (100%) delete mode 100644 lib/python/.coveragerc diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5a47aa19e5..061afb1a9a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -302,7 +302,7 @@ test-analyse-service: script: - "pip install pipenv" - "pipenv install gunicorn && pipenv install --dev --system --deploy" - - cd ./dbrepo-analyse-service/ && coverage run -m pytest test/test_determine_dt.py test/test_determine_pk.py test/test_s3_client.py --junitxml=report.xml && coverage html --omit="test/*" && coverage report --omit="test/*" > ./coverage.txt + - cd ./dbrepo-analyse-service/ && coverage run -m pytest test/test_determine_dt.py test/test_determine_pk.py test/test_s3_client.py --junitxml=report.xml && coverage html --omit="tests/*" && coverage report --omit="tests/*" > ./coverage.txt - "cat ./coverage.txt | grep -o 'TOTAL[^%]*%'" artifacts: when: always @@ -324,7 +324,7 @@ test-auth-service-init: - "pip install pipenv" - "pipenv install gunicorn && pipenv install --dev --system --deploy" script: - - cd ./dbrepo-auth-service/init/ && coverage run -m pytest test/test_unit_app.py --junitxml=report.xml && coverage html --omit="test/*" && coverage report --omit="test/*" > ./coverage.txt + - cd ./dbrepo-auth-service/init/ && coverage run -m pytest test/test_unit_app.py --junitxml=report.xml && coverage html --omit="tests/*" && coverage report --omit="tests/*" > ./coverage.txt - "cat ./coverage.txt | grep -o 'TOTAL[^%]*%'" artifacts: when: always @@ -403,7 +403,7 @@ test-lib: script: - "pip install pipenv" - "pipenv install gunicorn && pipenv install --dev --system --deploy" - - cd ./lib/python/ && coverage run -m pytest tests/test_unit_analyse.py tests/test_unit_container.py tests/test_unit_database.py tests/test_unit_identifier.py tests/test_unit_license.py tests/test_unit_query.py tests/test_unit_rest_client.py tests/test_unit_table.py tests/test_unit_user.py tests/test_unit_view.py --junitxml=report.xml && coverage html --omit="test/*" && coverage report --omit="test/*" > ./coverage.txt + - cd ./lib/python/ && coverage run -m pytest tests/test_unit_analyse.py tests/test_unit_container.py tests/test_unit_database.py tests/test_unit_identifier.py tests/test_unit_license.py tests/test_unit_query.py tests/test_unit_rest_client.py tests/test_unit_table.py tests/test_unit_user.py tests/test_unit_view.py tests/test_unit_rest_client.py --junitxml=report.xml && coverage html --omit="tests/*" && coverage report --omit="tests/*" > ./coverage.txt - "cat ./coverage.txt | grep -o 'TOTAL[^%]*%'" artifacts: when: always diff --git a/dbrepo-analyse-service/test/conftest.py b/dbrepo-analyse-service/tests/conftest.py similarity index 100% rename from dbrepo-analyse-service/test/conftest.py rename to dbrepo-analyse-service/tests/conftest.py diff --git a/dbrepo-analyse-service/test/s3_config.json b/dbrepo-analyse-service/tests/s3_config.json similarity index 100% rename from dbrepo-analyse-service/test/s3_config.json rename to dbrepo-analyse-service/tests/s3_config.json diff --git a/dbrepo-analyse-service/test/test_determine_dt.py b/dbrepo-analyse-service/tests/test_determine_dt.py similarity index 100% rename from dbrepo-analyse-service/test/test_determine_dt.py rename to dbrepo-analyse-service/tests/test_determine_dt.py diff --git a/dbrepo-analyse-service/test/test_determine_pk.py b/dbrepo-analyse-service/tests/test_determine_pk.py similarity index 100% rename from dbrepo-analyse-service/test/test_determine_pk.py rename to dbrepo-analyse-service/tests/test_determine_pk.py diff --git a/dbrepo-analyse-service/test/test_s3_client.py b/dbrepo-analyse-service/tests/test_s3_client.py similarity index 100% rename from dbrepo-analyse-service/test/test_s3_client.py rename to dbrepo-analyse-service/tests/test_s3_client.py diff --git a/dbrepo-auth-service/init/test/conftest.py b/dbrepo-auth-service/init/tests/conftest.py similarity index 100% rename from dbrepo-auth-service/init/test/conftest.py rename to dbrepo-auth-service/init/tests/conftest.py diff --git a/dbrepo-auth-service/init/test/test_integration_app.py b/dbrepo-auth-service/init/tests/test_integration_app.py similarity index 100% rename from dbrepo-auth-service/init/test/test_integration_app.py rename to dbrepo-auth-service/init/tests/test_integration_app.py diff --git a/dbrepo-auth-service/init/test/test_unit_app.py b/dbrepo-auth-service/init/tests/test_unit_app.py similarity index 100% rename from dbrepo-auth-service/init/test/test_unit_app.py rename to dbrepo-auth-service/init/tests/test_unit_app.py diff --git a/lib/python/.coveragerc b/lib/python/.coveragerc deleted file mode 100644 index 2301243c84..0000000000 --- a/lib/python/.coveragerc +++ /dev/null @@ -1,3 +0,0 @@ -[report] -omit = - */tests/* diff --git a/lib/python/dbrepo/api/dto.py b/lib/python/dbrepo/api/dto.py index bd0d13dc18..582f40c9b1 100644 --- a/lib/python/dbrepo/api/dto.py +++ b/lib/python/dbrepo/api/dto.py @@ -677,6 +677,7 @@ class View(BaseModel): owner: UserBrief internal_name: str is_public: bool + is_schema_public: bool initial_view: bool columns: List[ViewColumn] identifiers: List[Identifier] = field(default_factory=list) @@ -959,6 +960,7 @@ class Column(BaseModel): class ViewColumn(BaseModel): id: int name: str + ord: int database_id: int internal_name: str type: ColumnType diff --git a/lib/python/tests/test_unit_rest_client.py b/lib/python/tests/test_unit_rest_client.py index e50914719c..61a9432ce4 100644 --- a/lib/python/tests/test_unit_rest_client.py +++ b/lib/python/tests/test_unit_rest_client.py @@ -1,41 +1,100 @@ import os -from unittest import TestCase, mock, main +import unittest + +import requests_mock from dbrepo.RestClient import RestClient +from dbrepo.api.dto import JwtAuth +from dbrepo.api.exceptions import MalformedError, ServiceConnectionError, ServiceError, ForbiddenError, \ + AuthenticationError, ResponseCodeError -class DatabaseUnitTest(TestCase): +class RestClientUnitTest(unittest.TestCase): def test_constructor_succeeds(self): - # test - client = RestClient() - self.assertEqual("http://localhost", client.endpoint) - self.assertIsNone(client.username) - self.assertIsNone(client.password) - self.assertTrue(client.secure) - - @mock.patch.dict(os.environ, { - "REST_API_ENDPOINT": "https://test.dbrepo.tuwien.ac.at", - "REST_API_USERNAME": "foo", - "REST_API_PASSWORD": "bar", - "REST_API_SECURE": "false", - }) - def test_constructor_environment_succeeds(self): - # test - client = RestClient() - self.assertEqual("https://test.dbrepo.tuwien.ac.at", client.endpoint) - self.assertEqual("foo", client.username) - self.assertEqual("bar", client.password) - self.assertFalse(client.secure) - - def test_constructor_credentials_succeeds(self): - # test - client = RestClient(username='admin', password='pass') - self.assertEqual("http://localhost", client.endpoint) - self.assertEqual('admin', client.username) - self.assertEqual('pass', client.password) - self.assertTrue(client.secure) + with requests_mock.Mocker() as mock: + # test + os.environ['REST_API_SECURE'] = 'True' + response = RestClient() + self.assertTrue(response.secure) + + def test_get_jwt_auth_empty_succeeds(self): + with requests_mock.Mocker() as mock: + exp = JwtAuth(access_token='ey123', + refresh_token='ey456', + id_token='ey789', + expires_in=3600, + scope='scope', + token_type='Bearer', + not_before_policy=0, + session_state='session_state', + refresh_expires_in=7200) + # mock + mock.post('/api/user/token', json=exp.model_dump(), status_code=202) + # test + response = RestClient().get_jwt_auth() + self.assertEqual(exp, response) + + def test_get_jwt_auth_400_fails(self): + with requests_mock.Mocker() as mock: + # mock + mock.post('/api/user/token', status_code=400) + # test + try: + response = RestClient().get_jwt_auth() + except MalformedError: + pass + + def test_get_jwt_auth_403_fails(self): + with requests_mock.Mocker() as mock: + # mock + mock.post('/api/user/token', status_code=403) + # test + try: + response = RestClient().get_jwt_auth() + except ForbiddenError: + pass + + def test_get_jwt_auth_428_fails(self): + with requests_mock.Mocker() as mock: + # mock + mock.post('/api/user/token', status_code=428) + # test + try: + response = RestClient().get_jwt_auth() + except AuthenticationError: + pass + + def test_get_jwt_auth_502_fails(self): + with requests_mock.Mocker() as mock: + # mock + mock.post('/api/user/token', status_code=502) + # test + try: + response = RestClient().get_jwt_auth() + except ServiceConnectionError: + pass + + def test_get_jwt_auth_503_fails(self): + with requests_mock.Mocker() as mock: + # mock + mock.post('/api/user/token', status_code=503) + # test + try: + response = RestClient().get_jwt_auth() + except ServiceError: + pass + + def test_get_jwt_auth_unknown_fails(self): + with requests_mock.Mocker() as mock: + # mock + mock.post('/api/user/token', status_code=418) + # test + try: + response = RestClient().get_jwt_auth() + except ResponseCodeError: + pass if __name__ == "__main__": - main() + unittest.main() diff --git a/lib/python/tests/test_unit_view.py b/lib/python/tests/test_unit_view.py index 52ea405b28..29fd6aac2f 100644 --- a/lib/python/tests/test_unit_view.py +++ b/lib/python/tests/test_unit_view.py @@ -5,7 +5,7 @@ import requests_mock from pandas import DataFrame from dbrepo.RestClient import RestClient -from dbrepo.api.dto import View, ViewColumn, ColumnType, UserBrief +from dbrepo.api.dto import View, ViewColumn, ColumnType, UserBrief, ViewBrief from dbrepo.api.exceptions import ForbiddenError, NotExistsError, MalformedError, AuthenticationError @@ -21,26 +21,16 @@ class ViewUnitTest(unittest.TestCase): def test_get_views_succeeds(self): with requests_mock.Mocker() as mock: - exp = [View(id=1, + exp = [ViewBrief(id=1, name="Data", internal_name="data", database_id=1, initial_view=False, query="SELECT id FROM mytable WHERE deg > 0", query_hash="94c74728b11a690e51d64719868824735f0817b7", - owner=UserBrief(id='8638c043-5145-4be8-a3e4-4b79991b0a16', username='mweise'), + owned_by='8638c043-5145-4be8-a3e4-4b79991b0a16', is_public=True, - is_schema_public=True, - columns=[ViewColumn(id=1, - ord=0, - name="id", - internal_name="id", - database_id=1, - auto_generated=False, - type=ColumnType.BIGINT, - is_public=True, - is_null_allowed=False)], - identifiers=[])] + is_schema_public=True)] # mock mock.get('/api/database/1/view', json=[exp[0].model_dump()]) # test @@ -74,9 +64,7 @@ class ViewUnitTest(unittest.TestCase): name="id", internal_name="id", database_id=1, - auto_generated=False, type=ColumnType.BIGINT, - is_public=True, is_null_allowed=False)], identifiers=[]) # mock @@ -122,9 +110,7 @@ class ViewUnitTest(unittest.TestCase): name="id", internal_name="id", database_id=1, - auto_generated=False, type=ColumnType.BIGINT, - is_public=True, is_null_allowed=False)], identifiers=[]) # mock -- GitLab