/usr/lib/python2.7/dist-packages/openpyxl/descriptors/tests/test_sequence.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 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
import pytest
from openpyxl.xml.functions import fromstring, tostring, Element
from openpyxl.tests.helper import compare_xml
from ..serialisable import Serialisable
from ..base import Integer
@pytest.fixture
def Sequence():
from ..sequence import Sequence
return Sequence
@pytest.fixture
def Dummy(Sequence):
class Dummy(Serialisable):
value = Sequence(expected_type=int)
def __init__(self, value=()):
self.value = value
return Dummy
class TestSequence:
@pytest.mark.parametrize("value", [list(), tuple()])
def test_valid_ctor(self, Dummy, value):
dummy = Dummy()
dummy.value = value
assert dummy.value == list(value)
@pytest.mark.parametrize("value", ["", b"", dict(), 1, None])
def test_invalid_container(self, Dummy, value):
dummy = Dummy()
with pytest.raises(TypeError):
dummy.value = value
class TestPrimitive:
def test_to_tree(self, Dummy):
dummy = Dummy([1, '2', 3])
root = Element("root")
for node in Dummy.value.to_tree("el", dummy.value, ):
root.append(node)
xml = tostring(root)
expected = """
<root>
<el>1</el>
<el>2</el>
<el>3</el>
</root>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_from_xml(self, Dummy):
src = """
<root>
<value>1</value>
<value>2</value>
<value>3</value>
</root>
"""
node = fromstring(src)
dummy = Dummy.from_tree(node)
assert dummy.value == [1, 2, 3]
class SomeType(Serialisable):
value = Integer()
def __init__(self, value):
self.value = value
class TestComplex:
def test_to_tree(self, Sequence):
class Dummy:
vals = Sequence(expected_type=SomeType, name="vals")
dummy = Dummy()
dummy.vals = [SomeType(1), SomeType(2), SomeType(3)]
root = Element("root")
for node in Dummy.vals.to_tree("el", dummy.vals):
root.append(node)
xml = tostring(root)
expected = """
<root>
<el value="1"></el>
<el value="2"></el>
<el value="3"></el>
</root>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_from_xml(self, Sequence):
src = """
<root>
<vals value="1"></vals>
<vals value="2"></vals>
<vals value="3"></vals>
</root>
"""
node = fromstring(src)
class Dummy(Serialisable):
vals = Sequence(expected_type=SomeType)
def __init__(self, vals):
self.vals = vals
dummy = Dummy.from_tree(node)
assert dummy.vals == [SomeType(1), SomeType(2), SomeType(3)]
@pytest.fixture
def ValueSequence():
from .. sequence import ValueSequence
return ValueSequence
class TestValueSequence:
def test_to_tree(self, ValueSequence):
class Dummy(Serialisable):
tagname = "el"
size = ValueSequence(expected_type=int)
dummy = Dummy()
dummy.size = [1, 2, 3]
xml = tostring(dummy.to_tree())
expected = """
<el>
<size val="1"></size>
<size val="2"></size>
<size val="3"></size>
</el>
"""
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_from_tree(self, ValueSequence):
class Dummy(Serialisable):
tagname = "el"
__nested__ = ("size",)
size = ValueSequence(expected_type=int)
def __init__(self, size):
self.size = size
src = """
<el>
<size val="1"></size>
<size val="2"></size>
<size val="3"></size>
</el>
"""
node = fromstring(src)
desc = Dummy.size
vals = desc.from_tree(node)
dummy = Dummy.from_tree(node)
assert dummy.size == [1, 2, 3]
@pytest.fixture
def NestedSequence():
from ..sequence import NestedSequence
return NestedSequence
from openpyxl.styles import Font
@pytest.fixture
def ComplexObject(NestedSequence):
class Complex(Serialisable):
tagname = "style"
fonts = NestedSequence(expected_type=Font)
def __init__(self, fonts=()):
self.fonts = fonts
return Complex
class TestNestedSequence:
def test_ctor(self, ComplexObject):
style = ComplexObject()
ft1 = Font()
ft2 = Font(bold=True)
style.fonts = [ft1, ft2]
expected = """
<style>
<fonts count="2">
<font>
<name val="Calibri"></name>
<family val="2"></family>
<color rgb="00000000"></color>
<sz val="11"></sz>
</font>
<font>
<name val="Calibri"></name>
<family val="2"></family>
<b val="1"></b>
<color rgb="00000000"></color>
<sz val="11"></sz>
</font>
</fonts>
</style>
"""
tree = style.__class__.fonts.to_tree('fonts', style.fonts)
tree = style.to_tree()
xml = tostring(tree)
diff = compare_xml(xml, expected)
assert diff is None, diff
def test_from_tree(self, ComplexObject):
xml = """
<style>
<fonts count="2">
<font>
<name val="Calibri"></name>
<family val="2"></family>
<color rgb="00000000"></color>
<sz val="11"></sz>
</font>
<font>
<name val="Calibri"></name>
<family val="2"></family>
<b val="1"></b>
<color rgb="00000000"></color>
<sz val="11"></sz>
</font>
</fonts>
</style>
"""
node = fromstring(xml)
style = ComplexObject.from_tree(node)
assert len(style.fonts) == 2
assert style.fonts[1].bold is True
|