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
|
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
from datetime import datetime
from tempfile import NamedTemporaryFile
import pytest
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import petl as etl
from petl.io.xls import fromxls, toxls
from petl.test.helpers import ieq
def _get_test_xls():
try:
import pkg_resources
return pkg_resources.resource_filename('petl', 'test/resources/test.xls')
except:
return None
try:
import xlrd # noqa: F401
import xlwt # noqa: F401
except ImportError as e:
pytest.skip('SKIP xls tests: %s' % e, allow_module_level=True)
else:
def test_fromxls():
filename = _get_test_xls()
if filename is None:
return
tbl = fromxls(filename, 'Sheet1')
expect = (('foo', 'bar'),
('A', 1),
('B', 2),
('C', 2),
(u'é', datetime(2012, 1, 1)))
ieq(expect, tbl)
ieq(expect, tbl)
def test_fromxls_nosheet():
filename = _get_test_xls()
if filename is None:
return
tbl = fromxls(filename)
expect = (('foo', 'bar'),
('A', 1),
('B', 2),
('C', 2),
(u'é', datetime(2012, 1, 1)))
ieq(expect, tbl)
ieq(expect, tbl)
def test_fromxls_use_view():
filename = _get_test_xls()
if filename is None:
return
tbl = fromxls(filename, 'Sheet1', use_view=False)
expect = (('foo', 'bar'),
('A', 1),
('B', 2),
('C', 2),
(u'é', 40909.0))
ieq(expect, tbl)
ieq(expect, tbl)
def test_toxls():
expect = (('foo', 'bar'),
('A', 1),
('B', 2),
('C', 2))
f = NamedTemporaryFile(delete=False)
f.close()
toxls(expect, f.name, 'Sheet1')
actual = fromxls(f.name, 'Sheet1')
ieq(expect, actual)
ieq(expect, actual)
def test_toxls_headerless():
expect = []
f = NamedTemporaryFile(delete=False)
f.close()
toxls(expect, f.name, 'Sheet1')
actual = fromxls(f.name, 'Sheet1')
ieq(expect, actual)
ieq(expect, actual)
def test_toxls_date():
expect = (('foo', 'bar'),
(u'é', datetime(2012, 1, 1)),
(u'éé', datetime(2013, 2, 22)))
f = NamedTemporaryFile(delete=False)
f.close()
toxls(expect, f.name, 'Sheet1',
styles={'bar': xlwt.easyxf(num_format_str='DD/MM/YYYY')})
actual = fromxls(f.name, 'Sheet1')
ieq(expect, actual)
def test_integration():
expect = (('foo', 'bar'),
('A', 1),
('B', 2),
('C', 2))
f = NamedTemporaryFile(delete=False)
f.close()
etl.wrap(expect).toxls(f.name, 'Sheet1')
actual = etl.fromxls(f.name, 'Sheet1')
ieq(expect, actual)
ieq(expect, actual)
def test_passing_kwargs_to_xlutils_view():
filename = _get_test_xls()
if filename is None:
return
from petl.io.xlutils_view import View
org_init = View.__init__
def wrapper(self, *args, **kwargs):
assert "ignore_workbook_corruption" in kwargs
return org_init(self, *args, **kwargs)
with patch("petl.io.xlutils_view.View.__init__", wrapper):
tbl = fromxls(filename, 'Sheet1', use_view=True, ignore_workbook_corruption=True)
expect = (('foo', 'bar'),
('A', 1),
('B', 2),
('C', 2),
(u'é', datetime(2012, 1, 1)))
ieq(expect, tbl)
ieq(expect, tbl)
|