def test_removal(self):

        loader = dataloader.Loader()
        assert loader.load(3, int) == 3
        loader = dataloader.Loader()
        loader.handlers.pop(loader.index(int))
        with self.assertRaises(TypeError):
            loader.load(3, int)
 def test_broken_handler(self):
     loader = dataloader.Loader()
     loader.handlers.insert(0, (lambda t: 33 + t is None, lambda l, v, t: None))
     with self.assertRaises(TypeError):
         loader.load(1, int)
     loader.raiseconditionerrors = False
     assert loader.load(1, int) == 1
 def test_extra_basic(self):
     # Add more basic types
     loader = dataloader.Loader()
     with self.assertRaises(TypeError):
         assert loader.load(b'ciao', bytes) == b'ciao'
     loader.basictypes.add(bytes)
     assert loader.load(b'ciao', bytes) == b'ciao'
    def test_disable(self):
        class A(NamedTuple):
            i: 'int'

        loader = dataloader.Loader(frefs=None)
        with self.assertRaises(Exception):
            loader.load(3, A)
 def test_typevalue(self):
     loader = dataloader.Loader()
     try:
         loader.load([1, 2, 3, 'q'], List[int])
     except Exception as e:
         assert e.value == 'q'
         assert e.type_ == int
    def test_fail(self):
        class A(NamedTuple):
            a: int
            q: str

        loader = dataloader.Loader()
        with self.assertRaises(ValueError):
            loader.load({'a': 3}, A)
 def test_dict_is_not_list(self):
     loader = dataloader.Loader()
     with self.assertRaises(exceptions.TypedloadTypeError):
         loader.load({1: 1}, List[int])
     with self.assertRaises(exceptions.TypedloadTypeError):
         loader.load({1: 1}, Tuple[int, ...])
     with self.assertRaises(exceptions.TypedloadTypeError):
         loader.load({1: 1}, Set[int])
    def test_known_refs(self):
        class Node(NamedTuple):
            value: int = 1
            next: Optional['Node'] = None

        l = {'next': {}, 'value': 12}
        loader = dataloader.Loader()
        assert loader.load(l, Node) == Node(value=12, next=Node())
    def test_ellipsis(self):
        loader = dataloader.Loader()

        l = list(range(33))
        t = tuple(l)
        assert loader.load(l, Tuple[int, ...]) == t
        assert loader.load('abc', Tuple[str, ...]) == ('a', 'b', 'c')
        assert loader.load('a', Tuple[str, ...]) == ('a', )
Esempio n. 10
0
    def test_simple_defaults(self):
        class A(NamedTuple):
            a: int = 1
            b: str = '1'

        loader = dataloader.Loader()
        r = A(1, '1')
        assert loader.load({}, A) == r
Esempio n. 11
0
    def test_add_fref(self):
        class A(NamedTuple):
            i: 'alfio'

        loader = dataloader.Loader()
        with self.assertRaises(ValueError):
            loader.load({'i': 3}, A)
        loader.frefs['alfio'] = int
        assert loader.load({'i': 3}, A) == A(3)
