From fb1953e953faf714fdf8c50f245fff7f58db932e Mon Sep 17 00:00:00 2001 From: simonwt Date: Wed, 1 Apr 2026 23:00:42 +0100 Subject: [PATCH] Test the utils Comment create wrapper Test comment wrapper --- apiclient.py | 2 +- tests/utils/get_parent_pksk_from_path.py | 15 ++++++++++ tests/utils/get_post_pksk.py | 12 ++++++++ tests/wrappers/create_comment.py | 22 ++++++++++++++ tests/wrappers/create_post.py | 2 +- unittests.py | 5 +++- utils/__init__.py | 3 +- utils/get_parent_pksk_from_path.py | 5 ++-- utils/get_post_pksk.py | 9 ++++++ wrappers/comment/__init__.py | 1 + wrappers/comment/create_comment.py | 37 ++++++++++++++++++++++++ 11 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 tests/utils/get_parent_pksk_from_path.py create mode 100644 tests/utils/get_post_pksk.py create mode 100644 tests/wrappers/create_comment.py create mode 100644 utils/get_post_pksk.py create mode 100644 wrappers/comment/__init__.py create mode 100644 wrappers/comment/create_comment.py diff --git a/apiclient.py b/apiclient.py index c8134fe..f2f0dcf 100644 --- a/apiclient.py +++ b/apiclient.py @@ -75,7 +75,7 @@ class APIClient(BaseModel): return return_json - def sign_in(self,) -> None: + def sign_in(self,) -> dict: """ Authenticates with the API to obtain an access token. diff --git a/tests/utils/get_parent_pksk_from_path.py b/tests/utils/get_parent_pksk_from_path.py new file mode 100644 index 0000000..4286627 --- /dev/null +++ b/tests/utils/get_parent_pksk_from_path.py @@ -0,0 +1,15 @@ +import unittest +from utils.get_parent_pksk_from_path import get_parent_pksk_from_path +class TestGetParentPkskFromPath(unittest.TestCase): + def test_root_path(self): + self.assertEqual(get_parent_pksk_from_path('/'), 'maintrunk#maintrunk') + + def test_userprofile_path(self): + self.assertEqual(get_parent_pksk_from_path('/user/johndoe'), 'userprofile#johndoe') + + def test_subwiki_path(self): + self.assertEqual(get_parent_pksk_from_path('/branch/12345'), 'subwiki#12345') + + def test_invalid_entity(self): + with self.assertRaises(ValueError): + get_parent_pksk_from_path('/invalid/12345') \ No newline at end of file diff --git a/tests/utils/get_post_pksk.py b/tests/utils/get_post_pksk.py new file mode 100644 index 0000000..9264c8a --- /dev/null +++ b/tests/utils/get_post_pksk.py @@ -0,0 +1,12 @@ +import unittest +from utils.get_post_pksk import get_post_pksk + +class TestGetPostPksk(unittest.TestCase): + def test_get_post_pksk(self): + parent_pksk = 'userprofile#johndoe' + post_url = '/post/12345' + expected_result = { + "pk": 'userprofile#johndoe', + "sk": 'post#12345' + } + self.assertEqual(get_post_pksk(parent_pksk, post_url), expected_result) \ No newline at end of file diff --git a/tests/wrappers/create_comment.py b/tests/wrappers/create_comment.py new file mode 100644 index 0000000..fd3dd33 --- /dev/null +++ b/tests/wrappers/create_comment.py @@ -0,0 +1,22 @@ +import unittest +from wrappers.comment.create_comment import create_comment +class TestCreateComment(unittest.TestCase): + def setUp(self): + self.comment_text = "This is a test comment created via the create_comment wrapper function." + self.blur_label = None + + def test_create_comment(self): + result = create_comment( + post_slug='1774951384-98fe38df', + comment_text=self.comment_text, + parent_path='/', + blur_label=self.blur_label + ) + self.assertIsInstance(result, dict) + self.assertIn("job_function", result) + self.assertIn("payload", result) + self.assertEqual(result["job_function"], "comment.create") + self.assertEqual(result["payload"]["commentText"], self.comment_text) + self.assertEqual(result["payload"]["blurLabel"], self.blur_label) + self.assertEqual(result["payload"]["parent"]["pk"], "maintrunk#maintrunk") + self.assertEqual(result["payload"]["parent"]["sk"], "post#1774951384-98fe38df") \ No newline at end of file diff --git a/tests/wrappers/create_post.py b/tests/wrappers/create_post.py index b005db9..8762c9b 100644 --- a/tests/wrappers/create_post.py +++ b/tests/wrappers/create_post.py @@ -1,6 +1,6 @@ import unittest from wrappers.post.create_post import create_post -class TestCalculations(unittest.TestCase): +class TestCreatePost(unittest.TestCase): def setUp(self): self.post_text = "This is a test post created via the create_post wrapper function." diff --git a/unittests.py b/unittests.py index 114d28f..f15b044 100644 --- a/unittests.py +++ b/unittests.py @@ -1,5 +1,8 @@ import unittest -from tests.wrappers.create_post import TestCalculations +from tests.wrappers.create_post import TestCreatePost +from tests.wrappers.create_comment import TestCreateComment +from tests.utils.get_post_pksk import TestGetPostPksk +from tests.utils.get_parent_pksk_from_path import TestGetParentPkskFromPath unittest.main() diff --git a/utils/__init__.py b/utils/__init__.py index e8a61ad..2747861 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1 +1,2 @@ -from .get_parent_pksk_from_path import get_parent_pksk_from_path \ No newline at end of file +from .get_parent_pksk_from_path import get_parent_pksk_from_path +from .get_post_pksk import get_post_pksk \ No newline at end of file diff --git a/utils/get_parent_pksk_from_path.py b/utils/get_parent_pksk_from_path.py index 3a53842..4cd061a 100644 --- a/utils/get_parent_pksk_from_path.py +++ b/utils/get_parent_pksk_from_path.py @@ -3,12 +3,13 @@ def get_parent_pksk_from_path(parent_path): return 'maintrunk#maintrunk' entity, slug = parent_path.strip('/').split('/') + if entity == 'branch': entity = 'subwiki' - elif entity != 'user': + elif entity == 'user': entity = 'userprofile' if entity not in ['userprofile', 'subwiki']: - raise ValueError(f"Invalid parent entity: {entity}. Must be 'userprofile' or 'branch'.") + raise ValueError(f"Invalid parent entity: {entity}. Must be 'userprofile' or 'subwiki'.") return f"{entity}#{slug}" \ No newline at end of file diff --git a/utils/get_post_pksk.py b/utils/get_post_pksk.py new file mode 100644 index 0000000..a399768 --- /dev/null +++ b/utils/get_post_pksk.py @@ -0,0 +1,9 @@ +def get_post_pksk(parent_pksk, post_url): + + post_slug = post_url.strip('/post/') + + return { + "pk": parent_pksk, + "sk": f"post#{post_slug}" + } + diff --git a/wrappers/comment/__init__.py b/wrappers/comment/__init__.py new file mode 100644 index 0000000..eae8827 --- /dev/null +++ b/wrappers/comment/__init__.py @@ -0,0 +1 @@ +from .create_comment import create_comment \ No newline at end of file diff --git a/wrappers/comment/create_comment.py b/wrappers/comment/create_comment.py new file mode 100644 index 0000000..2711be7 --- /dev/null +++ b/wrappers/comment/create_comment.py @@ -0,0 +1,37 @@ + +from utils.get_parent_pksk_from_path import get_parent_pksk_from_path +from utils.get_post_pksk import get_post_pksk +def create_comment(comment_text, post_slug, parent_path, blur_label=None, version=3): + """ + Creates a new comment. + + Args: + comment_text (str): The text content of the comment. + parent_path (str): The parent path for the comment, in the format 'userprofile/slug' or 'subwiki/slug'. + blur_label (str, optional): An optional label for blurring the comment content. + version (int, optional): The version of the comment structure to use, default is 3. + + Returns: + dict: A dictionary containing the job name and payload for creating the comment + that will be processed by the API client wrapper function. + """ + + parent_pksk = get_parent_pksk_from_path(parent_path) + post_pksk = get_post_pksk(parent_pksk, post_slug) + return { + "job_function": "comment.create", + "payload": { + "blurLabel": blur_label, + "commentText": comment_text, + "parent": { + "pk": post_pksk['pk'], + "sk": post_pksk['sk'], + "slug": parent_path.split('/')[-1] + }, + "topLevel": { + "pk": post_pksk['pk'], + "sk": post_pksk['sk'] + }, + "version": version + } + } \ No newline at end of file