/usr/lib/python3/dist-packages/openpyxl/cell/tests/test_cell.py is in python3-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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
# Python stdlib imports
from datetime import (
time,
datetime,
timedelta,
date,
)
# 3rd party imports
import pytest
# package imports
from openpyxl.comments import Comment
from openpyxl.cell.cell import ERROR_CODES
@pytest.fixture
def DummyWorksheet():
from openpyxl.utils.indexed_list import IndexedList
from openpyxl.utils.datetime import CALENDAR_WINDOWS_1900
from openpyxl.cell import Cell
class Wb(object):
excel_base_date = CALENDAR_WINDOWS_1900
_number_formats = IndexedList()
_fonts = IndexedList()
_fills = IndexedList()
_borders = IndexedList()
_protections = IndexedList()
_alignments = IndexedList()
_number_formats = IndexedList()
_cell_styles = IndexedList()
class Ws(object):
encoding = 'utf-8'
parent = Wb()
title = "Dummy Worksheet"
_comment_count = 0
def cell(self, column, row):
return Cell(self, row=row, col_idx=column)
return Ws()
@pytest.fixture
def Cell():
from ..cell import Cell
return Cell
@pytest.fixture
def dummy_cell(DummyWorksheet, Cell):
ws = DummyWorksheet
cell = Cell(ws, column="A", row=1)
return cell
@pytest.fixture(params=[True, False])
def guess_types(request):
return request.param
@pytest.mark.parametrize("value, expected",
[
('4.2', 4.2),
('-42.000', -42),
( '0', 0),
('0.9999', 0.9999),
('99E-02', 0.99),
('4', 4),
('-1E3', -1000),
('2e+2', 200),
('3.1%', 0.031),
('03:40:16', time(3, 40, 16)),
('03:40', time(3, 40)),
('30:33.865633336', time(0, 30, 33, 865633))
]
)
def test_infer_numeric(dummy_cell, guess_types, value, expected):
cell = dummy_cell
cell.parent.parent._guess_types = guess_types
cell.value = value
if cell.guess_types:
assert cell.value == expected
else:
cell.value == value
def test_ctor(dummy_cell):
cell = dummy_cell
assert cell.data_type == 'n'
assert cell.column == 'A'
assert cell.row == 1
assert cell.coordinate == "A1"
assert cell.value is None
assert cell.comment is None
@pytest.mark.parametrize("datatype", ['n', 'd', 's', 'b', 'f', 'e'])
def test_null(dummy_cell, datatype):
cell = dummy_cell
cell.data_type = datatype
assert cell.data_type == datatype
cell.value = None
assert cell.data_type == 'n'
@pytest.mark.parametrize("value", ['hello', ".", '0800'])
def test_string(dummy_cell, value):
cell = dummy_cell
cell.value = 'hello'
assert cell.data_type == 's'
@pytest.mark.parametrize("value", ['=42', '=if(A1<4;-1;1)'])
def test_formula(dummy_cell, value):
cell = dummy_cell
cell.value = value
assert cell.data_type == 'f'
def test_not_formula(dummy_cell):
dummy_cell.value = "="
assert dummy_cell.data_type == 's'
assert dummy_cell.value == "="
@pytest.mark.parametrize("value", [True, False])
def test_boolean(dummy_cell, value):
cell = dummy_cell
cell.value = value
assert cell.data_type == 'b'
@pytest.mark.parametrize("error_string", ERROR_CODES)
def test_error_codes(dummy_cell, error_string):
cell = dummy_cell
cell.value = error_string
assert cell.data_type == 'e'
@pytest.mark.parametrize("value, internal, number_format",
[
(
datetime(2010, 7, 13, 6, 37, 41),
40372.27616898148,
"yyyy-mm-dd h:mm:ss"
),
(
date(2010, 7, 13),
40372,
"yyyy-mm-dd"
),
(
time(1, 3),
0.04375,
"h:mm:ss",
)
]
)
def test_insert_date(dummy_cell, value, internal, number_format):
cell = dummy_cell
cell.value = value
assert cell.data_type == 'n'
assert cell.internal_value == internal
assert cell.is_date
assert cell.number_format == number_format
@pytest.mark.parametrize("value, is_date",
[
(None, True,),
("testme", False),
(True, False),
]
)
def test_cell_formatted_as_date(dummy_cell, value, is_date):
cell = dummy_cell
cell.value = datetime.today()
cell.value = value
assert cell.is_date == is_date
assert cell.value == value
def test_set_bad_type(dummy_cell):
cell = dummy_cell
with pytest.raises(ValueError):
cell.set_explicit_value(1, 'q')
def test_illegal_chacters(dummy_cell):
from openpyxl.utils.exceptions import IllegalCharacterError
from openpyxl.compat import range
from itertools import chain
cell = dummy_cell
# The bytes 0x00 through 0x1F inclusive must be manually escaped in values.
illegal_chrs = chain(range(9), range(11, 13), range(14, 32))
for i in illegal_chrs:
with pytest.raises(IllegalCharacterError):
cell.value = chr(i)
with pytest.raises(IllegalCharacterError):
cell.value = "A {0} B".format(chr(i))
cell.value = chr(33)
cell.value = chr(9) # Tab
cell.value = chr(10) # Newline
cell.value = chr(13) # Carriage return
cell.value = " Leading and trailing spaces are legal "
values = (
('30:33.865633336', [('', '', '', '30', '33', '865633')]),
('03:40:16', [('03', '40', '16', '', '', '')]),
('03:40', [('03', '40', '', '', '', '')]),
('55:72:12', []),
)
@pytest.mark.parametrize("value, expected",
values)
def test_time_regex(value, expected):
from openpyxl.cell.cell import TIME_REGEX
m = TIME_REGEX.findall(value)
assert m == expected
def test_timedelta(dummy_cell):
cell = dummy_cell
cell.value = timedelta(days=1, hours=3)
assert cell.value == 1.125
assert cell.data_type == 'n'
assert cell.is_date is False
assert cell.number_format == "[hh]:mm:ss"
def test_repr(dummy_cell):
cell = dummy_cell
assert repr(cell), '<Cell Sheet1.A1>' == 'Got bad repr: %s' % repr(cell)
def test_comment_assignment(dummy_cell):
assert dummy_cell.comment is None
comm = Comment("text", "author")
dummy_cell.comment = comm
assert dummy_cell.comment == comm
def test_comment_count(dummy_cell):
cell = dummy_cell
ws = cell.parent
assert ws._comment_count == 0
cell.comment = Comment("text", "author")
assert ws._comment_count == 1
cell.comment = Comment("text", "author")
assert ws._comment_count == 1
cell.comment = None
assert ws._comment_count == 0
cell.comment = None
assert ws._comment_count == 0
def test_only_one_cell_per_comment(dummy_cell):
ws = dummy_cell.parent
comm = Comment('text', 'author')
dummy_cell.comment = comm
c2 = ws.cell(column=1, row=2)
with pytest.raises(AttributeError):
c2.comment = comm
def test_remove_comment(dummy_cell):
comm = Comment('text', 'author')
dummy_cell.comment = comm
dummy_cell.comment = None
assert dummy_cell.comment is None
def test_cell_offset(dummy_cell):
cell = dummy_cell
assert cell.offset(2, 1).coordinate == 'B3'
class TestEncoding:
try:
# Python 2
pound = unichr(163)
except NameError:
# Python 3
pound = chr(163)
test_string = ('Compound Value (' + pound + ')').encode('latin1')
def test_bad_encoding(self):
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
cell = ws['A1']
with pytest.raises(UnicodeDecodeError):
cell.check_string(self.test_string)
with pytest.raises(UnicodeDecodeError):
cell.value = self.test_string
def test_good_encoding(self):
from openpyxl import Workbook
wb = Workbook(encoding='latin1')
ws = wb.active
cell = ws['A1']
cell.value = self.test_string
def test_font(DummyWorksheet, Cell):
from openpyxl.styles import Font
font = Font(bold=True)
ws = DummyWorksheet
ws.parent._fonts.add(font)
cell = Cell(ws, column='A', row=1)
assert cell.font == font
def test_fill(DummyWorksheet, Cell):
from openpyxl.styles import PatternFill
fill = PatternFill(patternType="solid", fgColor="FF0000")
ws = DummyWorksheet
ws.parent._fills.add(fill)
cell = Cell(ws, column='A', row=1)
assert cell.fill == fill
def test_border(DummyWorksheet, Cell):
from openpyxl.styles import Border
border = Border()
ws = DummyWorksheet
ws.parent._borders.add(border)
cell = Cell(ws, column='A', row=1)
assert cell.border == border
def test_number_format(DummyWorksheet, Cell):
ws = DummyWorksheet
ws.parent._number_formats.add("dd--hh--mm")
cell = Cell(ws, column="A", row=1)
cell.number_format = "dd--hh--mm"
assert cell.number_format == "dd--hh--mm"
def test_alignment(DummyWorksheet, Cell):
from openpyxl.styles import Alignment
align = Alignment(wrapText=True)
ws = DummyWorksheet
ws.parent._alignments.add(align)
cell = Cell(ws, column="A", row=1)
assert cell.alignment == align
def test_protection(DummyWorksheet, Cell):
from openpyxl.styles import Protection
prot = Protection(locked=False)
ws = DummyWorksheet
ws.parent._protections.add(prot)
cell = Cell(ws, column="A", row=1)
assert cell.protection == prot
def test_pivot_button(DummyWorksheet, Cell):
ws = DummyWorksheet
cell = Cell(ws, column="A", row=1)
cell.style_id
cell._style.pivotButton = 1
assert cell.pivotButton is True
def test_quote_prefix(DummyWorksheet, Cell):
ws = DummyWorksheet
cell = Cell(ws, column="A", row=1)
cell.style_id
cell._style.quotePrefix = 1
assert cell.quotePrefix is True
|