Skip to content

Commit

Permalink
test: Adding testing for read_user_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
pesap committed Dec 4, 2024
1 parent 22dfd6d commit 6bb5028
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/r2x/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _load_file(fname: str, loader) -> dict:
with open(fname, "r") as f:
return loader(f)
except FileNotFoundError:
raise IOError(f"File {fname} not found.")
raise FileNotFoundError(f"File {fname} not found.")
except IOError as e:
raise IOError(f"Error reading the file {fname}: {e}")

Expand Down
44 changes: 43 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import json

import pytest
from r2x.utils import haskey
import yaml

from r2x.utils import haskey, read_user_dict


@pytest.mark.utils
Expand All @@ -16,3 +20,41 @@
)
def test_haskey(base_dict, path, expected):
assert haskey(base_dict, path) == expected


def test_read_user_dict(tmp_path):
user_dict = '{"fmap": true}'
parsed_dict = read_user_dict(user_dict)

assert isinstance(parsed_dict, dict)
assert parsed_dict.get("fmap", None) is not None

user_dict = '{"fmap": true : 2}'
with pytest.raises(ValueError):
_ = read_user_dict(user_dict)

sample_data = {"name": "test", "fmap": True, "input_model": "plexos"}

yaml_file = tmp_path / "data.yaml"
with yaml_file.open("w") as f:
yaml.dump(sample_data, f)

assert yaml_file.exists()
parsed_dict = read_user_dict(str(yaml_file))
assert isinstance(parsed_dict, dict)
assert parsed_dict.get("fmap", None) is not None
assert parsed_dict["input_model"] == "plexos"

json_file = tmp_path / "data.json"
with json_file.open("w") as f:
json.dump(sample_data, f, indent=4)

assert json_file.exists()
parsed_dict = read_user_dict(str(json_file))
assert isinstance(parsed_dict, dict)
assert parsed_dict.get("fmap", None) is not None
assert parsed_dict["input_model"] == "plexos"

yaml_file = tmp_path / "missing_file.yaml"
with pytest.raises(FileNotFoundError):
_ = read_user_dict(str(yaml_file))

0 comments on commit 6bb5028

Please sign in to comment.