Completed
Push — master ( 1286dd...44704a )
by Max
54s
created

test_set()   D

Complexity

Conditions 10

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
c 2
b 0
f 0
dl 0
loc 22
rs 4.5957

How to fix   Complexity   

Complexity

Complex classes like test_set() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import sys
2
3
import pytest
4
5
6
def test_import(namespaces):
7
    assert namespaces
8
9
10
def test_meta_basic(namespaces):
11
    class Test(metaclass=namespaces.Namespaceable):
12
        pass
13
    assert Test
14
15
16
def test_basic_namespace(namespaces):
17
    class Test(metaclass=namespaces.Namespaceable):
18
        with namespaces.Namespace() as ns:
19
            a = 1
20
        assert ns
21
    assert Test
22
    assert Test().ns
23
24
25
def test_delete(namespaces):
26
    class Test(metaclass=namespaces.Namespaceable):
27
        with namespaces.Namespace() as ns:
28
            a = 1
29
            del a
30
            b = 2
31
        assert ns
32
    assert Test
33
    assert Test().ns
34
    assert Test.ns.b == 2
35
    assert Test().ns.b == 2
36
    with pytest.raises(AttributeError):
37
        del Test().ns.b
38
    del Test.ns.b
39
    with pytest.raises(AttributeError):
40
        Test.ns.b
41
    with pytest.raises(AttributeError):
42
        Test().ns.b
43
44
45
def test_set(namespaces):
46
    class Test(metaclass=namespaces.Namespaceable):
47
        with namespaces.Namespace() as ns:
48
            a = 1
49
            del a
50
        assert ns
51
    assert Test
52
    assert Test().ns
53
    Test.ns.b = 2
54
    assert Test.ns.b == 2
55
    assert Test().ns.b == 2
56
    test = Test()
57
    test.ns.b = 3
58
    assert Test.ns.b == 2
59
    assert test.ns.b == 3
60
    test2 = Test()
61
    test2.ns.c = 3
62
    with pytest.raises(AttributeError):
63
        Test.ns.c
64
    with pytest.raises(AttributeError):
65
        test.ns.c
66
    assert test2.ns.c == 3
67
68
69
@pytest.mark.xfail(sys.version_info < (3, 4),
70
                   reason="python3.4 api changes?", strict=True)
71
def test_dir(namespaces):
72
    class Test(metaclass=namespaces.Namespaceable):
73
        with namespaces.Namespace() as ns:
74
            a = 1
75
        assert ns
76
    assert dir(Test.ns) == ['a']
77
    assert dir(Test().ns) == ['a']
78
79
80
def test_shadow(namespaces):
81
    class Test(metaclass=namespaces.Namespaceable):
82
        foo = 1
83
        with namespaces.Namespace() as ns:
84
            foo = 2
85
            assert foo == 2
86
        assert foo == 1
87
    assert Test().foo == 1
88
    assert Test().ns.foo == 2
89
90
91
def test_resume(namespaces):
92
    class Test(metaclass=namespaces.Namespaceable):
93
        foo = 1
94
        with namespaces.Namespace() as ns:
95
            foo = 2
96
            assert foo == 2
97
        foo = 3
98
        with ns:
99
            foo = 4
100
        assert foo == 3
101
    assert Test().foo == 3
102
    assert Test().ns.foo == 4
103
104
105
def test_redundant_resume(namespaces):
106
    class Test(metaclass=namespaces.Namespaceable):
107
        foo = 1
108
        with namespaces.Namespace() as ns:
109
            foo = 2
110
            assert foo == 2
111
        foo = 3
112
        ns = ns
113
        with ns as ns:
114
            foo = 4
115
        assert foo == 3
116
    assert Test().foo == 3
117
    assert Test().ns.foo == 4
118
119
120
def test_basic_inherit(namespaces):
121
    class Test(metaclass=namespaces.Namespaceable):
122
        foo = 1
123
        with namespaces.Namespace() as ns:
124
            foo = 2
125
126
    class Subclass(Test):
127
        pass
128
    assert Subclass().foo == 1
129
    assert Subclass().ns.foo == 2
130
131
132
def test_basic_super(namespaces):
133
    class Test(metaclass=namespaces.Namespaceable):
134
        with namespaces.Namespace() as ns:
135
            def hello(self):
136
                return 1
137
138
    class Subclass(Test):
139
        with namespaces.Namespace() as ns:
140
            def hello(self):
141
                return super().ns.hello()
142
143
    assert Test().ns.hello() == 1
144
    assert Subclass().ns.hello() == 1
145
146
147
def test_private(namespaces):
148
    class Test(metaclass=namespaces.Namespaceable):
149
        with namespaces.Namespace() as __ns:
150
            foo = 2
151
152
        def foo(self):
153
            return self.__ns.foo
154
155
    class Subclass(Test):
156
        pass
157
158
    assert Test().foo() == 2
159
    assert Subclass().foo() == 2
160
161
162
def test_nested_namespace(namespaces):
163
    class Test(metaclass=namespaces.Namespaceable):
164
        with namespaces.Namespace() as ns:
165
            with namespaces.Namespace() as ns:
166
                a = 1
167
    assert Test().ns.ns.a == 1
168
169
170
def test_basic_shadow(namespaces):
171
    class Test(metaclass=namespaces.Namespaceable):
172
        with namespaces.Namespace() as ns:
173
            foo = 2
174
175
    class Subclass(Test):
176
        ns = 1
177
    assert Subclass().ns == 1
178
179
180
def test_double_shadow(namespaces):
181
    class Test(metaclass=namespaces.Namespaceable):
182
        with namespaces.Namespace() as ns:
183
            foo = 2
184
185
    class Subclass(Test):
