diff --git a/adr/formatter.py b/adr/formatter.py index 850b5e1..1fe320b 100644 --- a/adr/formatter.py +++ b/adr/formatter.py @@ -21,6 +21,7 @@ def __init__(self, table_cls=SingleTable): self.table_cls = table_cls def __call__(self, data): + print(data) if isinstance(data, bytes): data = json.loads(data) diff --git a/test/formatter_tests/__init__.py b/test/formatter_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/formatter_tests/jsonformatter_expected.json b/test/formatter_tests/jsonformatter_expected.json new file mode 100644 index 0000000..c5e6c4e --- /dev/null +++ b/test/formatter_tests/jsonformatter_expected.json @@ -0,0 +1 @@ +{"expected_json": ["[\n [\n \"Heading A\",\n \"Heading B\"\n ],\n [\n 1,\n 2\n ],\n [\n 3,\n 4\n ],\n [\n 5,\n 6\n ]\n]", "{\n \"names\": [\n \"Heading A\",\n \"Heading B\",\n \"Heading C\"\n ],\n \"Heading A\": [\n \"x\",\n \"y\"\n ],\n \"Heading B\": [\n 11,\n 20.5\n ],\n \"Heading C\": [\n 12,\n 30.5\n ]\n}"]} \ No newline at end of file diff --git a/test/formatter_tests/markdownformatter_expected.json b/test/formatter_tests/markdownformatter_expected.json new file mode 100644 index 0000000..57f1fdc --- /dev/null +++ b/test/formatter_tests/markdownformatter_expected.json @@ -0,0 +1 @@ +{"expected_markdown": ["\n| Heading A | Heading B |\n|-----------|-----------|\n| 1 | 2 |\n| 3 | 4 |\n| 5 | 6 |\n", "\n| Heading A | Heading B | Heading C |\n|-----------|-----------|-----------|\n| x | 11 | 12 |\n| y | 20.5 | 30.5 |"]} \ No newline at end of file diff --git a/test/formatter_tests/tableformatter_expected.json b/test/formatter_tests/tableformatter_expected.json new file mode 100644 index 0000000..1247d14 --- /dev/null +++ b/test/formatter_tests/tableformatter_expected.json @@ -0,0 +1 @@ +{"expected_table": ["\n┌───────────┬───────────┐\n│ Heading A │ Heading B │\n├───────────┼───────────┤\n│ 1 │ 2 │\n│ 3 │ 4 │\n│ 5 │ 6 │\n└───────────┴───────────┘", "\n┌───────────┬───────────┬───────────┐\n│ Heading A │ Heading B │ Heading C │\n├───────────┼───────────┼───────────┤\n│ x │ 11 │ 12 │\n│ y │ 20.5 │ 30.5 │\n└───────────┴───────────┴───────────┘"]} \ No newline at end of file diff --git a/test/test_formatter.py b/test/test_formatter.py new file mode 100644 index 0000000..de628d1 --- /dev/null +++ b/test/test_formatter.py @@ -0,0 +1,43 @@ +import os +import io +import json +import pytest +import formatter_tests +from adr.formatter import all_formatters + + +test_data=([['Heading A', 'Heading B'],[1, 2],[3, 4],[5,6]],{'names': ['Heading A', 'Heading B', 'Heading C'], + 'Heading A': ['x', 'y'], + 'Heading B': [11, 20.5], + 'Heading C': [12, 30.5] +}) + +def get_data(path): + with io.open(path,'r',encoding="utf-8") as f: + data = json.load(f) + return data + + +test_dir=os.path.dirname(formatter_tests.__file__) + +expected_table=get_data(os.path.join(test_dir,'tableformatter_expected.json'))["expected_table"] +expected_json= get_data(os.path.join(test_dir,'jsonformatter_expected.json'))["expected_json"] +expected_markdown=get_data(os.path.join(test_dir,'markdownformatter_expected.json'))["expected_markdown"] + + + +@pytest.mark.parametrize("test_data,expected_json",list(zip(test_data,expected_json)),ids=["jsonformat_case1", "jsonformat_case2"]) +def test_jsonformatter(test_data,expected_json): + """check MarkdownFormatter method """ + assert all_formatters['json'](test_data)==expected_json.strip() + + +@pytest.mark.parametrize("test_data,expected_table",list(zip(test_data,expected_table)),ids=["tableformat_case1", "tableformat_case2"]) +def test_tableformatter(test_data,expected_table): + """check MarkdownFormatter method """ + assert all_formatters['table'](test_data)==expected_table.strip() + +@pytest.mark.parametrize("test_data,expected_markdown",list(zip(test_data,expected_markdown)),ids=["markdownformat1", "markdownformat2"]) +def test_markdownformatter(test_data,expected_markdown): + """check MarkdownFormatter method """ + assert all_formatters['markdown'](test_data)==expected_markdown.strip()