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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
# typedload
# Copyright (C) 2018-2023 Salvo "LtWorf" Tomaselli
#
# typedload is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
from enum import Enum
from ipaddress import IPv4Address, IPv4Network, IPv4Interface, IPv6Address, IPv6Network, IPv6Interface
from pathlib import Path
import re
from typing import Dict, List, NamedTuple, Optional, Set, Tuple, Union
import unittest
from uuid import UUID
from typedload import datadumper, dump, load
class EnumA(Enum):
A: int = 1
B: str = '2'
C: Tuple[int, int] = (1, 2)
class NamedA(NamedTuple):
a: int
b: str
c: str = 'no'
class TestDumpLoad(unittest.TestCase):
def test_enum(self):
assert load(dump(EnumA.C), EnumA) == EnumA.C
class TestLegacyDump(unittest.TestCase):
def test_dump(self):
A = NamedTuple('A',[('a', int), ('b', str)])
assert dump(A(1, '12')) == {'a': 1, 'b': '12'}
class TestStrconstructed(unittest.TestCase):
def test_dump_strconstructed(self):
dumper = datadumper.Dumper()
class Q:
def __str__(self):
return '42'
dumper.strconstructed.add(Q)
assert dumper.dump(Q()) == '42'
class TestBasicDump(unittest.TestCase):
def test_dump_namedtuple(self):
dumper = datadumper.Dumper()
assert dumper.dump(NamedA(1, 'a')) == {'a': 1, 'b': 'a'}
assert dumper.dump(NamedA(1, 'a', 'yes')) == {'a': 1, 'b': 'a', 'c': 'yes'}
dumper.hidedefault = False
assert dumper.dump(NamedA(1, 'a')) == {'a': 1, 'b': 'a', 'c': 'no'}
def test_dump_dict(self):
dumper = datadumper.Dumper()
assert dumper.dump({EnumA.B: 'ciao'}) == {'2': 'ciao'}
def test_dump_set(self):
dumper = datadumper.Dumper()
assert dumper.dump(set(range(3))) == [0, 1, 2]
assert dumper.dump(frozenset(range(3))) == [0, 1, 2]
def test_dump_enums(self):
dumper = datadumper.Dumper()
assert dumper.dump(EnumA.A) == 1
assert dumper.dump(EnumA.B) == '2'
assert dumper.dump(EnumA.C) == [1, 2]
def test_dump_iterables(self):
dumper = datadumper.Dumper()
assert dumper.dump([1]) == [1]
assert dumper.dump((1, 2)) == [1, 2]
assert dumper.dump([(1, 1), (0, 0)]) == [[1, 1], [0, 0]]
assert dumper.dump({1, 2}) == [1, 2]
def test_basic_types(self):
# Casting enabled, by default
dumper = datadumper.Dumper()
assert dumper.dump(1) == 1
assert dumper.dump('1') == '1'
assert dumper.dump(None) == None
dumper.basictypes = {int, str}
assert dumper.dump('1') == '1'
assert dumper.dump(1) == 1
with self.assertRaises(ValueError):
assert dumper.dump(None) == None
assert dumper.dump(True) == True
class TestHandlersDumper(unittest.TestCase):
def test_custom_handler(self):
class Q:
def __eq__(self, other):
return isinstance(other, Q)
dumper = datadumper.Dumper()
dumper.handlers.append((
lambda v: isinstance(v, Q),
lambda l, v: 12
))
assert dumper.dump(Q()) == 12
def test_broken_handler(self):
dumper = datadumper.Dumper()
dumper.handlers.insert(0, (lambda v: 'a' + v is None, lambda l, v: None))
with self.assertRaises(TypeError):
dumper.dump(1)
dumper.raiseconditionerrors = False
assert dumper.dump(1) == 1
def test_replace_handler(self):
dumper = datadumper.Dumper()
index = dumper.index([])
dumper.handlers[index] = (dumper.handlers[index][0], lambda *args: 3)
assert dumper.dump([11]) == 3
class TestDumper(unittest.TestCase):
def test_kwargs(self):
with self.assertRaises(ValueError):
dump(1, handlers=[])
class TestDumpCommonTypes(unittest.TestCase):
def test_path(self):
assert dump(Path('/')) == '/'
def test_ipaddress(self):
assert dump(IPv4Address('10.10.10.1')) == '10.10.10.1'
assert dump(IPv4Network('10.10.10.0/24')) == '10.10.10.0/24'
assert dump(IPv4Interface('10.10.10.1/24')) == '10.10.10.1/24'
assert dump(IPv6Address('fe80::123')) == 'fe80::123'
assert dump(IPv6Network('fe80::/64')) == 'fe80::/64'
assert dump(IPv6Interface('fe80::123/64')) == 'fe80::123/64'
def test_uuid(self):
assert dump(UUID('631b09cb-016e-11ef-97ce-000000000001')) == '631b09cb-016e-11ef-97ce-000000000001'
def test_pattern(self):
assert dump(re.compile(r'[bc](at|ot)\d+')) == r'[bc](at|ot)\d+'
assert dump(re.compile(br'[bc](at|ot)\d+')) == br'[bc](at|ot)\d+'
|