bfbd8d6b8b
Feature/daip2-7-crosswalk * add crosswalk framework * remove erroneous print statements * fix mypy error, format to black and isort * add README.md * Merged main into feature/daip2-7-crosswalk Approved-by: Katon Minhas
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import json
|
|
|
|
import pandas as pd
|
|
import pytest
|
|
|
|
from crosswalk_utils import CrosswalkBuilder, apply_crosswalk
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_excel_file(tmp_path):
|
|
data = {"from_col": ["A", "B", "C"], "to_col": ["one", "two", "three"]}
|
|
df = pd.DataFrame(data)
|
|
file_path = tmp_path / "sample.xlsx"
|
|
df.to_excel(file_path, index=False)
|
|
return file_path
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_json_file(tmp_path):
|
|
data = {
|
|
"metadata": {"from_col": "from_col", "to_col": "to_col"},
|
|
"mapping": {"A": "1", "B": "2", "C": "3"},
|
|
}
|
|
file_path = tmp_path / "sample.json"
|
|
with open(file_path, "w") as f:
|
|
json.dump(data, f)
|
|
return file_path
|
|
|
|
|
|
def test_from_excel(sample_excel_file):
|
|
builder = CrosswalkBuilder()
|
|
builder.from_excel(sample_excel_file, "from_col", "to_col")
|
|
assert builder.mapping == {"A": "one", "B": "two", "C": "three"}
|
|
assert builder.metadata == {"from_col": "from_col", "to_col": "to_col"}
|
|
|
|
|
|
def test_from_excel_missing_column(sample_excel_file):
|
|
builder = CrosswalkBuilder()
|
|
with pytest.raises(ValueError, match="Column 'nonexistent' not found"):
|
|
builder.from_excel(sample_excel_file, "nonexistent", "to_col")
|
|
|
|
|
|
def test_from_dict():
|
|
mapping = {"A": "1", "B": "2", "C": "3"}
|
|
builder = CrosswalkBuilder()
|
|
builder.from_dict(mapping)
|
|
assert builder.mapping == mapping
|
|
assert builder.metadata == {"from_col": None, "to_col": None}
|
|
|
|
|
|
def test_from_json(sample_json_file):
|
|
builder = CrosswalkBuilder()
|
|
builder.from_json(sample_json_file)
|
|
assert builder.mapping == {"A": "1", "B": "2", "C": "3"}
|
|
assert builder.metadata == {"from_col": "from_col", "to_col": "to_col"}
|
|
|
|
|
|
def test_json_roundtrip(tmp_path):
|
|
# Create and save a crosswalk
|
|
original = CrosswalkBuilder()
|
|
original.from_dict({"A": "1"}, "source", "target")
|
|
json_path = tmp_path / "test.json"
|
|
original.save(json_path)
|
|
|
|
# Load it back
|
|
loaded = CrosswalkBuilder().from_json(json_path)
|
|
|
|
# Check everything matches
|
|
assert loaded.mapping == original.mapping
|
|
assert loaded.metadata == original.metadata
|
|
|
|
|
|
def test_save(tmp_path):
|
|
mapping = {"A": "1", "B": "2", "C": "3"}
|
|
builder = CrosswalkBuilder()
|
|
builder.from_dict(mapping, "from_col", "to_col")
|
|
output_path = tmp_path / "output.json"
|
|
builder.save(output_path)
|
|
|
|
with open(output_path, "r") as f:
|
|
data = json.load(f)
|
|
|
|
assert data["mapping"] == mapping
|
|
assert data["metadata"] == {"from_col": "from_col", "to_col": "to_col"}
|
|
|
|
|
|
def test_apply_crosswalk():
|
|
mapping = {"A": "1", "B": "2", "C": "3"}
|
|
assert apply_crosswalk("A", mapping) == "1"
|
|
assert apply_crosswalk("D", mapping) == "D"
|
|
assert apply_crosswalk("D", mapping, default="0") == "0"
|