|
| 1 | +"""End-to-end URL and local-file structure adaptation tests.""" |
| 2 | + |
| 3 | +import io |
| 4 | +import json |
| 5 | +import urllib.request |
| 6 | +from email.message import Message |
| 7 | +from fractions import Fraction |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +import httk.core |
| 12 | +import pytest |
| 13 | +from httk.core import OptimadeResource, load_entry_type_schema |
| 14 | + |
| 15 | +from httk.atomistic import OptimadeStructure, UnitcellStructureView, build_supercell |
| 16 | + |
| 17 | +pytest.importorskip("httk.io") |
| 18 | + |
| 19 | +_STRUCTURES_ID = "https://schemas.optimade.org/defs/v1.3/entrytypes/optimade/structures" |
| 20 | + |
| 21 | + |
| 22 | +class _Response(io.BytesIO): |
| 23 | + headers = Message() |
| 24 | + |
| 25 | + |
| 26 | +def _entry_and_info() -> tuple[str, str]: |
| 27 | + schema = load_entry_type_schema(_STRUCTURES_ID) |
| 28 | + properties = { |
| 29 | + remote_name: {"$id": schema.properties[property_name].definition_id} |
| 30 | + for remote_name, property_name in { |
| 31 | + "remote_lattice": "lattice_vectors", |
| 32 | + "remote_fractional": "fractional_site_positions", |
| 33 | + "remote_species": "species", |
| 34 | + "remote_site_species": "species_at_sites", |
| 35 | + "remote_dimensions": "dimension_types", |
| 36 | + }.items() |
| 37 | + } |
| 38 | + entry = { |
| 39 | + "data": { |
| 40 | + "id": "fixture-1", |
| 41 | + "type": "structures", |
| 42 | + "attributes": { |
| 43 | + "remote_lattice": [[2, 0, 0], [0, 3, 0], [0, 0, 4]], |
| 44 | + "remote_fractional": [[0, 0, 0], [1 / 2, 1 / 2, 1 / 2]], |
| 45 | + "remote_species": [ |
| 46 | + {"name": "Na", "chemical_symbols": ["Na"], "concentration": [1]}, |
| 47 | + {"name": "Cl", "chemical_symbols": ["Cl"], "concentration": [1]}, |
| 48 | + ], |
| 49 | + "remote_site_species": ["Na", "Cl"], |
| 50 | + "remote_dimensions": [1, 1, 1], |
| 51 | + }, |
| 52 | + } |
| 53 | + } |
| 54 | + return json.dumps(entry), json.dumps({"data": {"properties": properties}}) |
| 55 | + |
| 56 | + |
| 57 | +def _mock_http(monkeypatch: pytest.MonkeyPatch, responses: dict[str, str]) -> None: |
| 58 | + real_urlopen = urllib.request.urlopen |
| 59 | + |
| 60 | + def fake_urlopen(url: Any, *, timeout: float | None) -> _Response: |
| 61 | + if url in responses: |
| 62 | + return _Response(responses[url].encode()) |
| 63 | + return real_urlopen(url, timeout=timeout) # type: ignore[return-value] |
| 64 | + |
| 65 | + monkeypatch.setattr(urllib.request, "urlopen", fake_urlopen) |
| 66 | + |
| 67 | + |
| 68 | +def _optimade_urls() -> tuple[str, str]: |
| 69 | + entry_url = "https://example.test/v1/structures/fixture-1" |
| 70 | + return entry_url, "https://example.test/v1/info/structures" |
| 71 | + |
| 72 | + |
| 73 | +def test_fetch_optimade_adapts_quartet_and_supercell(monkeypatch: pytest.MonkeyPatch) -> None: |
| 74 | + entry_url, info_url = _optimade_urls() |
| 75 | + entry, info = _entry_and_info() |
| 76 | + _mock_http(monkeypatch, {entry_url: entry, info_url: info}) |
| 77 | + |
| 78 | + fetched = httk.core.fetch(entry_url) |
| 79 | + |
| 80 | + assert isinstance(fetched, OptimadeStructure) |
| 81 | + assert fetched.cell.basis.to_fractions_approx() == [[Fraction(2), 0, 0], [0, Fraction(3), 0], [0, 0, Fraction(4)]] |
| 82 | + assert fetched.sites.reduced_coords.to_fractions() == [[0, 0, 0], [Fraction(1, 2), Fraction(1, 2), Fraction(1, 2)]] |
| 83 | + assert tuple(species.name for species in fetched.species) == ("Na", "Cl") |
| 84 | + assert fetched.species_at_sites == ("Na", "Cl") |
| 85 | + |
| 86 | + supercell = build_supercell(fetched, 2).structure |
| 87 | + assert len(supercell.sites) == 16 |
| 88 | + assert supercell.cell.basis.to_fractions_approx() == [[4, 0, 0], [0, 6, 0], [0, 0, 8]] |
| 89 | + |
| 90 | + |
| 91 | +def test_fetch_optimade_raw_and_view_preserve_resource(monkeypatch: pytest.MonkeyPatch) -> None: |
| 92 | + entry_url, info_url = _optimade_urls() |
| 93 | + entry, info = _entry_and_info() |
| 94 | + _mock_http(monkeypatch, {entry_url: entry, info_url: info}) |
| 95 | + |
| 96 | + raw = httk.core.fetch(entry_url, raw=True) |
| 97 | + fetched = httk.core.fetch(entry_url) |
| 98 | + |
| 99 | + assert raw["format"] == "optimade-entry" |
| 100 | + assert isinstance(raw["resource"], OptimadeResource) |
| 101 | + assert isinstance(fetched, OptimadeStructure) |
| 102 | + assert UnitcellStructureView(fetched).unwrap() is fetched.resource |
| 103 | + |
| 104 | + |
| 105 | +def test_load_poscar_and_fetch_file_cif_end_to_end(tmp_path: Path) -> None: |
| 106 | + poscar = tmp_path / "POSCAR" |
| 107 | + poscar.write_text( |
| 108 | + """He cell |
| 109 | +1.0 |
| 110 | +2.0 0.0 0.0 |
| 111 | +0.0 3.0 0.0 |
| 112 | +0.0 0.0 4.0 |
| 113 | +He |
| 114 | +1 |
| 115 | +Direct |
| 116 | +0.0 0.0 0.0 |
| 117 | +""", |
| 118 | + encoding="utf-8", |
| 119 | + ) |
| 120 | + loaded = httk.core.load(str(poscar)) |
| 121 | + supercell = build_supercell(loaded, 2).structure |
| 122 | + assert len(supercell.sites) == 8 |
| 123 | + assert supercell.cell.basis.to_fractions_approx() == [[4, 0, 0], [0, 6, 0], [0, 0, 8]] |
| 124 | + |
| 125 | + cif = tmp_path / "same.cif" |
| 126 | + cif.write_text( |
| 127 | + """data_same |
| 128 | +_cell_length_a 2 |
| 129 | +_cell_length_b 3 |
| 130 | +_cell_length_c 4 |
| 131 | +_cell_angle_alpha 90 |
| 132 | +_cell_angle_beta 90 |
| 133 | +_cell_angle_gamma 90 |
| 134 | +_space_group_IT_number 1 |
| 135 | +_space_group_name_H-M_alt 'P 1' |
| 136 | +loop_ |
| 137 | +_space_group_symop_operation_xyz |
| 138 | +'x,y,z' |
| 139 | +loop_ |
| 140 | +_atom_site_label |
| 141 | +_atom_site_type_symbol |
| 142 | +_atom_site_fract_x |
| 143 | +_atom_site_fract_y |
| 144 | +_atom_site_fract_z |
| 145 | +_atom_site_occupancy |
| 146 | +He1 He 0 0 0 1 |
| 147 | +""", |
| 148 | + encoding="utf-8", |
| 149 | + ) |
| 150 | + assert httk.core.fetch(cif.as_uri()) == httk.core.load(str(cif)) |
0 commit comments