Skip to content

Commit 6f5edf9

Browse files
committed
feat: add more testing and automatic test page setup
I'm borrowing some test code from notion-py for setting up the test page and a collection within it. This makes it so that each test run can have its own page. For the time being I'm making it so that each test is just adding/removing/changing what it needs. It might make sense to break each test into their own setups for a perfect clean slate. Added a new notion_test_parent_page_id option on the pytest to specify where the new test pages are made. Finished adding tests for the rest of the NotionApi functions. I've opt'd to use notion-py directly in these tests for data setup. Then the NotionApi class is uses. Pytest in CI uses -tb=native so that it doesn't show the local variables which would include the NOTION_TOKEN.
1 parent 52d2266 commit 6f5edf9

File tree

7 files changed

+165
-19
lines changed

7 files changed

+165
-19
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
flake8 . --count --exit-zero --statistics
2929
- name: Test with pytest
3030
run: |
31-
pytest shared/notionscripts/tests/ --notion_token ${{ secrets.NOTION_TOKEN }}
31+
pytest shared/notionscripts/tests/ --notion_token ${{ secrets.NOTION_TOKEN }} --notion_test_parent_page_id ${{ secrets.NOTION_TEST_PARENT_PAGE_ID }} --tb=native

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
markers =
3+
focus: only these tests are ran (as they are being worked on)

server/src/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def block_view(notion_token, block_id):
5252
try:
5353
notion_api = NotionApi(notion_token)
5454

55-
content = notion_api.get_block_content(block_id)
55+
content = notion_api.block_content(block_id)
5656

5757
return jsonify(content=content), 200
5858
except Exception as error:

shared/notionscripts/notion_api.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ def __init__(self, token):
1414
def client(self):
1515
return NotionClient(token_v2=self.token, monitor=False)
1616

17-
def get_block_content(self, block_id):
17+
def __collection_view(self, collection_id, view_id):
18+
return self.client().get_collection_view(f"https://www.notion.so/{collection_id}?v={view_id}")
19+
20+
def block_content(self, block_id):
1821
block = self.client().get_block(block_id)
1922

2023
content = [block.title]
@@ -24,16 +27,13 @@ def get_block_content(self, block_id):
2427

2528
return "\n".join(content)
2629

27-
def get_collection_view(self, collection_id, view_id):
28-
return self.client().get_collection_view(f"https://www.notion.so/{collection_id}?v={view_id}")
29-
3030
def block_append(self, block_id, text):
3131
block = self.client().get_block(block_id)
3232

3333
return block.children.add_new(TextBlock, title=text)
3434

35-
def collection_view(self, collection_id, view_id):
36-
collection_view = self.get_collection_view(collection_id, view_id)
35+
def collection_view_content(self, collection_id, view_id):
36+
collection_view = self.__collection_view(collection_id, view_id)
3737
results = collection_view.default_query().execute()
3838

3939
content = []
@@ -43,7 +43,7 @@ def collection_view(self, collection_id, view_id):
4343
return content
4444

4545
def collection_append(self, collection_id, view_id, data):
46-
collection_view = self.get_collection_view(collection_id, view_id)
46+
collection_view = self.__collection_view(collection_id, view_id)
4747

4848
row = collection_view.collection.add_row(**data)
4949

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import pytest
22

3+
from .notion_api_page_helper import * # noqa, F401
4+
35

46
def pytest_addoption(parser):
57
parser.addoption("--notion_token", action="store")
8+
parser.addoption("--notion_test_parent_page_id", action="store")
69

710

