infrasynth-backend-kit/tests/test_shared/test_results.py
2026-08-28 14:38:47 -05:00

60 lines
1.8 KiB
Python

from infrasynth.shared.results import Result
class TestResultOk:
def test_ok_creates_result_with_value(self):
result = Result.ok(42)
assert result.value == 42
assert result.error is None
assert result.is_ok is True
assert result.is_err is False
def test_ok_with_string(self):
result = Result.ok("hello")
assert result.value == "hello"
assert result.is_ok is True
def test_ok_with_list(self):
result = Result.ok([1, 2, 3])
assert result.value == [1, 2, 3]
assert result.is_ok is True
def test_ok_with_none(self):
result = Result.ok(None)
assert result.value is None
assert result.is_ok is True
class TestResultErr:
def test_err_creates_result_with_error(self):
result = Result.err("something went wrong")
assert result.value is None
assert result.error == "something went wrong"
assert result.is_ok is False
assert result.is_err is True
def test_err_with_dict(self):
result = Result.err({"code": 404, "message": "Not found"})
assert result.error == {"code": 404, "message": "Not found"}
assert result.is_err is True
def test_err_with_exception(self):
exc = ValueError("invalid")
result = Result.err(exc)
assert result.error is exc
assert result.is_err is True
class TestResultTypeIndependence:
def test_ok_and_err_are_different_instances(self):
ok_result = Result.ok(42)
err_result = Result.err("fail")
assert ok_result.is_ok is True
assert err_result.is_ok is False
def test_ok_holds_any_type(self):
assert Result.ok("str").is_ok
assert Result.ok(123).is_ok
assert Result.ok(1.5).is_ok
assert Result.ok({"key": "val"}).is_ok
assert Result.ok([1]).is_ok