Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assert_str_content_equal, add tests for testing utils #4205

Merged
merged 19 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix assert_str_content_equal and add test
  • Loading branch information
DanielYang59 committed Nov 29, 2024
commit ce750faab363a83e334d218397bc222f3207fb5d
11 changes: 7 additions & 4 deletions src/pymatgen/util/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
from pymatgen.util.typing import PathLike

MODULE_DIR: Path = Path(__file__).absolute().parent

STRUCTURES_DIR: Path = MODULE_DIR / ".." / "structures"

TEST_FILES_DIR: Path = Path(SETTINGS.get("PMG_TEST_FILES_DIR", f"{ROOT}/../tests/files"))
VASP_IN_DIR: str = f"{TEST_FILES_DIR}/io/vasp/inputs"
VASP_OUT_DIR: str = f"{TEST_FILES_DIR}/io/vasp/outputs"
Expand Down Expand Up @@ -72,18 +74,19 @@ def get_structure(cls, name: str) -> Structure:
return struct.copy()

@staticmethod
def assert_str_content_equal(actual: str, expected: str) -> bool:
def assert_str_content_equal(actual: str, expected: str) -> None:
"""Test if two strings are equal, ignoring whitespaces.

Args:
actual (str): The string to be checked.
expected (str): The reference string.

Returns:
bool
Raises:
AssertionError: When two strings are not equal.
"""
strip_whitespace = {ord(c): None for c in string.whitespace}
return actual.translate(strip_whitespace) == expected.translate(strip_whitespace)
if actual.translate(strip_whitespace) != expected.translate(strip_whitespace):
raise AssertionError("Strings are not equal (whitespaces ignored).")

def serialize_with_pickle(
self,
Expand Down
68 changes: 68 additions & 0 deletions tests/util/test_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import annotations

import os

import pytest

from pymatgen.core import Structure
from pymatgen.util.testing import (
FAKE_POTCAR_DIR,
MODULE_DIR,
STRUCTURES_DIR,
TEST_FILES_DIR,
VASP_IN_DIR,
VASP_OUT_DIR,
PymatgenTest,
)


def test_paths():
"""Test paths provided in testing util."""
assert MODULE_DIR.is_dir()

assert STRUCTURES_DIR.is_dir()
assert [f for f in os.listdir(STRUCTURES_DIR) if f.endswith(".json")]

assert TEST_FILES_DIR.is_dir()
assert os.path.isdir(VASP_IN_DIR)
assert os.path.isdir(VASP_OUT_DIR)

assert os.path.isdir(FAKE_POTCAR_DIR)
assert any(f.startswith("POTCAR") for _root, _dir, files in os.walk(FAKE_POTCAR_DIR) for f in files)


class TestPymatgenTest:
def test_tmp_dir(self):
pass

def test_get_structure(self):
structure = PymatgenTest.get_structure("LiFePO4")
assert isinstance(structure, Structure)

# TODO: need to check non-existent structure exception

def test_assert_str_content_equal(self):
# Cases where strings are equal
PymatgenTest.assert_str_content_equal("hello world", "hello world")
PymatgenTest.assert_str_content_equal(" hello world ", "hello world")
PymatgenTest.assert_str_content_equal("\nhello\tworld\n", "hello world")

# Test whitespace handling
PymatgenTest.assert_str_content_equal("", "")
PymatgenTest.assert_str_content_equal(" ", "")
PymatgenTest.assert_str_content_equal("hello\n", "hello")
PymatgenTest.assert_str_content_equal("hello\r\n", "hello")
PymatgenTest.assert_str_content_equal("hello\t", "hello")

# Cases where strings are not equal
with pytest.raises(AssertionError, match="Strings are not equal"):
PymatgenTest.assert_str_content_equal("hello world", "hello_world")

with pytest.raises(AssertionError, match="Strings are not equal"):
PymatgenTest.assert_str_content_equal("hello", "hello world")

def test_serialize_with_pickle(self):
pass

def test_assert_msonable(self):
pass
Loading