/usr/lib/python2.7/dist-packages/openpyxl/workbook/tests/test_defined_name.py is in python-openpyxl 2.4.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
import pytest
from openpyxl.xml.functions import fromstring, tostring
from openpyxl.tests.helper import compare_xml
@pytest.fixture
def DefinedName():
from ..defined_name import DefinedName
return DefinedName
@pytest.mark.parametrize("value, reserved",
[
("_xlnm.Print_Area", True),
("_xlnm.Print_Titles", True),
("_xlnm.Criteria", True),
("_xlnm._FilterDatabase", True),
("_xlnm.Extract", True),
("_xlnm.Consolidate_Area", True),
("_xlnm.Sheet_Title", True),
("_xlnm.Pi", False),
("Pi", False),
]
)
def test_reserved(value, reserved):
from ..defined_name import RESERVED_REGEX
match = RESERVED_REGEX.match(value) is not None
assert match == reserved
@pytest.mark.parametrize("value, expected",
[
("CD:DE", "CD:DE"),
("$CD:$DE", "$CD:$DE"),
]
)
def test_print_rows(value, expected):
from ..defined_name import COL_RANGE_RE
match = COL_RANGE_RE.match(value)
assert match.group("cols") == expected
@pytest.mark.parametrize("value, expected",
[
("1:1", "1:1"),
("$2:$5", "$2:$5"),
]
)
def test_print_cols(value, expected):
from ..defined_name import ROW_RANGE_RE
match = ROW_RANGE_RE.match(value)
assert match.group("rows") == expected
@pytest.mark.parametrize("value, expected",
[
("Sheet!$1:$1",
{ 'notquoted': 'Sheet', 'rows': '$1:$1'}
),
("Sheet!$1:$1,C:D",
{'cols': 'C:D', 'notquoted': 'Sheet', 'rows': '$1:$1'}
),
("'Blatt5'!$C:$D",
{'cols': '$C:$D', 'quoted': 'Blatt5',}
),
("'Sheet 1'!$A:$A,'Sheet 1'!$1:$1",
{'quoted': "Sheet 1", 'cols': '$A:$A', 'rows': "$1:$1"}
),
]
)
def test_print_titles(value, expected):
from ..defined_name import TITLES_REGEX
scanner = TITLES_REGEX.finditer(value)
kw = dict((k, v) for match in scanner
for k, v in match.groupdict().items() if v)
assert kw == expected
@pytest.mark.parametrize("value, expected",
[
("Sheet1!$1:$2,$A:$A",
("$1:$2", "$A:$A")
),
("'Sheet 1'!$A:$A,'Sheet 1'!$1:$1",
("$1:$1", "$A:$A"),
),
]
)
def test_unpack_print_titles(DefinedName, value, expected):
from ..defined_name import _unpack_print_titles
defn = DefinedName(name="Print_Titles")
defn.value = value
assert _unpack_print_titles(defn) == expected
@pytest.mark.parametrize("value, expected",
[
("Sheet1!$A$1:$E$15", ["$A$1:$E$15"]),
("'Blatt1'!$A$1:$F$14,'Blatt1'!$H$10:$I$17,Blatt1!$I$16:$K$25",
["$A$1:$F$14","$H$10:$I$17","$I$16:$K$25"]),
("MySheet!#REF!", []),
("'C,D'!$A$1:$B$3", ["$A$1:$B$3"]),
("Sheet!$A$1:$D$5,Sheet!$B$9:$F$14", ["$A$1:$D$5", "$B$9:$F$14"]),
]
)
def test_unpack_print_area(DefinedName, value, expected):
from ..defined_name import _unpack_print_area
defn = DefinedName(name="Print_Area")
defn.value = value
assert _unpack_print_area(defn) == expected
class TestDefinition:
def test_write(self, DefinedName):
defn = DefinedName(name="pi",)
defn.value = 3.14
xml = tostring(defn.to_tree())
expected = """
<definedName name="pi">3.14</definedName>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.mark.parametrize("src, name, value, value_type",
[
("""<definedName name="B1namedrange">Sheet1!$A$1</definedName>""",
"B1namedrange",
"Sheet1!$A$1",
"RANGE"
),
("""<definedName name="references_external_workbook">[1]Sheet1!$A$1</definedName>""",
"references_external_workbook",
"[1]Sheet1!$A$1",
"RANGE"
),
( """<definedName name="references_nr_in_ext_wb">[1]!B2range</definedName>""",
"references_nr_in_ext_wb",
"[1]!B2range",
"RANGE"
),
( """<definedName name="references_other_named_range">B1namedrange</definedName>""",
"references_other_named_range",
"B1namedrange",
"RANGE"
),
("""<definedName name="pi">3.14</definedName>""",
"pi",
"3.14",
"NUMBER"
),
("""<definedName name="pi">3.14</definedName>""",
"pi",
"3.14",
"NUMBER"
),
("""<definedName name="name">"charlie"</definedName>""",
"name",
'"charlie"',
"TEXT"
),
(
"""<definedName name="THE_GREAT_ANSWER">'My Sheeet with a , and '''!$U$16:$U$24,'My Sheeet with a , and '''!$V$28:$V$36</definedName>""",
"THE_GREAT_ANSWER",
"'My Sheeet with a , and '''!$U$16:$U$24,'My Sheeet with a , and '''!$V$28:$V$36",
"RANGE"
),
]
)
def test_from_xml(self, DefinedName, src, name, value, value_type):
node = fromstring(src)
defn = DefinedName.from_tree(node)
assert defn.name == name
assert defn.value == value
assert defn.type == value_type
@pytest.mark.parametrize("value, destinations",
[
(
"Sheet1!$C$5:$C$7,Sheet1!$C$9:$C$11,Sheet1!$E$5:$E$7",
(
("Sheet1", '$C$5:$C$7'),
("Sheet1", '$C$9:$C$11'),
("Sheet1", '$E$5:$E$7'),
)
),
(
"'Sheet 1'!$A$1",
(
("Sheet 1", "$A$1"),
)
),
]
)
def test_destinations(self, DefinedName, value, destinations):
defn = DefinedName(name="some")
defn.value = value
assert defn.type == "RANGE"
des = tuple(defn.destinations)
assert des == destinations
@pytest.mark.parametrize("name, expected",
[
("some_range", {'name':'some_range'}),
("Print_Titles", {'name':'_xlnm.Print_Titles'}),
]
)
def test_dict(self, DefinedName, name, expected):
defn = DefinedName(name)
assert dict(defn) == expected
@pytest.mark.parametrize("value, expected",
[
("'My Sheet'!$D$8", 'RANGE'),
("Sheet1!$A$1", 'RANGE'),
("[1]Sheet1!$A$1", 'RANGE'),
("[1]!B2range", 'RANGE'),
("OFFSET(MODEL!$A$1,'Stock Graphs'!$D$3-1,'Stock Graphs'!$C$25+5,'Stock Graphs'!$D$6,1)/1.65", 'FUNC'),
("B1namedrange", 'RANGE'), # this should not be a range
]
)
def test_check_type(self, DefinedName, value, expected):
defn = DefinedName(name="test")
defn.value = value
assert defn.type == expected
@pytest.mark.parametrize("value, expected",
[
("'My Sheet'!$D$8", False),
("Sheet1!$A$1", False),
("[1]Sheet1!$A$1", True),
("[1]!B2range", True),
])
def test_external_range(self, DefinedName, value, expected):
defn = DefinedName(name="test")
defn.value = value
assert defn.is_external is expected
@pytest.fixture
def DefinedNameList():
from ..defined_name import DefinedNameList
return DefinedNameList
class TestDefinitionList:
def test_read(self, DefinedNameList, datadir):
datadir.chdir()
with open("workbook.xml") as src:
xml = src.read()
node = fromstring(xml)
dl = DefinedNameList.from_tree(node)
assert len(dl) == 6
def test_append(self, DefinedNameList, DefinedName):
dl = DefinedNameList()
defn = DefinedName("test")
dl.append(defn)
assert len(dl) == 1
def test_append_only(self, DefinedNameList):
dl = DefinedNameList(definedName=("test",))
with pytest.raises(TypeError):
dl.append("test")
def test_contains(self, DefinedNameList, DefinedName):
dl = DefinedNameList()
defn = DefinedName("test")
dl.append(defn)
assert "test" in dl
@pytest.mark.parametrize("scope,",
[
None,
0,
]
)
def test_duplicate(self, DefinedNameList, DefinedName, scope):
dl = DefinedNameList()
defn = DefinedName("test", localSheetId=scope)
assert not dl._duplicate(defn)
dl.append(defn)
assert dl._duplicate(defn)
def test_cleanup(self, DefinedNameList, datadir):
datadir.chdir()
with open("broken_print_titles.xml") as src:
xml = src.read()
node = fromstring(xml)
dl = DefinedNameList.from_tree(node)
assert len(dl) == 4
dl._cleanup()
assert len(dl) == 2
def test_localnames(self, DefinedNameList, datadir):
datadir.chdir()
with open("workbook.xml", "rb") as src:
xml = src.read()
node = fromstring(xml)
dl = DefinedNameList.from_tree(node)
assert dl.localnames(0) == ['MySheetRef', 'MySheetValue']
|