From 28500563cf5cfbc52ed4f126d3c8b71fc69d3623 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Wed, 20 Nov 2024 15:35:57 +0100 Subject: [PATCH] fixing tests --- aiida_restapi/routers/nodes.py | 27 +++++++-------- docs/source/user_guide/graphql.md | 2 +- tests/test_nodes.py | 57 +++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/aiida_restapi/routers/nodes.py b/aiida_restapi/routers/nodes.py index 45b34fd..f7e8c2b 100644 --- a/aiida_restapi/routers/nodes.py +++ b/aiida_restapi/routers/nodes.py @@ -1,5 +1,7 @@ """Declaration of FastAPI application.""" +from __future__ import annotations + import json import os import tempfile @@ -44,6 +46,14 @@ async def get_nodes_download_formats() -> dict[str, Any]: return models.Node.get_all_download_formats() +@router.get('/nodes/full_types', response_model=dict[str, Any]) +@with_dbenv() +async def get_full_types() -> dict[str, Any]: + """Return full_types of the nodes""" + result = get_node_namespace(user_pk=None, count_nodes=False).get_description() + return result + + @router.get('/nodes/{nodes_id}', response_model=models.Node) @with_dbenv() async def read_node(nodes_id: int) -> Optional[models.Node]: @@ -134,16 +144,6 @@ async def create_upload_file( return models.Node.from_orm(orm_object) -from typing import Any # noqa: E402 - - -@router.post('/nodes/full_types', response_model=dict[str, Any]) -@with_dbenv() -async def get_full_types() -> dict[str, Any]: - """Return full_types of the nodes""" - return get_node_namespace(user_pk=None, count_nodes=False).get_description() - - ### TODO move to identifiers def construct_full_type(node_type: str, process_type: str) -> str: """Return the full type, which uniquely identifies any `Node` with the given `node_type` and `process_type`. @@ -242,13 +242,10 @@ def _infer_full_type(self, full_type: Union[str, None]) -> Union[str, None]: """Infer the full type based on the current namespace path and the given full type of the leaf.""" from aiida.common.utils import strip_prefix - if full_type is None or self._path is None: + if full_type or self._path is None: return full_type - else: - # for type checker - full_type_: str = full_type - full_type = strip_prefix(self._path, 'node.') + full_type_ = strip_prefix(self._path, 'node.') if full_type_.startswith('process.'): for basepath, full_type_template in self.process_full_type_mapping.items(): diff --git a/docs/source/user_guide/graphql.md b/docs/source/user_guide/graphql.md index 7f8da43..4f71520 100644 --- a/docs/source/user_guide/graphql.md +++ b/docs/source/user_guide/graphql.md @@ -369,7 +369,7 @@ http://localhost:5000/api/v4/nodes?attributes=true&attributes_filter=pbc1 http://localhost:5000/api/v4/nodes/full_types ``` -Not implemented for GraphQL, please use the FastAPI for this use case. +Not implemented for GraphQL, please use the REST API for this use case. ```html http://localhost:5000/api/v4/nodes/download_formats diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 9e2f7be..fcad89e 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -332,9 +332,60 @@ def test_create_bool_with_extra(client, authenticate): # pylint: disable=unused assert check_response.json()['extras']['extra_two'] == 'value_2' -def test_get_full_types(client): +def test_get_full_types(default_nodes, client): """Test get full_types nodes.""" response = client.get('/nodes/full_types') - assert response.status_code == 200 - assert response.json() == {} + assert response.status_code == 200, response.json() + assert response.json() == { + 'namespace': 'node', + 'full_type': 'node.%|', + 'label': 'node', + 'path': 'node', + 'subspaces': [ + { + 'namespace': 'data', + 'full_type': 'data.%|', + 'label': 'Data', + 'path': 'node.data', + 'subspaces': [ + { + 'namespace': 'core', + 'full_type': 'data.core.%|', + 'label': 'core', + 'path': 'node.data.core', + 'subspaces': [ + { + 'namespace': 'bool', + 'full_type': 'data.core.bool.Bool.|', + 'label': 'Bool', + 'path': 'node.data.core.bool', + 'subspaces': [], + }, + { + 'namespace': 'float', + 'full_type': 'data.core.float.Float.|', + 'label': 'Float', + 'path': 'node.data.core.float', + 'subspaces': [], + }, + { + 'namespace': 'int', + 'full_type': 'data.core.int.Int.|', + 'label': 'Int', + 'path': 'node.data.core.int', + 'subspaces': [], + }, + { + 'namespace': 'str', + 'full_type': 'data.core.str.Str.|', + 'label': 'Str', + 'path': 'node.data.core.str', + 'subspaces': [], + }, + ], + } + ], + } + ], + }