forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api.py
288 lines (225 loc) · 9.16 KB
/
test_api.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import inspect
from datetime import datetime
import pytest
from conda.api import DepsModifier, PackageCacheData, PrefixData, Solver, SubdirData, \
UpdateModifier
from conda.base.context import context
from conda.common.compat import isiterable, odict
from conda.common.constants import NULL
from conda.core.link import UnlinkLinkTransaction
from conda.models.channel import Channel
from conda.models.records import PackageCacheRecord, PackageRecord, PrefixRecord
class PositionalArgument:
pass
def inspect_arguments(f, arguments):
result = inspect.getargspec(f)
arg_names = result[0]
defaults = result.defaults or ()
default_val_first_idx = len(arg_names) - len(defaults)
arg_values = [PositionalArgument] * default_val_first_idx + list(defaults)
for (recorded_name, recorded_value), (arg_name, arg_value) in zip(arguments.items(), zip(arg_names, arg_values)):
print(recorded_name, arg_name)
assert recorded_name == arg_name
assert recorded_value == arg_value
def test_DepsModifier_contract():
assert DepsModifier.NO_DEPS
assert DepsModifier.ONLY_DEPS
assert DepsModifier.NOT_SET
def test_UpdateModifier_contract():
assert UpdateModifier.SPECS_SATISFIED_SKIP_SOLVE
assert UpdateModifier.FREEZE_INSTALLED
assert UpdateModifier.UPDATE_DEPS
assert UpdateModifier.UPDATE_SPECS
assert UpdateModifier.UPDATE_ALL
def test_Solver_inputs_contract():
init_args = odict((
('self', PositionalArgument),
('prefix', PositionalArgument),
('channels', PositionalArgument),
('subdirs', ()),
('specs_to_add', ()),
('specs_to_remove', ()),
))
inspect_arguments(Solver.__init__, init_args)
solve_final_state_args = odict((
('self', PositionalArgument),
('update_modifier', NULL),
('deps_modifier', NULL),
('prune', NULL),
('ignore_pinned', NULL),
('force_remove', NULL),
))
inspect_arguments(Solver.solve_final_state, solve_final_state_args)
solve_for_diff_args = odict((
('self', PositionalArgument),
('update_modifier', NULL),
('deps_modifier', NULL),
('prune', NULL),
('ignore_pinned', NULL),
('force_remove', NULL),
('force_reinstall', False),
))
inspect_arguments(Solver.solve_for_diff, solve_for_diff_args)
solve_for_transaction_args = odict((
('self', PositionalArgument),
('update_modifier', NULL),
('deps_modifier', NULL),
('prune', NULL),
('ignore_pinned', NULL),
('force_remove', NULL),
('force_reinstall', False),
))
inspect_arguments(Solver.solve_for_transaction, solve_for_transaction_args)
@pytest.mark.integration
def test_Solver_return_value_contract():
solver = Solver('/', (Channel('pkgs/main'),), specs_to_add=('openssl',))
solve_final_state_rv = solver.solve_final_state()
assert isiterable(solve_final_state_rv)
assert all(isinstance(pref, PackageRecord) for pref in solve_final_state_rv)
solve_for_diff_rv = solver.solve_for_diff()
assert len(solve_for_diff_rv) == 2
unlink_precs, link_precs = solve_for_diff_rv
assert isiterable(unlink_precs)
assert all(isinstance(pref, PackageRecord) for pref in unlink_precs)
assert isiterable(link_precs)
assert all(isinstance(pref, PackageRecord) for pref in link_precs)
solve_for_transaction_rv = solver.solve_for_transaction()
assert isinstance(solve_for_transaction_rv, UnlinkLinkTransaction)
def test_SubdirData_contract():
init_args = odict((
('self', PositionalArgument),
('channel', PositionalArgument),
))
inspect_arguments(SubdirData.__init__, init_args)
query_args = odict((
('self', PositionalArgument),
('package_ref_or_match_spec', PositionalArgument),
))
inspect_arguments(SubdirData.query, query_args)
query_all_args = odict((
('package_ref_or_match_spec', PositionalArgument),
('channels', None),
('subdirs', None),
))
inspect_arguments(SubdirData.query_all, query_all_args)
iter_records_args = odict((
('self', PositionalArgument),
))
inspect_arguments(SubdirData.iter_records, iter_records_args)
reload_args = odict((
('self', PositionalArgument),
))
inspect_arguments(SubdirData.reload, reload_args)
@pytest.mark.integration
def test_SubdirData_return_value_contract():
sd = SubdirData(Channel('pkgs/main/linux-64'))
query_result = sd.query('openssl')
assert isinstance(query_result, tuple)
assert all(isinstance(prec, PackageRecord) for prec in query_result)
query_all_result = sd.query_all('openssl', (Channel('pkgs/main'),), context.subdirs)
assert isinstance(query_all_result, tuple)
assert all(isinstance(prec, PackageRecord) for prec in query_all_result)
iter_records_result = sd.iter_records()
assert isiterable(iter_records_result)
assert all(isinstance(prec, PackageRecord) for prec in iter_records_result)
reload_result = sd.reload()
assert isinstance(reload_result, SubdirData)
def test_PackageCacheData_contract():
init_args = odict((
('self', PositionalArgument),
('pkgs_dir', PositionalArgument),
))
inspect_arguments(PackageCacheData.__init__, init_args)
get_args = odict((
('self', PositionalArgument),
('package_ref', PositionalArgument),
('default', NULL),
))
inspect_arguments(PackageCacheData.get, get_args)
query_args = odict((
('self', PositionalArgument),
('package_ref_or_match_spec', PositionalArgument),
))
inspect_arguments(PackageCacheData.query, query_args)
query_all_args = odict((
('package_ref_or_match_spec', PositionalArgument),
('pkgs_dirs', None),
))
inspect_arguments(PackageCacheData.query_all, query_all_args)
iter_records_args = odict((
('self', PositionalArgument),
))
inspect_arguments(PackageCacheData.iter_records, iter_records_args)
isinstance(PackageCacheData.is_writable, property)
first_writable_args = odict((
('pkgs_dirs', None),
))
inspect_arguments(PackageCacheData.first_writable, first_writable_args)
reload_args = odict((
('self', PositionalArgument),
))
inspect_arguments(PackageCacheData.reload, reload_args)
def test_PackageCacheData_return_value_contract():
pc = PackageCacheData(context.pkgs_dirs[0])
single_pcrec = next(pc.iter_records(), None)
if single_pcrec:
get_result = pc.get(PackageRecord.from_objects(single_pcrec))
assert isinstance(get_result, PackageCacheRecord)
query_result = pc.query('openssl')
assert isinstance(query_result, tuple)
assert all(isinstance(pcrec, PackageCacheRecord) for pcrec in query_result)
query_all_result = PackageCacheData.query_all('openssl')
assert isinstance(query_all_result, tuple)
assert all(isinstance(pcrec, PackageCacheRecord) for pcrec in query_all_result)
iter_records_result = pc.iter_records()
assert isiterable(iter_records_result)
assert all(isinstance(pcrec, PackageCacheRecord) for pcrec in iter_records_result)
is_writable_result = pc.is_writable
assert is_writable_result is True or is_writable_result is False
first_writable_result = PackageCacheData.first_writable()
assert isinstance(first_writable_result, PackageCacheData)
reload_result = pc.reload()
assert isinstance(reload_result, PackageCacheData)
def test_PrefixData_contract():
init_args = odict((
('self', PositionalArgument),
('prefix_path', PositionalArgument),
))
inspect_arguments(PrefixData.__init__, init_args)
get_args = odict((
('self', PositionalArgument),
('package_ref', PositionalArgument),
('default', NULL),
))
inspect_arguments(PrefixData.get, get_args)
query_args = odict((
('self', PositionalArgument),
('package_ref_or_match_spec', PositionalArgument),
))
inspect_arguments(PrefixData.query, query_args)
iter_records_args = odict((
('self', PositionalArgument),
))
inspect_arguments(PrefixData.iter_records, iter_records_args)
isinstance(PrefixData.is_writable, property)
reload_args = odict((
('self', PositionalArgument),
))
inspect_arguments(PrefixData.reload, reload_args)
def test_PrefixData_return_value_contract():
pd = PrefixData(context.conda_prefix)
single_prefix_rec = next(pd.iter_records())
get_result = pd.get(PackageRecord.from_objects(single_prefix_rec))
assert isinstance(get_result, PrefixRecord)
query_result = pd.query('openssl')
assert isinstance(query_result, tuple)
assert all(isinstance(prefix_rec, PrefixRecord) for prefix_rec in query_result)
iter_records_result = pd.iter_records()
assert isiterable(iter_records_result)
assert all(isinstance(prefix_rec, PrefixRecord) for prefix_rec in iter_records_result)
is_writable_result = pd.is_writable
assert is_writable_result is True or is_writable_result is False
reload_result = pd.reload()
assert isinstance(reload_result, PrefixData)