Test the utils

Comment create wrapper
Test comment wrapper
This commit is contained in:
simonwt 2026-04-01 23:00:42 +01:00
parent 010d54bc87
commit fb1953e953
11 changed files with 107 additions and 6 deletions

View file

@ -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.

View file

@ -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')

View file

@ -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)

View file

@ -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")

View file

@ -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."

View file

@ -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()

View file

@ -1 +1,2 @@
from .get_parent_pksk_from_path import get_parent_pksk_from_path
from .get_post_pksk import get_post_pksk

View file

@ -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}"

9
utils/get_post_pksk.py Normal file
View file

@ -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}"
}

View file

@ -0,0 +1 @@
from .create_comment import create_comment

View file

@ -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
}
}