File: test_core.py

package info (click to toggle)
python-echo 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 368 kB
  • sloc: python: 2,421; makefile: 148
file content (639 lines) | stat: -rw-r--r-- 14,703 bytes parent folder | download | duplicates (2)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import pytest
from unittest.mock import MagicMock

from echo import (CallbackProperty, add_callback,
                  remove_callback, delay_callback,
                  ignore_callback, callback_property,
                  HasCallbackProperties, keep_in_sync)


class Stub(object):
    prop1 = CallbackProperty()
    prop2 = CallbackProperty(5)
    prop3 = 5


class DecoratorStub(object):

    def __init__(self):
        self._val = 1

    @callback_property
    def prop(self):
        return self._val * 2

    @prop.setter
    def prop(self, value):
        self._val = value


def test_attribute_like_access():
    stub = Stub()
    assert stub.prop1 is None
    assert stub.prop2 == 5


def test_attribute_like_set():
    stub = Stub()
    stub.prop1 = 10
    assert stub.prop1 == 10


def test_class_access():
    stub = Stub()
    assert isinstance(type(stub).prop1, CallbackProperty)


def test_callback_fire_on_change():
    stub = Stub()
    test = MagicMock()
    add_callback(stub, 'prop1', test)
    stub.prop1 = 5
    test.assert_called_once_with(5)


def test_callbacks_only_called_on_value_change():
    stub = Stub()
    test = MagicMock()
    add_callback(stub, 'prop1', test)
    stub.prop1 = 5
    test.assert_called_once_with(5)
    stub.prop1 = 5
    assert test.call_count == 1


def test_callbacks_are_instance_specific():
    s1, s2 = Stub(), Stub()
    test = MagicMock()
    add_callback(s2, 'prop1', test)
    s1.prop1 = 100
    assert test.call_count == 0


def test_remove_callback():
    stub = Stub()
    test = MagicMock()
    add_callback(stub, 'prop1', test)
    remove_callback(stub, 'prop1', test)
    stub.prop1 = 5
    assert test.call_count == 0


def test_add_callback_attribute_error_on_bad_name():
    stub = Stub()
    with pytest.raises(AttributeError):
        add_callback(stub, 'bad_property', None)


def test_add_callback_type_error_if_not_calllback():
    stub = Stub()
    with pytest.raises(TypeError) as exc:
        add_callback(stub, 'prop3', None)
    assert exc.value.args[0] == "prop3 is not a CallbackProperty"


def test_remove_callback_attribute_error_on_bad_name():
    stub = Stub()
    with pytest.raises(AttributeError):
        remove_callback(stub, 'bad_property', None)


def test_remove_callback_wrong_function():
    stub = Stub()
    test = MagicMock()
    test2 = MagicMock()
    add_callback(stub, 'prop1', test)
    with pytest.raises(ValueError) as exc:
        remove_callback(stub, 'prop1', test2)
    assert exc.value.args[0].startswith('Callback function not found')


def test_remove_non_callback_property():
    stub = Stub()
    with pytest.raises(TypeError) as exc:
        remove_callback(stub, 'prop3', None)
    assert exc.value.args[0] == 'prop3 is not a CallbackProperty'


def test_remove_callback_not_found():
    stub = Stub()
    with pytest.raises(ValueError) as exc:
        remove_callback(stub, 'prop1', None)
    assert exc.value.args[0] == "Callback function not found: None"


def test_disable_callback():
    stub = Stub()
    test = MagicMock()

    add_callback(stub, 'prop1', test)
    Stub.prop1.disable(stub)

    stub.prop1 = 100
    assert test.call_count == 0

    Stub.prop1.enable(stub)
    stub.prop1 = 100
    assert test.call_count == 0  # not changed
    stub.prop1 = 200
    assert test.call_count == 1


def test_delay_callback():
    test = MagicMock()
    stub = Stub()

    add_callback(stub, 'prop1', test)
    with delay_callback(stub, 'prop1'):
        stub.prop1 = 100
        stub.prop1 = 200
        stub.prop1 = 300
        assert test.call_count == 0
    test.assert_called_once_with(300)


def test_delay_callback_nested():
    test = MagicMock()
    stub = Stub()

    add_callback(stub, 'prop1', test)
    with delay_callback(stub, 'prop1'):
        with delay_callback(stub, 'prop1'):
            stub.prop1 = 100
            stub.prop1 = 200
            stub.prop1 = 300
            assert test.call_count == 0
        assert test.call_count == 0
    test.assert_called_once_with(300)


def test_delay_callback_not_called_if_unmodified():
    test = MagicMock()
    stub = Stub()
    add_callback(stub, 'prop1', test)
    with delay_callback(stub, 'prop1'):
        pass
    assert test.call_count == 0


def test_callback_with_two_arguments():
    stub = Stub()
    stub.prop1 = 5
    on_change = MagicMock()
    add_callback(stub, 'prop1', on_change, echo_old=True)
    stub.prop1 = 10

    on_change.assert_called_once_with(5, 10)


