1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from decimal import Decimal
import pytest
import typepy
from ._common import EXCEPTION_RESULT, convert_wrapper
nan = float("nan")
inf = float("inf")
class Test_RealNumber:
@pytest.mark.parametrize(
["method", "strict_level", "value", "expected"],
[
["convert", 0, 1, Decimal("1")],
["convert", 0, 1.0, Decimal("1.0")],
["convert", 0, 1.1, Decimal("1.1")],
["convert", 0, "0", Decimal("0")],
["convert", 0, "1.0", Decimal("1.0")],
["convert", 0, "1.1", Decimal("1.1")],
["convert", 0, True, EXCEPTION_RESULT],
["convert", 0, None, EXCEPTION_RESULT],
["convert", 0, inf, EXCEPTION_RESULT],
["convert", 0, nan, EXCEPTION_RESULT],
["convert", 0, "test", EXCEPTION_RESULT],
["convert", 0, "", EXCEPTION_RESULT],
["convert", 1, 1, EXCEPTION_RESULT],
["convert", 1, 1.0, EXCEPTION_RESULT],
["convert", 1, 1.1, Decimal("1.1")],
["convert", 1, "0", EXCEPTION_RESULT],
["convert", 1, "1.0", EXCEPTION_RESULT],
["convert", 1, "1.1", Decimal("1.1")],
["convert", 1, True, EXCEPTION_RESULT],
["convert", 1, None, EXCEPTION_RESULT],
["convert", 1, inf, EXCEPTION_RESULT],
["convert", 1, nan, EXCEPTION_RESULT],
["convert", 1, "test", EXCEPTION_RESULT],
["convert", 1, "", EXCEPTION_RESULT],
["convert", 2, 1, EXCEPTION_RESULT],
["convert", 2, 1.0, EXCEPTION_RESULT],
["convert", 2, 1.1, Decimal("1.1")],
["convert", 2, "0", EXCEPTION_RESULT],
["convert", 2, "1.0", EXCEPTION_RESULT],
["convert", 2, "1.1", EXCEPTION_RESULT],
["convert", 2, True, EXCEPTION_RESULT],
["convert", 2, None, EXCEPTION_RESULT],
["convert", 2, inf, EXCEPTION_RESULT],
["convert", 2, nan, EXCEPTION_RESULT],
["convert", 2, "test", EXCEPTION_RESULT],
["convert", 2, "", EXCEPTION_RESULT],
["try_convert", 0, 1, Decimal("1")],
["try_convert", 0, 1.0, Decimal("1.0")],
["try_convert", 0, 1.1, Decimal("1.1")],
["try_convert", 0, "0", Decimal("0")],
["try_convert", 0, "1.0", Decimal("1.0")],
["try_convert", 0, "1.1", Decimal("1.1")],
["try_convert", 0, True, None],
["try_convert", 0, None, None],
["try_convert", 0, inf, None],
["try_convert", 0, nan, None],
["try_convert", 0, "test", None],
["try_convert", 0, "", None],
["try_convert", 1, 1, None],
["try_convert", 1, 1.0, None],
["try_convert", 1, 1.1, Decimal("1.1")],
["try_convert", 1, "0", None],
["try_convert", 1, "1.0", None],
["try_convert", 1, "1.1", Decimal("1.1")],
["try_convert", 1, True, None],
["try_convert", 1, None, None],
["try_convert", 1, inf, None],
["try_convert", 1, nan, None],
["try_convert", 1, "test", None],
["try_convert", 1, "", None],
["try_convert", 2, 1, None],
["try_convert", 2, 1.0, None],
["try_convert", 2, 1.1, Decimal("1.1")],
["try_convert", 2, "0", None],
["try_convert", 2, "1.0", None],
["try_convert", 2, "1.1", None],
["try_convert", 2, True, None],
["try_convert", 2, None, None],
["try_convert", 2, inf, None],
["try_convert", 2, nan, None],
["try_convert", 2, "test", None],
["try_convert", 2, "", None],
["force_convert", 0, 1, Decimal("1")],
["force_convert", 0, 1.0, Decimal("1.0")],
["force_convert", 0, 1.1, Decimal("1.1")],
["force_convert", 0, "0", Decimal("0")],
["force_convert", 0, "1.0", Decimal("1.0")],
["force_convert", 0, "1.1", Decimal("1.1")],
["force_convert", 0, True, Decimal("1")],
["force_convert", 0, None, EXCEPTION_RESULT],
["force_convert", 0, inf, Decimal("inf")],
["force_convert", 0, nan, Decimal("nan")],
["force_convert", 0, "test", EXCEPTION_RESULT],
["force_convert", 0, "", EXCEPTION_RESULT],
["force_convert", 1, 1, Decimal("1")],
["force_convert", 1, 1.0, Decimal("1.0")],
["force_convert", 1, 1.1, Decimal("1.1")],
["force_convert", 1, "0", Decimal("0")],
["force_convert", 1, "1.0", Decimal("1.0")],
["force_convert", 1, "1.1", Decimal("1.1")],
["force_convert", 1, True, Decimal("1")],
["force_convert", 1, None, EXCEPTION_RESULT],
["force_convert", 1, inf, Decimal("inf")],
["force_convert", 1, nan, Decimal("nan")],
["force_convert", 1, "test", EXCEPTION_RESULT],
["force_convert", 1, "", EXCEPTION_RESULT],
["force_convert", 2, 1, Decimal("1")],
["force_convert", 2, 1.0, Decimal("1.0")],
["force_convert", 2, 1.1, Decimal("1.1")],
["force_convert", 2, "0", Decimal("0")],
["force_convert", 2, "1.0", Decimal("1.0")],
["force_convert", 2, "1.1", Decimal("1.1")],
["force_convert", 2, True, Decimal("1")],
["force_convert", 2, None, EXCEPTION_RESULT],
["force_convert", 2, inf, Decimal("inf")],
["force_convert", 2, nan, Decimal("nan")],
["force_convert", 2, "test", EXCEPTION_RESULT],
["force_convert", 2, "", EXCEPTION_RESULT],
],
)
def test_normal(self, method, strict_level, value, expected):
from typepy import Nan, StrictLevel
actual = convert_wrapper(typepy.RealNumber(value, strict_level), method)
if Nan(expected, strict_level=StrictLevel.MIN).is_type():
assert Nan(actual, strict_level=StrictLevel.MIN).is_type()
else:
assert actual == expected
|