Skip to content

Commit

Permalink
refactor tests to work with new logger setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sHermanGriffiths committed Mar 16, 2024
1 parent 968af4b commit e6b38a6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
11 changes: 6 additions & 5 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
These tests verify how the n2y block classes convert notion data into Pandoc
abstract syntax tree (AST) objects, and then into markdown.
"""

import re
from unittest import mock

Expand Down Expand Up @@ -167,7 +168,7 @@ def generate_block(notion_block, plugins=None):
def process_block(notion_block, plugins=None):
n2y_block = generate_block(notion_block, plugins=plugins)
pandoc_ast = n2y_block.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, n2y_block.client.logger)
return pandoc_ast, markdown


Expand All @@ -178,7 +179,7 @@ def process_parent_block(notion_block, child_notion_blocks, plugins=None):
mock_get_child_notion_blocks.return_value = child_notion_blocks
n2y_block = generate_block(notion_block, plugins)
pandoc_ast = n2y_block.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, n2y_block.client.logger)
return pandoc_ast, markdown


Expand Down Expand Up @@ -774,7 +775,7 @@ def test_toc_block():
toc = client.wrap_notion_block(toc_block, None, True)
toc.render_toc([*toc_headers, *toc_headers])
pandoc_ast = toc.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert pandoc_ast == [
OrderedList((1, Decimal(), Period()), [toc_item_ast, toc_item_ast])
]
Expand All @@ -799,7 +800,7 @@ def test_toc_block_starting_h2():
toc = client.wrap_notion_block(toc_block, None, True)
toc.render_toc([*toc_headers_starting_h2, *toc_headers_starting_h2])
pandoc_ast = toc.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert (
markdown
== """\
Expand All @@ -825,7 +826,7 @@ def test_toc_block_h1_after_h2_base(wrap_notion_user):
toc = client.wrap_notion_block(toc_block, page, True)
toc.render_toc([*toc_headers_starting_h2, *toc_headers])
pandoc_ast = toc.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert (
markdown
== """\
Expand Down
28 changes: 16 additions & 12 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import copy

import yaml

from n2y.config import (
valid_notion_id,
merge_config,
load_config,
EXPORT_DEFAULTS,
_valid_notion_filter,
_validate_config_item,
EXPORT_DEFAULTS,
load_config,
merge_config,
valid_notion_id,
)
from n2y.logger import logger
from n2y.notion_mocks import mock_id


Expand Down Expand Up @@ -44,7 +46,7 @@ def test_load_config_basic(tmp_path):
}
)
)
config = load_config(config_path)
config = load_config(config_path, logger)
assert config is not None, "The config is invalid"
merged_export = config["exports"][0]
assert merged_export["id"] == export_id
Expand Down Expand Up @@ -100,7 +102,8 @@ def test_valid_notion_filter_simple():
{
"property": "title",
"direction": "ascending",
}
},
logger,
)


Expand All @@ -111,34 +114,35 @@ def test_valid_notion_filter_complex():
"property": "title",
"direction": "ascending",
}
]
],
logger,
)


def test_valid_config_item_missing_id():
config_item = mock_config_item("page")
del config_item["id"]
assert not _validate_config_item(config_item)
assert not _validate_config_item(config_item, logger)


def test_valid_config_item_missing_node_type():
config_item = mock_config_item("page")
del config_item["node_type"]
assert not _validate_config_item(config_item)
assert not _validate_config_item(config_item, logger)


def test_valid_config_item_invalid_node_type():
config_item = mock_config_item("page")
config_item["node_type"] = "invalid"
assert not _validate_config_item(config_item)
assert not _validate_config_item(config_item, logger)


def test_valid_config_item_missing_filename_template():
config_item = mock_config_item("database_as_files")
assert not _validate_config_item(config_item)
assert not _validate_config_item(config_item, logger)


def test_valid_config_item_malformed_filename_template():
config_item = mock_config_item("database_as_files")
config_item["filename_template"] = "{"
assert not _validate_config_item(config_item)
assert not _validate_config_item(config_item, logger)
20 changes: 10 additions & 10 deletions tests/test_jinjarenderpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_jinja_syntax_err():
jinja_code = "{% huhwhat 'hotel', 'california' %}"
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert "syntax error" in markdown
assert "Encountered unknown tag 'huhwhat'" in markdown

Expand All @@ -113,7 +113,7 @@ def test_jinja_render_gfm():
jinja_code = "{% for v in ['a', 'b'] %}{{v}}{% endfor %}"
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert markdown == "ab\n"


Expand All @@ -126,7 +126,7 @@ def test_jinja_render_gfm_with_second_pass():
with patch.object(Page, "block", new_callable=PropertyMock) as mock_block:
mock_block.return_value = page_block
pandoc_ast = page.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert markdown == "aa\n"


Expand All @@ -141,7 +141,7 @@ def test_jinja_render_html():
)
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()
markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert markdown == " Name\n ------\n a\n b\n"


Expand All @@ -167,7 +167,7 @@ def test_jinja_render_with_database(wrap_notion_user):
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()

markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert markdown == "ab\n"


Expand All @@ -193,7 +193,7 @@ def test_jinja_render_with_missing_database(wrap_notion_user):
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()

markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert 'You attempted to access the "MISSING" database' in markdown
assert 'the only available database is "My DB".' in markdown

Expand All @@ -220,7 +220,7 @@ def test_jinja_render_with_incorrect_db_property(wrap_notion_user):
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()

markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert (
'You attempted to access the "Foo" property of a database item on line 1'
in markdown
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_jinja_render_with_missing_page_property(wrap_notion_user):
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()

markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert 'You attempted to access the "MISSING" property' in markdown
assert 'the only available property is "title".' in markdown

Expand All @@ -279,7 +279,7 @@ def test_jinja_render_with_filter_error(wrap_notion_user):
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()

markdown = pandoc_ast_to_markdown(pandoc_ast)
markdown = pandoc_ast_to_markdown(pandoc_ast, client.logger)
assert 'Recieved the message "type str doesn\'t define __round__ method"' in markdown
assert 'The Jinja filter "round" raised this error' in markdown
assert 'argument(s): {\n\t"args": (\'a\',),\n\t"kwargs": {}\n}' in markdown
Expand All @@ -291,7 +291,7 @@ def test_jinja_render_plain():
jinja_code = "# <h1> asdf23"
page = process_jinja_block(client, caption, jinja_code)
pandoc_ast = page.to_pandoc()
text = pandoc_write_or_log_errors(pandoc_ast, "plain", [])
text = pandoc_write_or_log_errors(pandoc_ast, "plain", [], client.logger)
assert text == "# <h1> asdf23\n"


Expand Down

0 comments on commit e6b38a6

Please sign in to comment.