From 1e3e4654f35576cfd891bfe39c6a295665f149ea Mon Sep 17 00:00:00 2001 From: Alexey Leshchenko Date: Fri, 18 Feb 2022 13:46:09 +0300 Subject: [PATCH] Add a sample unit test --- loconotion/__init__.py | 0 loconotion/__main__.py | 2 +- loconotion/notionparser.py | 9 ++++++--- tests/__init__.py | 0 tests/test_parser.py | 12 ++++++++++++ 5 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 loconotion/__init__.py create mode 100644 tests/__init__.py create mode 100644 tests/test_parser.py diff --git a/loconotion/__init__.py b/loconotion/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/loconotion/__main__.py b/loconotion/__main__.py index 78ec90b..d023409 100644 --- a/loconotion/__main__.py +++ b/loconotion/__main__.py @@ -6,7 +6,7 @@ import sys import urllib.parse from pathlib import Path -from notionparser import Parser +from .notionparser import Parser log = logging.getLogger("loconotion") diff --git a/loconotion/notionparser.py b/loconotion/notionparser.py index 6686b1f..46c1cba 100644 --- a/loconotion/notionparser.py +++ b/loconotion/notionparser.py @@ -28,7 +28,7 @@ except ModuleNotFoundError as error: log.critical(f"ModuleNotFoundError: {error}. have your installed the requirements?") sys.exit() -from conditions import notion_page_loaded, toggle_block_has_opened +from .conditions import notion_page_loaded, toggle_block_has_opened class Parser: @@ -243,12 +243,13 @@ class Parser: options=chrome_options, ) - def parse_page(self, url: str, index: str = None): + def parse_page(self, url: str, index: str = None, parse_subpages=True): """Parse page at url and write it to file, then recursively parse all subpages. Args: url (str): URL of the page to parse. index (str, optional): URL of the index page. Defaults to None. + parse_subpages (bool): if subpages should be parsed. Used for testing. After the page at `url` has been parsed, calls itself recursively for every subpage it has discovered. @@ -296,7 +297,9 @@ class Parser: subpages = self.find_subpages(url, index, soup, hrefDomain) self.export_parsed_page(url, index, soup) - self.parse_subpages(index, subpages) + + if parse_subpages: + self.parse_subpages(index, subpages) def load_correct_theme(self, url): self.load(url) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..2fac9a7 --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,12 @@ +import pytest +from loconotion.notionparser import Parser + +def test_parse_sample_page(): + config={"page": "https://www.notion.so/Loconotion-Example-Page-03c403f4fdc94cc1b315b9469a8950ef"} + args = {"timeout": 10} + parser = Parser(config, args) + parser.processed_pages = {} + + parser.parse_page(parser.starting_url, parse_subpages=False) + + assert parser.starting_url in parser.processed_pages