Esempio n. 12
0
 def test_optional(self):
     loader = dataloader.Loader()
     assert loader.load(1, Optional[int]) == 1
     assert loader.load(None, Optional[int]) == None
     assert loader.load('1', Optional[int]) == 1
     with self.assertRaises(ValueError):
         loader.load('ciao', Optional[int])
         loader.basiccast = False
         loader.load('1', Optional[int])
    def test_nested(self):
        A = NamedTuple('A', [('a', int)])
        B = NamedTuple('B', [('a', A)])

        loader = dataloader.Loader()
        r = B(A(1))
        assert loader.load({'a': {'a': 1}}, B) == r
        with self.assertRaises(TypeError):
            loader.load({'a': {'a': 1}}, A)
 def test_simple(self):
     A = NamedTuple('A', [('a', int), ('b', str)])
     loader = dataloader.Loader()
     r = A(1,'1')
     assert loader.load({'a': 1, 'b': 1}, A) == r
     assert loader.load({'a': 1, 'b': 1, 'c': 3}, A) == r
     loader.failonextra = True
     with self.assertRaises(ValueError):
         loader.load({'a': 1, 'b': 1, 'c': 3}, A)
    def test_ComplicatedUnion(self):
        A = NamedTuple('A', [('a', int)])
        B = NamedTuple('B', [('a', str)])
        C = NamedTuple('C', [('val', Union[A, B])])

        loader = dataloader.Loader()
        loader.basiccast = False
        assert type(loader.load({'val': {'a': 1}}, C).val) == A
        assert type(loader.load({'val': {'a': '1'}}, C).val) == B
    def test_load_dict(self):
        loader = dataloader.Loader()
        class State(Enum):
            OK = 'ok'
            FAILED = 'failed'

        v = {'1': 'ok', '15': 'failed'}
        r = {1: State.OK, 15: State.FAILED}
        assert loader.load(v, Dict[int, State]) == r
    def test_load_tuple(self):
        loader = dataloader.Loader()

        assert loader.load([1, 2, 3], Tuple[int,int,int]) == (1, 2, 3)
        assert loader.load(['2', False, False], Tuple[int, bool]) == (2, False)

        with self.assertRaises(ValueError):
            loader.load(['2', False], Tuple[int, bool, bool])
            loader.failonextra = True
            assert loader.load(['2', False, False], Tuple[int, bool]) == (2, False)
 def test_basic_casting(self):
     # Casting enabled, by default
     loader = dataloader.Loader()
     assert loader.load(1, int) == 1
     assert loader.load(1.1, int) == 1
     assert loader.load(False, int) == 0
     assert loader.load('ciao', str) == 'ciao'
     assert loader.load('1', float) == 1.0
     with self.assertRaises(ValueError):
         loader.load('ciao', float)
Esempio n. 19
0
    def test_load_strconstructed(self):
        loader = dataloader.Loader()

        class Q:
            def __init__(self, p):
                self.param = p

        loader.strconstructed.add(Q)
        data = loader.load('42', Q)
        assert data.param == '42'
Esempio n. 20
0
    def test_namespace(self):
        loader = dataloader.Loader()
        data = argparse.Namespace(a=12, b='33')

        class A(NamedTuple):
            a: int
            b: int
            c: int = 1

        assert loader.load(data, A) == A(12, 33, 1)
        assert loader.load(data, Dict[str, int]) == {'a': 12, 'b': 33}
Esempio n. 21
0
    def test_tuple(self):
        loader = dataloader.Loader()

        with self.assertRaises(ValueError):
            assert loader.load([1], Tuple[int, int]) == (1, 2)

        assert loader.load([1, 2, 3], Tuple[int, int]) == (1, 2)
        loader.failonextra = True
        # Now the same will fail
        with self.assertRaises(ValueError):
            loader.load([1, 2, 3], Tuple[int, int]) == (1, 2)
    def test_load_nondict(self):

        class SimDict():

            def items(self):
                return zip(range(12), range(12))

        loader = dataloader.Loader()
        assert loader.load(SimDict(), Dict[str, int]) == {str(k): v for k,v in zip(range(12), range(12))}
        with self.assertRaises(AttributeError):
            loader.load(33, Dict[int, str])
    def test_custom_handler(self):
        class Q:
            def __eq__(self, other):
                return isinstance(other, Q)

        loader = dataloader.Loader()
        loader.handlers.append((
            lambda t: t == Q,
            lambda l, v, t: Q()
        ))
        assert loader.load('test', Q) == Q()
 def test_load_list_of_tuples(self):
     t = List[Tuple[str, int, Tuple[int, int]]]
     v = [
         ['a', 12, [1, 1]],
         ['b', 15, [3, 2]],
     ]
     r = [
         ('a', 12, (1, 1)),
         ('b', 15, (3, 2)),
     ]
     loader = dataloader.Loader()
     assert loader.load(v, t) == r
 def test_basic_nocasting(self):
     # Casting enabled, by default
     loader = dataloader.Loader()
     loader.basiccast = False
     assert loader.load(1, int) == 1
     assert loader.load(True, bool) == True
     assert loader.load(1.5, float) == 1.5
     with self.assertRaises(ValueError):
         loader.load(1.1, int)
         loader.load(False, int)
         loader.load('ciao', str)
         loader.load('1', float)
