Skip to content

Commit

Permalink
Creates Group model for post (#29)
Browse files Browse the repository at this point in the history
* Create Group_Post model

* Add from_orm function

* Change AiiDA version
  • Loading branch information
NinadBhat committed Jul 19, 2021
1 parent ac1ccf3 commit 35ea00f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
37 changes: 34 additions & 3 deletions aiida_restapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,8 +125,38 @@ 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: 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):
"""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.")
6 changes: 3 additions & 3 deletions aiida_restapi/routers/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_graphql/test_full/test_full.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
data:
aiidaVersion: 1.6.3
aiidaVersion: 1.6.4
node:
label: node 1
23 changes: 21 additions & 2 deletions tests/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,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"]
9 changes: 1 addition & 8 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/test_models/test_group_get_entities.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 35ea00f

Please sign in to comment.