Skip to content

Commit

Permalink
simplify logic and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfromearth committed Jan 3, 2025
1 parent d6710a6 commit d3c5d7f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
8 changes: 2 additions & 6 deletions ncompare/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,16 @@ def get_subgroups(node: Union[netCDF4.Dataset, netCDF4.Group, h5py.Group], file_
return []
elif file_type == "hdf5":
return [key for key in node.keys() if isinstance(node[key], h5py.Group)]
elif file_type == "netcdf": # should be "netcdf"
else: # should be "netcdf"
return list(node.groups)
else:
return []


def get_variables(node: Union[netCDF4.Dataset, netCDF4.Group, h5py.Group], file_type: str) -> list:
"""Get a sorted list of variables from a netCDF or HDF5 group."""
if file_type == "hdf5":
return [key for key in node.keys() if isinstance(node[key], h5py.Dataset)]
elif file_type == "netcdf":
else: # should be "netcdf"
return sorted(node.variables)
else:
raise RuntimeError(f"Unsupported file type: {file_type}")


def get_root_dims(file: FileToCompare) -> list:
Expand Down
5 changes: 1 addition & 4 deletions ncompare/utility_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ class FileToCompare:
def __post_init__(self):
# We'll validate the inputs here.
if not isinstance(self.path, (str, Path)):
raise ValueError(f"'path' must be a str or Path, was {type(self.path)}")
raise TypeError(f"'path' must be a str or Path, was {type(self.path)}")
if self.type not in ("netcdf", "hdf5"):
raise ValueError("'type' must be either 'netcdf' or 'hdf5'")

def __str__(self):
return f"path: {self.path} is considered a {self.type} file"


class SummaryDifferencesDict(TypedDict):
"""Represents the number and type of differences between two files."""
Expand Down
11 changes: 10 additions & 1 deletion tests/test_path_and_string_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

import pytest

from ncompare.path_and_string_operations import coerce_to_str, ensure_valid_path_exists
from ncompare.path_and_string_operations import (
coerce_to_str,
ensure_valid_path_exists,
validate_file_type,
)


def test_make_valid_path_with_simple_invalid_str_path():
Expand All @@ -50,6 +54,11 @@ def test_make_valid_path_from_Path_in_repo():
assert isinstance(returnval, Path)


def test_validate_file_type():
with pytest.raises(TypeError):
validate_file_type(Path(__file__))


def test_error_from_wrong_path_type():
with pytest.raises(TypeError):
ensure_valid_path_exists((0, 1))
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utility_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pathlib import Path

import pytest

from ncompare.utility_types import FileToCompare


def test_FileToCompare():
with pytest.raises(TypeError):
assert FileToCompare(path=123, type="netcdf")

with pytest.raises(ValueError):
assert FileToCompare(path=Path(__file__), type="beebop_type")

0 comments on commit d3c5d7f

Please sign in to comment.