Skip to content

Commit

Permalink
Add assertions for explicit path comparison (#147)
Browse files Browse the repository at this point in the history
Added some assertions  to prevent regressions for explicit path comparisons.

Bonus: improve typing of tests.
  • Loading branch information
twiggler authored Oct 10, 2024
1 parent 62aff0f commit c17dc6b
Showing 1 changed file with 55 additions and 35 deletions.
90 changes: 55 additions & 35 deletions tests/test_fieldtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import posixpath
import types
from datetime import datetime, timedelta, timezone
from typing import Callable

import pytest

Expand All @@ -25,6 +26,7 @@
fieldtype_for_value,
net,
posix_command,
posix_path,
uri,
windows_command,
windows_path,
Expand All @@ -42,7 +44,7 @@
UINT16_MAX = (1 << 16) - 1


def test_uint16():
def test_uint16() -> None:
desc = RecordDescriptor(
"test/uint16",
[
Expand All @@ -66,7 +68,7 @@ def test_uint16():
desc.recordType(UINT128_MAX)


def test_uint32():
def test_uint32() -> None:
TestRecord = RecordDescriptor(
"test/uint32",
[
Expand All @@ -91,7 +93,7 @@ def test_uint32():
TestRecord(UINT128_MAX)


def test_net_ipv4_address():
def test_net_ipv4_address() -> None:
TestRecord = RecordDescriptor(
"test/net/ipv4/address",
[
Expand Down Expand Up @@ -119,7 +121,7 @@ def test_net_ipv4_address():
assert r.ip is None


def test_net_ipv4_subnet():
def test_net_ipv4_subnet() -> None:
TestRecord = RecordDescriptor(
"test/net/ipv4/subnet",
[
Expand Down Expand Up @@ -162,7 +164,7 @@ def test_net_ipv4_subnet():
excinfo.match(r"Not a valid subnet '192\.168\.0\.106/28', did you mean '192\.168\.0\.96/28' ?")


def test_bytes():
def test_bytes() -> None:
TestRecord = RecordDescriptor(
"test/string",
[
Expand Down Expand Up @@ -194,7 +196,7 @@ def test_bytes():
assert r.body == b"HTTP/1.1 500 Error\r\n\r\nError"


def test_string():
def test_string() -> None:
TestRecord = RecordDescriptor(
"test/string",
[
Expand All @@ -217,7 +219,7 @@ def test_string():
assert r.name == "Ré\udceamy"


def test_wstring():
def test_wstring() -> None:
# Behaves the same as test/string, only available for backwards compatibility purposes
TestRecord = RecordDescriptor(
"test/wstring",
Expand All @@ -230,7 +232,7 @@ def test_wstring():
assert r.name == "Fox-IT"


def test_typedlist():
def test_typedlist() -> None:
TestRecord = RecordDescriptor(
"test/typedlist",
[
Expand Down Expand Up @@ -261,7 +263,7 @@ def test_typedlist():
r = TestRecord(uint32_value=["a", "b", "c"])


def test_stringlist():
def test_stringlist() -> None:
TestRecord = RecordDescriptor(
"test/string",
[
Expand All @@ -277,7 +279,7 @@ def test_stringlist():
assert r.value[0]


def test_dictlist():
def test_dictlist() -> None:
TestRecord = RecordDescriptor(
"test/dictlist",
[
Expand All @@ -294,7 +296,7 @@ def test_dictlist():
assert r.hits[1]["b"] == 4


def test_boolean():
def test_boolean() -> None:
TestRecord = RecordDescriptor(
"test/boolean",
[
Expand Down Expand Up @@ -324,7 +326,7 @@ def test_boolean():
r = TestRecord("True", "False")


def test_float():
def test_float() -> None:
TestRecord = RecordDescriptor(
"test/float",
[
Expand Down Expand Up @@ -353,7 +355,7 @@ def test_float():
r = TestRecord("abc")


def test_uri_type():
def test_uri_type() -> None:
TestRecord = RecordDescriptor(
"test/uri",
[
Expand Down Expand Up @@ -396,7 +398,7 @@ def test_uri_type():
assert r.path.dirname == "/usr/local/bin"


def test_datetime():
def test_datetime() -> None:
TestRecord = RecordDescriptor(
"test/datetime",
[
Expand Down Expand Up @@ -452,7 +454,7 @@ def test_datetime():
("2006-11-10T14:29:55.585192699999999-07:00", datetime(2006, 11, 10, 21, 29, 55, 585192, tzinfo=UTC)),
],
)
def test_datetime_formats(tmp_path, value, expected_dt):
def test_datetime_formats(tmp_path: pathlib.Path, value: str, expected_dt: datetime) -> None:
TestRecord = RecordDescriptor(
"test/datetime",
[
Expand All @@ -473,7 +475,7 @@ def test_datetime_formats(tmp_path, value, expected_dt):
assert record.dt == expected_dt


def test_digest():
def test_digest() -> None:
TestRecord = RecordDescriptor(
"test/digest",
[
Expand Down Expand Up @@ -530,7 +532,7 @@ def test_digest():
excinfo.match(r".*Invalid MD5.*")


def custom_pure_path(sep, altsep):
def custom_pure_path(sep: str, altsep: str) -> pathlib.PurePath:
# Starting from Python 3.12, pathlib._Flavours are removed as you can
# now properly subclass pathlib.Path
# The flavour property of Path's is replaced by a link to e.g.
Expand Down Expand Up @@ -572,7 +574,7 @@ class PureCustomPath(pathlib.PurePath):
("/foo/bar", False),
],
)
def test__is_posixlike_path(path_, is_posix):
def test__is_posixlike_path(path_: pathlib.PurePath | str, is_posix: bool) -> None:
assert _is_posixlike_path(path_) == is_posix


Expand All @@ -587,11 +589,11 @@ def test__is_posixlike_path(path_, is_posix):
("/foo/bar", False),
],
)
def test__is_windowslike_path(path_, is_windows):
def test__is_windowslike_path(path_: pathlib.PurePath, is_windows: bool) -> None:
assert _is_windowslike_path(path_) == is_windows


def test_path():
def test_path() -> None:
TestRecord = RecordDescriptor(
"test/path",
[
Expand Down Expand Up @@ -688,7 +690,9 @@ def test_path():
),
],
)
def test_path_multiple_parts(path_parts, expected_instance):
def test_path_multiple_parts(
path_parts: tuple[str | pathlib.PurePath, ...], expected_instance: type[pathlib.PurePath]
) -> None:
assert isinstance(flow.record.fieldtypes.path(*path_parts), expected_instance)


Expand All @@ -708,7 +712,7 @@ def test_path_multiple_parts(path_parts, expected_instance):
("user/.bash_history", "user/.bash_history"),
],
)
def test_path_posix(path_initializer, path, expected_repr):
def test_path_posix(path_initializer: Callable[[str], pathlib.PurePath], path: str, expected_repr: str) -> None:
TestRecord = RecordDescriptor(
"test/path",
[
Expand Down Expand Up @@ -754,7 +758,9 @@ def test_path_posix(path_initializer, path, expected_repr):
("c:\\my'quotes\".txt", "'c:\\my\\'quotes\".txt'", "c:\\my'quotes\".txt"),
],
)
def test_path_windows(path_initializer, path, expected_repr, expected_str):
def test_path_windows(
path_initializer: Callable[[str], pathlib.PurePath], path: str, expected_repr: str, expected_str: str
) -> None:
TestRecord = RecordDescriptor(
"test/path",
[
Expand All @@ -767,16 +773,28 @@ def test_path_windows(path_initializer, path, expected_repr, expected_str):
assert str(record.path) == expected_str


def test_windows_path_eq():
def test_windows_path_eq() -> None:
path = windows_path("c:\\windows\\test.exe")
assert path == "c:\\windows\\test.exe"
assert path == "c:/windows/test.exe"
assert path == "c:/windows\\test.exe"
assert path == "c:\\WINDOWS\\tEsT.ExE"
assert path != "c:/windows\\test2.exe"
assert path == windows_path("c:\\windows\\test.exe")
assert path != posix_path("c:\\windows\\test.exe")


def test_fieldtype_for_value():
def test_posix_path_eq() -> None:
path = posix_path("/usr/local/bin/test")
assert path == "/usr/local/bin/test"
assert path != "\\usr\\local\\bin\\test"
assert path != "/usr/local/bin/test2"
assert path != "/uSr/lOcAl/bIn/tEst"
assert path == posix_path("/usr/local/bin/test")
assert path != windows_path("/usr/local/bin/test2")


def test_fieldtype_for_value() -> None:
assert fieldtype_for_value(True) == "boolean"
assert fieldtype_for_value(False) == "boolean"
assert fieldtype_for_value(1337) == "varint"
Expand All @@ -790,7 +808,7 @@ def test_fieldtype_for_value():
assert fieldtype_for_value(pathlib.PurePosixPath("/foo/bar.py")) == "path"


def test_dynamic():
def test_dynamic() -> None:
TestRecord = RecordDescriptor(
"test/dynamic",
[
Expand Down Expand Up @@ -852,7 +870,7 @@ def test_dynamic():
),
],
)
def test_format_defang(record_type, value, expected):
def test_format_defang(record_type: str, value: str, expected: str) -> None:
TestRecord = RecordDescriptor(
"test/format/defang",
[
Expand All @@ -877,7 +895,7 @@ def test_format_defang(record_type, value, expected):
("x", b"", ""),
],
)
def test_format_hex(spec, value, expected):
def test_format_hex(spec: str, value: bytes, expected: str) -> None:
TestRecord = RecordDescriptor(
"test/format/hex",
[
Expand Down Expand Up @@ -907,7 +925,9 @@ def test_format_hex(spec, value, expected):
(b"hello \xa7 world", "ignore", "hello world"),
],
)
def test_string_serialization(tmp_path, filename, str_bytes, unicode_errors, expected_str):
def test_string_serialization(
tmp_path: pathlib.Path, filename: str, str_bytes: bytes, unicode_errors: str, expected_str: str
) -> None:
TestRecord = RecordDescriptor(
"test/record",
[
Expand All @@ -929,15 +949,15 @@ def test_string_serialization(tmp_path, filename, str_bytes, unicode_errors, exp
assert record.str_value == expected_str


def test_datetime_strip_nanoseconds():
def test_datetime_strip_nanoseconds() -> None:
d1 = dt("1984-01-01T08:10:12.123456789Z")
d2 = dt("1984-01-01T08:10:12.123456Z")
assert isinstance(d1, dt)
assert isinstance(d2, dt)
assert d1 == d2


def test_datetime_handle_nanoseconds_without_timezone():
def test_datetime_handle_nanoseconds_without_timezone() -> None:
d1 = dt("2006-11-10T14:29:55.5851926")
d2 = dt("2006-11-10T14:29:55")
assert isinstance(d1, dt)
Expand All @@ -957,7 +977,7 @@ def test_datetime_handle_nanoseconds_without_timezone():
"out.jsonl",
],
)
def test_datetime_timezone_aware(tmp_path, record_filename):
def test_datetime_timezone_aware(tmp_path: pathlib.Path, record_filename: str) -> None:
TestRecord = RecordDescriptor(
"test/tz",
[
Expand All @@ -981,7 +1001,7 @@ def test_datetime_timezone_aware(tmp_path, record_filename):
assert record._generated.tzinfo == UTC


def test_datetime_comparisions():
def test_datetime_comparisions() -> None:
with pytest.raises(TypeError, match=".* compare .*naive"):
assert dt("2023-01-01") > datetime(2022, 1, 1)

Expand Down Expand Up @@ -1140,7 +1160,7 @@ def test_command_failed() -> None:
fieldtypes.path,
],
)
def test_empty_path(path_cls) -> None:
def test_empty_path(path_cls: type[pathlib.PurePath]) -> None:
# initialize with empty string
p1 = path_cls("")
assert p1 == ""
Expand Down Expand Up @@ -1181,7 +1201,7 @@ def test_record_empty_path() -> None:
assert repr(r) == "<test/path value=''>"


def test_empty_path_serialization(tmp_path) -> None:
def test_empty_path_serialization(tmp_path: pathlib.Path) -> None:
TestRecord = RecordDescriptor(
"test/path",
[
Expand Down

0 comments on commit c17dc6b

Please sign in to comment.