8-
@pytest.fixture(scope='session')
11+
@pytest.fixture(scope="session")
912
def notion_token(request):
1013
value = request.config.option.notion_token
1114
if value is None:
1215
pytest.skip()
1316
return value
17+
18+
19+
@pytest.fixture(scope="session")
20+
def notion_test_parent_page_id(request):
21+
value = request.config.option.notion_test_parent_page_id
22+
if value is None:
23+
pytest.skip()
24+
return value
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pytest
2+
3+
from datetime import datetime
4+
from notion.client import NotionClient
5+
from notion.block import PageBlock, CollectionViewBlock
6+
7+
# Some parts of this file are inspired/using the code from notion-py
8+
# https://github.com/jamalex/notion-py/blob/b7041ade477c1f59edab1b6fc025326d406dd92a/notion/smoke_test.py
9+
10+
11+
def get_test_page():
12+
return test_page
13+
14+
15+
@pytest.fixture(scope="session", autouse=True)
16+
def setup_test_page(request, notion_token, notion_test_parent_page_id):
17+
global notion_client, test_page
18+
19+
notion_client = NotionClient(token_v2=notion_token, monitor=False)
20+
test_page = create_test_page(notion_test_parent_page_id)
21+
22+
yield
23+
24+
test_page.remove(permanently=True)
25+
26+
27+
def create_test_page(notion_test_parent_page_id):
28+
test_parent_page = notion_client.get_block(notion_test_parent_page_id)
29+
30+
test_page = test_parent_page.children.add_new(
31+
PageBlock,
32+
title="Test page at {}".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")),
33+
)
34+
35+
return test_page
36+
37+
38+
def create_collection_view():
39+
schema = {
40+
"%9:q": {"name": "Enabled", "type": "checkbox"},
41+
"=d{|": {
42+
"name": "Tags",
43+
"type": "multi_select",
44+
"options": [
45+
{
46+
"color": "orange",
47+
"id": "79560dab-c776-43d1-9420-27f4011fcaec",
48+
"value": "A",
49+
},
50+
{
51+
"color": "default",
52+
"id": "002c7016-ac57-413a-90a6-64afadfb0c44",
53+
"value": "B",
54+
},
55+
{
56+
"color": "blue",
57+
"id": "77f431ab-aeb2-48c2-9e40-3a630fb86a5b",
58+
"value": "C",
59+
},
60+
],
61+
},
62+
"=d{q": {
63+
"name": "Category",
64+
"type": "select",
65+
"options": [
66+
{
67+
"color": "orange",
68+
"id": "59560dab-c776-43d1-9420-27f4011fcaec",
69+
"value": "A",
70+
},
71+
{
72+
"color": "default",
73+
"id": "502c7016-ac57-413a-90a6-64afadfb0c44",
74+
"value": "B",
75+
},
76+
{
77+
"color": "blue",
78+
"id": "57f431ab-aeb2-48c2-9e40-3a630fb86a5b",
79+
"value": "C",
80+
},
81+
],
82+
},
83+
"4Jv$": {"name": "Value", "type": "number"},
84+
"title": {"name": "Name", "type": "title"},
85+
}
86+
87+
cvb = test_page.children.add_new(CollectionViewBlock)
88+
cvb.collection = notion_client.get_collection(
89+
notion_client.create_record("collection", parent=cvb, schema=schema)
90+
)
91+
cvb.title = "Test collection"
92+
view = cvb.views.add_new(view_type="table")
93+
94+
return view
Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,57 @@
11
from notionscripts.notion_api import NotionApi
22

3+
from .notion_api_page_helper import get_test_page, create_collection_view
34

4-
def test_get_block_content(notion_token):
5+
from notion.block import TextBlock
6+
7+
import pytest
8+
9+
10+
def test_block_content(notion_token):
11+
notion_api = NotionApi(token=notion_token)
12+
test_page = get_test_page()
13+
14+
block = test_page.children.add_new(TextBlock, title="test get block content")
15+
content = notion_api.block_content(block.id)
16+
17+
assert content == "test get block content"
18+
19+
20+
@pytest.mark.focus
21+
def test_block_append(notion_token):
522
notion_api = NotionApi(token=notion_token)
6-
block_id = "f91655c4122a44f49f19ea20db5dcdf7"
23+
test_page = get_test_page()
724

8-
content = notion_api.get_block_content(block_id)
25+
block = test_page.children.add_new(TextBlock, title="test block append")
26+
new_block = notion_api.block_append(block.id, "appending text")
927

10-
assert content == "Test block"
28+
assert new_block.title == "appending text"
29+
assert new_block.parent.id == block.id
1130

1231

13-
def test_get_collection_view(notion_token):
32+
def test_collection_view_content(notion_token):
1433
notion_api = NotionApi(token=notion_token)
15-
collection_id = "4c2221595f184ccc8b8ffb490b4a4f90"
16-
view_id = "0f5afdd5859d45d9a3514c6e5cbe8cc6"
17-
collection_view = notion_api.get_collection_view(collection_id, view_id)
1834

19-
assert collection_view.id == "0f5afdd5-859d-45d9-a351-4c6e5cbe8cc6"
35+
collection_view = create_collection_view()
36+
collection_id = collection_view.parent.id.replace("-", "")
37+
view_id = collection_view.id.replace("-", "")
38+
39+
collection_view.collection.add_row(name="test row")
40+
collection_view_content = notion_api.collection_view_content(collection_id, view_id)
41+
42+
assert collection_view_content[0]["name"] == "test row"
43+
44+
45+
def test_collection_view_append(notion_token):
46+
notion_api = NotionApi(token=notion_token)
47+
48+
collection_view = create_collection_view()
49+
collection_id = collection_view.parent.id.replace("-", "")
50+
view_id = collection_view.id.replace("-", "")
51+
52+
notion_api.collection_append(collection_id, view_id, {"enabled": True, "value": 10, "name": "test row"})
53+
collection_view_content = notion_api.collection_view_content(collection_id, view_id)
54+
55+
assert collection_view_content[0]["name"] == "test row"
56+
assert collection_view_content[0]["enabled"] is True
57+
assert collection_view_content[0]["value"] == 10

0 commit comments

Comments
 (0)