186
        ns = 1
187
188
    class DoubleSubclass(Subclass):
189
        with namespaces.Namespace() as ns:
190
            bar = 1
191
    assert not hasattr(DoubleSubclass().ns, 'foo')
192
193
194
def test_overlap(namespaces):
195
    class Test(metaclass=namespaces.Namespaceable):
196
        with namespaces.Namespace() as ns:
197
            foo = 2
198
199
    class Subclass(Test):
200
        with namespaces.Namespace() as ns:
201
            bar = 3
202
    assert Subclass().ns.foo == 2
203
    assert Subclass().ns.bar == 3
204
205
206
def test_advanced_overlap(namespaces):
207
    class Test(metaclass=namespaces.Namespaceable):
208
        with namespaces.Namespace() as ns:
209
            foo = 2
210
            with namespaces.Namespace() as ns:
211
                qux = 4
212
213
    class Subclass(Test):
214
        with namespaces.Namespace() as ns:
215
            bar = 3
216
    assert Subclass().ns.foo == 2
217
    assert Subclass().ns.bar == 3
218
    assert Subclass().ns.ns.qux == 4
219
220
221
def test_empty_nameless(namespaces):
222
    class Test(metaclass=namespaces.Namespaceable):
223
        with pytest.raises(RuntimeError):
224
            with namespaces.Namespace():
225
                pass
226
227
228
def test_non_empty_nameless(namespaces):
229
    class Test(metaclass=namespaces.Namespaceable):
230
        with pytest.raises(RuntimeError):
231
            with namespaces.Namespace():
232
                a = 1
233
234
235
def test_rename(namespaces):
236
    class Test(metaclass=namespaces.Namespaceable):
237
        with namespaces.Namespace() as ns:
238
            pass
239
        with pytest.raises(ValueError):
240
            with ns as ns2:
241
                pass
242
243
244
def test_use_namespace(namespaces):
245
    class Test(metaclass=namespaces.Namespaceable):
246
        with namespaces.Namespace() as ns:
247
            foo = 1
248
        assert ns.foo == 1
249
        ns.bar = 2
250
        assert ns.bar == 2
251
    assert Test.ns.foo == 1
252
    assert Test.ns.bar == 2
253
254
255
def test_basic_prop(namespaces):
256
    class Test(metaclass=namespaces.Namespaceable):
257
        with namespaces.Namespace() as ns:
258
            @property
259
            def foo(self):
260
                return 1
261
    assert Test().ns.foo == 1
262
263
264
def test_override_method(namespaces):
265
    class Test(metaclass=namespaces.Namespaceable):
266
        with namespaces.Namespace() as ns:
267
            def foo(self):
268
                return 1
269
    test = Test()
270
    assert test.ns.foo() == 1
271
    test.ns.foo = 2
272
    print(vars(test))
273
    assert test.ns.foo == 2
274
    del test.ns.foo
275
    assert test.ns.foo() == 1
276
    Test.ns.foo = 3
277
    assert Test.ns.foo == 3
278
    assert test.ns.foo == 3
279
280
281
def test_can_t_preload_with_namespace(namespaces):
282
    with pytest.raises(ValueError):
283
        namespaces.Namespace(ns=namespaces.Namespace())
284
285
286
def test_add_later(namespaces):
287
    class Test(metaclass=namespaces.Namespaceable):
288
        pass
289
290
    Test.ns = namespaces.Namespace()
291
    Test.ns.ns = namespaces.Namespace()
292
    Test.ns.value = 1
293
    Test.ns.ns.value = 2
294
    assert Test.ns.value == 1
295
    assert Test.ns.ns.value == 2
296
297
298
@pytest.mark.xfail(sys.version_info < (3, 6),
299
                   reason="python3.6 api changes", strict=True)
300
def test_3_6_descriptor(namespaces):
301
    class Descriptor:
302
        def __set_name__(self, owner, name):
303
            self.owner = owner
304
            self.name = name
305
    assert namespaces.namespaces._DescriptorInspector(
306
        Descriptor()).is_descriptor
307
308
    class Test(metaclass=namespaces.Namespaceable):
309
        with namespaces.Namespace() as ns:
310
            d = Descriptor()
311
312
    assert Test.ns.d.name == 'd'
313
314
315
def test_basic_meta(namespaces):
316
    class Meta(type, metaclass=namespaces.Namespaceable):
317
        with namespaces.Namespace() as ns:
318
            meta_var = 1
319
320
    class Test(metaclass=Meta):
321
        pass
322
323
    assert Meta.ns.meta_var == 1
324
    assert Test.ns.meta_var == 1
325
326
327
def test_somewhat_weird_meta(namespaces):
328
    class Meta(namespaces.Namespaceable, metaclass=namespaces.Namespaceable):
329
        with namespaces.Namespace() as ns:
330
            meta_var = 1
331
332
    class Test(metaclass=Meta):
333
        pass
334
335
    assert Meta.ns.meta_var == 1
336
    assert Test.ns.meta_var == 1
337
    with pytest.raises(AttributeError):
338
        Test().ns.meta_var
339
340
341
def test_somewhat_weirder_meta(namespaces):
342
    class Meta(namespaces.Namespaceable, metaclass=namespaces.Namespaceable):
343
        with namespaces.Namespace() as ns:
344
            meta_var = 1
345
346
    class Test(metaclass=Meta):
347
        with namespaces.Namespace() as ns:
348
            cls_var = 2
349
350
    assert Meta.ns.meta_var == 1
351
    assert Test.ns.meta_var == 1
352
    assert Test.ns.cls_var == 2
353
    assert Test().ns.cls_var == 2
354
    with pytest.raises(AttributeError):
355
        Test().ns.meta_var
356