From a01aa548c9e338b6563c041b5d2c8e9950314632 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 29 Sep 2024 19:41:26 +0200 Subject: [PATCH 1/3] Adding a version route --- boaviztapi/routers/utils_router.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/boaviztapi/routers/utils_router.py b/boaviztapi/routers/utils_router.py index d5d08be9..7a682b71 100644 --- a/boaviztapi/routers/utils_router.py +++ b/boaviztapi/routers/utils_router.py @@ -1,7 +1,7 @@ import os -import os import pandas as pd +import toml from fastapi import APIRouter, Query from boaviztapi.dto.component.cpu import CPU @@ -23,6 +23,12 @@ _ram_manuf = pd.read_csv(os.path.join(data_dir, 'crowdsourcing/ram_manufacture.csv')) + +@utils_router.get('/version', description="Get the version of the API") +async def version(): + return toml.loads(open(os.path.join(os.path.dirname(__file__), '../../pyproject.toml'), 'r').read())['tool']['poetry'][ + 'version'] + @utils_router.get('/country_code', description=country_code) async def utils_get_all_countries(): return get_available_countries() From 476eabd0029eff012b01777c30e7b0b442e7f1ed Mon Sep 17 00:00:00 2001 From: olivier de Meringo Date: Wed, 2 Oct 2024 11:45:31 +0200 Subject: [PATCH 2/3] feat(test): add tests for the api route that returns version. Related to #299 --- tests/api/test_utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/api/test_utils.py b/tests/api/test_utils.py index 58e727dc..71695c14 100644 --- a/tests/api/test_utils.py +++ b/tests/api/test_utils.py @@ -1,5 +1,6 @@ import pytest from httpx import AsyncClient +import re from boaviztapi.main import app @@ -54,3 +55,17 @@ async def test_complete_cpu_from_name(): async with AsyncClient(app=app, base_url="http://test") as ac: res = await ac.get('/v1/utils/name_to_cpu?cpu_name=deijeijdiejdzij') assert res.json() == "CPU name deijeijdiejdzij is not found in our database" + +@pytest.mark.asyncio +async def test_get_api_version_is_not_empty_string(): + async with AsyncClient(app=app, base_url="http://test") as ac: + res = await ac.get('/v1/utils/version') + assert res.json() + +@pytest.mark.asyncio +async def test_get_api_version_is_semver(): + async with AsyncClient(app=app, base_url="http://test") as ac: + res = await ac.get('/v1/utils/version') + # Check returned version matches semver regex + # See https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string + assert re.match("^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$", res.json()) \ No newline at end of file From 8093bc17cab8597f5fc14bd1c62d9bdc820479c7 Mon Sep 17 00:00:00 2001 From: olivier de Meringo Date: Wed, 2 Oct 2024 11:54:49 +0200 Subject: [PATCH 3/3] feat(test): remove the semver format test because API is not strictly using this scheme. Related to #299 --- tests/api/test_utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/api/test_utils.py b/tests/api/test_utils.py index 71695c14..acc610e5 100644 --- a/tests/api/test_utils.py +++ b/tests/api/test_utils.py @@ -62,10 +62,10 @@ async def test_get_api_version_is_not_empty_string(): res = await ac.get('/v1/utils/version') assert res.json() -@pytest.mark.asyncio -async def test_get_api_version_is_semver(): - async with AsyncClient(app=app, base_url="http://test") as ac: - res = await ac.get('/v1/utils/version') - # Check returned version matches semver regex - # See https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string - assert re.match("^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$", res.json()) \ No newline at end of file +# @pytest.mark.asyncio +# async def test_get_api_version_is_semver(): +# async with AsyncClient(app=app, base_url="http://test") as ac: +# res = await ac.get('/v1/utils/version') +# # Check returned version matches semver regex +# # See https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string +# assert re.match("^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$", res.json()) \ No newline at end of file