Esempio n. 26
0
    def test_simple(self):
        class A(NamedTuple):
            a: int
            b: str

        loader = dataloader.Loader()
        r = A(1, '1')
        assert loader.load({'a': 1, 'b': 1}, A) == r
        assert loader.load({'a': 1, 'b': 1, 'c': 3}, A) == r
        loader.failonextra = True
        with self.assertRaises(ValueError):
            loader.load({'a': 1, 'b': 1, 'c': 3}, A)
Esempio n. 27
0
    def test_nested(self):
        class A(NamedTuple):
            a: int

        class B(NamedTuple):
            a: A

        loader = dataloader.Loader()
        r = B(A(1))
        assert loader.load({'a': {'a': 1}}, B) == r
        with self.assertRaises(TypeError):
            loader.load({'a': {'a': 1}}, A)
Esempio n. 28
0
    def test_debug_union(self):
        loader = dataloader.Loader()

        class A(NamedTuple):
            a: int

        class B(NamedTuple):
            a: int

        assert isinstance(loader.load({'a': 1}, Union[A, B]), (A, B))
        loader.uniondebugconflict = True
        with self.assertRaises(TypeError):
            loader.load({'a': 1}, Union[A, B])
Esempio n. 29
0
    def test_load_difficult_enum(self):
        class TestEnum(Enum):
            A: int = 1
            B: Tuple[int, int, int] = (1, 2, 3)

        loader = dataloader.Loader()
        assert loader.load(1, TestEnum) == TestEnum.A
        assert loader.load((1, 2, 3), TestEnum) == TestEnum.B
        assert loader.load([1, 2, 3], TestEnum) == TestEnum.B
        assert loader.load([1, 2, 3, 4], TestEnum) == TestEnum.B
        loader.failonextra = True
        with self.assertRaises(ValueError):
            loader.load([1, 2, 3, 4], TestEnum)
Esempio n. 30
0
    def test_stopboard(self):
        class VehicleType(Enum):
            ST = 'ST'
            TRAM = 'TRAM'
            BUS = 'BUS'
            WALK = 'WALK'
            BOAT = 'BOAT'

        class BoardItem(NamedTuple):
            name: str
            type: VehicleType
            date: str
            time: str
            stop: str
            stopid: str
            journeyid: str
            sname: Optional[str] = None
            track: str = ''
            rtDate: Optional[str] = None
            rtTime: Optional[str] = None
            direction: Optional[str] = None
            accessibility: str = ''
            bgColor: str = '#0000ff'
            fgColor: str = '#ffffff'
            stroke: Optional[str] = None
            night: bool = False

        c = {
            'JourneyDetailRef': {
                'ref':
                'https://api.vasttrafik.se/bin/rest.exe/v2/journeyDetail?ref=859464%2F301885%2F523070%2F24954%2F80%3Fdate%3D2018-04-08%26station_evaId%3D5862002%26station_type%3Ddep%26format%3Djson%26'
            },
            'accessibility': 'wheelChair',
            'bgColor': '#00394d',
            'date': '2018-04-08',
            'direction': 'Kortedala',
            'fgColor': '#fa8719',
            'journeyid': '9015014500604285',
            'name': 'Spårvagn 6',
            'rtDate': '2018-04-08',
            'rtTime': '12:27',
            'sname': '6',
            'stop': 'SKF, Göteborg',
            'stopid': '9022014005862002',
            'stroke': 'Solid',
            'time': '12:17',
            'track': 'B',
            'type': 'TRAM'
        }
        loader = dataloader.Loader()
        loader.load(c, BoardItem)