1
from datasets.utils.patching import _PatchedModuleObj, patch_submodule
3
from . import _test_patching
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
13
assert _test_patching.os is original_os
14
assert _test_patching.path is original_path
15
assert _test_patching.join is original_join
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
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
26
assert isinstance(_test_patching.os, _PatchedModuleObj)
27
assert isinstance(_test_patching.os.path, _PatchedModuleObj)
28
assert _test_patching.os.path.join is mock
31
assert isinstance(_test_patching.path, _PatchedModuleObj)
32
assert _test_patching.path.join is mock
35
assert _test_patching.join is mock
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
42
# Even renamed modules or objects must be patched
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
49
# check renamed_path.join
50
assert isinstance(_test_patching.renamed_path, _PatchedModuleObj)
51
assert _test_patching.renamed_path.join is mock
54
assert _test_patching.renamed_join is mock
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
61
# check that everthing is back to normal when the patch is over
63
assert _test_patching.os is original_os
64
assert _test_patching.path is original_path
65
assert _test_patching.join is original_join
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
72
def test_patch_submodule_builtin():
73
assert _test_patching.open is open
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
81
# check that everthing is back to normal when the patch is over
83
assert _test_patching.open is open
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):
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
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
109
assert _test_patching.open is mock
111
assert _test_patching.open is open
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
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
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
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
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
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):
150
with patch_submodule(_test_patching, "os.__attribute_that_doesn_exist__", mock):