@pytest.mark.parametrize('context_func', (delay_callback, ignore_callback))
def test_context_on_non_callback(context_func):
    stub = Stub()
    with pytest.raises(TypeError) as exc:
        with context_func(stub, 'prop3'):
            pass
    assert exc.value.args[0] == "prop3 is not a CallbackProperty"


def test_delay_multiple():
    stub = Stub()
    test = MagicMock()
    test2 = MagicMock()

    add_callback(stub, 'prop1', test)
    add_callback(stub, 'prop2', test2)

    with delay_callback(stub, 'prop1', 'prop2'):
        stub.prop1 = 50
        stub.prop1 = 100
        stub.prop2 = 200
        assert test.call_count == 0
        assert test2.call_count == 0

    test.assert_called_once_with(100)
    test2.assert_called_once_with(200)


def test_ignore_multiple():
    stub = Stub()
    test = MagicMock()
    test2 = MagicMock()

    add_callback(stub, 'prop1', test)
    add_callback(stub, 'prop2', test2)

    with ignore_callback(stub, 'prop1', 'prop2'):
        stub.prop1 = 100
        stub.prop2 = 200
        assert test.call_count == 0
        assert test2.call_count == 0

    assert test.call_count == 0
    assert test2.call_count == 0


def test_delay_only_calls_if_changed():
    stub = Stub()
    test = MagicMock()

    add_callback(stub, 'prop1', test)

    with delay_callback(stub, 'prop1'):
        pass
    assert test.call_count == 0

    val = stub.prop1
    with delay_callback(stub, 'prop1'):
        stub.prop1 = val
    assert test.call_count == 0


def test_decorator_form():
    stub = DecoratorStub()
    test = MagicMock()
    add_callback(stub, 'prop', test)

    assert stub.prop == 2

    stub.prop = 5
    test.assert_called_once_with(10)

    assert stub.prop == 10


def test_docstring():

    class Simple(object):
        a = CallbackProperty(docstring='important')

    s = Simple()
    assert type(s).a.__doc__ == 'important'


class State(HasCallbackProperties):

    a = CallbackProperty()
    b = CallbackProperty()
    c = CallbackProperty()
    d = 1


def test_class_add_remove_callback():

    state = State()

    class mockclass(object):

        def __init__(self):
            self.call_count = 0
            self.args = ()
            self.kwargs = {}

        def __call__(self, *args, **kwargs):
            self.args = args
            self.kwargs = kwargs
            self.call_count += 1

    test1 = mockclass()
    state.add_callback('a', test1)

    # Deliberaty adding to c twice to make sure it works fine with two callbacks
    test2 = mockclass()
    state.add_callback('c', test2)

    test3 = mockclass()
    state.add_callback('c', test3, echo_old=True)

    test4 = mockclass()
    state.add_global_callback(test4)

    state.a = 1
    assert test1.call_count == 1
    assert test1.args == (1,)
    assert test2.call_count == 0
    assert test3.call_count == 0
    assert test4.call_count == 1
    assert test4.kwargs == dict(a=1)

    state.b = 1
    assert test1.call_count == 1
    assert test2.call_count == 0
    assert test3.call_count == 0
    assert test4.call_count == 2
    assert test4.kwargs == dict(b=1)

    state.c = 1
    assert test1.call_count == 1
    assert test2.call_count == 1
    assert test4.kwargs == dict(c=1)
    assert test3.call_count == 1
    assert test3.args == (None, 1)
    assert test4.call_count == 3
    assert test4.kwargs == dict(c=1)

    state.remove_callback('a', test1)

    state.a = 2
    state.b = 2
    state.c = 2
    assert test1.call_count == 1
    assert test2.call_count == 2
    assert test3.call_count == 2
    assert test4.call_count == 6

    state.remove_callback('c', test2)

    state.a = 3
    state.b = 3
    state.c = 3
    assert test1.call_count == 1
    assert test2.call_count == 2
    assert test3.call_count == 3
    assert test4.call_count == 9

    state.remove_callback('c', test3)

    state.a = 4
    state.b = 4
    state.c = 4
    assert test1.call_count == 1
    assert test2.call_count == 2
    assert test3.call_count == 3
    assert test4.call_count == 12

    state.remove_global_callback(test4)

    state.a = 6
    state.b = 6
    state.c = 6
    assert test1.call_count == 1
    assert test2.call_count == 2
    assert test3.call_count == 3
    assert test4.call_count == 12


def test_class_is_callback_property():

    state = State()
    assert state.is_callback_property('a')
    assert state.is_callback_property('b')
    assert state.is_callback_property('c')
    assert not state.is_callback_property('d')


def test_class_add_remove_callback_invalid():

    def callback():
        pass

    state = State()
    state.z = 'banana'
    with pytest.raises(TypeError) as exc:
        state.add_callback('banana', callback)
    assert exc.value.args[0] == "attribute 'banana' is not a callback property"
    with pytest.raises(TypeError) as exc:
        state.remove_callback('banana', callback)
    assert exc.value.args[0] == "attribute 'banana' is not a callback property"


