File: testutils.py

package info (click to toggle)
python-param 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,048 kB
  • sloc: python: 17,980; makefile: 3
file content (423 lines) | stat: -rw-r--r-- 11,539 bytes parent folder | download
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import datetime as dt
import os

from functools import partial

import param
import pytest

from param import guess_param_types, resolve_path
from param.parameterized import bothmethod
from param._utils import _is_mutable_container, iscoroutinefunction


try:
    import numpy as np
except ImportError:
    np = None

try:
    import pandas as pd
except ImportError:
    pd = None

now = dt.datetime.now()
today = dt.date.today()

guess_param_types_data = {
    'Parameter': (param.Parameter(), param.Parameter),
    'Date': (today, param.Date),
    'Datetime': (now, param.Date),
    'Boolean': (True, param.Boolean),
    'Integer': (1, param.Integer),
    'Number': (1.2, param.Number),
    'String': ('test', param.String),
    'Dict': (dict(a=1), param.Dict),
    'NumericTuple': ((1, 2), param.NumericTuple),
    'Tuple': (('a', 'b'), param.Tuple),
    'DateRange': ((dt.date(2000, 1, 1), dt.date(2001, 1, 1)), param.DateRange),
    'List': ([1, 2], param.List),
    'Unsupported_None': (None, param.Parameter),
}

if np:
    guess_param_types_data.update({
        'Array':(np.ndarray([1, 2]), param.Array),
    })
if pd:
    guess_param_types_data.update({
        'DataFrame': (pd.DataFrame(data=dict(a=[1])), param.DataFrame),
        'Series': (pd.Series([1, 2]), param.Series),
    })

@pytest.mark.parametrize('val,p', guess_param_types_data.values(), ids=guess_param_types_data.keys())
def test_guess_param_types(val, p):
    input = {'key': val}
    output = guess_param_types(**input)
    assert isinstance(output, dict)
    assert len(output) == 1
    assert 'key' in output
    out_param = output['key']
    assert isinstance(out_param, p)
    if not type(out_param) == param.Parameter:
        assert out_param.default is val
        assert out_param.constant

@pytest.fixture
def reset_search_paths():
    # The default is [os.getcwd()] which doesn't play well with the testing
    # framework where every test creates a new temporary directory.
    # This fixture sets it temporarily to [].
    original = resolve_path.search_paths
    try:
        resolve_path.search_paths = []
        yield
    finally:
        resolve_path.search_paths = original


def test_resolve_path_file_default():
    assert resolve_path.path_to_file is True
    assert resolve_path.search_paths == [os.getcwd()]


