-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
123 lines (96 loc) · 3.3 KB
/
test.py
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
# coding=utf-8
#
# This file is part of ORMless (https://github.com/kirca/ormless)
#
# Copyright (c) 2016 Kiril Vangelovski ([email protected])
#
# This Source Code Form is subject to the terms of the MIT License
# If a copy of the licence was not distributed with this file, You can
# obtain one at https://opensource.org/licenses/MIT .
from ormless import convert, typed
from ormless.annotations import types
from ormless.utils import flip
from functools import partial
from collections import namedtuple
ANamedTuple = partial(namedtuple, 'ANamedTuple')
class AClass:
attr1 = 'abc'
attr2 = 100
def test_convert_one():
@convert
@types(ANamedTuple(['attr1', 'attr2']))
def f(arg1):
return arg1
arg_type = f.__annotations__['arg1']
converted_arg = f(AClass())
assert isinstance(converted_arg, arg_type), (
"The argument is not converted")
def test_convert_only_first():
@convert
@types(ANamedTuple(['attr1', 'attr2']))
def f(arg1, arg2):
return arg1, arg2
arg1_type = f.__annotations__['arg1']
arg1 = AClass()
arg2 = 'test'
converted_arg1, returned_arg2 = f(arg1, arg2)
assert isinstance(converted_arg1, arg1_type), (
"The first argument is not converted")
assert arg2 == returned_arg2, (
"The second argument is changed")
def test_convert_multi():
@convert
@types([ANamedTuple(['attr1', 'attr2'])])
def f(arg1):
return arg1
arg_type = f.__annotations__['arg1'][0]
converted_args = f([AClass(), AClass()])
is_arg_type = partial(flip(isinstance), arg_type)
assert len(converted_args), (
"Arguments are missing")
assert all(map(is_arg_type, converted_args)), (
"Not all argument are converted")
def test_convert_kwargs():
@convert
@types(kwarg1=ANamedTuple(['attr1', 'attr2']))
def f(kwarg1=None):
return kwarg1
kwarg_type = f.__annotations__['kwarg1']
converted_kwarg = f(AClass())
assert isinstance(converted_kwarg, kwarg_type), (
"The keyword argument is not converted")
def test_convert_only_first_kwarg():
@convert
@types(ANamedTuple(['attr1', 'attr2']))
def f(kwarg1=None, kwarg2=None):
return kwarg1, kwarg2
kwarg1_type = f.__annotations__['kwarg1']
kwarg1 = AClass()
kwarg2 = 'test'
converted_kwarg1, returned_kwarg2 = f(kwarg1=kwarg1,
kwarg2=kwarg2)
assert isinstance(converted_kwarg1, kwarg1_type), (
"The first keyword argument is not converted")
assert kwarg2 == returned_kwarg2, (
"The second keyword argument is changed")
ATypedNamedTuple = partial(typed, partial(namedtuple, 'ANamedTuple1'))
class AParentClass:
attr1 = 'efg'
attr2 = AClass()
def test_convert_one_typed():
@convert
@types(
ATypedNamedTuple(
['attr1', 'attr2'],
{'attr1': str,
'attr2': ANamedTuple(['attr1', 'attr2']),
}))
def f(arg1):
return arg1
arg_type = f.__annotations__['arg1']
arg_field_type = arg_type._field_types['attr2']
converted_arg = f(AParentClass())
assert isinstance(converted_arg, arg_type), (
"The argument is not converted")
assert isinstance(converted_arg.attr2, arg_field_type), (
"The argument's field is not converted")