Skip to content

Commit

Permalink
test: 💍 comments
Browse files Browse the repository at this point in the history
add tests for manage comment
  • Loading branch information
osikun committed Jul 14, 2023
1 parent 4eab237 commit 60344c8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pyfacebook/api/facebook/resource/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_batch(
for comment_id, item in data.items()
}

def create_comment(
def create(
self,
object_id: str,
attachment_id: Optional[str] = None,
Expand Down Expand Up @@ -129,7 +129,7 @@ def create_comment(
else:
return Comment.new_from_json_dict(data=data)

def update_comment(
def update(
self,
comment_id: str,
attachment_id: Optional[str] = None,
Expand Down Expand Up @@ -187,7 +187,7 @@ def update_comment(
else:
return Comment.new_from_json_dict(data=data)

def delete_comment(self, comment_id: str):
def delete(self, comment_id: str):
"""
Delete a comment by using this endpoint
Expand Down
6 changes: 6 additions & 0 deletions testdata/facebook/apidata/comments/create_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "245778718206154_229887283291835",
"message": "Message from api and updated",
"permalink_url": "https://www.facebook.com/245628824887810/posts/245778718206154?comment_id=229887283291835",
"created_time": "2023-07-14T10:45:55+0000"
}
103 changes: 103 additions & 0 deletions tests/facebook/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,106 @@ def test_get_batch(helpers, fb_api):
return_json=True,
)
assert comments_json[cm_ids[1]]["id"] == cm_ids[1]


def test_create_comment(helpers, fb_api):
object_id = "post_id"
with responses.RequestsMock() as m:
m.add(
method=responses.POST,
url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/comments",
json=helpers.load_json(
"testdata/facebook/apidata/comments/create_resp.json"
),
)
comment = fb_api.comment.create(object_id=object_id, message="message from api")
assert comment.id

comment = fb_api.comment.create(
object_id=object_id,
message="message from api",
attachment_url="photo url",
)
assert comment.id

comment = fb_api.comment.create(
object_id=object_id,
message="message from api",
attachment_share_url="gif url",
)
assert comment.id

comment_json = fb_api.comment.create(
object_id=object_id,
attachment_id="123245",
message="message from api",
return_json=True,
)
assert comment_json["id"]


def test_update_comment(helpers, fb_api):
comment_id = "245778718206154_229887283291835"
with responses.RequestsMock() as m:
m.add(
method=responses.POST,
url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}",
json=helpers.load_json(
"testdata/facebook/apidata/comments/create_resp.json"
),
)
comment = fb_api.comment.update(
comment_id=comment_id,
attachment_id="123245",
message="message from api",
fields="id,message,permalink_url,created_time",
)
assert comment.id

comment = fb_api.comment.update(
comment_id=comment_id,
attachment_url="photo url",
message="message from api",
fields="id,message,permalink_url,created_time",
)
assert comment.id

comment = fb_api.comment.update(
comment_id=comment_id,
message="message from api",
files={"image data": b"bytes for open files"},
fields="id,message,permalink_url,created_time",
return_json=True,
)
assert comment["id"]

with responses.RequestsMock() as m:
m.add(
method=responses.POST,
url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}",
json={"success": True},
)
comment = fb_api.comment.update(
comment_id=comment_id,
message="message from api",
attachment_share_url="gif url",
)
assert comment["success"]

comment_hidden = fb_api.comment.update(
comment_id=comment_id,
is_hidden=True,
)
assert comment_hidden["success"]


def test_delete_comment(fb_api):
comment_id = "245778718206154_229887283291835"
with responses.RequestsMock() as m:
m.add(
method=responses.DELETE,
url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}",
json={"success": True},
)
data = fb_api.comment.delete(comment_id=comment_id)
assert data["success"]

0 comments on commit 60344c8

Please sign in to comment.