/usr/lib/python2.7/dist-packages/openpyxl/formatting/tests/test_rule.py is in python-openpyxl 2.3.0-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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | # coding=utf8
from __future__ import absolute_import
# copyright 2010-2015 openpyxl
import pytest
from openpyxl.xml.functions import fromstring, tostring
from openpyxl.xml.constants import SHEET_MAIN_NS
from openpyxl.styles import Color
from openpyxl.tests.helper import compare_xml
@pytest.fixture
def FormatObject():
from ..rule import FormatObject
return FormatObject
class TestFormatObject:
def test_create(self, FormatObject):
xml = fromstring("""<cfvo type="num" val="3"/>""")
cfvo = FormatObject.from_tree(xml)
assert cfvo.type == "num"
assert cfvo.val == 3
assert cfvo.gte is None
def test_serialise(self, FormatObject):
cfvo = FormatObject(type="percent", val=4)
xml = tostring(cfvo.to_tree())
expected = """<cfvo type="percent" val="4"/>"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.fixture
def ColorScale():
from ..rule import ColorScale
return ColorScale
@pytest.fixture
def ColorScaleRule():
from ..rule import ColorScaleRule
return ColorScaleRule
class TestColorScale:
def test_create(self, ColorScale):
src = """
<colorScale xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<cfvo type="min"/>
<cfvo type="max"/>
<color rgb="FFFF7128"/>
<color rgb="FFFFEF9C"/>
</colorScale>
"""
xml = fromstring(src)
cs = ColorScale.from_tree(xml)
assert len(cs.cfvo) == 2
assert len(cs.color) == 2
def test_serialise(self, ColorScale, FormatObject):
fo1 = FormatObject(type="min", val="0")
fo2 = FormatObject(type="percent", val="50")
fo3 = FormatObject(type="max", val="0")
col1 = Color(rgb="FFFF0000")
col2 = Color(rgb="FFFFFF00")
col3 = Color(rgb="FF00B050")
cs = ColorScale(cfvo=[fo1, fo2, fo3], color=[col1, col2, col3])
xml = tostring(cs.to_tree())
expected = """
<colorScale>
<cfvo type="min" val="0"/>
<cfvo type="percent" val="50"/>
<cfvo type="max" val="0"/>
<color rgb="FFFF0000"/>
<color rgb="FFFFFF00"/>
<color rgb="FF00B050"/>
</colorScale>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_two_colors(self, ColorScaleRule):
cfRule = ColorScaleRule(start_type='min', start_value=None,
start_color='FFAA0000', end_type='max', end_value=None,
end_color='FF00AA00')
xml = tostring(cfRule.to_tree())
expected = """
<cfRule priority="0" type="colorScale">
<colorScale>
<cfvo type="min"/>
<cfvo type="max"/>
<color rgb="FFAA0000"/>
<color rgb="FF00AA00"/>
</colorScale>
</cfRule>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_three_colors(self, ColorScaleRule):
cfRule = ColorScaleRule(start_type='percentile', start_value=10,
start_color='FFAA0000', mid_type='percentile', mid_value=50,
mid_color='FF0000AA', end_type='percentile', end_value=90,
end_color='FF00AA00')
xml = tostring(cfRule.to_tree())
expected = """
<cfRule priority="0" type="colorScale">
<colorScale>
<cfvo type="percentile" val="10"></cfvo>
<cfvo type="percentile" val="50"></cfvo>
<cfvo type="percentile" val="90"></cfvo>
<color rgb="FFAA0000"></color>
<color rgb="FF0000AA"></color>
<color rgb="FF00AA00"></color>
</colorScale>
</cfRule>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.fixture
def DataBar():
from ..rule import DataBar
return DataBar
class TestDataBar:
def test_create(self, DataBar):
src = """
<dataBar xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<cfvo type="min"/>
<cfvo type="max"/>
<color rgb="FF638EC6"/>
</dataBar>
"""
xml = fromstring(src)
db = DataBar.from_tree(xml)
assert len(db.cfvo) == 2
assert db.color.value == "FF638EC6"
def test_serialise(self, DataBar, FormatObject):
fo1 = FormatObject(type="min", val="0")
fo2 = FormatObject(type="percent", val="50")
db = DataBar(minLength=4, maxLength=10, cfvo=[fo1, fo2], color="FF2266", showValue=True)
xml = tostring(db.to_tree())
expected = """
<dataBar maxLength="10" minLength="4" showValue="1">
<cfvo type="min" val="0"></cfvo>
<cfvo type="percent" val="50"></cfvo>
<color rgb="00FF2266"></color>
</dataBar>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.fixture
def IconSet():
from ..rule import IconSet
return IconSet
class TestIconSet:
def test_create(self, IconSet):
src = """
<iconSet iconSet="5Rating" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<cfvo type="percent" val="0"/>
<cfvo type="percentile" val="20"/>
<cfvo type="percentile" val="40"/>
<cfvo type="percentile" val="60"/>
<cfvo type="percentile" val="80"/>
</iconSet>
"""
xml = fromstring(src)
icon = IconSet.from_tree(xml)
assert icon.iconSet == "5Rating"
assert len(icon.cfvo) == 5
def test_serialise(self, IconSet, FormatObject):
fo1 = FormatObject(type="num", val="2")
fo2 = FormatObject(type="num", val="4")
fo3 = FormatObject(type="num", val="6")
fo4 = FormatObject(type="percent", val="0")
icon = IconSet(cfvo=[fo1, fo2, fo3, fo4], iconSet="4ArrowsGray", reverse=True, showValue=False)
xml = tostring(icon.to_tree())
expected = """
<iconSet iconSet="4ArrowsGray" showValue="0" reverse="1">
<cfvo type="num" val="2"/>
<cfvo type="num" val="4"/>
<cfvo type="num" val="6"/>
<cfvo type="percent" val="0"/>
</iconSet>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
@pytest.fixture
def Rule():
from ..rule import Rule
return Rule
class TestRule:
def test_create(self, Rule, datadir):
datadir.chdir()
with open("worksheet.xml") as src:
xml = fromstring(src.read())
rules = []
for el in xml.findall("{%s}conditionalFormatting/{%s}cfRule" % (SHEET_MAIN_NS, SHEET_MAIN_NS)):
rules.append(Rule.from_tree(el))
assert len(rules) == 30
assert rules[17].formula == ['2', '7']
assert rules[-1].formula == ["AD1>3",]
def test_serialise(self, Rule):
rule = Rule(type="cellIs", dxfId="26", priority="13", operator="between")
rule.formula = ["2", "7"]
xml = tostring(rule.to_tree())
expected = """
<cfRule type="cellIs" dxfId="26" priority="13" operator="between">
<formula>2</formula>
<formula>7</formula>
</cfRule>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_non_ascii_formula(self, Rule):
rule = Rule(type="cellIs", priority=10, formula=[b"D\xc3\xbcsseldorf".decode("utf-8")])
xml = tostring(rule.to_tree())
expected = """
<cfRule priority="10" type="cellIs">
<formula>Düsseldorf</formula>
</cfRule>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_formula_rule():
from ..rule import FormulaRule
from openpyxl.styles.differential import DifferentialStyle
cf = FormulaRule(formula=['ISBLANK(C1)'], stopIfTrue=True)
assert dict(cf) == {'priority': '0', 'stopIfTrue': '1', 'type': 'expression'}
assert cf.formula == ['ISBLANK(C1)']
assert cf.dxf == DifferentialStyle()
def test_cellis_rule():
from ..rule import CellIsRule
from openpyxl.styles import PatternFill
red_fill = PatternFill(start_color='FFEE1111', end_color='FFEE1111',
fill_type='solid')
rule = CellIsRule(operator='<', formula=['C$1'], stopIfTrue=True, fill=red_fill)
assert dict(rule) == {'operator': 'lessThan', 'priority': '0', 'type': 'cellIs', 'stopIfTrue':'1'}
assert rule.formula == ['C$1']
assert rule.dxf.fill == red_fill
@pytest.mark.parametrize("value, expansion",
[
('<=', 'lessThanOrEqual'),
('>', 'greaterThan'),
('!=', 'notEqual'),
('=', 'equal'),
('>=', 'greaterThanOrEqual'),
('==', 'equal'),
('<', 'lessThan'),
]
)
def test_operator_expansion(value, expansion):
from ..rule import CellIsRule
cf1 = CellIsRule(operator=value, formula=[])
cf2 = CellIsRule(operator=expansion, formula=[])
assert cf1.operator == expansion
assert cf2.operator == expansion
def test_iconset_rule():
from ..rule import IconSetRule
rule = IconSetRule('5Arrows', 'percent', [10, 20, 30, 40, 50])
xml = tostring(rule.to_tree())
expected = """
<cfRule priority="0" type="iconSet">
<iconSet iconSet="5Arrows">
<cfvo type="percent" val="10"/>
<cfvo type="percent" val="20"/>
<cfvo type="percent" val="30"/>
<cfvo type="percent" val="40"/>
<cfvo type="percent" val="50"/>
</iconSet>
</cfRule>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_databar_rule():
from ..rule import DataBarRule
rule = DataBarRule(start_type='percentile', start_value=10,
end_type='percentile', end_value='90', color="FF638EC6")
xml = tostring(rule.to_tree())
expected = """
<cfRule type="dataBar" priority="0">
<dataBar>
<cfvo type="percentile" val="10"/>
<cfvo type="percentile" val="90"/>
<color rgb="FF638EC6"/>
</dataBar>
</cfRule>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
|