|
11 | 11 | import pandas._testing as tm
|
12 | 12 |
|
13 | 13 | from pandas.io.formats.css import CSSWarning
|
14 |
| -from pandas.io.formats.excel import CSSToExcelConverter |
| 14 | +from pandas.io.formats.excel import ( |
| 15 | + CssExcelCell, |
| 16 | + CSSToExcelConverter, |
| 17 | +) |
15 | 18 |
|
16 | 19 |
|
17 | 20 | @pytest.mark.parametrize(
|
@@ -340,3 +343,89 @@ def test_css_named_colors_from_mpl_present():
|
340 | 343 | pd_colors = CSSToExcelConverter.NAMED_COLORS
|
341 | 344 | for name, color in mpl_colors.items():
|
342 | 345 | assert name in pd_colors and pd_colors[name] == color[1:]
|
| 346 | + |
| 347 | + |
| 348 | +@pytest.mark.parametrize( |
| 349 | + "styles,expected", |
| 350 | + [ |
| 351 | + ([("color", "green"), ("color", "red")], "color: red;"), |
| 352 | + ([("font-weight", "bold"), ("font-weight", "normal")], "font-weight: normal;"), |
| 353 | + ([("text-align", "center"), ("TEXT-ALIGN", "right")], "text-align: right;"), |
| 354 | + ], |
| 355 | +) |
| 356 | +def test_css_excel_cell_precedence(styles, expected): |
| 357 | + """It applies favors latter declarations over former declarations""" |
| 358 | + # See GH 47371 |
| 359 | + converter = CSSToExcelConverter() |
| 360 | + converter.__call__.cache_clear() |
| 361 | + css_styles = {(0, 0): styles} |
| 362 | + cell = CssExcelCell( |
| 363 | + row=0, |
| 364 | + col=0, |
| 365 | + val="", |
| 366 | + style=None, |
| 367 | + css_styles=css_styles, |
| 368 | + css_row=0, |
| 369 | + css_col=0, |
| 370 | + css_converter=converter, |
| 371 | + ) |
| 372 | + converter.__call__.cache_clear() |
| 373 | + |
| 374 | + assert cell.style == converter(expected) |
| 375 | + |
| 376 | + |
| 377 | +@pytest.mark.parametrize( |
| 378 | + "styles,cache_hits,cache_misses", |
| 379 | + [ |
| 380 | + ([[("color", "green"), ("color", "red"), ("color", "green")]], 0, 1), |
| 381 | + ( |
| 382 | + [ |
| 383 | + [("font-weight", "bold")], |
| 384 | + [("font-weight", "normal"), ("font-weight", "bold")], |
| 385 | + ], |
| 386 | + 1, |
| 387 | + 1, |
| 388 | + ), |
| 389 | + ([[("text-align", "center")], [("TEXT-ALIGN", "center")]], 1, 1), |
| 390 | + ( |
| 391 | + [ |
| 392 | + [("font-weight", "bold"), ("text-align", "center")], |
| 393 | + [("font-weight", "bold"), ("text-align", "left")], |
| 394 | + ], |
| 395 | + 0, |
| 396 | + 2, |
| 397 | + ), |
| 398 | + ( |
| 399 | + [ |
| 400 | + [("font-weight", "bold"), ("text-align", "center")], |
| 401 | + [("font-weight", "bold"), ("text-align", "left")], |
| 402 | + [("font-weight", "bold"), ("text-align", "center")], |
| 403 | + ], |
| 404 | + 1, |
| 405 | + 2, |
| 406 | + ), |
| 407 | + ], |
| 408 | +) |
| 409 | +def test_css_excel_cell_cache(styles, cache_hits, cache_misses): |
| 410 | + """It caches unique cell styles""" |
| 411 | + # See GH 47371 |
| 412 | + converter = CSSToExcelConverter() |
| 413 | + converter.__call__.cache_clear() |
| 414 | + |
| 415 | + css_styles = {(0, i): _style for i, _style in enumerate(styles)} |
| 416 | + for css_row, css_col in css_styles: |
| 417 | + CssExcelCell( |
| 418 | + row=0, |
| 419 | + col=0, |
| 420 | + val="", |
| 421 | + style=None, |
| 422 | + css_styles=css_styles, |
| 423 | + css_row=css_row, |
| 424 | + css_col=css_col, |
| 425 | + css_converter=converter, |
| 426 | + ) |
| 427 | + cache_info = converter.__call__.cache_info() |
| 428 | + converter.__call__.cache_clear() |
| 429 | + |
| 430 | + assert cache_info.hits == cache_hits |
| 431 | + assert cache_info.misses == cache_misses |
0 commit comments