Completed
Push — master ( 82fa36...4edb63 )
by Klaus
29s
created

test_parse_individual_arguments()   B

Complexity

Conditions 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 31
rs 8.8571
1
#!/usr/bin/env python
2
# coding=utf-8
3
4
from __future__ import division, print_function, unicode_literals
5
6
import pytest
7
import shlex
8
from docopt import docopt
9
10
from sacred.arg_parser import (_convert_value, get_config_updates, format_usage)
11
from sacred.commandline_options import gather_command_line_options
12
13
14
@pytest.mark.parametrize("argv,expected", [
15
    ('',                 {}),
16
    ('run',              {'COMMAND': 'run'}),
17
    ('with 1 2',         {'with': True, 'UPDATE': ['1', '2']}),
18
    ('evaluate',         {'COMMAND': 'evaluate'}),
19
    ('help',             {'help': True}),
20
    ('help evaluate',    {'help': True, 'COMMAND': 'evaluate'}),
21
    ('-h',               {'--help': True}),
22
    ('--help',           {'--help': True}),
23
    ('-m foo',           {'--mongo_db': 'foo'}),
24
    ('--mongo_db=bar',   {'--mongo_db': 'bar'}),
25
    ('-l 10',            {'--loglevel': '10'}),
26
    ('--loglevel=30',    {'--loglevel': '30'}),
27
    ('--force', {'--force': True}),
28
    ('run with a=17 b=1 -m localhost:22222', {'COMMAND': 'run',
29
                                              'with': True,
30
                                              'UPDATE': ['a=17', 'b=1'],
31
                                              '--mongo_db': 'localhost:22222'}),
32
    ('evaluate with a=18 b=2 -l30', {'COMMAND': 'evaluate',
33
                                     'with': True,
34
                                     'UPDATE': ['a=18', 'b=2'],
35
                                     '--loglevel': '30'}),
36
])
37
def test_parse_individual_arguments(argv, expected):
38
    options = gather_command_line_options()
39
    usage = format_usage("test.py", "", {}, options)
40
    argv = shlex.split(argv)
41
    plain = docopt(usage, [], help=False)
42
    args = docopt(usage, argv, help=False)
43
    plain.update(expected)
44
    assert args == plain
45
46
47
@pytest.mark.parametrize("update,expected", [
48
    (None,              {}),
49
    (['a=5'],           {'a': 5}),
50
    (['foo.bar=6'],     {'foo': {'bar': 6}}),
51
    (['a=9', 'b=0'],    {'a': 9, 'b': 0}),
52
    (["hello='world'"], {'hello': 'world'}),
53
    (['hello="world"'], {'hello': 'world'}),
54
    (["f=23.5"],        {'f': 23.5}),
55
    (["n=None"],        {'n': None}),
56
    (["t=True"],        {'t': True}),
57
    (["f=False"],       {'f': False}),
58
])
59
def test_get_config_updates(update, expected):
60
    assert get_config_updates(update) == (expected, [])
61
62
63
@pytest.mark.parametrize("value,expected", [
64
    ('None',          None),
65
    ('True',          True),
66
    ('False',         False),
67
    ('246',           246),
68
    ('1.0',           1.0),
69
    ('1.',            1.0),
70
    ('.1',            0.1),
71
    ('1e3',           1e3),
72
    ('-.4e-12',       -0.4e-12),
73
    ('-.4e-12',       -0.4e-12),
74
    ('[1,2,3]',       [1, 2, 3]),
75
    ('[1.,.1]', [1., .1]),
76
    ('[True, False]', [True, False]),
77
    ('[None, None]', [None, None]),
78
    ('[1.0,2.0,3.0]', [1.0, 2.0, 3.0]),
79
    ('{"a":1}', {'a': 1}),
80
    ('{"foo":1, "bar":2.0}', {'foo': 1, 'bar': 2.0}),
81
    ('{"a":1., "b":.2}', {'a': 1., 'b': .2}),
82
    ('{"a":True, "b":False}', {'a': True, 'b': False}),
83
    ('{"a":None}', {'a': None}),
84
    ('{"a":[1, 2.0, True, None], "b":"foo"}', {"a": [1, 2.0, True, None],
85
                                               "b": "foo"}),
86
    ('bob', 'bob'),
87
    ('"hello world"', 'hello world'),
88
    ("'hello world'", 'hello world'),
89
])
90
def test_convert_value(value, expected):
91
    assert _convert_value(value) == expected
92