Skip to content

Commit

Permalink
Add byte_count property to ValidationError class
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Aug 12, 2023
1 parent 1c33ee7 commit 6cf1b67
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions pathvalidate/_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def validate(self, value: PathType) -> None:
ErrorAttrKey.REASON: ErrorReason.INVALID_LENGTH,
ErrorAttrKey.PLATFORM: self.platform,
ErrorAttrKey.FS_ENCODING: self._fs_encoding,
ErrorAttrKey.BYTE_COUNT: byte_ct,
}
if byte_ct > self.max_len:
raise ValidationError(
Expand Down
1 change: 1 addition & 0 deletions pathvalidate/_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def validate(self, value: PathType) -> None:
ErrorAttrKey.REASON: ErrorReason.INVALID_LENGTH,
ErrorAttrKey.PLATFORM: self.platform,
ErrorAttrKey.FS_ENCODING: self._fs_encoding,
ErrorAttrKey.BYTE_COUNT: byte_ct,
}

if byte_ct > self.max_len:
Expand Down
11 changes: 11 additions & 0 deletions pathvalidate/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def _to_error_code(code: int) -> str:


class ErrorAttrKey:
BYTE_COUNT = "byte_count"
DESCRIPTION = "description"
FS_ENCODING = "fs_encoding"
PLATFORM = "platform"
Expand Down Expand Up @@ -122,11 +123,17 @@ def fs_encoding(self) -> Optional[str]:
"""Optional[str]: File system encoding."""
return self.__fs_encoding

@property
def byte_count(self) -> Optional[int]:
"""Optional[int]: Byte count of the path."""
return self.__byte_count

def __init__(self, *args, **kwargs) -> None: # type: ignore
if ErrorAttrKey.REASON not in kwargs:
raise ValueError(f"{ErrorAttrKey.REASON} must be specified")

self.__reason: ErrorReason = kwargs.pop(ErrorAttrKey.REASON)
self.__byte_count: Optional[int] = kwargs.pop(ErrorAttrKey.BYTE_COUNT, None)
self.__platform: Optional[Platform] = kwargs.pop(ErrorAttrKey.PLATFORM, None)
self.__description: Optional[str] = kwargs.pop(ErrorAttrKey.DESCRIPTION, None)
self.__reserved_name: str = kwargs.pop(ErrorAttrKey.RESERVED_NAME, "")
Expand Down Expand Up @@ -157,6 +164,8 @@ def as_slog(self) -> Dict[str, str]:
slog[ErrorAttrKey.REUSABLE_NAME] = str(self.__reusable_name)
if self.__fs_encoding:
slog[ErrorAttrKey.FS_ENCODING] = self.__fs_encoding
if self.__byte_count:
slog[ErrorAttrKey.BYTE_COUNT] = str(self.__byte_count)

return slog

Expand All @@ -175,6 +184,8 @@ def __str__(self) -> str:
item_list.append(f"{ErrorAttrKey.REUSABLE_NAME}={self.reusable_name}")
if self.__fs_encoding:
item_list.append(f"{ErrorAttrKey.FS_ENCODING}={self.__fs_encoding}")
if self.__byte_count is not None:
item_list.append(f"{ErrorAttrKey.BYTE_COUNT}={self.__byte_count:,d}")

if item_list:
header += ": "
Expand Down
22 changes: 15 additions & 7 deletions test/test_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ def test_min_len(self, value, min_len, expected):
if expected is None:
validate_filename(value, min_len=min_len)
assert is_valid_filename(value, min_len=min_len)
else:
with pytest.raises(ValidationError) as e:
validate_filename(value, min_len=min_len)
assert e.value.reason == expected
return

with pytest.raises(ValidationError) as e:
validate_filename(value, min_len=min_len)
assert e.value.reason == expected
assert e.value.fs_encoding
assert e.value.byte_count > 0

@pytest.mark.parametrize(
["value", "platform", "max_len", "expected"],
Expand All @@ -212,6 +215,8 @@ def test_max_len(self, value, platform, max_len, expected):
with pytest.raises(ValidationError) as e:
validate_filename(value, platform=platform, max_len=max_len)
assert e.value.reason == expected
assert e.value.fs_encoding
assert e.value.byte_count > 0

@pytest.mark.parametrize(
["value", "platform", "fs_encoding", "max_len", "expected"],
Expand All @@ -237,6 +242,8 @@ def test_max_len_fs_encoding(self, value, platform, fs_encoding, max_len, expect
with pytest.raises(ValidationError) as e:
validate_filename(value, **kwargs)
assert e.value.reason == expected
assert e.value.fs_encoding
assert e.value.byte_count > 0

@pytest.mark.parametrize(
["value", "min_len", "max_len", "expected"],
Expand All @@ -252,9 +259,10 @@ def test_minmax_len(self, value, min_len, max_len, expected):
if expected is None:
validate_filename(value, min_len=min_len, max_len=max_len)
assert is_valid_filename(value, min_len=min_len, max_len=max_len)
else:
with pytest.raises(expected):
validate_filename(value, min_len=min_len, max_len=max_len)
return

with pytest.raises(expected):
validate_filename(value, min_len=min_len, max_len=max_len)

@pytest.mark.skipif(not is_faker_installed(), reason="requires faker")
@pytest.mark.parametrize(["locale"], [[None], ["ja_JP"]])
Expand Down
6 changes: 6 additions & 0 deletions test/test_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ def test_normal_min_len(self, value, min_len, expected):
with pytest.raises(ValidationError) as e:
validate_filepath(value, min_len=min_len)
assert e.value.reason == expected
assert e.value.fs_encoding
assert e.value.byte_count > 0

@pytest.mark.parametrize(
["value", "platform", "max_len", "expected"],
Expand Down Expand Up @@ -211,6 +213,8 @@ def test_normal_max_len(self, value, platform, max_len, expected):
with pytest.raises(ValidationError) as e:
validate_filepath(value, **kwargs)
assert e.value.reason == ErrorReason.INVALID_LENGTH
assert e.value.fs_encoding
assert e.value.byte_count > 0

@pytest.mark.parametrize(
["value", "platform", "fs_encoding", "max_len", "expected"],
Expand All @@ -236,6 +240,8 @@ def test_max_len_fs_encoding(self, value, platform, fs_encoding, max_len, expect
with pytest.raises(ValidationError) as e:
validate_filepath(value, **kwargs)
assert e.value.reason == expected
assert e.value.fs_encoding
assert e.value.byte_count > 0

@pytest.mark.parametrize(
["value", "min_len", "max_len", "expected"],
Expand Down

0 comments on commit 6cf1b67

Please sign in to comment.