From 7d4467908049e3ba15f24be68bdbf3e11e98729c Mon Sep 17 00:00:00 2001 From: nadaa Date: Wed, 3 Oct 2018 00:11:18 +0300 Subject: [PATCH 1/2] Add test_formatter --- adr/formatter.py | 1 + test/test_formatter.py | 114 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 test/test_formatter.py 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/test_formatter.py b/test/test_formatter.py new file mode 100644 index 0000000..15ca6b1 --- /dev/null +++ b/test/test_formatter.py @@ -0,0 +1,114 @@ +import pytest +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] +}) + +expected_table=( + """ +┌───────────┬───────────┐ +│ Heading A │ Heading B │ +├───────────┼───────────┤ +│ 1 │ 2 │ +│ 3 │ 4 │ +│ 5 │ 6 │ +└───────────┴───────────┘""", + """ +┌───────────┬───────────┬───────────┐ +│ Heading A │ Heading B │ Heading C │ +├───────────┼───────────┼───────────┤ +│ x │ 11 │ 12 │ +│ y │ 20.5 │ 30.5 │ +└───────────┴───────────┴───────────┘""" +) + +expected_json=( +""" +[ + [ + "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 + ] +} +""" +) + +expected_tab=( +""" +Heading A Heading B +1 2 +3 4 +5 6 +""" +) + +expected_markdown=( +""" +| Heading A | Heading B | +|-----------|-----------| +| 1 | 2 | +| 3 | 4 | +| 5 | 6 | +""", +""" +| Heading A | Heading B | Heading C | +|-----------|-----------|-----------| +| x | 11 | 12 | +| y | 20.5 | 30.5 |""" +) + +@pytest.mark.parametrize("test_data,expected_json",list(zip(test_data,expected_json))) +def test_jsonformatter(test_data,expected_json): + """check JSONFormatter method """ + assert all_formatters['json'](test_data)==expected_json.strip() + +@pytest.mark.parametrize("test_data,expected_table",list(zip(test_data,expected_table))) +def test_tableformatter(test_data,expected_table): + """check TableFormatter method """ + assert all_formatters['table'](test_data)==expected_table.strip() + +@pytest.mark.parametrize("test_data,expected_markdown",list(zip(test_data,expected_markdown))) +def test_markdownformatter(test_data,expected_markdown): + """check MarkdownFormatter method """ + assert all_formatters['markdown'](test_data)==expected_markdown.strip() + +# @pytest.mark.parametrize("test_data,expected_tab",list(zip(test_data,expected_tab))) +# def test_tableformatter(test_data,expected_tab): +# assert all_formatters['tab'](test_data)==expected_tab.strip() From ea3719fd914e31db9b98cf92044b1715aed6d17c Mon Sep 17 00:00:00 2001 From: nadaa Date: Fri, 12 Oct 2018 21:41:32 +0300 Subject: [PATCH 2/2] update test_formatter --- test/formatter_tests/__init__.py | 0 .../jsonformatter_expected.json | 1 + .../markdownformatter_expected.json | 1 + .../tableformatter_expected.json | 1 + test/test_formatter.py | 157 +++++------------- 5 files changed, 46 insertions(+), 114 deletions(-) create mode 100644 test/formatter_tests/__init__.py create mode 100644 test/formatter_tests/jsonformatter_expected.json create mode 100644 test/formatter_tests/markdownformatter_expected.json create mode 100644 test/formatter_tests/tableformatter_expected.json 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 index 15ca6b1..de628d1 100644 --- a/test/test_formatter.py +++ b/test/test_formatter.py @@ -1,114 +1,43 @@ -import pytest -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] -}) - -expected_table=( - """ -┌───────────┬───────────┐ -│ Heading A │ Heading B │ -├───────────┼───────────┤ -│ 1 │ 2 │ -│ 3 │ 4 │ -│ 5 │ 6 │ -└───────────┴───────────┘""", - """ -┌───────────┬───────────┬───────────┐ -│ Heading A │ Heading B │ Heading C │ -├───────────┼───────────┼───────────┤ -│ x │ 11 │ 12 │ -│ y │ 20.5 │ 30.5 │ -└───────────┴───────────┴───────────┘""" -) - -expected_json=( -""" -[ - [ - "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 - ] -} -""" -) - -expected_tab=( -""" -Heading A Heading B -1 2 -3 4 -5 6 -""" -) - -expected_markdown=( -""" -| Heading A | Heading B | -|-----------|-----------| -| 1 | 2 | -| 3 | 4 | -| 5 | 6 | -""", -""" -| Heading A | Heading B | Heading C | -|-----------|-----------|-----------| -| x | 11 | 12 | -| y | 20.5 | 30.5 |""" -) - -@pytest.mark.parametrize("test_data,expected_json",list(zip(test_data,expected_json))) -def test_jsonformatter(test_data,expected_json): - """check JSONFormatter method """ - assert all_formatters['json'](test_data)==expected_json.strip() - -@pytest.mark.parametrize("test_data,expected_table",list(zip(test_data,expected_table))) -def test_tableformatter(test_data,expected_table): - """check TableFormatter method """ - assert all_formatters['table'](test_data)==expected_table.strip() - -@pytest.mark.parametrize("test_data,expected_markdown",list(zip(test_data,expected_markdown))) -def test_markdownformatter(test_data,expected_markdown): - """check MarkdownFormatter method """ - assert all_formatters['markdown'](test_data)==expected_markdown.strip() - -# @pytest.mark.parametrize("test_data,expected_tab",list(zip(test_data,expected_tab))) -# def test_tableformatter(test_data,expected_tab): -# assert all_formatters['tab'](test_data)==expected_tab.strip() +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()