File: test_qc_task.py

package info (click to toggle)
python-emmet-core 0.84.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 77,220 kB
  • sloc: python: 16,355; makefile: 30
file content (120 lines) | stat: -rw-r--r-- 4,089 bytes parent folder | download
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
import pytest
from tests.conftest_qchem import assert_schemas_equal, get_test_object


@pytest.mark.parametrize(
    "object_name, task_name",
    [
        pytest.param("SinglePointTest", "standard", id="SinglePointTest"),
        pytest.param("OptimizationTest", "standard", id="OptimizationTest"),
    ],
)  # Can add more later, something like freq, pesscan, ts,
# FFOptTest, once we get flows working for qchem in atomate2


def test_input_summary(test_dir, object_name, task_name):
    from monty.json import MontyDecoder, jsanitize
    from emmet.core.qc_tasks import InputDoc
    from emmet.core.qchem.calculation import Calculation

    test_object = get_test_object(object_name)
    dir_name = test_dir / "qchem" / test_object.folder

    files = test_object.task_files[task_name]
    calc_doc = Calculation.from_qchem_files(dir_name, task_name, **files)

    test_doc = InputDoc.from_qchem_calc_doc(calc_doc)
    valid_doc = test_object.task_doc["input"]
    assert_schemas_equal(test_doc, valid_doc)

    # test document can be jsanitized
    d = jsanitize(test_doc, strict=True, enum_values=True, allow_bson=True)

    # and decoded
    MontyDecoder().process_decoded(d)


@pytest.mark.parametrize(
    "object_name, task_name",
    [
        pytest.param("SinglePointTest", "standard", id="SinglePointTest"),
        pytest.param("OptimizationTest", "standard", id="OptimizationTest"),
    ],
)
def test_output_summary(test_dir, object_name, task_name):
    from monty.json import MontyDecoder, jsanitize
    from emmet.core.qc_tasks import OutputDoc
    from emmet.core.qchem.calculation import Calculation

    test_object = get_test_object(object_name)
    dir_name = test_dir / "qchem" / test_object.folder

    files = test_object.task_files[task_name]
    calc_doc = Calculation.from_qchem_files(dir_name, task_name, **files)

    test_doc = OutputDoc.from_qchem_calc_doc(calc_doc)
    valid_doc = test_object.task_doc["output"]
    assert_schemas_equal(test_doc, valid_doc)

    # test document can be janitized
    d = jsanitize(test_doc, strict=True, enum_values=True, allow_bson=True)

    # and decoded
    MontyDecoder().process_decoded(d)


@pytest.mark.parametrize(
    "object_name",
    [
        pytest.param("SinglePointTest", id="SinglePointTest"),
        pytest.param("OptimizationTest", id="OptimizationTest"),
    ],
)
def test_task_doc(test_dir, object_name):
    from monty.json import MontyDecoder, jsanitize
    from emmet.core.qc_tasks import TaskDoc

    test_object = get_test_object(object_name)
    dir_name = test_dir / "qchem" / test_object.folder
    test_doc = TaskDoc.from_directory(dir_name)
    assert_schemas_equal(test_doc, test_object.task_doc)

    # test document can be jsanitized
    d = jsanitize(test_doc, strict=True, enum_values=True, allow_bson=True)

    # and decoded
    MontyDecoder().process_decoded(d)

    # Test that additional_fields works
    test_doc = TaskDoc.from_directory(dir_name, additional_fields={"foo": "bar"})
    assert test_doc.model_dump()["additional_fields"] == {"foo": "bar"}


@pytest.mark.parametrize(
    "object_name",
    [
        pytest.param("SinglePointTest", id="SinglePointTest"),
        pytest.param("OptimizationTest", id="OptimizationTest"),
    ],
)
def test_task_doc_val_flag(test_dir, object_name):
    from monty.json import MontyDecoder, jsanitize
    from emmet.core.qc_tasks import TaskDoc

    test_object = get_test_object(object_name)
    dir_name = test_dir / "qchem" / test_object.folder
    print(f"The test object is {test_object.task_doc}")
    test_doc = TaskDoc.from_directory(dir_name, validate_lot=False)
    assert_schemas_equal(test_doc, test_object.task_doc)

    # test document can be jsanitized
    d = jsanitize(test_doc, strict=True, enum_values=True, allow_bson=True)

    # and decoded
    MontyDecoder().process_decoded(d)

    # Test that additional_fields works
    test_doc = TaskDoc.from_directory(
        dir_name, validate_lot=False, additional_fields={"foo": "bar"}
    )
    assert test_doc.model_dump()["additional_fields"] == {"foo": "bar"}