/usr/lib/python2.7/dist-packages/openpyxl/formula/tests/test_translate.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 | from __future__ import absolute_import
import pytest
@pytest.fixture
def Translator():
from .. import translate
return translate.Translator
@pytest.fixture
def Tokenizer():
from .. import tokenizer
return tokenizer.Tokenizer
@pytest.fixture
def TranslatorError():
from .. import translate
return translate.TranslatorError
class TestTranslator(object):
@pytest.mark.parametrize("origin, row, col", [
("A1", 1, 1),
("AA1", 1, 27),
("AA1001", 1001, 27),
("AA1001001001", 1001001001, 27),
("XFD111", 111, 16384),
])
def test_init(self, Translator, origin, row, col):
trans = Translator("=formula", origin)
assert trans.row == row
assert trans.col == col
assert trans.tokenizer.formula == "=formula"
@pytest.mark.parametrize("formula", [
'=IF(A$3<40%,"",INDEX(Pipeline!B$4:B$138,#REF!))',
"='Summary slices'!$C$3",
'=-MAX(Pipeline!AA4:AA138)',
'=TEXT(-S7/1000,"$#,##0""M""")',
"=IF(A$3<1.3E-8,\"\",IF(ISNA('External Ref'!K7)," +
'"N/A",TEXT(K7*1E+12,"0")&"bp"',
'=+IF(A$3<>$B7,"",(MIN(IF({TRUE, FALSE;1,2},A6:B6,$S7))>=' +
'LOWER_BOUND)*($BR6>$S72123))',
'=$A:$A,$C:$C',
"Just text"
"123.456",
"31/12/1999",
"",
])
def test_get_tokens(self, Translator, Tokenizer, formula):
trans = Translator(formula, "A1")
tok = Tokenizer(formula)
for t1, t2 in zip(tok.items, trans.get_tokens()):
assert t1.value == t2.value
assert t1.type == t2.type
assert t1.subtype == t2.subtype
@pytest.mark.parametrize("test_str, groups", [
["1:1", ("1", "1")],
["1234:5678", ("1234", "5678")],
["$1234:78910", ("$1234", "78910")],
["$12321:$23432", ("$12321", "$23432")],
["112233:$445566", ("112233", "$445566")],
["A:A", None],
["$ABC:AZZ", None],
["$DEF:$FOV",None],
["HA:$JA", None],
["named1", None],
["A15", None],
["$AB303", None],
["YY$101", None],
["$ZZ$99", None],
["B2:C3", None],
["$ATV25:$BBC35", None],
["WWW$918:WWW$919", None],
["$III$305:$IIT$503", None],
])
def test_row_range_re(self, Translator, test_str, groups):
match = Translator.ROW_RANGE_RE.match(test_str)
if groups is None:
assert match is None
else:
assert match.groups() == groups
@pytest.mark.parametrize("test_str, groups", [
["1:1", None],
["1234:5678", None],
["$1234:78910", None],
["$12321:$23432", None],
["112233:$445566", None],
["A:A", ("A", "A")],
["$ABC:AZZ", ("$ABC", "AZZ")],
["$DEF:$FOV", ("$DEF", "$FOV")],
["HA:$JA", ("HA", "$JA")],
["named1", None],
["A15", None],
["$AB303", None],
["YY$101", None],
["$ZZ$99", None],
["B2:C3", None],
["$ATV25:$BBC35", None],
["WWW$918:WWW$919", None],
["$III$305:$IIT$503", None],
])
def test_col_range_re(self, Translator, test_str, groups):
match = Translator.COL_RANGE_RE.match(test_str)
if groups is None:
assert match is None
else:
assert match.groups() == groups
@pytest.mark.parametrize("test_str, groups", [
["1:1", None],
["1234:5678", None],
["$1234:78910", None],
["$12321:$23432", None],
["112233:$445566", None],
["A:A", None],
["$ABC:AZZ", None],
["$DEF:$FOV", None],
["HA:$JA", None],
["named1", None],
["A15", ("A", "15")],
["$AB303", ("$AB", "303")],
["YY$101", ("YY", "$101")],
["$ZZ$99", ("$ZZ", "$99")],
["B2:C3", None],
["$ATV25:$BBC35", None],
["WWW$918:WWW$919", None],
["$III$305:$IIT$503", None],
])
def test_cell_ref_re(self, Translator, test_str, groups):
match = Translator.CELL_REF_RE.match(test_str)
if groups is None:
assert match is None
else:
assert match.groups() == groups
@pytest.mark.parametrize("test_str, rdelta, value", [
('1', 1, '2'),
('$222333', 1, '$222333'),
('1048576', -100, '1048476'),
('$1012023', -100, '$1012023'),
('101', 0, '101'),
('$101', 0, '$101'),
('12', -15, None),
('$12', -15, '$12'),
])
def test_translate_row(self, Translator, TranslatorError,
test_str, rdelta, value):
if value is None:
with pytest.raises(TranslatorError):
Translator.translate_row(test_str, rdelta)
else:
assert Translator.translate_row(test_str, rdelta) == value
@pytest.mark.parametrize("test_str, cdelta, value", [
('A', 1, 'B'),
('XED', 26, 'XFD'),
('$XED', 26, '$XED'),
('WWW', -52, 'WUW'),
('$WWW', -52, '$WWW'),
('ABC', 0, 'ABC'),
('$ABC', 0, '$ABC'),
('AA', -100, None),
('$AA', -100, '$AA'),
])
def test_translate_col(self, Translator, TranslatorError,
test_str, cdelta, value):
if value is None:
with pytest.raises(TranslatorError):
Translator.translate_col(test_str, cdelta)
else:
assert Translator.translate_col(test_str, cdelta) == value
@pytest.mark.parametrize("test_str, value", [
('A$3', ("", 'A$3')),
('Pipeline!B$4:B$138', ('Pipeline!', 'B$4:B$138')),
("'Summary slices'!$C$3", ("'Summary slices'!", '$C$3')),
("'Lions! Tigers! Bears!'!$OM$1", ("'Lions! Tigers! Bears!'!", '$OM$1')),
('named_range_1', ("", 'named_range_1')),
('Sheet-2!named_range_2', ("Sheet-2!", 'named_range_2')),
])
def test_strip_ws_name(self, Translator, test_str, value):
assert Translator.strip_ws_name(test_str) == value
@pytest.mark.parametrize("test_str, rdelta, cdelta, value", [
("1:1", 2, 1, "3:3"),
("$1234:78910", 1, 10, "$1234:78911"),
("$12321:$23432", 3, 5, "$12321:$23432"),
("112233:$445566", -3, -20, "112230:$445566"),
("987:999", 0, 12, "987:999"),
("1:5", -2, 3, None),
("A:A", 0, 1, "B:B"),
("$ABC:AZZ", 1, 3, "$ABC:BAC"),
("$DEF:$FOV", 25, 25, "$DEF:$FOV"),
("HA:$JA", -5, -15, "GL:$JA"),
("named1", -33, 33, "named1"),
("A15", -3, 4, "E12"),
("$AB303", 3, 2, "$AB306"),
("YY$101", 4, 2, "ZA$101"),
("$ZZ$99", 5, 2, "$ZZ$99"),
("B2:C3", 4, 3, "E6:F7"),
("$ATV25:$BBC35", 5, 3, "$ATV30:$BBC40"),
("WWW$918:WWW$919", 5, 4, "WXA$918:WXA$919"),
("$III$305:$IIT$503", 25, 35, "$III$305:$IIT$503"),
])
def test_translate_range(self, Translator, TranslatorError, test_str,
rdelta, cdelta, value):
if value is None:
with pytest.raises(TranslatorError):
Translator.translate_range(test_str, rdelta, cdelta)
else:
assert value == Translator.translate_range(test_str,
rdelta, cdelta)
@pytest.mark.parametrize("formula, origin, dest, result", [
('=IF(A$3<40%,"",INDEX(Pipeline!B$4:B$138,#REF!))', "A1", "B2",
'=IF(B$3<40%,"",INDEX(Pipeline!C$4:C$138,#REF!))'),
("='Summary slices'!$C$3", "A1", "B2", "='Summary slices'!$C$3"),
('=-MAX(Pipeline!AA4:AA138)', "A1", "B2",
'=-MAX(Pipeline!AB5:AB139)'),
('=TEXT(-\'External Ref\'!K7/DENOMINATOR,"$#,##0""M""")', "A1", "B2",
'=TEXT(-\'External Ref\'!L8/DENOMINATOR,"$#,##0""M""")'),
("=ROWS('Sh 1'!$1:3)+COLUMNS('Sh 2'!$A:C)", "A1", "B2",
"=ROWS('Sh 1'!$1:4)+COLUMNS('Sh 2'!$A:D)"),
("Just text", "A1", "B2", "Just text"),
("123.456", "A1", "B2", "123.456"),
("31/12/1999", "A1", "B2", "31/12/1999"),
("", "A1", "B2", ""),
])
def test_translate_formula(self, Translator, formula,
origin, dest, result):
trans = Translator(formula, origin)
assert trans.translate_formula(dest) == result
|