-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_models.py
243 lines (188 loc) · 6.35 KB
/
test_models.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import hashlib
import tomlkit.items
import pytest
from plette import models
def test_hash_from_hash():
v = hashlib.md5(b"foo")
h = models.Hash.from_hash(v)
assert h.name == "md5"
assert h.value == "acbd18db4cc2f85cedef654fccc4a4d8"
def test_hash_from_line():
h = models.Hash.from_line("md5:acbd18db4cc2f85cedef654fccc4a4d8")
assert h.name == "md5"
assert h.value == "acbd18db4cc2f85cedef654fccc4a4d8"
def test_hash_as_line():
h = models.Hash({"md5": "acbd18db4cc2f85cedef654fccc4a4d8"})
assert h.as_line() == "md5:acbd18db4cc2f85cedef654fccc4a4d8"
def test_source_from_data():
s = models.Source(
{
"name": "devpi",
"url": "https://$USER:$PASS@mydevpi.localhost",
"verify_ssl": False,
}
)
assert s.name == "devpi"
assert s.url == "https://$USER:$PASS@mydevpi.localhost"
assert s.verify_ssl is False
def test_source_as_data_expanded(monkeypatch):
monkeypatch.setattr("os.environ", {"USER": "user", "PASS": "pa55"})
s = models.Source(
{
"name": "devpi",
"url": "https://$USER:$PASS@mydevpi.localhost",
"verify_ssl": False,
}
)
assert s.url_expanded == "https://user:pa55@mydevpi.localhost"
def test_source_as_data_expanded_partial(monkeypatch):
monkeypatch.setattr("os.environ", {"USER": "user"})
s = models.Source(
{
"name": "devpi",
"url": "https://$USER:$PASS@mydevpi.localhost",
"verify_ssl": False,
}
)
assert s.url_expanded == "https://user:$PASS@mydevpi.localhost"
def test_requires_python_version():
r = models.Requires({"python_version": "8.19"})
assert r.python_version == "8.19"
def test_requires_python_version_no_full_version():
r = models.Requires({"python_version": "8.19"})
with pytest.raises(AttributeError) as ctx:
r.python_full_version
assert str(ctx.value) == "python_full_version"
def test_requires_python_full_version():
r = models.Requires({"python_full_version": "8.19"})
assert r.python_full_version == "8.19"
def test_requires_python_full_version_no_version():
r = models.Requires({"python_full_version": "8.19"})
with pytest.raises(AttributeError) as ctx:
r.python_version
assert str(ctx.value) == "python_version"
def test_allows_python_version_and_full():
r = models.Requires({"python_version": "8.1", "python_full_version": "8.1.9"})
assert r.python_version == "8.1"
assert r.python_full_version == "8.1.9"
def test_package_str():
p = models.Package("*")
p.version == "*"
def test_package_dict():
p = models.Package({"version": "*"})
p.version == "*"
def test_package_wrong_key():
p = models.Package({"path": ".", "editable": True})
assert p.editable is True
with pytest.raises(AttributeError) as ctx:
p.version
assert str(ctx.value) == "version"
def test_package_with_wrong_specfiers():
with pytest.raises(models.base.DataValidationError) as ctx:
_ = models.Package(1.2)
assert str(ctx.value) == "invalid type for package data: <class 'float'>"
def test_package_with_specfiers():
value = 1.2
float_value = tomlkit.items.Float(value, tomlkit.items.Trivia(), str(value))
p = models.Package(float_value)
assert p.version == float_value
def test_package_with_wrong_extras():
with pytest.raises(models.base.DataValidationError):
_ = models.Package({"version": "==1.20.0", "extras": "broker"})
def test_package_with_extras():
p = models.Package({"version": "==1.20.0", "extras": ["broker", "tests"]})
assert p.extras == ['broker', 'tests']
HASH = "9aaf3dbaf8c4df3accd4606eb2275d3b91c9db41be4fd5a97ecc95d79a12cfe6"
def test_meta():
m = models.Meta(
{
"hash": {"sha256": HASH},
"pipfile-spec": 6,
"requires": {},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": True,
},
],
}
)
assert m.hash.name == "sha256"
@pytest.fixture()
def sources():
return models.SourceCollection(
[
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": True,
},
{
"name": "devpi",
"url": "http://127.0.0.1:$DEVPI_PORT/simple",
"verify_ssl": True,
},
]
)
def test_get_slice(sources):
sliced = sources[:1]
assert isinstance(sliced, models.SourceCollection)
assert len(sliced) == 1
assert sliced[0] == models.Source(
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": True,
}
)
def test_set_slice(sources):
sources[1:] = [
{
"name": "localpi-4433",
"url": "https://127.0.0.1:4433/simple",
"verify_ssl": False,
},
{
"name": "localpi-8000",
"url": "http://127.0.0.1:8000/simple",
"verify_ssl": True,
},
]
assert sources._data == [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": True,
},
{
"name": "localpi-4433",
"url": "https://127.0.0.1:4433/simple",
"verify_ssl": False,
},
{
"name": "localpi-8000",
"url": "http://127.0.0.1:8000/simple",
"verify_ssl": True,
},
]
def test_del_slice(sources):
del sources[:1]
assert sources._data == [
{
"name": "devpi",
"url": "http://127.0.0.1:$DEVPI_PORT/simple",
"verify_ssl": True,
},
]
def test_validation_error():
data = {"name": "test", "url": "https://pypi.org/simple", "verify_ssl": 1}
with pytest.raises(models.base.DataValidationError) as exc_info:
models.Source.validate(data)
error_message = str(exc_info.value)
assert "Invalid type for field verify_ssl: <class 'int'>" in error_message
data = {"name": "test", "verify_ssl": False}
with pytest.raises(models.base.DataValidationError) as exc_info:
models.Source.validate(data)
error_message = str(exc_info.value)
assert "Missing required field: url" in error_message