File size: 2,036 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import string
import time
from typing import Any, Callable

import pytest

from ...base import random_token, translate_dict_func, default_func, ControllableService


@pytest.mark.unittest
class TestInteractionBaseCommon:

    def test_random_token(self):
        assert len(random_token()) == 64
        assert len(random_token(32)) == 32
        assert set(random_token()) - set(string.hexdigits) == set()

    def test_translate_dict_func(self):
        assert translate_dict_func({
            'a': lambda: 2,
            'b': lambda: 3,
            'sum': lambda: 5,
        })() == {
            'a': 2,
            'b': 3,
            'sum': 5
        }
        assert translate_dict_func(
            {
                'a': lambda ax, bx: 2 + ax,
                'b': lambda ax, bx: 3 + bx,
                'sum': lambda ax, bx: 5 + ax + bx,
            }
        )(4, 5) == {
            'a': 6,
            'b': 8,
            'sum': 14
        }

    def test_default_func(self):

        def _calculate(a: int, b: int, callback: Callable[..., Any] = None):
            return default_func(233)(callback)(a, b)

        assert _calculate(1, 2) == 233
        assert _calculate(1, 2, lambda a, b: a + b) == 3
        assert _calculate(1, 2, lambda a, b: a * b) == 2


@pytest.mark.unittest
class TestInteractionBaseControllableService:

    def test_it(self):
        _start, _shutdown, _finished = False, False, False

        class _Service(ControllableService):

            def start(self):
                nonlocal _start
                _start = True

            def shutdown(self):
                nonlocal _shutdown
                _shutdown = True

            def join(self):
                time.sleep(1.0)
                nonlocal _finished
                _finished = True

        assert (_start, _shutdown, _finished) == (False, False, False)
        with _Service():
            assert (_start, _shutdown, _finished) == (True, False, False)

        assert (_start, _shutdown, _finished) == (True, True, True)