datasets

Форк
0
/
test_patching.py 
151 строка · 5.9 Кб
1
from datasets.utils.patching import _PatchedModuleObj, patch_submodule
2

3
from . import _test_patching
4

5

6
def test_patch_submodule():
7
    import os as original_os
8
    from os import path as original_path
9
    from os import rename as original_rename
10
    from os.path import dirname as original_dirname
11
    from os.path import join as original_join
12

13
    assert _test_patching.os is original_os
14
    assert _test_patching.path is original_path
15
    assert _test_patching.join is original_join
16

17
    assert _test_patching.renamed_os is original_os
18
    assert _test_patching.renamed_path is original_path
19
    assert _test_patching.renamed_join is original_join
20

21
    mock = "__test_patch_submodule_mock__"
22
    with patch_submodule(_test_patching, "os.path.join", mock):
23
        # Every way to access os.path.join must be patched, and the rest must stay untouched
24

25
        # check os.path.join
26
        assert isinstance(_test_patching.os, _PatchedModuleObj)
27
        assert isinstance(_test_patching.os.path, _PatchedModuleObj)
28
        assert _test_patching.os.path.join is mock
29

30
        # check path.join
31
        assert isinstance(_test_patching.path, _PatchedModuleObj)
32
        assert _test_patching.path.join is mock
33

34
        # check join
35
        assert _test_patching.join is mock
36

37
        # check that the other attributes are untouched
38
        assert _test_patching.os.rename is original_rename
39
        assert _test_patching.path.dirname is original_dirname
40
        assert _test_patching.os.path.dirname is original_dirname
41

42
        # Even renamed modules or objects must be patched
43

44
        # check renamed_os.path.join
45
        assert isinstance(_test_patching.renamed_os, _PatchedModuleObj)
46
        assert isinstance(_test_patching.renamed_os.path, _PatchedModuleObj)
47
        assert _test_patching.renamed_os.path.join is mock
48

49
        # check renamed_path.join
50
        assert isinstance(_test_patching.renamed_path, _PatchedModuleObj)
51
        assert _test_patching.renamed_path.join is mock
52

53
        # check renamed_join
54
        assert _test_patching.renamed_join is mock
55

56
        # check that the other attributes are untouched
57
        assert _test_patching.renamed_os.rename is original_rename
58
        assert _test_patching.renamed_path.dirname is original_dirname
59
        assert _test_patching.renamed_os.path.dirname is original_dirname
60

61
    # check that everthing is back to normal when the patch is over
62

63
    assert _test_patching.os is original_os
64
    assert _test_patching.path is original_path
65
    assert _test_patching.join is original_join
66

67
    assert _test_patching.renamed_os is original_os
68
    assert _test_patching.renamed_path is original_path
69
    assert _test_patching.renamed_join is original_join
70

71

72
def test_patch_submodule_builtin():
73
    assert _test_patching.open is open
74

75
    mock = "__test_patch_submodule_builtin_mock__"
76
    # _test_patching has "open" in its globals
77
    assert _test_patching.open is open
78
    with patch_submodule(_test_patching, "open", mock):
79
        assert _test_patching.open is mock
80

81
    # check that everthing is back to normal when the patch is over
82

83
    assert _test_patching.open is open
84

85

86
def test_patch_submodule_missing():
87
    # pandas.read_csv is not present in _test_patching
88
    mock = "__test_patch_submodule_missing_mock__"
89
    with patch_submodule(_test_patching, "pandas.read_csv", mock):
90
        pass
91

92

93
def test_patch_submodule_missing_builtin():
94
    # builtin should always be mocked even if they're not in the globals
95
    # in case they're loaded at one point
96
    mock = "__test_patch_submodule_missing_builtin_mock__"
97
    # _test_patching doesn't have "len" in its globals
98
    assert getattr(_test_patching, "len", None) is None
99
    with patch_submodule(_test_patching, "len", mock):
100
        assert _test_patching.len is mock
101
    assert _test_patching.len is len
102

103

104
def test_patch_submodule_start_and_stop():
105
    mock = "__test_patch_submodule_start_and_stop_mock__"
106
    patch = patch_submodule(_test_patching, "open", mock)
107
    assert _test_patching.open is open
108
    patch.start()
109
    assert _test_patching.open is mock
110
    patch.stop()
111
    assert _test_patching.open is open
112

113

114
def test_patch_submodule_successive():
115
    from os import rename as original_rename
116
    from os.path import dirname as original_dirname
117
    from os.path import join as original_join
118

119
    mock_join = "__test_patch_submodule_successive_join__"
120
    mock_dirname = "__test_patch_submodule_successive_dirname__"
121
    mock_rename = "__test_patch_submodule_successive_rename__"
122
    assert _test_patching.os.path.join is original_join
123
    assert _test_patching.os.path.dirname is original_dirname
124
    assert _test_patching.os.rename is original_rename
125

126
    with patch_submodule(_test_patching, "os.path.join", mock_join):
127
        with patch_submodule(_test_patching, "os.rename", mock_rename):
128
            with patch_submodule(_test_patching, "os.path.dirname", mock_dirname):
129
                assert _test_patching.os.path.join is mock_join
130
                assert _test_patching.os.path.dirname is mock_dirname
131
                assert _test_patching.os.rename is mock_rename
132

133
    # try another order
134
    with patch_submodule(_test_patching, "os.rename", mock_rename):
135
        with patch_submodule(_test_patching, "os.path.join", mock_join):
136
            with patch_submodule(_test_patching, "os.path.dirname", mock_dirname):
137
                assert _test_patching.os.path.join is mock_join
138
                assert _test_patching.os.path.dirname is mock_dirname
139
                assert _test_patching.os.rename is mock_rename
140

141
    assert _test_patching.os.path.join is original_join
142
    assert _test_patching.os.path.dirname is original_dirname
143
    assert _test_patching.os.rename is original_rename
144

145

146
def test_patch_submodule_doesnt_exist():
147
    mock = "__test_patch_submodule_doesnt_exist_mock__"
148
    with patch_submodule(_test_patching, "__module_that_doesn_exist__.__attribute_that_doesn_exist__", mock):
149
        pass
150
    with patch_submodule(_test_patching, "os.__attribute_that_doesn_exist__", mock):
151
        pass
152

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.