Chapter 2: Ticket -Create/Update Comments issue and workaround

Hi Everyone,
For anyone else trying to go through the course ticket and is having the issue where your pytest is running fine but the api test(from Stats) is failing.
Issue:
The pytest is updating the comment of the movie with comment_id with as ObjectId(<somevalue>) where as the api that is being called from the Stats page is updating with a string as stated on line:

        comment_id = expect(post_data.get('comment_id'), str, 'comment_id')
        edit_result = update_comment(
        comment_id, user_email, updated_comment, datetime.now()
        )

So, as you can see the output of expect() is a string instead of an ObjectId which will make the Stats page not unable to update comment.

Workaround:
As a solution we can add a condition in the update_comment() in db.py file to check if the given comment_id is a string and convert that into an ObjectId().

1 Like

Hallo sir,
Do you mean like this:
response = db.comments.update_one({"_id": ObjectId(comment_id), “email”: user_email},{ “$set”: { “text”: text,“date”: date } })

This worked for me for both tests and validation status code :

if isinstance(comment_id,str):
    comment_id = ObjectId(comment_id)
1 Like