From 2c9cf378b774f5127cdec5d587300cc2c3aa4d2b Mon Sep 17 00:00:00 2001 From: Alex Mountjoy Date: Wed, 14 Sep 2022 13:12:51 -0700 Subject: [PATCH 1/5] Unit test - Test for page expansion on link to page. --- n2y/notion_mocks.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_blocks.py | 22 ++++++++++++++-------- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/n2y/notion_mocks.py b/n2y/notion_mocks.py index b336afb4..28793f12 100644 --- a/n2y/notion_mocks.py +++ b/n2y/notion_mocks.py @@ -144,3 +144,48 @@ def mock_select_option(name, **kwargs): def mock_relation_value(): return {"id": mock_id()} + + +def mock_page(title = "Mock Title"): + + # Returns the data that would have been returned by the Notion API + # on a client.get_page() request. + + user = mock_user() + created_time = datetime.now().isoformat() + notion_id = mock_id() + hyphenated_title = title.replace(" ", "-") + + page_object = { + 'object':'page', + 'id':notion_id, + 'created_time':created_time, + 'last_edited_time':created_time, + 'created_by':user, + 'last_edited_by':user, + 'cover': None, + 'icon': None, + 'parent':{'type':'page_id','page_id':mock_id()}, + 'archived': False, + 'properties':{ + 'title':{ + 'id':'title', + 'type':'title', + 'title':[{ + 'type':'text','text':{'content':title,'link':None}, + 'annotations':{ + 'bold':False, + 'italic':False, + 'strikethrough':False, + 'underline':False, + 'code':False, + 'color':'default'}, + 'plain_text':title,'href':None + }] + } + }, + 'url':f'https://www.notion.so/{hyphenated_title}-{notion_id}', + } + + return page_object + diff --git a/tests/test_blocks.py b/tests/test_blocks.py index 5391f3f7..9d05ee28 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -48,6 +48,7 @@ mock_paragraph_block, mock_rich_text, mock_page_mention, + mock_page, ) @@ -592,19 +593,24 @@ def test_synced_block_unshared(): assert unshared_reference_markdown == "" -@pytest.mark.xfail(reason="Not fully implemented") def test_link_to_page_page(): # TODO: implement these tests; use mock.patch to mockout the notion API's # response to the get_database_or_page call inside the default linktopage thing # Also, create a separate `mock_page` and `mock_database` calls to test. # Follow the format of the data in the Notion API docs for these - mock_page_id = mock_id() - notion_block = mock_block( - "link_to_page", {"type": "page_id", "page_id": mock_page_id} - ) - pandoc_ast, markdown = process_block(notion_block) - assert pandoc_ast == Para([Str("All Blocks Test Page")]) - assert markdown == "All Blocks Test Page\n" + + notion_id = mock_id() + + mock_link_to_page_block = mock_block( + "link_to_page", {"type": "page_id", "page_id": mock_id()} ) + + page = mock_page("Linked Page") + + with mock.patch("n2y.notion.Client._get_url", return_value = page): + pandoc_ast, markdown = process_block(mock_link_to_page_block) + + assert pandoc_ast == Para([Str("Linked"), Space(), Str("Page")]) + assert markdown == "Linked Page\n" @pytest.mark.xfail(reason="Not fully implemented") From 3f7ceb804fa7f2ecddbc13cf48d00a90a29ca6c5 Mon Sep 17 00:00:00 2001 From: Alex Mountjoy Date: Wed, 14 Sep 2022 13:39:41 -0700 Subject: [PATCH 2/5] Unit test - Test for page expansion on link to page. --- n2y/notion_mocks.py | 61 ++++++++++++++++++++++---------------------- tests/test_blocks.py | 12 +++------ 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/n2y/notion_mocks.py b/n2y/notion_mocks.py index 28793f12..74e4b434 100644 --- a/n2y/notion_mocks.py +++ b/n2y/notion_mocks.py @@ -146,10 +146,10 @@ def mock_relation_value(): return {"id": mock_id()} -def mock_page(title = "Mock Title"): +def mock_page(title="Mock Title"): - # Returns the data that would have been returned by the Notion API - # on a client.get_page() request. + # Returns a mock version the data that would be returned by + # the Notion API on a client.get_page() request. user = mock_user() created_time = datetime.now().isoformat() @@ -157,35 +157,34 @@ def mock_page(title = "Mock Title"): hyphenated_title = title.replace(" ", "-") page_object = { - 'object':'page', - 'id':notion_id, - 'created_time':created_time, - 'last_edited_time':created_time, - 'created_by':user, - 'last_edited_by':user, - 'cover': None, - 'icon': None, - 'parent':{'type':'page_id','page_id':mock_id()}, - 'archived': False, - 'properties':{ - 'title':{ - 'id':'title', - 'type':'title', - 'title':[{ - 'type':'text','text':{'content':title,'link':None}, - 'annotations':{ - 'bold':False, - 'italic':False, - 'strikethrough':False, - 'underline':False, - 'code':False, - 'color':'default'}, - 'plain_text':title,'href':None + 'object': 'page', + 'id': notion_id, + 'created_time': created_time, + 'last_edited_time': created_time, + 'created_by': user, + 'last_edited_by': user, + 'cover': None, + 'icon': None, + 'parent': {'type': 'page_id', 'page_id': mock_id()}, + 'archived': False, + 'properties': { + 'title': { + 'id': 'title', + 'type': 'title', + 'title': [{ + 'type': 'text', 'text': {'content': title, 'link': None}, + 'annotations': { + 'bold': False, + 'italic': False, + 'strikethrough': False, + 'underline': False, + 'code': False, + 'color': 'default'}, + 'plain_text': title, 'href': None }] - } - }, - 'url':f'https://www.notion.so/{hyphenated_title}-{notion_id}', + } + }, + 'url': f'https://www.notion.so/{hyphenated_title}-{notion_id}', } return page_object - diff --git a/tests/test_blocks.py b/tests/test_blocks.py index 9d05ee28..9e78fcba 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -594,19 +594,13 @@ def test_synced_block_unshared(): def test_link_to_page_page(): - # TODO: implement these tests; use mock.patch to mockout the notion API's - # response to the get_database_or_page call inside the default linktopage thing - # Also, create a separate `mock_page` and `mock_database` calls to test. - # Follow the format of the data in the Notion API docs for these - notion_id = mock_id() - mock_link_to_page_block = mock_block( - "link_to_page", {"type": "page_id", "page_id": mock_id()} ) - + "link_to_page", {"type": "page_id", "page_id": mock_id()}) + page = mock_page("Linked Page") - with mock.patch("n2y.notion.Client._get_url", return_value = page): + with mock.patch("n2y.notion.Client._get_url", return_value=page): pandoc_ast, markdown = process_block(mock_link_to_page_block) assert pandoc_ast == Para([Str("Linked"), Space(), Str("Page")]) From 7cc0b8311ad9eccb93808ee600744b6b69a1f190 Mon Sep 17 00:00:00 2001 From: Alex Mountjoy Date: Wed, 14 Sep 2022 13:51:52 -0700 Subject: [PATCH 3/5] Variable name change --- n2y/blocks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/n2y/blocks.py b/n2y/blocks.py index c625fbd1..856ced95 100644 --- a/n2y/blocks.py +++ b/n2y/blocks.py @@ -583,13 +583,13 @@ def to_pandoc(self): # TODO: in the future, if we are exporting the linked page too, then add # a link to the page. For now, we just display the text of the page. - page = self.client.get_page_or_database(self.linked_page_id) - if page is None: - msg = "Permission denied when attempting to access linked page [%s]" + node = self.client.get_page_or_database(self.linked_page_id) + if node is None: + msg = "Permission denied when attempting to access linked node [%s]" logger.warning(msg, self.notion_url) return None else: - title = page.title.to_pandoc() + title = node.title.to_pandoc() return Para(title) From df044332281f33679e285f1bd18f547263927f47 Mon Sep 17 00:00:00 2001 From: Alex Mountjoy Date: Wed, 14 Sep 2022 14:08:55 -0700 Subject: [PATCH 4/5] Unit test - Test for page expansion on link to page. --- n2y/notion_mocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n2y/notion_mocks.py b/n2y/notion_mocks.py index 74e4b434..e8ec7cbc 100644 --- a/n2y/notion_mocks.py +++ b/n2y/notion_mocks.py @@ -148,7 +148,7 @@ def mock_relation_value(): def mock_page(title="Mock Title"): - # Returns a mock version the data that would be returned by + # Returns a mock version the data that would be returned by # the Notion API on a client.get_page() request. user = mock_user() From 3560f0017baaf38ffc14320eeaa331e03ed88936 Mon Sep 17 00:00:00 2001 From: Alex Mountjoy Date: Thu, 15 Sep 2022 01:13:42 -0700 Subject: [PATCH 5/5] Simplify mock_page - Simplify notion_mocks.py method mock_page - Various stylistic changes --- n2y/notion_mocks.py | 23 ++++------------------- tests/test_blocks.py | 20 ++------------------ 2 files changed, 6 insertions(+), 37 deletions(-) diff --git a/n2y/notion_mocks.py b/n2y/notion_mocks.py index e8ec7cbc..fa1efd93 100644 --- a/n2y/notion_mocks.py +++ b/n2y/notion_mocks.py @@ -147,16 +147,11 @@ def mock_relation_value(): def mock_page(title="Mock Title"): - - # Returns a mock version the data that would be returned by - # the Notion API on a client.get_page() request. - user = mock_user() created_time = datetime.now().isoformat() notion_id = mock_id() hyphenated_title = title.replace(" ", "-") - - page_object = { + return { 'object': 'page', 'id': notion_id, 'created_time': created_time, @@ -171,20 +166,10 @@ def mock_page(title="Mock Title"): 'title': { 'id': 'title', 'type': 'title', - 'title': [{ - 'type': 'text', 'text': {'content': title, 'link': None}, - 'annotations': { - 'bold': False, - 'italic': False, - 'strikethrough': False, - 'underline': False, - 'code': False, - 'color': 'default'}, - 'plain_text': title, 'href': None - }] + 'title': mock_rich_text_array([ + (title, []), + ]), } }, 'url': f'https://www.notion.so/{hyphenated_title}-{notion_id}', } - - return page_object diff --git a/tests/test_blocks.py b/tests/test_blocks.py index 9e78fcba..1ff18ae4 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -594,32 +594,16 @@ def test_synced_block_unshared(): def test_link_to_page_page(): - mock_link_to_page_block = mock_block( - "link_to_page", {"type": "page_id", "page_id": mock_id()}) - + "link_to_page", {"type": "page_id", "page_id": mock_id()}, + ) page = mock_page("Linked Page") - with mock.patch("n2y.notion.Client._get_url", return_value=page): pandoc_ast, markdown = process_block(mock_link_to_page_block) - assert pandoc_ast == Para([Str("Linked"), Space(), Str("Page")]) assert markdown == "Linked Page\n" -@pytest.mark.xfail(reason="Not fully implemented") -def test_link_to_page_database(): - mock_database_id = mock_id() - notion_block = mock_block( - "link_to_page", {"type": "database_id", "database_id": mock_database_id} - ) - - pandoc_ast, markdown = process_block(notion_block) - - assert pandoc_ast == Para([Str("Simple Test Database")]) - assert markdown == "Simple Test Database\n" - - def test_column_block(): column_block = mock_block("column", {}, has_children=True) children = [mock_paragraph_block([("child", [])])]