From 73be13f2b242ad2ab73b2497a2dcabfee7498023 Mon Sep 17 00:00:00 2001 From: Ninad Bhat Date: Fri, 18 Jun 2021 20:37:45 +1000 Subject: [PATCH 1/3] Create Group_Post model --- aiida_restapi/models.py | 20 ++++++++++++++++--- aiida_restapi/routers/groups.py | 6 +++--- tests/test_groups.py | 12 +++++++++-- tests/test_models.py | 9 +-------- tests/test_models/test_group_get_entities.yml | 5 ++++- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/aiida_restapi/models.py b/aiida_restapi/models.py index b520e05..d5047c2 100644 --- a/aiida_restapi/models.py +++ b/aiida_restapi/models.py @@ -7,7 +7,8 @@ # pylint: disable=too-few-public-methods from datetime import datetime -from typing import ClassVar, List, Optional, Type, TypeVar +from typing import ClassVar, Dict, List, Optional, Type, TypeVar +from uuid import UUID from aiida import orm from pydantic import BaseModel, Field @@ -124,8 +125,21 @@ class Group(AiidaModel): _orm_entity = orm.Group - id: Optional[int] = Field(description="Id of the object") + id: int = Field(description="Unique id (pk)") + uuid: UUID = Field(description="Universally unique id") + label: str = Field(description="Label of group") + type_string: str = Field(description="type of the group") + description: Optional[str] = Field(description="Description of group") + extras: Dict = Field(description="extra data about for the group") + time: Optional[datetime] = Field(description="Created time") + user_id: Optional[int] = Field(description="Created by user id (pk)") + + +class Group_Post(AiidaModel): + """AiiDA Group Post model.""" + + _orm_entity = orm.Group + label: str = Field(description="Used to access the group. Must be unique.") type_string: Optional[str] = Field(description="Type of the group") - user_id: Optional[str] = Field(description="Id of the user that created the node.") description: Optional[str] = Field(description="Short description of the group.") diff --git a/aiida_restapi/routers/groups.py b/aiida_restapi/routers/groups.py index b082959..6fcab8d 100644 --- a/aiida_restapi/routers/groups.py +++ b/aiida_restapi/routers/groups.py @@ -6,7 +6,7 @@ from aiida.cmdline.utils.decorators import with_dbenv from fastapi import APIRouter, Depends -from aiida_restapi.models import Group, User +from aiida_restapi.models import Group, Group_Post, User from .auth import get_current_active_user @@ -41,8 +41,8 @@ async def read_group(group_id: int) -> Optional[Group]: @router.post("/groups", response_model=Group) -async def create_user( - group: Group, +async def create_group( + group: Group_Post, current_user: User = Depends( get_current_active_user ), # pylint: disable=unused-argument diff --git a/tests/test_groups.py b/tests/test_groups.py index c17b489..981aa54 100644 --- a/tests/test_groups.py +++ b/tests/test_groups.py @@ -14,8 +14,16 @@ def test_get_group_projectable(client): response = client.get("/groups/projectable_properties") assert response.status_code == 200 - - assert response.json() == ["id", "label", "type_string", "user_id", "description"] + assert response.json() == [ + "id", + "uuid", + "label", + "type_string", + "description", + "extras", + "time", + "user_id", + ] def test_get_single_group(default_groups, client): # pylint: disable=unused-argument diff --git a/tests/test_models.py b/tests/test_models.py index 197ff08..41ad2a0 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -7,14 +7,7 @@ def replace_dynamic(data: dict) -> dict: """Replace dynamic fields with their type name.""" - for key in [ - "id", - "uuid", - "dbnode_id", - "user_id", - "mtime", - "ctime", - ]: + for key in ["id", "uuid", "dbnode_id", "user_id", "mtime", "ctime", "time"]: if key in data: data[key] = type(data[key]).__name__ return data diff --git a/tests/test_models/test_group_get_entities.yml b/tests/test_models/test_group_get_entities.yml index 8a3decd..2459c6c 100644 --- a/tests/test_models/test_group_get_entities.yml +++ b/tests/test_models/test_group_get_entities.yml @@ -1,5 +1,8 @@ - description: regrerssion_test + extras: {} id: int label: regression_label_1 + time: datetime type_string: core - user_id: str + user_id: int + uuid: UUID From 1013aca124fdbecaa31a68d461e712a2041c0aa9 Mon Sep 17 00:00:00 2001 From: Ninad Bhat Date: Mon, 28 Jun 2021 17:30:34 +1000 Subject: [PATCH 2/3] Add from_orm function --- aiida_restapi/models.py | 21 +++++++++++++++++++-- tests/test_groups.py | 11 +++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/aiida_restapi/models.py b/aiida_restapi/models.py index d5047c2..5c011af 100644 --- a/aiida_restapi/models.py +++ b/aiida_restapi/models.py @@ -131,8 +131,25 @@ class Group(AiidaModel): type_string: str = Field(description="type of the group") description: Optional[str] = Field(description="Description of group") extras: Dict = Field(description="extra data about for the group") - time: Optional[datetime] = Field(description="Created time") - user_id: Optional[int] = Field(description="Created by user id (pk)") + time: datetime = Field(description="Created time") + user_id: int = Field(description="Created by user id (pk)") + + @classmethod + def from_orm(cls, orm_entity: orm.Group) -> orm.Group: + query = ( + orm.QueryBuilder() + .append( + cls._orm_entity, + filters={"id": orm_entity.id}, + tag="fields", + project=["user_id", "time"], + ) + .limit(1) + ) + orm_entity.user_id = query.dict()[0]["fields"]["user_id"] + orm_entity.time = query.dict()[0]["fields"]["time"] + + return super().from_orm(orm_entity) class Group_Post(AiidaModel): diff --git a/tests/test_groups.py b/tests/test_groups.py index 981aa54..bb732f0 100644 --- a/tests/test_groups.py +++ b/tests/test_groups.py @@ -41,4 +41,15 @@ def test_create_group(client, authenticate): # pylint: disable=unused-argument response = client.get("/groups") first_names = [group["label"] for group in response.json()] + assert "test_label_create" in first_names + + +def test_create_group_returns_user_id( + client, authenticate +): # pylint: disable=unused-argument + """Test creating a new group returns user_id.""" + response = client.post("/groups", json={"label": "test_label_create"}) + assert response.status_code == 200, response.content + + assert response.json()["user_id"] From 836b8cc2b944b8d3213c0cabbc6c759ee3dd5084 Mon Sep 17 00:00:00 2001 From: Ninad Bhat Date: Tue, 29 Jun 2021 12:36:40 +1000 Subject: [PATCH 3/3] Change AiiDA version --- tests/test_graphql/test_full/test_full.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_graphql/test_full/test_full.yml b/tests/test_graphql/test_full/test_full.yml index 4091239..f5dd59e 100644 --- a/tests/test_graphql/test_full/test_full.yml +++ b/tests/test_graphql/test_full/test_full.yml @@ -1,4 +1,4 @@ data: - aiidaVersion: 1.6.3 + aiidaVersion: 1.6.4 node: label: node 1