def test_keep_in_sync():

    class State1(object):
        a = CallbackProperty()
        b = CallbackProperty()

    class State2(object):
        c = CallbackProperty()

    state1 = State1()
    state2 = State2()

    state1_control = State1()
    state2_control = State2()

    s1 = keep_in_sync(state1, 'a', state1, 'b')
    s2 = keep_in_sync(state1, 'a', state2, 'c')

    state1.a = 1
    assert state1.b == 1
    assert state1_control.b is None
    assert state2.c == 1
    assert state2_control.c is None

    state1.b = 3
    assert state1.a == 3
    assert state1_control.a is None
    assert state2.c == 3
    assert state2_control.c is None

    state2.c = 5
    assert state1.a == 5
    assert state1_control.a is None
    assert state1.b == 5
    assert state1_control.b is None

    s1.disable_syncing()

    state1.a = 7
    assert state1.b == 5
    assert state1_control.b is None
    assert state2.c == 7
    assert state2_control.c is None

    s2.disable_syncing()


def test_cleanup_when_objects_destroyed():

    state = State()

    class BasicClass():

        def __init__(self, s):
            self.s = s
            self.s.add_callback('a', self.callback)
            self.raise_error = False

        def callback(self, arg):
            if self.raise_error:
                raise ValueError('Should never get here')

    def isolated(state):
        c = BasicClass(state)
        state.a = 1
        c.raise_error = True

    isolated(state)

    state.a = 2


def test_cleanup_when_objects_destroyed_kwargs():

    state = State()

    class BasicClass():

        def __init__(self, s):
            self.s = s
            self.s.add_global_callback(self.callback)
            self.raise_error = False

        def callback(self, **kwargs):
            if self.raise_error:
                raise ValueError('Should never get here')

    def isolated(state):
        c = BasicClass(state)
        state.a = 1
        c.raise_error = True

    isolated(state)

    state.a = 2


def test_delay_global_callback():

    # Regression test to make sure that delay_callback works for global
    # callbacks too.

    state = State()

    test1 = MagicMock()
    state.add_callback('a', test1)

    test2 = MagicMock()
    state.add_global_callback(test2)

    with delay_callback(state, 'a'):
        state.a = 100
        assert test1.call_count == 0
        assert test2.call_count == 0

    test1.assert_called_once_with(100)
    test2.assert_called_once_with(a=100)

    test2.reset_mock()

    with delay_callback(state, 'a'):
        state.b = 200
        assert test2.call_count == 1

    test2.assert_called_once_with(b=200)

    test2.reset_mock()

    with delay_callback(state, 'a', 'b'):
        state.a = 300
        state.b = 400
        assert test2.call_count == 0

    test2.assert_called_once_with(a=300, b=400)


def test_delay_global_callback_stub():

    # Make sure that adding the global callback delay functionality doesn't
    # break things when we are dealing with a plain class without HasCallbackProperties

    stub = Stub()

    test1 = MagicMock()
    add_callback(stub, 'prop1', test1)

    with delay_callback(stub, 'prop1'):
        stub.prop1 = 100
        assert test1.call_count == 0

    test1.assert_called_once_with(100)


def test_ignore_global_callback():

    # Regression test to make sure that ignore_callback works for global
    # callbacks too.

    state = State()

    test1 = MagicMock()
    state.add_callback('a', test1)

    test2 = MagicMock()
    state.add_global_callback(test2)

    with ignore_callback(state, 'a'):
        state.a = 100
        assert test1.call_count == 0
        assert test2.call_count == 0

    assert test1.call_count == 0
    assert test2.call_count == 0

    test2.reset_mock()

    with ignore_callback(state, 'a'):
        state.b = 200
        assert test2.call_count == 1

    test2.assert_called_once_with(b=200)

    test2.reset_mock()

    with ignore_callback(state, 'a', 'b'):
        state.a = 300
        state.b = 400
        assert test2.call_count == 0

    assert test2.call_count == 0


def test_ignore_global_callback_stub():

    # Make sure that adding the global callback ignore functionality doesn't
    # break things when we are dealing with a plain class without HasCallbackProperties

    stub = Stub()

    test1 = MagicMock()
    add_callback(stub, 'prop1', test1)

    with ignore_callback(stub, 'prop1'):
        stub.prop1 = 100
        assert test1.call_count == 0

    assert test1.call_count == 0


def test_delay_in_delayed_callback():

    # Regression test for a bug that occurred if a delayed callback included
    # a delay itself.

    state = State()

    def callback(*args, **kwargs):
        with delay_callback(state, 'a'):
            state.a = 2

    state.add_callback('a', callback)

    with delay_callback(state, 'a', 'b'):
        state.a = 100


def test_ignore_in_ignored_callback():

    # Regression test for a bug that occurred if a delayed callback included
    # a delay itself.

    state = State()

    def callback(*args, **kwargs):
        with ignore_callback(state, 'a'):
            state.a = 2

    state.add_callback('a', callback)

    with ignore_callback(state, 'a', 'b'):
        state.a = 100