Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
81 lines (73 loc) · 3.21 KB
/
Copy pathconftest.py
File metadata and controls
81 lines (73 loc) · 3.21 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
import copy
import warnings
from collections.abc import Iterator
import pytest
from nameparser.config import CONSTANTS
# Scalar (non-collection) config attributes that individual tests mutate on the
# global CONSTANTS singleton. Several tests change these without restoring them;
# the original suite only survived because unittest happens to run methods in
# alphabetical order, so a later test reset the value. pytest runs in definition
# order, so we snapshot and restore these around every test to keep tests
# isolated regardless of order.
#
# empty_attribute_default is gone from this list: it was removed in 2.0 (#255,
# see nameparser/_config_shim.py's Constants.__setattr__), so there is no
# longer a second parsing path to snapshot/restore or dual-run against. Empty
# attributes are always ''.
_SCALAR_CONFIG_ATTRS = (
"string_format",
"initials_format",
"initials_delimiter",
"initials_separator",
"suffix_delimiter",
"capitalize_name",
"force_mixed_case_capitalization",
)
# Collection config attributes (the SetManager / TupleManager constants). Tests
# that customize the global CONSTANTS — e.g. adding or removing a title — mutate
# these in place, so a shallow snapshot of the reference would not protect later
# tests. We snapshot independent copies and restore them, making collection
# mutations order-independent too.
#
# regexes is gone from this list: it is a read-only _RegexesProxy in 2.0 (set
# once in Constants.__init__), and reassigning it raises TypeError, so it can
# neither be mutated by a test nor restored by this fixture.
_COLLECTION_CONFIG_ATTRS = (
"prefixes",
"suffix_acronyms",
"suffix_not_acronyms",
"titles",
"first_name_titles",
"conjunctions",
"bound_first_names",
"non_first_name_prefixes",
"capitalization_exceptions",
"nickname_delimiters",
"maiden_delimiters",
)
@pytest.fixture(autouse=True)
def _isolate_constants() -> Iterator[None]:
"""Snapshot and restore the shared CONSTANTS singleton around every test.
Formerly also drove a parametrized dual-run over empty_attribute_default
settings (reproducing the original tests.py __main__ block, which ran the
whole suite twice as a regression check that the three parsing code paths
agreed). That setting no longer exists in 2.0 (#255: empty attributes are
always ''), so only the isolation half of the original fixture remains.
"""
scalar_snapshot = {attr: getattr(CONSTANTS, attr) for attr in _SCALAR_CONFIG_ATTRS}
collection_snapshot = {
attr: copy.deepcopy(getattr(CONSTANTS, attr))
for attr in _COLLECTION_CONFIG_ATTRS
}
yield
# Restoring through the shared singleton is itself a mutation and would
# otherwise emit the shared-CONSTANTS DeprecationWarning (#262) on every
# single test's teardown; this fixture is infrastructure; it is not
# exercising that deprecation, so it's silenced narrowly here rather than
# at every test.
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
for attr, value in scalar_snapshot.items():
setattr(CONSTANTS, attr, value)
for attr, value in collection_snapshot.items():
setattr(CONSTANTS, attr, value)