Skip to content

Commit

Permalink
Test number
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 2, 2024
1 parent ff4cc89 commit f109bf9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
81 changes: 80 additions & 1 deletion src/jsonyx/test/test_dumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@

__all__: list[str] = []

from decimal import Decimal
from typing import TYPE_CHECKING

import pytest
from jsonyx.allow import NAN_AND_INFINITY
# pylint: disable-next=W0611
from jsonyx.test import get_json # type: ignore # noqa: F401

if TYPE_CHECKING:
from types import ModuleType


class _BadDecimal(Decimal):
def __str__(self) -> str:
return repr(self)


class _BadFloat(float):
def __str__(self) -> str:
return repr(self)


class _BadInt(int):
def __str__(self) -> str:
return repr(self)


@pytest.mark.parametrize(("obj", "expected"), [
(True, "true"),
(False, "false"),
Expand All @@ -23,5 +40,67 @@
def test_singletons(
json: ModuleType, obj: bool | None, expected: str, # noqa: FBT001
) -> None:
"""Test Python singletons."""
"""Test singletons."""
assert json.dumps(obj, end="") == expected


@pytest.mark.parametrize("num", [0, 1])
@pytest.mark.parametrize("num_type", [_BadDecimal, _BadInt, Decimal, int])
def test_int(
json: ModuleType, num: int, num_type: type[Decimal | int],
) -> None:
"""Test integer."""
assert json.dumps(num_type(num), end="") == repr(num)


@pytest.mark.parametrize("num_type", [_BadDecimal, _BadFloat, Decimal, float])
def test_rational_number(
json: ModuleType, num_type: type[Decimal | float],
) -> None:
"""Test rational number."""
assert json.dumps(num_type("0.0"), end="") == "0.0"


@pytest.mark.parametrize("num", ["NaN", "Infinity", "-Infinity"])
@pytest.mark.parametrize("num_type", [_BadDecimal, _BadFloat, Decimal, float])
def test_nan_and_infinity(
json: ModuleType, num: str, num_type: type[Decimal | float],
) -> None:
"""Test NaN and infinity."""
assert json.dumps(num_type(num), allow=NAN_AND_INFINITY, end="") == num


@pytest.mark.parametrize("num", ["NaN", "Infinity", "-Infinity"])
@pytest.mark.parametrize("num_type", [_BadDecimal, _BadFloat, Decimal, float])
def test_nan_and_infinity_not_allowed(
json: ModuleType, num: str, num_type: type[Decimal | float],
) -> None:
"""Test NaN and infinity if not allowed."""
with pytest.raises(ValueError, match="is not allowed"):
json.dumps(num_type(num))


@pytest.mark.parametrize("num", ["NaN2", "-NaN", "-NaN2"])
@pytest.mark.parametrize("num_type", [_BadDecimal, Decimal])
def test_nan_payload(
json: ModuleType, num: str, num_type: type[Decimal],
) -> None:
"""Test NaN payload."""
assert json.dumps(num_type(num), allow=NAN_AND_INFINITY, end="") == "NaN"


@pytest.mark.parametrize("num", ["NaN2", "-NaN", "-NaN2"])
@pytest.mark.parametrize("num_type", [_BadDecimal, Decimal])
def test_nan_payload_not_allowed(
json: ModuleType, num: str, num_type: type[Decimal],
) -> None:
"""Test NaN payload if not allowed."""
with pytest.raises(ValueError, match="is not allowed"):
json.dumps(num_type(num))


@pytest.mark.parametrize("num_type", [_BadDecimal, Decimal])
def test_signaling_nan(json: ModuleType, num_type: type[Decimal]) -> None:
"""Test signaling NaN."""
with pytest.raises(ValueError, match="is not JSON serializable"):
json.dumps(num_type("sNaN"))
8 changes: 4 additions & 4 deletions src/jsonyx/test/test_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_literal_names(
def test_nan_and_infinity(
json: ModuleType, s: str, use_decimal: bool, # noqa: FBT001
) -> None:
"""Test NaN and infinity with decimal and float."""
"""Test NaN and infinity."""
obj: object = json.loads(
s, allow=NAN_AND_INFINITY, use_decimal=use_decimal,
)
Expand All @@ -99,7 +99,7 @@ def test_nan_and_infinity(
def test_nan_and_infinity_not_allowed(
json: ModuleType, s: str, use_decimal: bool, # noqa: FBT001
) -> None:
"""Test NaN and infinity with decimal and float if not allowed."""
"""Test NaN and infinity if not allowed."""
with pytest.raises(json.JSONSyntaxError) as exc_info:
json.loads(s, use_decimal=use_decimal)

Expand Down Expand Up @@ -142,10 +142,10 @@ def test_int(json: ModuleType, s: str) -> None:
"1.1e1", "-1e1", "-1.1", "-1.1e1",
])
@pytest.mark.parametrize("use_decimal", [True, False])
def test_decimal_and_float(
def test_rational_number(
json: ModuleType, s: str, use_decimal: bool, # noqa: FBT001
) -> None:
"""Test decimal and float."""
"""Test rational number."""
obj: object = json.loads(s, use_decimal=use_decimal)
expected_type: type[Decimal | float] = Decimal if use_decimal else float
assert isinstance(obj, expected_type)
Expand Down

0 comments on commit f109bf9

Please sign in to comment.