-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TST/CLN: declass smaller test files in tests\io\excel #26764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,123 @@ | ||
import pytest | ||
|
||
import pandas.util._test_decorators as td | ||
|
||
from pandas import DataFrame | ||
from pandas.util.testing import ensure_clean | ||
|
||
from pandas.io.excel import ExcelWriter, _OpenpyxlWriter | ||
|
||
|
||
@td.skip_if_no('openpyxl') | ||
@pytest.mark.parametrize("ext", ['.xlsx']) | ||
class TestOpenpyxlTests: | ||
|
||
def test_to_excel_styleconverter(self, ext): | ||
from openpyxl import styles | ||
|
||
hstyle = { | ||
"font": { | ||
"color": '00FF0000', | ||
"bold": True, | ||
}, | ||
"borders": { | ||
"top": "thin", | ||
"right": "thin", | ||
"bottom": "thin", | ||
"left": "thin", | ||
}, | ||
"alignment": { | ||
"horizontal": "center", | ||
"vertical": "top", | ||
}, | ||
"fill": { | ||
"patternType": 'solid', | ||
'fgColor': { | ||
'rgb': '006666FF', | ||
'tint': 0.3, | ||
}, | ||
}, | ||
"number_format": { | ||
"format_code": "0.00" | ||
}, | ||
"protection": { | ||
"locked": True, | ||
"hidden": False, | ||
openpyxl = pytest.importorskip("openpyxl") | ||
|
||
pytestmark = pytest.mark.parametrize("ext", ['.xlsx']) | ||
|
||
|
||
def test_to_excel_styleconverter(ext): | ||
from openpyxl import styles | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can move this up |
||
|
||
hstyle = { | ||
"font": { | ||
"color": '00FF0000', | ||
"bold": True, | ||
}, | ||
"borders": { | ||
"top": "thin", | ||
"right": "thin", | ||
"bottom": "thin", | ||
"left": "thin", | ||
}, | ||
"alignment": { | ||
"horizontal": "center", | ||
"vertical": "top", | ||
}, | ||
"fill": { | ||
"patternType": 'solid', | ||
'fgColor': { | ||
'rgb': '006666FF', | ||
'tint': 0.3, | ||
}, | ||
} | ||
|
||
font_color = styles.Color('00FF0000') | ||
font = styles.Font(bold=True, color=font_color) | ||
side = styles.Side(style=styles.borders.BORDER_THIN) | ||
border = styles.Border(top=side, right=side, bottom=side, left=side) | ||
alignment = styles.Alignment(horizontal='center', vertical='top') | ||
fill_color = styles.Color(rgb='006666FF', tint=0.3) | ||
fill = styles.PatternFill(patternType='solid', fgColor=fill_color) | ||
|
||
number_format = '0.00' | ||
|
||
protection = styles.Protection(locked=True, hidden=False) | ||
|
||
kw = _OpenpyxlWriter._convert_to_style_kwargs(hstyle) | ||
assert kw['font'] == font | ||
assert kw['border'] == border | ||
assert kw['alignment'] == alignment | ||
assert kw['fill'] == fill | ||
assert kw['number_format'] == number_format | ||
assert kw['protection'] == protection | ||
|
||
def test_write_cells_merge_styled(self, ext): | ||
from pandas.io.formats.excel import ExcelCell | ||
|
||
sheet_name = 'merge_styled' | ||
|
||
sty_b1 = {'font': {'color': '00FF0000'}} | ||
sty_a2 = {'font': {'color': '0000FF00'}} | ||
|
||
initial_cells = [ | ||
ExcelCell(col=1, row=0, val=42, style=sty_b1), | ||
ExcelCell(col=0, row=1, val=99, style=sty_a2), | ||
] | ||
|
||
sty_merged = {'font': {'color': '000000FF', 'bold': True}} | ||
sty_kwargs = _OpenpyxlWriter._convert_to_style_kwargs(sty_merged) | ||
openpyxl_sty_merged = sty_kwargs['font'] | ||
merge_cells = [ | ||
ExcelCell(col=0, row=0, val='pandas', | ||
mergestart=1, mergeend=1, style=sty_merged), | ||
] | ||
|
||
with ensure_clean(ext) as path: | ||
writer = _OpenpyxlWriter(path) | ||
writer.write_cells(initial_cells, sheet_name=sheet_name) | ||
writer.write_cells(merge_cells, sheet_name=sheet_name) | ||
|
||
wks = writer.sheets[sheet_name] | ||
xcell_b1 = wks['B1'] | ||
xcell_a2 = wks['A2'] | ||
assert xcell_b1.font == openpyxl_sty_merged | ||
assert xcell_a2.font == openpyxl_sty_merged | ||
|
||
@pytest.mark.parametrize("mode,expected", [ | ||
('w', ['baz']), ('a', ['foo', 'bar', 'baz'])]) | ||
def test_write_append_mode(self, ext, mode, expected): | ||
import openpyxl | ||
df = DataFrame([1], columns=['baz']) | ||
|
||
with ensure_clean(ext) as f: | ||
wb = openpyxl.Workbook() | ||
wb.worksheets[0].title = 'foo' | ||
wb.worksheets[0]['A1'].value = 'foo' | ||
wb.create_sheet('bar') | ||
wb.worksheets[1]['A1'].value = 'bar' | ||
wb.save(f) | ||
|
||
writer = ExcelWriter(f, engine='openpyxl', mode=mode) | ||
df.to_excel(writer, sheet_name='baz', index=False) | ||
writer.save() | ||
|
||
wb2 = openpyxl.load_workbook(f) | ||
result = [sheet.title for sheet in wb2.worksheets] | ||
assert result == expected | ||
|
||
for index, cell_value in enumerate(expected): | ||
assert wb2.worksheets[index]['A1'].value == cell_value | ||
}, | ||
"number_format": { | ||
"format_code": "0.00" | ||
}, | ||
"protection": { | ||
"locked": True, | ||
"hidden": False, | ||
}, | ||
} | ||
|
||
font_color = styles.Color('00FF0000') | ||
font = styles.Font(bold=True, color=font_color) | ||
side = styles.Side(style=styles.borders.BORDER_THIN) | ||
border = styles.Border(top=side, right=side, bottom=side, left=side) | ||
alignment = styles.Alignment(horizontal='center', vertical='top') | ||
fill_color = styles.Color(rgb='006666FF', tint=0.3) | ||
fill = styles.PatternFill(patternType='solid', fgColor=fill_color) | ||
|
||
number_format = '0.00' | ||
|
||
protection = styles.Protection(locked=True, hidden=False) | ||
|
||
kw = _OpenpyxlWriter._convert_to_style_kwargs(hstyle) | ||
assert kw['font'] == font | ||
assert kw['border'] == border | ||
assert kw['alignment'] == alignment | ||
assert kw['fill'] == fill | ||
assert kw['number_format'] == number_format | ||
assert kw['protection'] == protection | ||
|
||
|
||
def test_write_cells_merge_styled(ext): | ||
from pandas.io.formats.excel import ExcelCell | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you move imports to the top |
||
|
||
sheet_name = 'merge_styled' | ||
|
||
sty_b1 = {'font': {'color': '00FF0000'}} | ||
sty_a2 = {'font': {'color': '0000FF00'}} | ||
|
||
initial_cells = [ | ||
ExcelCell(col=1, row=0, val=42, style=sty_b1), | ||
ExcelCell(col=0, row=1, val=99, style=sty_a2), | ||
] | ||
|
||
sty_merged = {'font': {'color': '000000FF', 'bold': True}} | ||
sty_kwargs = _OpenpyxlWriter._convert_to_style_kwargs(sty_merged) | ||
openpyxl_sty_merged = sty_kwargs['font'] | ||
merge_cells = [ | ||
ExcelCell(col=0, row=0, val='pandas', | ||
mergestart=1, mergeend=1, style=sty_merged), | ||
] | ||
|
||
with ensure_clean(ext) as path: | ||
writer = _OpenpyxlWriter(path) | ||
writer.write_cells(initial_cells, sheet_name=sheet_name) | ||
writer.write_cells(merge_cells, sheet_name=sheet_name) | ||
|
||
wks = writer.sheets[sheet_name] | ||
xcell_b1 = wks['B1'] | ||
xcell_a2 = wks['A2'] | ||
assert xcell_b1.font == openpyxl_sty_merged | ||
assert xcell_a2.font == openpyxl_sty_merged | ||
|
||
|
||
@pytest.mark.parametrize("mode,expected", [ | ||
('w', ['baz']), ('a', ['foo', 'bar', 'baz'])]) | ||
def test_write_append_mode(ext, mode, expected): | ||
df = DataFrame([1], columns=['baz']) | ||
|
||
with ensure_clean(ext) as f: | ||
wb = openpyxl.Workbook() | ||
wb.worksheets[0].title = 'foo' | ||
wb.worksheets[0]['A1'].value = 'foo' | ||
wb.create_sheet('bar') | ||
wb.worksheets[1]['A1'].value = 'bar' | ||
wb.save(f) | ||
|
||
writer = ExcelWriter(f, engine='openpyxl', mode=mode) | ||
df.to_excel(writer, sheet_name='baz', index=False) | ||
writer.save() | ||
|
||
wb2 = openpyxl.load_workbook(f) | ||
result = [sheet.title for sheet in wb2.worksheets] | ||
assert result == expected | ||
|
||
for index, cell_value in enumerate(expected): | ||
assert wb2.worksheets[index]['A1'].value == cell_value |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,29 @@ | ||
import pandas.util._test_decorators as td | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas.util.testing as tm | ||
from pandas.util.testing import ensure_clean | ||
|
||
from pandas.io.excel import ExcelFile | ||
|
||
xlrd = pytest.importorskip("xlrd") | ||
xlwt = pytest.importorskip("xlwt") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we already have a file we can use that contains the content of |
||
|
||
@td.skip_if_no('xlrd') | ||
class TestXlrdReader: | ||
""" | ||
This is the base class for the xlrd tests, and 3 different file formats | ||
are supported: xls, xlsx, xlsm | ||
""" | ||
|
||
@td.skip_if_no("xlwt") | ||
def test_read_xlrd_book(self, read_ext, frame): | ||
import xlrd | ||
df = frame | ||
def test_read_xlrd_book(read_ext, frame): | ||
df = frame | ||
|
||
engine = "xlrd" | ||
sheet_name = "SheetA" | ||
engine = "xlrd" | ||
sheet_name = "SheetA" | ||
|
||
with ensure_clean(read_ext) as pth: | ||
df.to_excel(pth, sheet_name) | ||
book = xlrd.open_workbook(pth) | ||
with ensure_clean(read_ext) as pth: | ||
df.to_excel(pth, sheet_name) | ||
book = xlrd.open_workbook(pth) | ||
|
||
with ExcelFile(book, engine=engine) as xl: | ||
result = pd.read_excel(xl, sheet_name, index_col=0) | ||
tm.assert_frame_equal(df, result) | ||
|
||
result = pd.read_excel(book, sheet_name=sheet_name, | ||
engine=engine, index_col=0) | ||
with ExcelFile(book, engine=engine) as xl: | ||
result = pd.read_excel(xl, sheet_name, index_col=0) | ||
tm.assert_frame_equal(df, result) | ||
|
||
result = pd.read_excel(book, sheet_name=sheet_name, | ||
engine=engine, index_col=0) | ||
tm.assert_frame_equal(df, result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this just a fixture definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want more fixtures?
we could have a fixture and inject into all tests or we could alias this mark and decorate all tests.
this just applies the parametrisation to all tests in module automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not yet but we should probably create a
write_ext
fixture for use here and in the writer tests (the existing fixture is applicable to extensions that we read)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just find this very opaque here (we don't use this idiom anywhere else)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably better done in a separate PR IMO as that will require a decent amount of updates