Skip to content

Commit d47fc0c

Browse files
simonjayhawkinsjreback
authored andcommittedJun 10, 2019
TST/CLN: declass smaller test files in tests\io\excel (#26764)
1 parent efc7f2f commit d47fc0c

File tree

4 files changed

+241
-251
lines changed

4 files changed

+241
-251
lines changed
 
Lines changed: 115 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,123 @@
11
import pytest
22

3-
import pandas.util._test_decorators as td
4-
53
from pandas import DataFrame
64
from pandas.util.testing import ensure_clean
75

86
from pandas.io.excel import ExcelWriter, _OpenpyxlWriter
97

10-
11-
@td.skip_if_no('openpyxl')
12-
@pytest.mark.parametrize("ext", ['.xlsx'])
13-
class TestOpenpyxlTests:
14-
15-
def test_to_excel_styleconverter(self, ext):
16-
from openpyxl import styles
17-
18-
hstyle = {
19-
"font": {
20-
"color": '00FF0000',
21-
"bold": True,
22-
},
23-
"borders": {
24-
"top": "thin",
25-
"right": "thin",
26-
"bottom": "thin",
27-
"left": "thin",
28-
},
29-
"alignment": {
30-
"horizontal": "center",
31-
"vertical": "top",
32-
},
33-
"fill": {
34-
"patternType": 'solid',
35-
'fgColor': {
36-
'rgb': '006666FF',
37-
'tint': 0.3,
38-
},
39-
},
40-
"number_format": {
41-
"format_code": "0.00"
42-
},
43-
"protection": {
44-
"locked": True,
45-
"hidden": False,
8+
openpyxl = pytest.importorskip("openpyxl")
9+
10+
pytestmark = pytest.mark.parametrize("ext", ['.xlsx'])
11+
12+
13+
def test_to_excel_styleconverter(ext):
14+
from openpyxl import styles
15+
16+
hstyle = {
17+
"font": {
18+
"color": '00FF0000',
19+
"bold": True,
20+
},
21+
"borders": {
22+
"top": "thin",
23+
"right": "thin",
24+
"bottom": "thin",
25+
"left": "thin",
26+
},
27+
"alignment": {
28+
"horizontal": "center",
29+
"vertical": "top",
30+
},
31+
"fill": {
32+
"patternType": 'solid',
33+
'fgColor': {
34+
'rgb': '006666FF',
35+
'tint': 0.3,
4636
},
47-
}
48-
49-
font_color = styles.Color('00FF0000')
50-
font = styles.Font(bold=True, color=font_color)
51-
side = styles.Side(style=styles.borders.BORDER_THIN)
52-
border = styles.Border(top=side, right=side, bottom=side, left=side)
53-
alignment = styles.Alignment(horizontal='center', vertical='top')
54-
fill_color = styles.Color(rgb='006666FF', tint=0.3)
55-
fill = styles.PatternFill(patternType='solid', fgColor=fill_color)
56-
57-
number_format = '0.00'
58-
59-
protection = styles.Protection(locked=True, hidden=False)
60-
61-
kw = _OpenpyxlWriter._convert_to_style_kwargs(hstyle)
62-
assert kw['font'] == font
63-
assert kw['border'] == border
64-
assert kw['alignment'] == alignment
65-
assert kw['fill'] == fill
66-
assert kw['number_format'] == number_format
67-
assert kw['protection'] == protection
68-
69-
def test_write_cells_merge_styled(self, ext):
70-
from pandas.io.formats.excel import ExcelCell
71-
72-
sheet_name = 'merge_styled'
73-
74-
sty_b1 = {'font': {'color': '00FF0000'}}
75-
sty_a2 = {'font': {'color': '0000FF00'}}
76-
77-
initial_cells = [
78-
ExcelCell(col=1, row=0, val=42, style=sty_b1),
79-
ExcelCell(col=0, row=1, val=99, style=sty_a2),
80-
]
81-
82-
sty_merged = {'font': {'color': '000000FF', 'bold': True}}
83-
sty_kwargs = _OpenpyxlWriter._convert_to_style_kwargs(sty_merged)
84-
openpyxl_sty_merged = sty_kwargs['font']
85-
merge_cells = [
86-
ExcelCell(col=0, row=0, val='pandas',
87-
mergestart=1, mergeend=1, style=sty_merged),
88-
]
89-
90-
with ensure_clean(ext) as path:
91-
writer = _OpenpyxlWriter(path)
92-
writer.write_cells(initial_cells, sheet_name=sheet_name)
93-
writer.write_cells(merge_cells, sheet_name=sheet_name)
94-
95-
wks = writer.sheets[sheet_name]
96-
xcell_b1 = wks['B1']
97-
xcell_a2 = wks['A2']
98-
assert xcell_b1.font == openpyxl_sty_merged
99-
assert xcell_a2.font == openpyxl_sty_merged
100-
101-
@pytest.mark.parametrize("mode,expected", [
102-
('w', ['baz']), ('a', ['foo', 'bar', 'baz'])])
103-
def test_write_append_mode(self, ext, mode, expected):
104-
import openpyxl
105-
df = DataFrame([1], columns=['baz'])
106-
107-
with ensure_clean(ext) as f:
108-
wb = openpyxl.Workbook()
109-
wb.worksheets[0].title = 'foo'
110-
wb.worksheets[0]['A1'].value = 'foo'
111-
wb.create_sheet('bar')
112-
wb.worksheets[1]['A1'].value = 'bar'
113-
wb.save(f)
114-
115-
writer = ExcelWriter(f, engine='openpyxl', mode=mode)
116-
df.to_excel(writer, sheet_name='baz', index=False)
117-
writer.save()
118-
119-
wb2 = openpyxl.load_workbook(f)
120-
result = [sheet.title for sheet in wb2.worksheets]
121-
assert result == expected
122-
123-
for index, cell_value in enumerate(expected):
124-
assert wb2.worksheets[index]['A1'].value == cell_value
37+
},
38+
"number_format": {
39+
"format_code": "0.00"
40+
},
41+
"protection": {
42+
"locked": True,
43+
"hidden": False,
44+
},
45+
}
46+
47+
font_color = styles.Color('00FF0000')
48+
font = styles.Font(bold=True, color=font_color)
49+
side = styles.Side(style=styles.borders.BORDER_THIN)
50+
border = styles.Border(top=side, right=side, bottom=side, left=side)
51+
alignment = styles.Alignment(horizontal='center', vertical='top')
52+
fill_color = styles.Color(rgb='006666FF', tint=0.3)
53+
fill = styles.PatternFill(patternType='solid', fgColor=fill_color)
54+
55+
number_format = '0.00'
56+
57+
protection = styles.Protection(locked=True, hidden=False)
58+
59+
kw = _OpenpyxlWriter._convert_to_style_kwargs(hstyle)
60+
assert kw['font'] == font
61+
assert kw['border'] == border
62+
assert kw['alignment'] == alignment
63+
assert kw['fill'] == fill
64+
assert kw['number_format'] == number_format
65+
assert kw['protection'] == protection
66+
67+
68+
def test_write_cells_merge_styled(ext):
69+
from pandas.io.formats.excel import ExcelCell
70+
71+
sheet_name = 'merge_styled'
72+
73+
sty_b1 = {'font': {'color': '00FF0000'}}
74+
sty_a2 = {'font': {'color': '0000FF00'}}
75+
76+
initial_cells = [
77+
ExcelCell(col=1, row=0, val=42, style=sty_b1),
78+
ExcelCell(col=0, row=1, val=99, style=sty_a2),
79+
]
80+
81+
sty_merged = {'font': {'color': '000000FF', 'bold': True}}
82+
sty_kwargs = _OpenpyxlWriter._convert_to_style_kwargs(sty_merged)
83+
openpyxl_sty_merged = sty_kwargs['font']
84+
merge_cells = [
85+
ExcelCell(col=0, row=0, val='pandas',
86+
mergestart=1, mergeend=1, style=sty_merged),
87+
]
88+
89+
with ensure_clean(ext) as path:
90+
writer = _OpenpyxlWriter(path)
91+
writer.write_cells(initial_cells, sheet_name=sheet_name)
92+
writer.write_cells(merge_cells, sheet_name=sheet_name)
93+
94+
wks = writer.sheets[sheet_name]
95+
xcell_b1 = wks['B1']
96+
xcell_a2 = wks['A2']
97+
assert xcell_b1.font == openpyxl_sty_merged
98+
assert xcell_a2.font == openpyxl_sty_merged
99+
100+
101+
@pytest.mark.parametrize("mode,expected", [
102+
('w', ['baz']), ('a', ['foo', 'bar', 'baz'])])
103+
def test_write_append_mode(ext, mode, expected):
104+
df = DataFrame([1], columns=['baz'])
105+
106+
with ensure_clean(ext) as f:
107+
wb = openpyxl.Workbook()
108+
wb.worksheets[0].title = 'foo'
109+
wb.worksheets[0]['A1'].value = 'foo'
110+
wb.create_sheet('bar')
111+
wb.worksheets[1]['A1'].value = 'bar'
112+
wb.save(f)
113+
114+
writer = ExcelWriter(f, engine='openpyxl', mode=mode)
115+
df.to_excel(writer, sheet_name='baz', index=False)
116+
writer.save()
117+
118+
wb2 = openpyxl.load_workbook(f)
119+
result = [sheet.title for sheet in wb2.worksheets]
120+
assert result == expected
121+
122+
for index, cell_value in enumerate(expected):
123+
assert wb2.worksheets[index]['A1'].value == cell_value

‎pandas/tests/io/excel/test_xlrd.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
import pandas.util._test_decorators as td
1+
import pytest
22

33
import pandas as pd
44
import pandas.util.testing as tm
55
from pandas.util.testing import ensure_clean
66

77
from pandas.io.excel import ExcelFile
88

9+
xlrd = pytest.importorskip("xlrd")
10+
xlwt = pytest.importorskip("xlwt")
911

10-
@td.skip_if_no('xlrd')
11-
class TestXlrdReader:
12-
"""
13-
This is the base class for the xlrd tests, and 3 different file formats
14-
are supported: xls, xlsx, xlsm
15-
"""
1612

17-
@td.skip_if_no("xlwt")
18-
def test_read_xlrd_book(self, read_ext, frame):
19-
import xlrd
20-
df = frame
13+
def test_read_xlrd_book(read_ext, frame):
14+
df = frame
2115

22-
engine = "xlrd"
23-
sheet_name = "SheetA"
16+
engine = "xlrd"
17+
sheet_name = "SheetA"
2418

25-
with ensure_clean(read_ext) as pth:
26-
df.to_excel(pth, sheet_name)
27-
book = xlrd.open_workbook(pth)
19+
with ensure_clean(read_ext) as pth:
20+
df.to_excel(pth, sheet_name)
21+
book = xlrd.open_workbook(pth)
2822

29-
with ExcelFile(book, engine=engine) as xl:
30-
result = pd.read_excel(xl, sheet_name, index_col=0)
31-
tm.assert_frame_equal(df, result)
32-
33-
result = pd.read_excel(book, sheet_name=sheet_name,
34-
engine=engine, index_col=0)
23+
with ExcelFile(book, engine=engine) as xl:
24+
result = pd.read_excel(xl, sheet_name, index_col=0)
3525
tm.assert_frame_equal(df, result)
26+
27+
result = pd.read_excel(book, sheet_name=sheet_name,
28+
engine=engine, index_col=0)
29+
tm.assert_frame_equal(df, result)

‎pandas/tests/io/excel/test_xlsxwriter.py

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,64 @@
22

33
import pytest
44

5-
import pandas.util._test_decorators as td
6-
75
from pandas import DataFrame
86
from pandas.util.testing import ensure_clean
97

108
from pandas.io.excel import ExcelWriter
119

10+
xlsxwriter = pytest.importorskip("xlsxwriter")
11+
12+
pytestmark = pytest.mark.parametrize("ext", ['.xlsx'])
13+
14+
15+
def test_column_format(ext):
16+
# Test that column formats are applied to cells. Test for issue #9167.
17+
# Applicable to xlsxwriter only.
18+
with warnings.catch_warnings():
19+
# Ignore the openpyxl lxml warning.
20+
warnings.simplefilter("ignore")
21+
openpyxl = pytest.importorskip("openpyxl")
22+
23+
with ensure_clean(ext) as path:
24+
frame = DataFrame({'A': [123456, 123456],
25+
'B': [123456, 123456]})
26+
27+
writer = ExcelWriter(path)
28+
frame.to_excel(writer)
29+
30+
# Add a number format to col B and ensure it is applied to cells.
31+
num_format = '#,##0'
32+
write_workbook = writer.book
33+
write_worksheet = write_workbook.worksheets()[0]
34+
col_format = write_workbook.add_format({'num_format': num_format})
35+
write_worksheet.set_column('B:B', None, col_format)
36+
writer.save()
37+
38+
read_workbook = openpyxl.load_workbook(path)
39+
try:
40+
read_worksheet = read_workbook['Sheet1']
41+
except TypeError:
42+
# compat
43+
read_worksheet = read_workbook.get_sheet_by_name(name='Sheet1')
44+
45+
# Get the number format from the cell.
46+
try:
47+
cell = read_worksheet['B2']
48+
except TypeError:
49+
# compat
50+
cell = read_worksheet.cell('B2')
51+
52+
try:
53+
read_num_format = cell.number_format
54+
except Exception:
55+
read_num_format = cell.style.number_format._format_code
56+
57+
assert read_num_format == num_format
58+
59+
60+
def test_write_append_mode_raises(ext):
61+
msg = "Append mode is not supported with xlsxwriter!"
1262

13-
@td.skip_if_no('xlsxwriter')
14-
@pytest.mark.parametrize("ext", ['.xlsx'])
15-
class TestXlsxWriterTests:
16-
17-
@td.skip_if_no('openpyxl')
18-
def test_column_format(self, ext):
19-
# Test that column formats are applied to cells. Test for issue #9167.
20-
# Applicable to xlsxwriter only.
21-
with warnings.catch_warnings():
22-
# Ignore the openpyxl lxml warning.
23-
warnings.simplefilter("ignore")
24-
import openpyxl
25-
26-
with ensure_clean(ext) as path:
27-
frame = DataFrame({'A': [123456, 123456],
28-
'B': [123456, 123456]})
29-
30-
writer = ExcelWriter(path)
31-
frame.to_excel(writer)
32-
33-
# Add a number format to col B and ensure it is applied to cells.
34-
num_format = '#,##0'
35-
write_workbook = writer.book
36-
write_worksheet = write_workbook.worksheets()[0]
37-
col_format = write_workbook.add_format({'num_format': num_format})
38-
write_worksheet.set_column('B:B', None, col_format)
39-
writer.save()
40-
41-
read_workbook = openpyxl.load_workbook(path)
42-
try:
43-
read_worksheet = read_workbook['Sheet1']
44-
except TypeError:
45-
# compat
46-
read_worksheet = read_workbook.get_sheet_by_name(name='Sheet1')
47-
48-
# Get the number format from the cell.
49-
try:
50-
cell = read_worksheet['B2']
51-
except TypeError:
52-
# compat
53-
cell = read_worksheet.cell('B2')
54-
55-
try:
56-
read_num_format = cell.number_format
57-
except Exception:
58-
read_num_format = cell.style.number_format._format_code
59-
60-
assert read_num_format == num_format
61-
62-
def test_write_append_mode_raises(self, ext):
63-
msg = "Append mode is not supported with xlsxwriter!"
64-
65-
with ensure_clean(ext) as f:
66-
with pytest.raises(ValueError, match=msg):
67-
ExcelWriter(f, engine='xlsxwriter', mode='a')
63+
with ensure_clean(ext) as f:
64+
with pytest.raises(ValueError, match=msg):
65+
ExcelWriter(f, engine='xlsxwriter', mode='a')

‎pandas/tests/io/excel/test_xlwt.py

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,68 @@
11
import numpy as np
22
import pytest
33

4-
import pandas.util._test_decorators as td
5-
64
import pandas as pd
75
from pandas import DataFrame, MultiIndex
86
from pandas.util.testing import ensure_clean
97

108
from pandas.io.excel import ExcelWriter, _XlwtWriter
119

10+
xlwt = pytest.importorskip("xlwt")
1211

13-
@td.skip_if_no('xlwt')
14-
@pytest.mark.parametrize("ext,", ['.xls'])
15-
class TestXlwtTests:
16-
17-
def test_excel_raise_error_on_multiindex_columns_and_no_index(
18-
self, ext):
19-
# MultiIndex as columns is not yet implemented 9794
20-
cols = MultiIndex.from_tuples([('site', ''),
21-
('2014', 'height'),
22-
('2014', 'weight')])
23-
df = DataFrame(np.random.randn(10, 3), columns=cols)
24-
with pytest.raises(NotImplementedError):
25-
with ensure_clean(ext) as path:
26-
df.to_excel(path, index=False)
27-
28-
def test_excel_multiindex_columns_and_index_true(self, ext):
29-
cols = MultiIndex.from_tuples([('site', ''),
30-
('2014', 'height'),
31-
('2014', 'weight')])
32-
df = pd.DataFrame(np.random.randn(10, 3), columns=cols)
33-
with ensure_clean(ext) as path:
34-
df.to_excel(path, index=True)
35-
36-
def test_excel_multiindex_index(self, ext):
37-
# MultiIndex as index works so assert no error #9794
38-
cols = MultiIndex.from_tuples([('site', ''),
39-
('2014', 'height'),
40-
('2014', 'weight')])
41-
df = DataFrame(np.random.randn(3, 10), index=cols)
12+
pytestmark = pytest.mark.parametrize("ext,", ['.xls'])
13+
14+
15+
def test_excel_raise_error_on_multiindex_columns_and_no_index(ext):
16+
# MultiIndex as columns is not yet implemented 9794
17+
cols = MultiIndex.from_tuples([('site', ''),
18+
('2014', 'height'),
19+
('2014', 'weight')])
20+
df = DataFrame(np.random.randn(10, 3), columns=cols)
21+
with pytest.raises(NotImplementedError):
4222
with ensure_clean(ext) as path:
4323
df.to_excel(path, index=False)
4424

45-
def test_to_excel_styleconverter(self, ext):
46-
import xlwt
47-
48-
hstyle = {"font": {"bold": True},
49-
"borders": {"top": "thin",
50-
"right": "thin",
51-
"bottom": "thin",
52-
"left": "thin"},
53-
"alignment": {"horizontal": "center", "vertical": "top"}}
54-
55-
xls_style = _XlwtWriter._convert_to_style(hstyle)
56-
assert xls_style.font.bold
57-
assert xlwt.Borders.THIN == xls_style.borders.top
58-
assert xlwt.Borders.THIN == xls_style.borders.right
59-
assert xlwt.Borders.THIN == xls_style.borders.bottom
60-
assert xlwt.Borders.THIN == xls_style.borders.left
61-
assert xlwt.Alignment.HORZ_CENTER == xls_style.alignment.horz
62-
assert xlwt.Alignment.VERT_TOP == xls_style.alignment.vert
63-
64-
def test_write_append_mode_raises(self, ext):
65-
msg = "Append mode is not supported with xlwt!"
66-
67-
with ensure_clean(ext) as f:
68-
with pytest.raises(ValueError, match=msg):
69-
ExcelWriter(f, engine='xlwt', mode='a')
25+
26+
def test_excel_multiindex_columns_and_index_true(ext):
27+
cols = MultiIndex.from_tuples([('site', ''),
28+
('2014', 'height'),
29+
('2014', 'weight')])
30+
df = pd.DataFrame(np.random.randn(10, 3), columns=cols)
31+
with ensure_clean(ext) as path:
32+
df.to_excel(path, index=True)
33+
34+
35+
def test_excel_multiindex_index(ext):
36+
# MultiIndex as index works so assert no error #9794
37+
cols = MultiIndex.from_tuples([('site', ''),
38+
('2014', 'height'),
39+
('2014', 'weight')])
40+
df = DataFrame(np.random.randn(3, 10), index=cols)
41+
with ensure_clean(ext) as path:
42+
df.to_excel(path, index=False)
43+
44+
45+
def test_to_excel_styleconverter(ext):
46+
hstyle = {"font": {"bold": True},
47+
"borders": {"top": "thin",
48+
"right": "thin",
49+
"bottom": "thin",
50+
"left": "thin"},
51+
"alignment": {"horizontal": "center", "vertical": "top"}}
52+
53+
xls_style = _XlwtWriter._convert_to_style(hstyle)
54+
assert xls_style.font.bold
55+
assert xlwt.Borders.THIN == xls_style.borders.top
56+
assert xlwt.Borders.THIN == xls_style.borders.right
57+
assert xlwt.Borders.THIN == xls_style.borders.bottom
58+
assert xlwt.Borders.THIN == xls_style.borders.left
59+
assert xlwt.Alignment.HORZ_CENTER == xls_style.alignment.horz
60+
assert xlwt.Alignment.VERT_TOP == xls_style.alignment.vert
61+
62+
63+
def test_write_append_mode_raises(ext):
64+
msg = "Append mode is not supported with xlwt!"
65+
66+
with ensure_clean(ext) as f:
67+
with pytest.raises(ValueError, match=msg):
68+
ExcelWriter(f, engine='xlwt', mode='a')

0 commit comments

Comments
 (0)
Please sign in to comment.