From 48bcdcc3dd662c921062433a95cac14787007608 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 8 Oct 2024 15:51:11 -0300 Subject: [PATCH 1/6] Add TestXLSXStyle --- tests/test_utilities.py | 81 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index f8755e3..66540e6 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,4 +1,83 @@ -from drf_excel.utilities import get_setting +from openpyxl.styles import Font, PatternFill, Alignment, Border, Color, Side + +from drf_excel.utilities import get_setting, XLSXStyle + + +class TestXLSXStyle: + def test_no_style_dict(self): + style = XLSXStyle() + assert style.font is None + assert style.fill is None + assert style.alignment is None + assert style.number_format is None + assert style.border is None + + def test_with_font_style(self): + style = XLSXStyle( + { + "font": {"name": "Arial", "size": 14, "bold": True}, + } + ) + + assert isinstance(style.font, Font) + assert style.font.name == "Arial" + assert style.font.size == 14 + assert style.font.bold is True + + def test_with_fill_style(self): + style = XLSXStyle( + { + "fill": { + "fill_type": "solid", + "start_color": "FFCCFFCC", + }, + } + ) + + assert isinstance(style.fill, PatternFill) + assert style.fill.fill_type == "solid" + assert isinstance(style.fill.start_color, Color) + assert style.fill.start_color.value == "FFCCFFCC" + assert style.fill.start_color.type == "rgb" + + def test_with_alignment_style(self): + style = XLSXStyle( + { + "alignment": { + "horizontal": "center", + "wrap_text": True, + "text_rotation": 20, + }, + } + ) + + assert isinstance(style.alignment, Alignment) + assert style.alignment.horizontal == "center" + assert style.alignment.wrap_text is True + assert style.alignment.text_rotation == 20 + + def test_with_border_style(self): + style = XLSXStyle( + {"border_side": {"border_style": "thin", "color": "FF000000"}} + ) + + assert isinstance(style.border, Border) + assert isinstance(style.border.left, Side) + assert style.border.left.border_style == "thin" + assert isinstance(style.border.left.color, Color) + assert style.border.left.color.value == "FF000000" + assert style.border.left.color.type == "rgb" + assert ( + style.border.left + == style.border.right + == style.border.top + == style.border.bottom + ) + + def test_with_number_format_style(self): + style = XLSXStyle({"format": "0.00E+00"}) + + assert style.number_format == "0.00E+00" def test_get_setting_not_found(): From 7adf7ad1e3915432188455cc11b85c1c61c70953 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 8 Oct 2024 15:56:02 -0300 Subject: [PATCH 2/6] Add TestGetAttribute --- tests/test_utilities.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 66540e6..cf69243 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,6 +1,8 @@ +from types import SimpleNamespace + from openpyxl.styles import Font, PatternFill, Alignment, Border, Color, Side -from drf_excel.utilities import get_setting, XLSXStyle +from drf_excel.utilities import get_setting, XLSXStyle, get_attribute class TestXLSXStyle: @@ -80,5 +82,27 @@ def test_with_number_format_style(self): assert style.number_format == "0.00E+00" +class TestGetAttribute: + def test_existing(self): + obj = SimpleNamespace(a=1) + assert get_attribute(obj, "a") == 1 + + def test_non_existing(self): + obj = SimpleNamespace(a=1) + assert get_attribute(obj, "b") is None + + def test_non_existing_with_default(self): + obj = SimpleNamespace(a=1) + assert get_attribute(obj, "b", default="c") == "c" + + def test_from_getter_method(self): + class Foo: + def get_b(self): + return 1 + + obj = Foo() + assert get_attribute(obj, "b") == 1 + + def test_get_setting_not_found(): assert get_setting("INTEGER_FORMAT") is None From 8ba1f8d8ada2eefb0ceaa5a06cfafd8d8a252eea Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 8 Oct 2024 15:59:04 -0300 Subject: [PATCH 3/6] Add TestGetSetting --- tests/test_utilities.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index cf69243..d604ca4 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -104,5 +104,13 @@ def get_b(self): assert get_attribute(obj, "b") == 1 -def test_get_setting_not_found(): - assert get_setting("INTEGER_FORMAT") is None +class TestGetSetting: + def test_not_defined(self): + assert get_setting("DUMMY") is None + + def test_not_defined_with_default(self): + assert get_setting("DUMMY", "default") == "default" + + def test_defined(self, settings): + settings.DRF_EXCEL_DUMMY = "custom-value" + assert get_setting("DUMMY") == "custom-value" From f9fa50b0ebe2f834d716d7fbc33718652adcaa23 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 8 Oct 2024 16:10:33 -0300 Subject: [PATCH 4/6] Add test_sanitize_value with parametrize --- tests/test_utilities.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index d604ca4..269c1ae 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,8 +1,10 @@ from types import SimpleNamespace +import pytest +import datetime as dt from openpyxl.styles import Font, PatternFill, Alignment, Border, Color, Side -from drf_excel.utilities import get_setting, XLSXStyle, get_attribute +from drf_excel.utilities import get_setting, XLSXStyle, get_attribute, sanitize_value class TestXLSXStyle: @@ -114,3 +116,39 @@ def test_not_defined_with_default(self): def test_defined(self, settings): settings.DRF_EXCEL_DUMMY = "custom-value" assert get_setting("DUMMY") == "custom-value" + + +@pytest.mark.parametrize( + ("value", "expected_output"), + [ + # Regular values without illegal characters + (None, None), + ("test", "test"), + (True, "True"), + (False, False), # Bug? + (1, "1"), + (dt.date(2020, 1, 1), "2020-01-01"), + (dt.datetime(2020, 1, 1, 1, 1, 1), "2020-01-01 01:01:01"), + # With illegal characters + ("test\000", "test"), + ("t\001est", "test"), + ("test\005er", "tester"), + ("tes\010t", "test"), + ("test\013 me", "test me"), + ("test\014", "test"), + ("test\016", "test"), + ("test\020", "test"), + ("test\030", "test"), + ("test\037", "test"), + # Escape if starts with these characters + ("=test", "'=test"), + ("-example", "'-example"), + ("+foo", "'+foo"), + ("@bar", "'@bar"), + ("\tbaz", "'\tbaz"), + ("\nqux", "'\nqux"), + ("\rquux", "'\rquux"), + ], +) +def test_sanitize_value(value, expected_output): + assert sanitize_value(value) == expected_output From 1fcf4d973a29c7f64db8dfd140836c4744d38ab9 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 8 Oct 2024 16:26:22 -0300 Subject: [PATCH 5/6] Add TestSetCellStyle --- tests/test_utilities.py | 48 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 269c1ae..01a2abc 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -2,9 +2,19 @@ import pytest import datetime as dt -from openpyxl.styles import Font, PatternFill, Alignment, Border, Color, Side -from drf_excel.utilities import get_setting, XLSXStyle, get_attribute, sanitize_value +from openpyxl.cell import Cell +from openpyxl.styles import Font, PatternFill, Alignment, Border, Color, Side +from openpyxl.workbook import Workbook +from openpyxl.worksheet.worksheet import Worksheet + +from drf_excel.utilities import ( + get_setting, + XLSXStyle, + get_attribute, + sanitize_value, + set_cell_style, +) class TestXLSXStyle: @@ -152,3 +162,37 @@ def test_defined(self, settings): ) def test_sanitize_value(value, expected_output): assert sanitize_value(value) == expected_output + + +class TestSetCellStyle: + @pytest.fixture + def cell(self): + worksheet = Worksheet(Workbook()) + return Cell(worksheet) + + def test_no_style(self, cell): + set_cell_style(cell, None) + assert cell.font is not None + + def test_with_styles(self, cell): + style = XLSXStyle( + { + "font": {"name": "Arial"}, + "fill": {"fill_type": "solid"}, + "alignment": {"horizontal": "center"}, + "border_side": {"border_style": "thin"}, + "format": "0.00", + }, + ) + set_cell_style(cell, style) + assert cell.font.name == "Arial" + assert cell.fill.fill_type == "solid" + assert cell.alignment.horizontal == "center" + assert cell.border.left.border_style == "thin" + assert cell.number_format == "0.00" + + def test_with_partial_styles(self, cell): + style = XLSXStyle({"font": {"name": "Arial"}}) + set_cell_style(cell, style) + assert cell.font.name == "Arial" + assert cell.number_format == "General" From fd9180c8659d871f9a714cec024350c494ace844 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 8 Oct 2024 16:26:35 -0300 Subject: [PATCH 6/6] Add type hint --- drf_excel/utilities.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drf_excel/utilities.py b/drf_excel/utilities.py index d5470e8..b46492c 100644 --- a/drf_excel/utilities.py +++ b/drf_excel/utilities.py @@ -1,5 +1,5 @@ from django.conf import settings as django_settings -from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE +from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE, Cell from openpyxl.styles import Alignment, Border, Font, PatternFill, Side ESCAPE_CHARS = ("=", "-", "+", "@", "\t", "\r", "\n") @@ -86,7 +86,7 @@ def sanitize_value(value): return value -def set_cell_style(cell, style: XLSXStyle): +def set_cell_style(cell: Cell, style: XLSXStyle): # We are not applying the whole style directly, otherwise we cannot override any part of it if style: # Only set properties that are provided