-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_dtyper.py
126 lines (85 loc) Β· 3.07 KB
/
test_dtyper.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
124
125
126
from typing import Optional
import pytest
from typer import Argument, Option, Typer
import dtyper
command = Typer().command
dcommand = dtyper.Typer().command
@dtyper.function
@command(help='test')
def simple_command(
bucket: str = Argument(..., help='The bucket to use'),
keys: str = Argument('keys', help='The keys to download'),
pid: Optional[int] = Option(None, help='pid'),
):
return bucket, keys, pid
def test_simple_command():
assert simple_command('bukket', pid=3) == ('bukket', 'keys', 3)
@dcommand(help='test')
def simple_command2(
bucket: str = Argument(..., help='The bucket to use'),
keys: str = Argument('keys', help='The keys to download'),
pid: Optional[int] = Option(None, help='pid'),
):
return bucket, keys, pid
def test_simple_command2():
assert simple_command2('bukket', pid=3) == ('bukket', 'keys', 3)
def test_args():
with pytest.raises(TypeError, match="unexpected keyword argument 'frog'"):
simple_command('bukket', frog=30)
with pytest.raises(TypeError, match='too many positional arguments'):
simple_command('bukket', 'key', 12, 30)
with pytest.raises(TypeError, match="required argument: 'bucket'"):
simple_command()
@command(help='test')
def a_command(
bucket: str = Argument(..., help='The bucket to use'),
keys: str = Argument('keys', help='The keys to download'),
pid: Optional[int] = Option(None, help='pid'),
):
return ACommand(**locals())()
@dtyper.dataclass(a_command)
class ACommand:
def __post_init__(self):
self.bucket += '-post'
def __call__(self):
return self.bucket, self.keys, self.pod
@property
def pod(self):
return self.pid
def test_acommand():
assert ACommand('bukket')() == ('bukket-post', 'keys', None)
assert ACommand('bukket', 'kois', pid=3)() == ('bukket-post', 'kois', 3)
match = "missing 1 required positional argument: 'bucket'"
with pytest.raises(TypeError, match=match):
ACommand()
@dtyper.dataclass(a_command)
def c_function(self):
return self.bucket, self.keys, self.pid
def test_c_function():
assert c_function('bukket')() == ('bukket', 'keys', None)
@dtyper.dataclass(simple_command2)
def simple_class(sc):
return sc, True
def test_simple_class():
cls = simple_class(bucket='b', keys=['key'])
assert cls() == (cls, True)
def test_aliases():
from dtyper import Argument, Option
assert Argument is globals()['Argument']
assert Option is globals()['Option']
@dtyper.function
@command(help='test')
def less_simple_command(
bucket: str = Argument(..., help='The bucket to use'),
keys: str = Argument('keys', help='The keys to download'),
pid: Optional[int] = Option(None, help='pid'),
pod: Optional[int] = Option(..., help='pid'),
):
return bucket, keys, pid, pod
def test_less_simple_command():
actual = less_simple_command('bukket', pid=3, pod=2)
expected = 'bukket', 'keys', 3, 2
assert actual == expected
def test_less_simple_command_error():
with pytest.raises(TypeError):
less_simple_command('bukket', pid=3)