File size: 1,852 Bytes
e67043b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi.testclient import TestClient
from .api import build_tool, ChemicalPropAPI
from typing import Tuple, Optional, List


class ChemicalPropMock(ChemicalPropAPI):
    def __init__(self) -> None:
        self._endpoint = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/"

    def get_name_by_cid(self, cid: str, top_k: Optional[int] = None) -> List[str]:
        ans = ["A", "B", "C", "D", "E"]
        if top_k is None:
            top_k = len(ans)
        return ans[:top_k]

    def get_cid_by_struct(self, smiles: str) -> List[str]:
        return ["123"]

    def get_cid_by_name(self, name: str, name_type: Optional[str] = None) -> List[str]:
        return ["123"]

    def get_prop_by_cid(self, cid: str) -> str:
        return {"works": "well"}


app = build_tool({"debug": True, "chemical_prop_api": ChemicalPropMock()})
client = TestClient(app)


def test_get_name():
    response = client.get("/get_name", params={"cid": 123})
    assert response.status_code == 200
    assert response.json() == {"names": ["A", "B", "C"]}


def test_get_all_names():
    response = client.get("/get_allname", params={"cid": 123})
    assert response.status_code == 200
    assert response.json() == {"names": ["A", "B", "C", "D", "E"]}


def test_get_id_by_struct():
    response = client.get("/get_id_by_struct", params={"smiles": "C1=CC=CC=C1"})
    assert response.status_code == 200
    assert response.json() == {"state": "matched", "content": "123"}


def test_get_id():
    response = client.get("/get_id", params={"name": "benzene"})
    assert response.status_code == 200
    assert response.json() == {
        "state": "precise",
        "content": "123",
    }


def test_get_prop():
    response = client.get("/get_prop", params={"cid": "123"})
    assert response.status_code == 200
    assert response.json() == {"works": "well"}