Skip to content

Commit

Permalink
update: restructure standata tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Oct 29, 2024
1 parent 7e39ebc commit 7c196bd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 61 deletions.
101 changes: 101 additions & 0 deletions tests/py/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import json
import os
from pathlib import Path

Check failure on line 3 in tests/py/unit/test_cli.py

View workflow job for this annotation

GitHub Actions / run-py-linter (3.8.6)

Ruff (F401)

tests/py/unit/test_cli.py:3:21: F401 `pathlib.Path` imported but unused
from unittest.mock import patch

import pytest
import yaml
from mat3ra.standata.build.cli import main

# Test data
SAMPLE_CONFIG = {
"categories": {"dimensionality": ["2D", "3D"], "type": ["metal", "semiconductor"]},
"entities": [
{"filename": "material1.json", "categories": ["2D", "metal"]},
{"filename": "material2.json", "categories": ["3D", "semiconductor"]},
],
}

SAMPLE_ENTITY = {"name": "Test Material", "isNonPeriodic": False, "lattice": {"a": 1.0, "b": 1.0, "c": 1.0}}


@pytest.fixture
def temp_dir(tmp_path):
"""Create a temporary directory with test files."""
# Create config file
config_path = tmp_path / "categories.yml"
with open(config_path, "w") as f:
yaml.dump(SAMPLE_CONFIG, f)

# Create entity files
for entity in SAMPLE_CONFIG["entities"]:
entity_path = tmp_path / entity["filename"]
with open(entity_path, "w") as f:
json.dump(SAMPLE_ENTITY, f)

return tmp_path


def test_create_category_structure(temp_dir):
"""Test creation of category structure."""
# Pass None as destination to avoid typer.Option issue
main(yaml_config=str(temp_dir / "categories.yml"), destination=None)

# Verify the structure was created correctly
categories_root = temp_dir / "by_category"
assert (categories_root / "dimensionality/2D").exists()
assert (categories_root / "dimensionality/3D").exists()
assert (categories_root / "type/metal").exists()
assert (categories_root / "type/semiconductor").exists()


def test_custom_destination(temp_dir):
"""Test creating category structure in custom destination."""
custom_dest = temp_dir / "custom_dest"
custom_dest.mkdir()

# Use explicit keyword arguments
main(yaml_config=str(temp_dir / "categories.yml"), destination=str(custom_dest))

# Verify the structure was created in custom destination
categories_root = custom_dest / "by_category"
assert (categories_root / "dimensionality/2D").exists()
assert (categories_root / "type/metal").exists()


def test_symlink_creation(temp_dir):
"""Test if symlinks are created correctly."""
main(yaml_config=str(temp_dir / "categories.yml"), destination=None)

# Verify symlinks
metal_link = temp_dir / "by_category/type/metal/material1.json"
assert metal_link.exists()
assert metal_link.is_symlink()
assert metal_link.resolve() == (temp_dir / "material1.json").resolve()


@pytest.mark.skipif(os.name == "nt", reason="Symlinks might not work on Windows without admin privileges")
def test_permission_error(temp_dir):
"""Test handling of permission errors during symlink creation."""
with patch("pathlib.Path.symlink_to", side_effect=PermissionError):
with pytest.raises(PermissionError):
main(yaml_config=str(temp_dir / "categories.yml"), destination=None)


def test_nonexistent_config(temp_dir):
"""Test handling of non-existent config file."""
with pytest.raises(FileNotFoundError):
main(yaml_config=str(temp_dir / "nonexistent.yml"), destination=None)


def test_duplicate_run(temp_dir):
"""Test running the command twice (should handle existing symlinks)."""
# Run twice with explicit keyword arguments
main(yaml_config=str(temp_dir / "categories.yml"), destination=None)
main(yaml_config=str(temp_dir / "categories.yml"), destination=None)

# Verify structure is still correct
categories_root = temp_dir / "by_category"
assert (categories_root / "dimensionality/2D").exists()
metal_link = categories_root / "type/metal/material1.json"
assert metal_link.is_symlink()
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -92,64 +92,3 @@ def test_load_invalid_yaml(temp_dir):
assert isinstance(config, StandataConfig)
assert len(config.categories) == 0
assert len(config.entities) == 0


# CLI Integration Tests
@pytest.mark.integration
def test_create_category_structure(temp_dir):
"""Test creation of category structure."""
config = StandataBuilder.build_from_file(temp_dir / "categories.yml")

# Set up the category directory
categories_root = temp_dir / "by_category"
categories_root.mkdir(exist_ok=True)

# Create category directories and symlinks
for entity in config.entities:
categories = config.convert_tags_to_categories_list(*entity.categories)
entity_path = temp_dir / entity.filename

for category in categories:
category_dir = categories_root / category
category_dir.mkdir(parents=True, exist_ok=True)
linked_entity = category_dir / entity.filename
if not linked_entity.exists():
try:
linked_entity.symlink_to(entity_path)
except PermissionError:
pytest.skip("No permission to create symlinks")

# Verify the structure
assert (categories_root / "dimensionality/2D").exists()
assert (categories_root / "dimensionality/3D").exists()
assert (categories_root / "type/metal").exists()
assert (categories_root / "type/semiconductor").exists()


@pytest.mark.integration
def test_symlink_creation(temp_dir):
"""Test symlink creation for materials."""
config = StandataBuilder.build_from_file(temp_dir / "categories.yml")
categories_root = temp_dir / "by_category"
categories_root.mkdir(exist_ok=True)

# Create symlinks for one material
entity = config.entities[0] # First material
categories = config.convert_tags_to_categories_list(*entity.categories)
entity_path = temp_dir / entity.filename

for category in categories:
category_dir = categories_root / category
category_dir.mkdir(parents=True, exist_ok=True)
linked_entity = category_dir / entity.filename
if not linked_entity.exists():
try:
linked_entity.symlink_to(entity_path)
except PermissionError:
pytest.skip("No permission to create symlinks")

# Verify symlinks
metal_link = categories_root / "type/metal/material1.json"
if metal_link.exists():
assert metal_link.is_symlink()
assert metal_link.resolve() == (temp_dir / "material1.json").resolve()

0 comments on commit 7c196bd

Please sign in to comment.