Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Feb 27, 2025
1 parent bccf141 commit 50b5df3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/pymatgen/symmetry/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

import numpy as np
from monty.json import MontyEncoder
from tabulate import tabulate

from pymatgen.core.structure import PeriodicSite, Structure
from pymatgen.core.structure import FileFormats, PeriodicSite, Structure

if TYPE_CHECKING:
from collections.abc import Sequence
Expand All @@ -16,6 +17,7 @@
from typing_extensions import Self

from pymatgen.symmetry.analyzer import SpacegroupOperations
from pymatgen.util.typing import PathLike


class SymmetrizedStructure(Structure):
Expand Down Expand Up @@ -128,6 +130,8 @@ def as_dict(self) -> dict[str, Any]:
"""MSONable dict."""
structure = Structure.from_sites(self.sites)
return {
"@module": type(self).__module__,
"@class": type(self).__name__,
"structure": structure.as_dict(),
"spacegroup": self.spacegroup,
"equivalent_positions": self.site_labels,
Expand All @@ -149,3 +153,11 @@ def from_dict(cls, dct: dict[str, Any]) -> Self:
equivalent_positions=dct["equivalent_positions"],
wyckoff_letters=dct["wyckoff_letters"],
)

def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
"""Use `MontyEncoder` as default JSON encoder."""
filename, fmt = str(filename), cast(FileFormats, fmt.lower())
if fmt == "json":
kwargs.setdefault("cls", MontyEncoder)

return super().to(filename, fmt, **kwargs)
22 changes: 22 additions & 0 deletions tests/symmetry/test_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

from pymatgen.analysis.structure_analyzer import SpacegroupAnalyzer
from pymatgen.core import Lattice, Structure
from pymatgen.util.testing import PymatgenTest


class TestSymmetrizedStructure(PymatgenTest):
def setUp(self):
self.structure = Structure(
lattice=Lattice.cubic(3),
species=("Fe", "Fe"),
coords=((0, 0, 0), (0.5, 0.5, 0.5)),
)

self.symm_structure = SpacegroupAnalyzer(self.structure).get_symmetrized_structure()

def test_as_dict(self):
self.assert_msonable(self.symm_structure)

def test_serialize(self):
self.symm_structure.to(fmt="json")

0 comments on commit 50b5df3

Please sign in to comment.