File: test_submission_resource.py

package info (click to toggle)
python-maggma 0.70.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,416 kB
  • sloc: python: 10,150; makefile: 12
file content (153 lines) | stat: -rw-r--r-- 4,506 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import json
from datetime import datetime
from random import randint

import pytest
from fastapi import FastAPI
from pydantic import BaseModel, Field
from starlette.testclient import TestClient

from maggma.api.query_operator import PaginationQuery
from maggma.api.query_operator.core import QueryOperator
from maggma.api.resource import SubmissionResource
from maggma.stores import MemoryStore


class Owner(BaseModel):
    name: str = Field(..., title="Owner's name")
    age: int = Field(None, title="Owne'r Age")
    weight: float = Field(None, title="Owner's weight")
    last_updated: datetime = Field(None, title="Last updated date for this record")


owners = (
    [Owner(name=f"Person{i}", age=i + 3, weight=100 + i) for i in list(range(10))]
    + [Owner(name="PersonAge9", age=9, weight=float(randint(155, 195)))]
    + [Owner(name="PersonWeight150", age=randint(10, 15), weight=float(150))]
    + [Owner(name="PersonAge20Weight200", age=20, weight=float(200))]
)

total_owners = len(owners)


@pytest.fixture()
def owner_store():
    store = MemoryStore("owners", key="name")
    store.connect()
    store.update([d.dict() for d in owners])
    return store


@pytest.fixture()
def post_query_op():
    class PostQuery(QueryOperator):
        def query(self, name):
            return {"criteria": {"name": name}}

    return PostQuery()


@pytest.fixture()
def patch_query_op():
    class PatchQuery(QueryOperator):
        def query(self, name, update):
            return {"criteria": {"name": name}, "update": update}

    return PatchQuery()


def test_init(owner_store, post_query_op, patch_query_op):
    resource = SubmissionResource(
        store=owner_store,
        get_query_operators=[PaginationQuery()],
        post_query_operators=[post_query_op],
        patch_query_operators=[patch_query_op],
        model=Owner,
    )
    assert len(resource.router.routes) == 5


def test_msonable(owner_store, post_query_op):
    owner_resource = SubmissionResource(
        store=owner_store,
        get_query_operators=[PaginationQuery()],
        post_query_operators=[post_query_op],
        model=Owner,
    )
    endpoint_dict = owner_resource.as_dict()

    for k in ["@class", "@module", "store", "model"]:
        assert k in endpoint_dict

    assert isinstance(endpoint_dict["model"], str)
    assert endpoint_dict["model"] == "tests.api.test_submission_resource.Owner"


def test_submission_search(owner_store, post_query_op):
    endpoint = SubmissionResource(
        store=owner_store,
        get_query_operators=[PaginationQuery()],
        post_query_operators=[post_query_op],
        calculate_submission_id=True,
        model=Owner,
    )
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)

    assert client.get("/").status_code == 200
    assert client.post("/?name=test_name").status_code == 200


def test_submission_patch(owner_store, post_query_op, patch_query_op):
    endpoint = SubmissionResource(
        store=owner_store,
        get_query_operators=[PaginationQuery()],
        post_query_operators=[post_query_op],
        patch_query_operators=[patch_query_op],
        calculate_submission_id=True,
        model=Owner,
    )
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)
    update = json.dumps({"last_updated": "2023-06-22T17:32:11.645713"})

    assert client.get("/").status_code == 200
    assert client.patch(f"/?name=PersonAge9&update={update}").status_code == 200


def test_key_fields(owner_store, post_query_op):
    endpoint = SubmissionResource(
        store=owner_store,
        get_query_operators=[PaginationQuery()],
        post_query_operators=[post_query_op],
        calculate_submission_id=False,
        model=Owner,
    )
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)

    assert client.get("/Person1/").status_code == 200
    assert client.get("/Person1/").json()["data"][0]["name"] == "Person1"


def test_patch_submission(owner_store, post_query_op):
    endpoint = SubmissionResource(
        store=owner_store,
        get_query_operators=[PaginationQuery()],
        post_query_operators=[post_query_op],
        calculate_submission_id=False,
        model=Owner,
    )
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)

    assert client.get("/Person1/").status_code == 200
    assert client.get("/Person1/").json()["data"][0]["name"] == "Person1"