Skip to content

Commit

Permalink
update: remove duplication using conftest
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Oct 29, 2024
1 parent 7c196bd commit da74d72
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 74 deletions.
40 changes: 40 additions & 0 deletions tests/py/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import json

import pytest
import yaml

# Shared 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


@pytest.fixture
def builder(temp_dir):
"""Create a StandataBuilder instance."""
from mat3ra.standata.build.builder import StandataBuilder

return StandataBuilder(temp_dir)
36 changes: 0 additions & 36 deletions tests/py/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
import json
import os
from pathlib import Path
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
Expand All @@ -54,7 +22,6 @@ def test_custom_destination(temp_dir):
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
Expand All @@ -67,7 +34,6 @@ 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()
Expand All @@ -90,11 +56,9 @@ def test_nonexistent_config(temp_dir):

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"
Expand Down
38 changes: 0 additions & 38 deletions tests/py/unit/test_standata_build.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
import json
from pathlib import Path

import pytest
import yaml
from mat3ra.standata.base import StandataConfig, StandataEntity
from mat3ra.standata.build.builder import StandataBuilder

# 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


@pytest.fixture
def builder(temp_dir):
"""Create a StandataBuilder instance."""
return StandataBuilder(temp_dir)


# StandataBuilder Tests
def test_builder_initialization(builder):
"""Test StandataBuilder initialization."""
assert isinstance(builder, StandataBuilder)
Expand Down

0 comments on commit da74d72

Please sign in to comment.