def test_resolve_path_file_not_found():
    with pytest.raises(IOError, match='File surelyyoudontexist was not found in the following'):
        resolve_path('surelyyoudontexist')


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_file_not_found_other(tmpdir):
    cdir = os.getcwd()
    os.chdir(str(tmpdir))
    try:
        with pytest.raises(IOError, match='File notthere was not found in the following'):
            resolve_path('notthere')
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_folder_not_found(tmpdir):
    cdir = os.getcwd()
    os.chdir(str(tmpdir))
    try:
        with pytest.raises(IOError, match='Folder notthere was not found in the following'):
            resolve_path('notthere', path_to_file=False)
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_either_not_found(tmpdir):
    cdir = os.getcwd()
    os.chdir(str(tmpdir))
    try:
        with pytest.raises(IOError, match='Path notthere was not found in the following'):
            resolve_path('notthere', path_to_file=None)
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
@pytest.mark.parametrize('path_to_file', [True, False, None])
def test_resolve_path_abs_not_found(tmpdir, path_to_file):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    os.chdir(str(tmpdir))
    try:
        with pytest.raises(IOError, match='not found'):
            resolve_path(fp, path_to_file=path_to_file)
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_cwd_file(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    open(fp, 'w').close()
    os.chdir(str(tmpdir))
    try:
        p = resolve_path('foo')
        assert os.path.isfile(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_cwd_folder(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    os.mkdir(fp)
    os.chdir(str(tmpdir))
    try:
        p = resolve_path('foo', path_to_file=False)
        assert os.path.isdir(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_cwd_either_file(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    open(fp, 'w').close()
    os.chdir(str(tmpdir))
    try:
        p = resolve_path('foo', path_to_file=None)
        assert os.path.isfile(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_cwd_either_folder(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    os.mkdir(fp)
    os.chdir(str(tmpdir))
    try:
        p = resolve_path('foo', path_to_file=None)
        assert os.path.isdir(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_abs_file(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    open(fp, 'w').close()
    os.chdir(str(tmpdir))
    try:
        p = resolve_path(fp)
        assert os.path.isfile(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_abs_folder(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    os.mkdir(fp)
    os.chdir(str(tmpdir))
    try:
        p = resolve_path(fp, path_to_file=False)
        assert os.path.isdir(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_abs_either_file(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    open(fp, 'w').close()
    os.chdir(str(tmpdir))
    try:
        p = resolve_path(fp, path_to_file=None)
        assert os.path.isfile(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_abs_either_folder(tmpdir):
    cdir = os.getcwd()
    fp = os.path.join(str(tmpdir), 'foo')
    os.mkdir(fp)
    os.chdir(str(tmpdir))
    try:
        p = resolve_path(fp, path_to_file=None)
        assert os.path.isdir(p)
        assert os.path.basename(p) == 'foo'
        assert os.path.isabs(p)
        assert p == fp
    finally:
        os.chdir(cdir)


def test_resolve_path_search_paths_file(tmpdir):
    fp = os.path.join(str(tmpdir), 'foo')
    open(fp, 'w').close()
    p = resolve_path('foo', search_paths=[str(tmpdir)])
    assert os.path.isfile(p)
    assert os.path.basename(p) == 'foo'
    assert os.path.isabs(p)
    assert p == fp


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_search_paths_folder(tmpdir):
    fp = os.path.join(str(tmpdir), 'foo')
    os.mkdir(fp)
    p = resolve_path('foo', search_paths=[str(tmpdir)], path_to_file=False)
    assert os.path.isdir(p)
    assert os.path.basename(p) == 'foo'
    assert os.path.isabs(p)
    assert p == fp


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_search_paths_either_file(tmpdir):
    fp = os.path.join(str(tmpdir), 'foo')
    open(fp, 'w').close()
    p = resolve_path('foo', search_paths=[str(tmpdir)], path_to_file=None)
    assert os.path.isfile(p)
    assert os.path.basename(p) == 'foo'
    assert os.path.isabs(p)
    assert p == fp


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_search_paths_either_folder(tmpdir):
    fp = os.path.join(str(tmpdir), 'foo')
    os.mkdir(fp)
    p = resolve_path('foo', search_paths=[str(tmpdir)], path_to_file=None)
    assert os.path.isdir(p)
    assert os.path.basename(p) == 'foo'
    assert os.path.isabs(p)
    assert p == fp


@pytest.mark.usefixtures('reset_search_paths')
def test_resolve_path_search_paths_multiple_file(tmpdir):
    d1 = os.path.join(str(tmpdir), 'd1')
    d2 = os.path.join(str(tmpdir), 'd2')
    os.mkdir(d1)
    os.mkdir(d2)
    fp1 = os.path.join(d1, 'foo1')
    open(fp1, 'w').close()
    fp2 = os.path.join(d2, 'foo2')
    open(fp2, 'w').close()
    p = resolve_path('foo1', search_paths=[d1, d2])
    assert os.path.isfile(p)
    assert os.path.basename(p) == 'foo1'
    assert os.path.isabs(p)
    assert p == fp1

    p = resolve_path('foo2', search_paths=[d1, d2])
    assert os.path.isfile(p)
    assert os.path.basename(p) == 'foo2'
    assert os.path.isabs(p)
    assert p == fp2


def test_both_method():

    class A:

        @bothmethod
        def method(self_or_cls):
            return self_or_cls

    assert A.method() is A

    a = A()

    assert a.method() is a


def test_error_prefix_unbound_defined():
    with pytest.raises(ValueError, match="Number parameter 'x' only"):
        x = param.Number('wrong')  # noqa


def test_error_prefix_unbound_unexpected_pattern():
    from param import Number
    with pytest.raises(ValueError, match="Number parameter only"):
        Number('wrong')


def test_error_prefix_before_class_creation():
    with pytest.raises(ValueError, match="Number parameter 'x' only"):
        class P(param.Parameterized):
            x = param.Number('wrong')


def test_error_prefix_set_class():
    class P(param.Parameterized):
        x = param.Number()
    with pytest.raises(ValueError, match="Number parameter 'P.x' only"):
        P.x = 'wrong'


def test_error_prefix_instantiate():
    class P(param.Parameterized):
        x = param.Number()
    with pytest.raises(ValueError, match="Number parameter 'P.x' only"):
        P(x='wrong')


def test_error_prefix_set_instance():
    class P(param.Parameterized):
        x = param.Number()

    p = P()

    with pytest.raises(ValueError, match="Number parameter 'P.x' only"):
        p.x = 'wrong'


@pytest.mark.parametrize(
        ('obj,ismutable'),
        [
            ([1, 2], True),
            ({1, 2}, True),
            ({'a': 1, 'b': 2}, True),
            ((1, 2), False),
            ('string', False),
            (frozenset([1, 2]), False)
        ]
)
def test__is_mutable_container(obj, ismutable):
    assert _is_mutable_container(obj) is ismutable


async def coro():
    return


def test_iscoroutinefunction_coroutine():
    assert iscoroutinefunction(coro)


def test_iscoroutinefunction_partial_coroutine():
    pcoro = partial(partial(coro))
    assert iscoroutinefunction(pcoro)


async def agen():
    yield


def test_iscoroutinefunction_asyncgen():
    assert iscoroutinefunction(agen)


def test_iscoroutinefunction_partial_asyncgen():
    pagen = partial(partial(agen))
    assert iscoroutinefunction(pagen)