Completed
Push — master ( 1a32f0...6baa7b )
by Klaus
01:15
created

test_tee_output()   C

Complexity

Conditions 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 7
c 4
b 0
f 1
dl 0
loc 32
rs 5.5
1
#!/usr/bin/env python
2
# coding=utf-8
3
from __future__ import division, print_function, unicode_literals
4
import os
5
import sys
6
import tempfile
7
8
import pytest
9
10
from sacred.utils import (PATHCHANGE, convert_to_nested_dict,
11
                          get_by_dotted_path, is_prefix, is_subdir,
12
                          iter_path_splits, iter_prefixes, iterate_flattened,
13
                          iterate_flattened_separately, join_paths,
14
                          recursive_update, set_by_dotted_path, get_inheritors,
15
                          convert_camel_case_to_snake_case,
16
                          apply_backspaces_and_linefeeds)
17
18
19
def test_recursive_update():
20
    d = {'a': {'b': 1}}
21
    res = recursive_update(d, {'c': 2, 'a': {'d': 3}})
22
    assert d is res
23
    assert res == {'a': {'b': 1, 'd': 3}, 'c': 2}
24
25
26
def test_iterate_flattened_separately():
27
    d = {'a1': 1,
28
         'b2': {'bar': 'foo', 'foo': 'bar'},
29
         'c1': 'f',
30
         'd1': [1, 2, 3],
31
         'e2': {}}
32
    res = list(iterate_flattened_separately(d, ['foo', 'bar']))
33
    assert res == [('a1', 1), ('c1', 'f'), ('d1', [1, 2, 3]), ('e2', {}),
34
                   ('b2', PATHCHANGE), ('b2.foo', 'bar'), ('b2.bar', 'foo')]
35
36
37
def test_iterate_flattened():
38
    d = {'a': {'aa': 1, 'ab': {'aba': 8}}, 'b': 3}
39
    assert list(iterate_flattened(d)) == \
40
        [('a.aa', 1), ('a.ab.aba', 8), ('b', 3)]
41
42
43
def test_set_by_dotted_path():
44
    d = {'foo': {'bar': 7}}
45
    set_by_dotted_path(d, 'foo.bar', 10)
46
    assert d == {'foo': {'bar': 10}}
47
48
49
def test_set_by_dotted_path_creates_missing_dicts():
50
    d = {'foo': {'bar': 7}}
51
    set_by_dotted_path(d, 'foo.d.baz', 3)
52
    assert d == {'foo': {'bar': 7, 'd': {'baz': 3}}}
53
54
55
def test_get_by_dotted_path():
56
    assert get_by_dotted_path({'a': 12}, 'a') == 12
57
    assert get_by_dotted_path({'a': 12}, '') == {'a': 12}
58
    assert get_by_dotted_path({'foo': {'a': 12}}, 'foo.a') == 12
59
    assert get_by_dotted_path({'foo': {'a': 12}}, 'foo.b') is None
60
61
62
def test_iter_path_splits():
63
    assert list(iter_path_splits('foo.bar.baz')) ==\
64
        [('', 'foo.bar.baz'),
65
         ('foo', 'bar.baz'),
66
         ('foo.bar', 'baz')]
67
68
69
def test_iter_prefixes():
70
    assert list(iter_prefixes('foo.bar.baz')) == \
71
        ['foo', 'foo.bar', 'foo.bar.baz']
72
73
74
def test_join_paths():
75
    assert join_paths() == ''
76
    assert join_paths('foo') == 'foo'
77
    assert join_paths('foo', 'bar') == 'foo.bar'
78
    assert join_paths('a', 'b', 'c', 'd') == 'a.b.c.d'
79
    assert join_paths('', 'b', '', 'd') == 'b.d'
80
    assert join_paths('a.b', 'c.d.e') == 'a.b.c.d.e'
81
    assert join_paths('a.b.', 'c.d.e') == 'a.b.c.d.e'
82
83
84
def test_is_prefix():
85
    assert is_prefix('', 'foo')
86
    assert is_prefix('foo', 'foo.bar')
87
    assert is_prefix('foo.bar', 'foo.bar.baz')
88
89
    assert not is_prefix('a', 'foo.bar')
90
    assert not is_prefix('a.bar', 'foo.bar')
91
    assert not is_prefix('foo.b', 'foo.bar')
92
    assert not is_prefix('foo.bar', 'foo.bar')
93
94
95
def test_convert_to_nested_dict():
96
    dotted_dict = {'foo.bar': 8, 'foo.baz': 7}
97
    assert convert_to_nested_dict(dotted_dict) == {'foo': {'bar': 8, 'baz': 7}}
98
99
100
def test_convert_to_nested_dict_nested():
101
    dotted_dict = {'a.b': {'foo.bar': 8}, 'a.b.foo.baz': 7}
102
    assert convert_to_nested_dict(dotted_dict) == \
103
        {'a': {'b': {'foo': {'bar': 8, 'baz': 7}}}}
104
105
106
@pytest.mark.parametrize('path,parent,expected', [
107
    ('/var/test2', '/var/test', False),
108
    ('/var/test', '/var/test2', False),
109
    ('var/test2', 'var/test', False),
110
    ('var/test', 'var/test2', False),
111
    ('/var/test/sub', '/var/test', True),
112
    ('/var/test', '/var/test/sub', False),
113
    ('var/test/sub', 'var/test', True),
114
    ('var/test', 'var/test', True),
115
    ('var/test', 'var/test/fake_sub/..', True),
116
    ('var/test/sub/sub2/sub3/../..', 'var/test', True),
117
    ('var/test/sub', 'var/test/fake_sub/..', True),
118
    ('var/test', 'var/test/sub', False)
119
])
120
def test_is_subdirectory(path, parent, expected):
121
    assert is_subdir(path, parent) == expected
122
123
124
def test_get_inheritors():
125
    class A(object):
126
        pass
127
128
    class B(A):
129
        pass
130
131
    class C(B):
132
        pass
133
134
    class D(A):
135
        pass
136
137
    class E(object):
138
        pass
139
140
    assert get_inheritors(A) == {B, C, D}
141
142
143
@pytest.mark.parametrize('name,expected', [
144
    ('CamelCase', 'camel_case'),
145
    ('snake_case', 'snake_case'),
146
    ('CamelCamelCase', 'camel_camel_case'),
147
    ('Camel2Camel2Case', 'camel2_camel2_case'),
148
    ('getHTTPResponseCode', 'get_http_response_code'),
149
    ('get2HTTPResponseCode', 'get2_http_response_code'),
150
    ('HTTPResponseCode', 'http_response_code'),
151
    ('HTTPResponseCodeXYZ', 'http_response_code_xyz')
152
])
153
def test_convert_camel_case_to_snake_case(name, expected):
154
    assert convert_camel_case_to_snake_case(name) == expected
155
156
157
@pytest.mark.parametrize('text,expected', [
158
    ('', ''),
159
    ('\b', ''),
160
    ('\r', ''),
161
    ('ab\bc', 'ac'),
162
    ('\ba', 'a'),
163
    ('ab\nc\b\bd', 'ab\nd'),
164
    ('abc\rdef', 'def'),
165
    ('abc\r', 'abc'),
166
    ('abc\rd', 'dbc'),
167
    ('abc\r\nd', 'abc\nd'),
168
    ('abc\ndef\rg', 'abc\ngef'),
169
    ('abc\ndef\r\rg', 'abc\ngef')
170
])
171
def test_apply_backspaces_and_linefeeds(text, expected):
172
    assert apply_backspaces_and_linefeeds(text) == expected
173