/usr/share/pyshared/openpyxl/tests/test_workbook.py is in python-openpyxl 1.7.0+ds1-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 | # coding: utf-8
# file openpyxl/tests/test_workbook.py
# Copyright (c) 2010-2011 openpyxl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# @license: http://www.opensource.org/licenses/mit-license.php
# @author: see AUTHORS file
# 3rd party imports
from nose.tools import eq_, with_setup, raises
try: # For Python 3.2 and later
from nose.tools import assert_is_instance
except ImportError:
def assert_is_instance(a, b):
assert isinstance(a, b)
import os.path as osp
# package imports
from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook
from openpyxl.namedrange import NamedRange
from openpyxl.shared.exc import ReadOnlyWorkbookException
from openpyxl.tests.helper import TMPDIR, clean_tmpdir, make_tmpdir
import datetime
def test_get_active_sheet():
wb = Workbook()
active_sheet = wb.get_active_sheet()
eq_(active_sheet, wb.worksheets[0])
def test_create_sheet():
wb = Workbook()
new_sheet = wb.create_sheet(0)
eq_(new_sheet, wb.worksheets[0])
def test_create_sheet_with_name():
wb = Workbook()
new_sheet = wb.create_sheet(0, title='LikeThisName')
eq_(new_sheet, wb.worksheets[0])
def test_add_correct_sheet():
wb = Workbook()
new_sheet = wb.create_sheet(0)
wb.add_sheet(new_sheet)
eq_(new_sheet, wb.worksheets[2])
@raises(AssertionError)
def test_add_incorrect_sheet():
wb = Workbook()
wb.add_sheet("Test")
@raises(ReadOnlyWorkbookException)
def test_create_sheet_readonly():
wb = Workbook()
wb._set_optimized_read()
wb.create_sheet()
def test_remove_sheet():
wb = Workbook()
new_sheet = wb.create_sheet(0)
wb.remove_sheet(new_sheet)
assert new_sheet not in wb.worksheets
def test_get_sheet_by_name():
wb = Workbook()
new_sheet = wb.create_sheet()
title = 'my sheet'
new_sheet.title = title
found_sheet = wb.get_sheet_by_name(title)
eq_(new_sheet, found_sheet)
def test_get_index():
wb = Workbook()
new_sheet = wb.create_sheet(0)
sheet_index = wb.get_index(new_sheet)
eq_(sheet_index, 0)
def test_get_sheet_names():
wb = Workbook()
names = ['Sheet', 'Sheet1', 'Sheet2', 'Sheet3', 'Sheet4', 'Sheet5']
for count in range(5):
wb.create_sheet(0)
actual_names = wb.get_sheet_names()
eq_(sorted(actual_names), sorted(names))
def test_get_named_ranges():
wb = Workbook()
eq_(wb.get_named_ranges(), wb._named_ranges)
def test_add_named_range():
wb = Workbook()
new_sheet = wb.create_sheet()
named_range = NamedRange('test_nr', [(new_sheet, 'A1')])
wb.add_named_range(named_range)
named_ranges_list = wb.get_named_ranges()
assert named_range in named_ranges_list
def test_get_named_range():
wb = Workbook()
new_sheet = wb.create_sheet()
named_range = NamedRange('test_nr', [(new_sheet, 'A1')])
wb.add_named_range(named_range)
found_named_range = wb.get_named_range('test_nr')
eq_(named_range, found_named_range)
def test_remove_named_range():
wb = Workbook()
new_sheet = wb.create_sheet()
named_range = NamedRange('test_nr', [(new_sheet, 'A1')])
wb.add_named_range(named_range)
wb.remove_named_range(named_range)
named_ranges_list = wb.get_named_ranges()
assert named_range not in named_ranges_list
@with_setup(setup=make_tmpdir, teardown=clean_tmpdir)
def test_add_local_named_range():
wb = Workbook()
new_sheet = wb.create_sheet()
named_range = NamedRange('test_nr', [(new_sheet, 'A1')])
named_range.scope = new_sheet
wb.add_named_range(named_range)
dest_filename = osp.join(TMPDIR, 'local_named_range_book.xlsx')
wb.save(dest_filename)
@with_setup(setup=make_tmpdir, teardown=clean_tmpdir)
def test_write_regular_date():
today = datetime.datetime(2010, 1, 18, 14, 15, 20, 1600)
book = Workbook()
sheet = book.get_active_sheet()
sheet.cell("A1").value = today
dest_filename = osp.join(TMPDIR, 'date_read_write_issue.xlsx')
book.save(dest_filename)
test_book = load_workbook(dest_filename)
test_sheet = test_book.get_active_sheet()
eq_(test_sheet.cell("A1").value, today)
@with_setup(setup=make_tmpdir, teardown=clean_tmpdir)
def test_write_regular_float():
float_value = 1.0 / 3.0
book = Workbook()
sheet = book.get_active_sheet()
sheet.cell("A1").value = float_value
dest_filename = osp.join(TMPDIR, 'float_read_write_issue.xlsx')
book.save(dest_filename)
test_book = load_workbook(dest_filename)
test_sheet = test_book.get_active_sheet()
eq_(test_sheet.cell("A1").value, float_value)
@raises(UnicodeDecodeError)
def test_bad_encoding():
try:
# Python 2
pound = unichr(163)
except NameError:
# Python 3
pound = chr(163)
test_string = ('Compound Value (' + pound + ')').encode('latin1')
utf_book = Workbook()
utf_sheet = utf_book.get_active_sheet()
utf_sheet.cell('A1').value = test_string
def test_good_encoding():
try:
# Python 2
pound = unichr(163)
except NameError:
# Python 3
pound = chr(163)
test_string = ('Compound Value (' + pound + ')').encode('latin1')
lat_book = Workbook(encoding='latin1')
lat_sheet = lat_book.get_active_sheet()
lat_sheet.cell('A1').value = test_string
class AlternativeWorksheet(object):
def __init__(self, parent_workbook, title=None):
self.parent_workbook = parent_workbook
if not title:
title = 'AlternativeSheet'
self.title = title
def test_worksheet_class():
wb = Workbook(worksheet_class=AlternativeWorksheet)
assert_is_instance(wb.worksheets[0], AlternativeWorksheet)
@raises(AssertionError)
def test_add_invalid_worksheet_class_instance():
wb = Workbook()
ws = AlternativeWorksheet(parent_workbook=wb)
wb.add_sheet(worksheet=ws)
|