Skip to content

Commit

Permalink
Tests for generic stuff from macrogen.report
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed Mar 28, 2020
1 parent a8fb0ca commit 9d51dab
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/macrogen/report.py
Expand Up @@ -8,6 +8,7 @@
from html import escape
from itertools import chain, repeat, groupby
from operator import itemgetter, attrgetter
from os import fspath
from pathlib import Path
from typing import Iterable, List, Dict, Mapping, Tuple, Sequence, Union, Generator, Optional, Set
from urllib.parse import urlencode
Expand Down Expand Up @@ -66,7 +67,7 @@ def __init__(self, **table_attrs):
self.rows = []
self.row_attrs = []

def column(self, title='', format_spec=None, attrs={}, **header_attrs):
def column(self, title='', format_spec=None, attrs={}, **header_attrs) -> 'HtmlTable':
"""
Adds a column to this table.
Expand All @@ -91,6 +92,7 @@ def column(self, title='', format_spec=None, attrs={}, **header_attrs):
formatter = lambda data: format(data, format_spec)

self.formatters.append(formatter)
assert isinstance(attrs, Mapping)
self.attrs.append(attrs)
self.header_attrs.append(header_attrs)
return self
Expand Down Expand Up @@ -251,7 +253,7 @@ def write_html(filename: Path, content: str, head: str = None, breadcrumbs: List
}});
</script>
<?php include "../includes/footer.php"?>"""
with open(filename, 'wt', encoding='utf-8') as f:
with open(fspath(filename), 'wt', encoding='utf-8') as f:
f.write(prefix)
f.write(content)
f.write(suffix)
Expand Down
116 changes: 116 additions & 0 deletions tests/test_report_generic.py
@@ -0,0 +1,116 @@
from datetime import date
from pathlib import Path
from unittest.mock import MagicMock, Mock

import pytest
from macrogen import Reference, Witness
from macrogen.report import HtmlTable, _build_attrs, _fmt_node, _edition_link, _invert_mapping, _flatten


def test_build_attrs():
attrs = _build_attrs({'data_foo': '1', 'title': 'Hunz & Co.'})
assert attrs == ' data-foo="1" title="Hunz &amp; Co."'


def test_simple_table():
table = HtmlTable()
table.column("Col 1")
table.column("Col 2")
assert table.format_table() == ('<table class="pure-table"><thead><th>Col 1</th><th>Col 2</th></thead>'
'<tbody>\n\n</tbody></table>')


def test_header():
table = (HtmlTable().column("Col 1", data_sortable_type="alpha").column("Col 2"))
assert table._format_header() == '<table class="pure-table"><thead>' \
'<th data-sortable-type="alpha">Col 1</th>' \
'<th>Col 2</th></thead><tbody>'


@pytest.fixture
def trivial_table():
trivial_table = HtmlTable()
trivial_table.column("Test")
trivial_table.row((1,))
return trivial_table


def test_trivial_col(trivial_table):
assert trivial_table._format_column(0, 1) == "<td>1</td>"


def test_trivial_row(trivial_table):
row = trivial_table._format_row(trivial_table.rows[0])
assert row == "<tr><td>1</td></tr>"


def test_trivial_body(trivial_table):
html = trivial_table.format_table()
assert html == '<table class="pure-table"><thead><th>Test</th></thead><tbody>\n' \
'<tr><td>1</td></tr>\n' \
'</tbody></table>'


def test_early_fail_wrong_col_attr():
table = HtmlTable()
with pytest.raises(Exception):
table.column('Col 1', attrs={'class', 'test'}) # set instead of dict


def test_body():
table = (HtmlTable()
.column('Col 1', attrs={'class': 'test'})
.column('Col 2'))
table.row((1, 2), data_row='1')
table.row((3, 4), data_row='2')
html = table.format_table()
assert html == '<table class="pure-table">' \
'<thead><th>Col 1</th><th>Col 2</th></thead><tbody>\n' \
'<tr data-row="1"><td class="test">1</td><td>2</td></tr>\n' \
'<tr data-row="2"><td class="test">3</td><td>4</td></tr>\n' \
'</tbody></table>'


def test_format_string():
table = (HtmlTable().column('Test', format_spec='Val: {:02d}'))
assert table._format_column(0, 1) == "<td>Val: 01</td>"


def test_format_spec():
table = HtmlTable().column('', format_spec='02d')
assert table._format_column(0, 1) == '<td>01</td>'


def test_format_fun():
table = HtmlTable().column('Test', format_spec=lambda v: 'Hello ' + str(v))
assert table._format_column(0, "World") == "<td>Hello World</td>"


### no tests for the entire reports


def test_fmt_node_ref():
node = MagicMock(spec=Reference)
node.__str__ = Mock(return_value='2 H')
node.filename = Path('2_H.dot')
assert _fmt_node(node) == '<a href="2_H">2 H</a>'


def test_fmt_node_date():
_fmt_node(date(1749, 8, 28)) == '1749-08-28'


def test_edition_link():
wit = MagicMock(spec=Witness)
wit.__str__ = Mock(return_value='2 H')
wit.sigil_t = '2_H'
html = _edition_link(wit)
assert html == '<a href="/document?sigil=2_H">2 H</a>'


def _test_invert_mapping():
assert _invert_mapping({1: 'a', 2: 'b', 3: 'b'}) == {'a': 1, 'b': {2, 3}}


def test_flatten():
assert _flatten([1, 2, [3, [4, None]]]) == [1, 2, 3, 4]

0 comments on commit 9d51dab

Please sign in to comment.