forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunitutil.py
More file actions
145 lines (109 loc) · 4.31 KB
/
unitutil.py
File metadata and controls
145 lines (109 loc) · 4.31 KB
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
# encoding: utf-8
"""
Utility functions for unit testing
"""
import os
from mock import create_autospec, Mock, patch, PropertyMock
from docx.oxml.shared import serialize_for_reading
_thisdir = os.path.split(__file__)[0]
test_file_dir = os.path.abspath(os.path.join(_thisdir, 'test_files'))
def abspath(relpath):
thisdir = os.path.split(__file__)[0]
return os.path.abspath(os.path.join(thisdir, relpath))
def actual_xml(elm):
return serialize_for_reading(elm)
def absjoin(*paths):
return os.path.abspath(os.path.join(*paths))
def docx_path(name):
"""
Return the absolute path to test .docx file with root name *name*.
"""
return absjoin(_thisdir, 'test_files', '%s.docx' % name)
def test_file(name):
"""
Return the absolute path to test file having *name*.
"""
return absjoin(_thisdir, 'test_files', name)
# ===========================================================================
# pytest mocking helpers
# ===========================================================================
def class_mock(request, q_class_name, autospec=True, **kwargs):
"""
Return a mock patching the class with qualified name *q_class_name*.
The mock is autospec'ed based on the patched class unless the optional
argument *autospec* is set to False. Any other keyword arguments are
passed through to Mock(). Patch is reversed after calling test returns.
"""
_patch = patch(q_class_name, autospec=autospec, **kwargs)
request.addfinalizer(_patch.stop)
return _patch.start()
def cls_attr_mock(request, cls, attr_name, name=None, **kwargs):
"""
Return a mock for attribute *attr_name* on *cls* where the patch is
reversed after pytest uses it.
"""
name = request.fixturename if name is None else name
_patch = patch.object(cls, attr_name, name=name, **kwargs)
request.addfinalizer(_patch.stop)
return _patch.start()
def function_mock(request, q_function_name, **kwargs):
"""
Return a mock patching the function with qualified name
*q_function_name*. Patch is reversed after calling test returns.
"""
_patch = patch(q_function_name, **kwargs)
request.addfinalizer(_patch.stop)
return _patch.start()
def initializer_mock(request, cls):
"""
Return a mock for the __init__ method on *cls* where the patch is
reversed after pytest uses it.
"""
_patch = patch.object(cls, '__init__', return_value=None)
request.addfinalizer(_patch.stop)
return _patch.start()
def instance_mock(request, cls, name=None, spec_set=True, **kwargs):
"""
Return a mock for an instance of *cls* that draws its spec from the class
and does not allow new attributes to be set on the instance. If *name* is
missing or |None|, the name of the returned |Mock| instance is set to
*request.fixturename*. Additional keyword arguments are passed through to
the Mock() call that creates the mock.
"""
name = name if name is not None else request.fixturename
return create_autospec(
cls, _name=name, spec_set=spec_set, instance=True, **kwargs
)
def loose_mock(request, name=None, **kwargs):
"""
Return a "loose" mock, meaning it has no spec to constrain calls on it.
Additional keyword arguments are passed through to Mock(). If called
without a name, it is assigned the name of the fixture.
"""
if name is None:
name = request.fixturename
return Mock(name=name, **kwargs)
def method_mock(request, cls, method_name, **kwargs):
"""
Return a mock for method *method_name* on *cls* where the patch is
reversed after pytest uses it.
"""
_patch = patch.object(cls, method_name, **kwargs)
request.addfinalizer(_patch.stop)
return _patch.start()
def property_mock(request, cls, prop_name, **kwargs):
"""
Return a mock for property *prop_name* on class *cls* where the patch is
reversed after pytest uses it.
"""
_patch = patch.object(cls, prop_name, new_callable=PropertyMock, **kwargs)
request.addfinalizer(_patch.stop)
return _patch.start()
def var_mock(request, q_var_name, **kwargs):
"""
Return a mock patching the variable with qualified name *q_var_name*.
Patch is reversed after calling test returns.
"""
_patch = patch(q_var_name, **kwargs)
request.addfinalizer(_patch.stop)
return _patch.start()