Test_add_comment is failing [Solved]

My test_add_comment is failing and I can’t figure out why.
You will see on the stdout print, that the comments returned from get_movie call do not include the newly inserted comment. My pipeline in get_movie has a sort -1 and a limit 2 (just to help in debugging).
Really need help.
Thanks.

============================= test session starts =============================
platform win32 – Python 3.7.2, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: C:\CODING\PYTHON\MongoDBUni\M220\mflix-python, inifile:
plugins: flask-0.10.0
collected 43 items

tests\test_create_update_comments.py FF.F [100%]

================================== FAILURES ===================================
______________________________ test_add_comment _______________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.create_update_comments
def test_add_comment(client):
    result = add_comment(movie_id, user, comment['text'], now)
    assert isinstance(result, InsertOneResult)
    assert result.acknowledged is True
    assert result.inserted_id is not None

    comments = get_movie(movie_id).get('comments')
    print("COMMENTS AT TEST_ADD_COMMENT : ", comments)
  assert comments[0].get('_id') == result.inserted_id

E AssertionError: assert ObjectId(‘5c9a45d60f147440f0d66906’) == ObjectId(‘5c9a9fb30f147437d8dbb072’)
E + where ObjectId(‘5c9a45d60f147440f0d66906’) = <built-in method get of dict object at 0x00000195DCCF1F30>(’_id’)
E + where <built-in method get of dict object at 0x00000195DCCF1F30> = {’_id’: ObjectId(‘5c9a45d60f147440f0d66906’), ‘date’: datetime.datetime(2019, 3, 26, 15, 31, 34, 288000), ‘email’: ‘foobar@baz.com’, ‘movie_id’: ObjectId(‘573a13eff29313caabdd82f3’), …}.get
E + and ObjectId(‘5c9a9fb30f147437d8dbb072’) = <pymongo.results.InsertOneResult object at 0x00000195DCCE4F48>.inserted_id

tests\test_create_update_comments.py:42: AssertionError
---------------------------- Captured stdout call -----------------------------
NEWLY INSERTED COMMENT : {‘name’: ‘foobar’, ‘email’: ‘foobar@baz.com’, ‘movie_id’: ‘573a13eff29313caabdd82f3’, ‘text’: ‘fe-fi-fo-fum’, ‘date’: datetime.datetime(2019, 3, 26, 21, 54, 59, 168909), ‘_id’: ObjectId(‘5c9a9fb30f147437d8dbb072’)}
MOVIE ID: 573a13eff29313caabdd82f3
COMMENTS AT GET MOVIE : [{’_id’: ObjectId(‘5c9a45d60f147440f0d66906’), ‘name’: ‘foobar’, ‘email’: ‘foobar@baz.com’, ‘movie_id’: ObjectId(‘573a13eff29313caabdd82f3’), ‘text’: ‘fe-fi-fo-fum’, ‘date’: datetime.datetime(2019, 3, 26, 15, 31, 34, 288000)}, {’_id’: ObjectId(‘5c9a0eb50f147442e0f1d7e5’), ‘name’: ‘ccnt8muy5’, ‘email’: ‘mk45fqkbt@o88zp.u04’, ‘movie_id’: ObjectId(‘573a13eff29313caabdd82f3’), ‘text’: ‘feefee’, ‘date’: datetime.datetime(2019, 3, 26, 11, 36, 21, 384000)}]
COMMENTS AT TEST_ADD_COMMENT : [{’_id’: ObjectId(‘5c9a45d60f147440f0d66906’), ‘name’: ‘foobar’, ‘email’: ‘foobar@baz.com’, ‘movie_id’: ObjectId(‘573a13eff29313caabdd82f3’), ‘text’: ‘fe-fi-fo-fum’, ‘date’: datetime.datetime(2019, 3, 26, 15, 31, 34, 288000)}, {’_id’: ObjectId(‘5c9a0eb50f147442e0f1d7e5’), ‘name’: ‘ccnt8muy5’, ‘email’: ‘mk45fqkbt@o88zp.u04’, ‘movie_id’: ObjectId(‘573a13eff29313caabdd82f3’), ‘text’: ‘feefee’, ‘date’: datetime.datetime(2019, 3, 26, 11, 36, 21, 384000)}]

Well, found 1 error. When comparing the records using Compass you can clearly see that the movie_id is not of the ObjectId type. That makes the whole difference. The error was actually at the add_comment function. So, don’t forget, use ObjectId(value) when manipulating the movie or comment ids.

3 Likes

Hi @Andre_56646

Hopefully this has resolved your issue, can you confirm that your test is now passing as I just want to check you are not still stuck at this point ?

Kindest regards,
Eoin

Hi Eoin,

Thanks for reaching out. Yes, I confirm I have solved the issue and managed to unblock my progress. I have also managed to successfully complete all the labs but unfortunately too late to submit within the allocated timeframe.
Best Regards,
Andre.

1 Like