Exemple #1
0
class TestFormatValidationTheme:
    """
    Test validation of format dictionaries.
    """
    def test_invalid_attribute_config(self, empty_theme):
        """
        Test that invalid attribute names in config raises a ValueError.
        """
        config = {"potato": {"font_size": 9}}
        with pytest.raises(ValueError):
            empty_theme.apply_config(config)

    def test_invalid_format_label_config(self, empty_theme):
        """
        Test that invalid format labels in config raises a ValueError.
        """
        config = {"notes": {"not_a_format": 5}}
        with pytest.raises(ValueError):
            empty_theme.apply_config(config)

    @pytest.mark.parametrize("attr", Theme()._format_attributes)
    def test_invalid_format_label_single_attr(self, attr, empty_theme):
        """
        Test that `validate_single_format` decorator catches invalid format
        labels in individual format dictionaries.
        """
        format_dict = {"not_a_format": 5}
        with pytest.raises(ValueError):
            getattr(empty_theme, "update_" + attr)(format_dict)

    @pytest.mark.parametrize("attr", Theme()._format_attributes)
    def test_valid_format_label(self, attr, empty_theme):
        """
        Test that Theme attribute is correctly updated when valid format label
        is used.
        """
        getattr(empty_theme, "update_" + attr)({"font_size": 9})

        exp = {"font_size": 9}
        got = getattr(empty_theme, attr)

        assert exp == got

    def test_valid_format_label_config(self, empty_theme):
        """
        Test that valid format labels in config changes specified format attr
        but not an unrelated attr. Previous bug updated all formats whenever
        one was updated.
        """
        config = {"notes": {"font_size": 5}}
        empty_theme.apply_config(config)

        exp = {"font_size": 5}
        got = empty_theme.notes_format

        assert exp == got

        got2 = empty_theme.title_format

        assert {} == got2
Exemple #2
0
    def test__smart_write_formatted_rich_text(self, testbook):
        testbook.wb.set_theme(Theme({}))

        testbook.ws._smart_write(1, 2, valid_text_elements[1], {"bold": True})
        # Strings are stored in a lookup table for efficiency
        got_string = testbook.ws.str_table.string_table
        exp_string = {
            '<r><t xml:space="preserve">More than </t></r><r><rPr><b'
            '/><i/><sz val="11"/><color theme="1"/><rFont val="Calib'
            'ri"/><family val="2"/><scheme val="minor"/></rPr><t xml'
            ':space="preserve">just </t></r><r><rPr><b/><sz val="11"'
            '/><color theme="1"/><rFont val="Calibri"/><family val="'
            '2"/><scheme val="minor"/></rPr><t>a string</t></r>':
            0
        }

        assert got_string == exp_string

        # String is referenced using a named tuple (string, Format)
        # Here we get first element, which references string lookup location
        cell = testbook.ws.table[1][2]
        got_lookup = cell[0]
        exp_lookup = 0
        assert got_lookup == exp_lookup

        format_obj = cell[1]
        assert format_obj.bold
Exemple #3
0
    def test_file_init(self):
        """
        Test initialisation of Theme using default theme yaml config file.
        """
        config_file = resource_filename("gptables", "themes/gptheme.yaml")
        got = Theme(config_file)

        exp = gptheme

        assert exp == got
Exemple #4
0
class TestCleanInitTheme:
    """
    Test initialisation of the Theme class without config.
    """
    @pytest.mark.parametrize("attr", Theme()._format_attributes)
    def test_default_format_attrs(self, attr, empty_theme):
        """Test Theme attribute default types"""
        exp = {}
        got = getattr(empty_theme, attr)
        assert exp == got

    def test_default_other_attrs(self, empty_theme):
        assert empty_theme.footer_order == []
        assert empty_theme.missing_value == None

    def test_print_attributes(self, empty_theme):
        """Test Theme print_attributes()"""
        from io import StringIO

        file_handler = StringIO()
        with redirect_stdout(file_handler):
            empty_theme.print_attributes()

        got = file_handler.getvalue()

        exp = ("""cover_title_format : {}
cover_subtitle_format : {}
cover_text_format : {}
title_format : {}
subtitle_format : {}
scope_format : {}
units_format : {}
column_heading_format : {}
index_1_format : {}
index_2_format : {}
index_3_format : {}
data_format : {}
source_format : {}
legend_format : {}
annotations_format : {}
notes_format : {}
footer_order : []
missing_value : None
""")

        assert got == exp
Exemple #5
0
    def test__smart_write_rich_text(self, testbook):
        testbook.wb.set_theme(Theme({}))

        testbook.ws._smart_write(0, 0, valid_text_elements[1], {})
        # Strings are stored in a lookup table for efficiency
        got_string = testbook.ws.str_table.string_table
        exp_string = {
            '<r><t xml:space="preserve">More than </t></r><r><rPr><i'
            '/><sz val="11"/><color theme="1"/><rFont val="Calibri"/'
            '><family val="2"/><scheme val="minor"/></rPr><t xml:spa'
            'ce="preserve">just </t></r><r><rPr><sz val="11"/><color'
            ' theme="1"/><rFont val="Calibri"/><family val="2"/><sch'
            'eme val="minor"/></rPr><t>a string</t></r>':
            0
        }
        assert got_string == exp_string

        # String is referenced using a named tuple (string, Format)
        # Here we get first element, which references string lookup location
        got_lookup = testbook.ws.table[0][0][0]
        exp_lookup = 0
        assert got_lookup == exp_lookup
Exemple #6
0
    def test_dict_init(self):
        config = {
            "global": {
                "font_size": 9,
                "font_name": "Arial"
            },
            "cover_title": {
                'font_size': 12,
                'bold': True
            },
            "cover_subtitle": {
                'font_size': 10,
                'bold': True
            },
            "cover_text": None,
            "title": {
                "bold": True,
                "font_size": 11
            },
            "subtitle": {
                "font_size": 10
            },
            "scope": None,
            "units": {
                "align": "right",
                "italic": True
            },
            "column_heading": {
                "bold": True,
                "bottom": 1
            },
            "index_1": {
                "bold": True
            },
            "index_2": None,
            "index_3": {
                "italic": True
            },
            "data": None,
            "source": {
                "font_size": 7
            },
            "legend": {
                "font_size": 7
            },
            "annotations": {
                "font_size": 7
            },
            "notes": {
                "font_size": 7
            },
            "footer_order": [
                "source",
                "legend",
                "annotations",
                "notes",
            ],
            "missing_value": ':'
        }
        got = Theme(config)

        exp = gptheme

        assert exp == got
Exemple #7
0
def empty_theme():
    yield Theme()