From a0fec23822f9421bedf6a295e44585e8864f27dd Mon Sep 17 00:00:00 2001 From: Victor San Kho Lin Date: Fri, 22 Mar 2024 14:26:30 +1100 Subject: [PATCH] Fixed sequence_run_manager endpoint unit test * Improved SRM unit testing regime, coverage report Related #139 --- .gitignore | 3 ++ .../sequence_run_manager/.coveragerc | 5 ++++ .../stateless/sequence_run_manager/Makefile | 23 +++++++++++++++ .../stateless/sequence_run_manager/README.md | 8 ++++++ .../deps/requirements-dev.txt | 1 + .../deps/requirements-test.txt | 1 + .../tests/test_viewsets.py | 28 +++++++++++++++++-- 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 lib/workload/stateless/sequence_run_manager/.coveragerc diff --git a/.gitignore b/.gitignore index 6c1075b69..83e9d32a6 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,6 @@ Brewfile.lock.json *.xml target/ + +.coverage +htmlcov/ diff --git a/lib/workload/stateless/sequence_run_manager/.coveragerc b/lib/workload/stateless/sequence_run_manager/.coveragerc new file mode 100644 index 000000000..7a6acfff5 --- /dev/null +++ b/lib/workload/stateless/sequence_run_manager/.coveragerc @@ -0,0 +1,5 @@ +[run] +omit = */tests/*,*tests*,sequence_run_manager/management/*,sequence_run_manager/migrations/*,sequence_run_manager/wsgi.py,migrate.py,manage.py + +[report] +omit = */tests/*,*tests*,sequence_run_manager/management/*,sequence_run_manager/migrations/*,sequence_run_manager/wsgi.py,migrate.py,manage.py diff --git a/lib/workload/stateless/sequence_run_manager/Makefile b/lib/workload/stateless/sequence_run_manager/Makefile index b4aa027ff..bfe78fec1 100644 --- a/lib/workload/stateless/sequence_run_manager/Makefile +++ b/lib/workload/stateless/sequence_run_manager/Makefile @@ -1,3 +1,6 @@ +.EXPORT_ALL_VARIABLES: +DJANGO_SETTINGS_MODULE = sequence_run_manager.settings.local + .PHONY: test suite install: @@ -17,6 +20,26 @@ test: install up suite down suite: @python manage.py test +migrate: + @python manage.py migrate + +start: migrate + @python manage.py runserver_plus + +openapi: + @python manage.py generateschema > orcabus.srm.openapi.yaml + +validate: openapi + @python -m openapi_spec_validator orcabus.srm.openapi.yaml + +coverage: install up migrate + @echo $$DJANGO_SETTINGS_MODULE + @coverage run --source='.' manage.py test + +report: + @coverage report -m + @coverage html + up: @docker compose up --wait -d diff --git a/lib/workload/stateless/sequence_run_manager/README.md b/lib/workload/stateless/sequence_run_manager/README.md index e3f6b54d3..2567cd269 100644 --- a/lib/workload/stateless/sequence_run_manager/README.md +++ b/lib/workload/stateless/sequence_run_manager/README.md @@ -134,6 +134,14 @@ python manage.py generateschema > orcabus.srm.openapi.yaml ## Testing +### Coverage report + +``` +make coverage report +``` + +_The html report is in `htmlcov/index.html`._ + ### Run test suite ``` diff --git a/lib/workload/stateless/sequence_run_manager/deps/requirements-dev.txt b/lib/workload/stateless/sequence_run_manager/deps/requirements-dev.txt index b94a57fec..c5c7f1546 100644 --- a/lib/workload/stateless/sequence_run_manager/deps/requirements-dev.txt +++ b/lib/workload/stateless/sequence_run_manager/deps/requirements-dev.txt @@ -1,3 +1,4 @@ -r requirements-test.txt django_extensions==3.2.3 drf_yasg==1.21.7 +openapi-spec-validator diff --git a/lib/workload/stateless/sequence_run_manager/deps/requirements-test.txt b/lib/workload/stateless/sequence_run_manager/deps/requirements-test.txt index 71c4d5340..416e2d10e 100644 --- a/lib/workload/stateless/sequence_run_manager/deps/requirements-test.txt +++ b/lib/workload/stateless/sequence_run_manager/deps/requirements-test.txt @@ -5,3 +5,4 @@ pytz==2024.1 mockito==1.5.0 # Intentionally leave out the boto3 version here. They bump like daily and latest is always fine. boto3 +coverage==7.4.4 diff --git a/lib/workload/stateless/sequence_run_manager/sequence_run_manager/tests/test_viewsets.py b/lib/workload/stateless/sequence_run_manager/sequence_run_manager/tests/test_viewsets.py index 11412ac4e..497ec154e 100644 --- a/lib/workload/stateless/sequence_run_manager/sequence_run_manager/tests/test_viewsets.py +++ b/lib/workload/stateless/sequence_run_manager/sequence_run_manager/tests/test_viewsets.py @@ -4,12 +4,15 @@ from django.utils.timezone import now from sequence_run_manager.models.sequence import Sequence +from sequence_run_manager.urls.base import api_base logger = logging.getLogger() logger.setLevel(logging.INFO) class SequenceViewSetTestCase(TestCase): + endpoint = f"/{api_base}sequence" + def setUp(self): Sequence.objects.create( instrument_run_id="190101_A01052_0001_BH5LY7ACGT", @@ -29,22 +32,41 @@ def test_get_api(self): """ # Get sequence list logger.info("Get sequence API") - response = self.client.get("/sequence/") + response = self.client.get(self.endpoint) self.assertEqual(response.status_code, 200, "Ok status response is expected") logger.info("Check if API return result") result_response = response.data["results"] self.assertGreater(len(result_response), 0, "A result is expected") + def test_get_by_uk_surrogate_key(self): + """ + python manage.py test sequence_run_manager.tests.test_viewsets.SequenceViewSetTestCase.test_get_by_uk_surrogate_key + """ logger.info("Check if unique data has a single entry") - response = self.client.get("/sequence/?sequence_run_id=r.AAAAAA") + response = self.client.get(f"{self.endpoint}/?instrument_run_id=190101_A01052_0001_BH5LY7ACGT") results_response = response.data["results"] self.assertEqual( len(results_response), 1, "Single result is expected for unique data" ) + def test_get_by_sequence_run_id(self): + """ + python manage.py test sequence_run_manager.tests.test_viewsets.SequenceViewSetTestCase.test_get_by_sequence_run_id + """ + logger.info("Check if unique data has a single entry") + response = self.client.get(f"{self.endpoint}/?sequence_run_id=r.AAAAAA") + results_response = response.data["results"] + self.assertEqual( + len(results_response), 1, "Single result is expected for unique data" + ) + + def test_get_by_invalid_parameter(self): + """ + python manage.py test sequence_run_manager.tests.test_viewsets.SequenceViewSetTestCase.test_get_by_invalid_parameter + """ logger.info("Check if wrong parameter") - response = self.client.get("/sequence/?lib_id=LBR0001") + response = self.client.get(f"{self.endpoint}/?lib_id=LBR0001") results_response = response.data["results"] self.assertEqual( len(results_response),