From be2f687e774640cdac7c817d1d8c919de5c2413a Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 16 Aug 2024 15:44:34 +0100 Subject: [PATCH] Fix unit test for service_run --- softpack_core/artifacts.py | 4 ++-- softpack_core/graphql.py | 10 ---------- softpack_core/service.py | 4 ++++ tests/unit/conftest.py | 16 ---------------- tests/unit/test_service.py | 25 +++++++++++++++++++++++++ 5 files changed, 31 insertions(+), 28 deletions(-) delete mode 100644 tests/unit/conftest.py create mode 100644 tests/unit/test_service.py diff --git a/softpack_core/artifacts.py b/softpack_core/artifacts.py index 96fae0d..c295f6e 100644 --- a/softpack_core/artifacts.py +++ b/softpack_core/artifacts.py @@ -229,7 +229,7 @@ def clone_repo(self, branch: Optional[str] = None) -> None: path = self.settings.artifacts.path.expanduser() / ".git" if path.is_dir(): shutil.rmtree(path) - + self.repo = pygit2.clone_repository( self.settings.artifacts.repo.url, path=path, @@ -237,7 +237,7 @@ def clone_repo(self, branch: Optional[str] = None) -> None: bare=True, checkout_branch=branch, ) - + self.reference = "/".join( [ "refs/remotes", diff --git a/softpack_core/graphql.py b/softpack_core/graphql.py index 227af70..5dac160 100644 --- a/softpack_core/graphql.py +++ b/softpack_core/graphql.py @@ -32,16 +32,6 @@ class GraphQL(API): ] commands = Typer(help="GraphQL commands.") - @staticmethod - @commands.command("query", help="Execute a GraphQL query.") - def query_command() -> None: - """Execute a GraphQL query. - - Returns: - None. - """ - app.echo("GraphQL Query") - class Schema(strawberry.Schema): """GraphQL Schema class.""" diff --git a/softpack_core/service.py b/softpack_core/service.py index 0b2dc42..454cb14 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -5,6 +5,7 @@ """ +import multiprocessing import urllib.parse from pathlib import Path @@ -51,6 +52,7 @@ def run( help="Create and use this branch of Artefacts repo.", ), ] = 'main', + serviceReady = None, ) -> None: """Start the SoftPack Core REST API service. @@ -68,6 +70,8 @@ def run( artifacts.clone_repo(branch=branch) + if serviceReady is not None: + serviceReady.set() uvicorn.run( "softpack_core.app:app.router", host=app.settings.server.host, diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py deleted file mode 100644 index 4000028..0000000 --- a/tests/unit/conftest.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Copyright (c) 2023 Genome Research Ltd. - -This source code is licensed under the MIT license found in the -LICENSE file in the root directory of this source tree. -""" - - -import pytest -from fastapi.testclient import TestClient - -from softpack_core.app import app - - -@pytest.fixture -def client() -> TestClient: - return TestClient(app.router) diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py new file mode 100644 index 0000000..9f4f463 --- /dev/null +++ b/tests/unit/test_service.py @@ -0,0 +1,25 @@ +"""Copyright (c) 2023 Genome Research Ltd. + +This source code is licensed under the MIT license found in the +LICENSE file in the root directory of this source tree. +""" + +import httpx +from box import Box + +from softpack_core import __version__ +from softpack_core.app import app +import multiprocessing +from softpack_core.service import ServiceAPI +from time import sleep + + +def test_service_run() -> None: + ready = multiprocessing.Event() + run = multiprocessing.Process(target=ServiceAPI.run, kwargs={"serviceReady": ready}) + run.start() + ready.wait(timeout=120) + response = httpx.get(app.url()) + run.terminate() + status = Box(response.json()) + assert status.softpack.core.version == __version__