File: test_synthesis.py

package info (click to toggle)
python-mp-api 0.45.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,004 kB
  • sloc: python: 6,712; makefile: 14
file content (140 lines) | stat: -rw-r--r-- 4,649 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
import os
from typing import List

import pytest
from emmet.core.synthesis import SynthesisRecipe, SynthesisTypeEnum

from mp_api.client.routes.materials.synthesis import SynthesisRester


@pytest.fixture
def rester():
    rester = SynthesisRester()
    yield rester
    rester.session.close()


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
def test_client(rester):
    search_method = rester.search

    if search_method is not None:
        q = {"keywords": ["silicon"]}

        doc = search_method(**q)[0]

        assert doc.doi is not None
        assert doc.paragraph_string is not None
        assert doc.synthesis_type is not None


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
def test_filters_keywords(rester):
    search_method = rester.search

    if search_method is not None:
        doc = search_method(keywords=["silicon"])[0]

        assert isinstance(doc.search_score, float)
        highlighted = sum((x["texts"] for x in doc.highlights), [])
        assert "silicon" in " ".join([x["value"] for x in highlighted]).lower()


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
def test_filters_synthesis_type(rester):
    search_method = rester.search

    if search_method is not None:
        doc = search_method(
            synthesis_type=[SynthesisTypeEnum.solid_state], num_chunks=1
        )
        assert all(x.synthesis_type == SynthesisTypeEnum.solid_state for x in doc)

        doc = search_method(synthesis_type=[SynthesisTypeEnum.sol_gel], num_chunks=1)
        assert all(x.synthesis_type == SynthesisTypeEnum.sol_gel for x in doc)


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
@pytest.mark.xfail  # Needs fixing
def test_filters_temperature_range(rester):
    search_method = rester.search

    if search_method is not None:
        docs: List[SynthesisRecipe] = search_method(
            condition_heating_temperature_min=700,
            condition_heating_temperature_max=1000,
            num_chunks=5,
        )
        for doc in docs:
            for op in doc.operations:
                for temp in op.conditions.heating_temperature:
                    for val in temp.values:
                        assert 700 <= val <= 1000


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
@pytest.mark.xfail  # Needs fixing
def test_filters_time_range(rester):
    search_method = rester.search

    if search_method is not None:
        docs: List[SynthesisRecipe] = search_method(
            condition_heating_time_min=7, condition_heating_time_max=11, num_chunks=5
        )
        for doc in docs:
            for op in doc.operations:
                for temp in op.conditions.heating_time:
                    for val in temp.values:
                        assert 7 <= val <= 11


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
def test_filters_atmosphere(rester):
    search_method = rester.search

    if search_method is not None:
        docs: List[SynthesisRecipe] = search_method(
            condition_heating_atmosphere=["air", "O2"],
            num_chunks=5,
        )
        for doc in docs:
            found = False
            for op in doc.operations:
                for atm in op.conditions.heating_atmosphere:
                    if atm in ["air", "O2"]:
                        found = True
            assert found


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
def test_filters_mixing_device(rester):
    search_method = rester.search

    if search_method is not None:
        docs: List[SynthesisRecipe] = search_method(
            condition_mixing_device=["zirconia", "Al2O3"],
            num_chunks=5,
        )
        for doc in docs:
            found = False
            for op in doc.operations:
                if op.conditions.mixing_device in ["zirconia", "Al2O3"]:
                    found = True
            assert found


@pytest.mark.skipif(os.getenv("MP_API_KEY", None) is None, reason="No API key found.")
def test_filters_mixing_media(rester):
    search_method = rester.search

    if search_method is not None:
        docs: List[SynthesisRecipe] = search_method(
            condition_mixing_media=["water", "alcohol"],
            num_chunks=5,
        )
        for doc in docs:
            found = False
            for op in doc.operations:
                if op.conditions.mixing_media in ["water", "alcohol"]:
                    found = True
            assert found