Pytest only work for Connection

Hello guys.

I’m trying to figure out what happened with my tests. When i run the pytest -m connection it work like expected, but any other test do not work.

Bellow you can see the projection test, and we can see also the answer from Mongo in that message, so we have a HTTP/200 but the test, for any reason, don’t go ahead. Please, take a look.

(M220) joaonetto@HunterNET:~/Documents/Desenvolvimento/codeForPython/M220-mongoDB$ pytest -m projection
========================================================================================================= test session starts ==========================================================================================================
platform darwin -- Python 3.7.4, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: /Users/joaonetto/Documents/Desenvolvimento/codeForPython/M220-mongoDB, inifile:
plugins: flask-0.10.0
collected 43 items                                                                                                                                                                                                                     

tests/test_projection.py FF                                                                                                                                                                                                      [100%]

=============================================================================================================== FAILURES ===============================================================================================================
_____________________________________________________________________________________________________ test_basic_country_search_db _____________________________________________________________________________________________________

client = <FlaskClient <Flask 'mflix.factory'>>

    @pytest.mark.projection
    def test_basic_country_search_db(client):
        countries = ['Kosovo']
        result = get_movies_by_country(countries)
>       assert len(result) == 2
E       AssertionError: assert 1 == 2
E        +  where 1 = len([{'_id': ObjectId('573a1390f29313caabcd4135'), 'awards': {'nominations': 0, 'text': '1 win.', 'wins': 1}, 'cast': ['Charles Kayser', 'John Ott'], 'countries': ['USA'], ...}])

tests/test_projection.py:15: AssertionError
__________________________________________________________________________________________________ test_basic_country_search_shape_db __________________________________________________________________________________________________

client = <FlaskClient <Flask 'mflix.factory'>>

    @pytest.mark.projection
    def test_basic_country_search_shape_db(client):
        countries = ['Russia', 'Japan']
        result = get_movies_by_country(countries)
>       assert len(result) == 1237
E       AssertionError: assert 1 == 1237
E        +  where 1 = len([{'_id': ObjectId('573a1390f29313caabcd4135'), 'awards': {'nominations': 0, 'text': '1 win.', 'wins': 1}, 'cast': ['Charles Kayser', 'John Ott'], 'countries': ['USA'], ...}])

tests/test_projection.py:22: AssertionError
========================================================================================================= 41 tests deselected ==========================================================================================================
=============================================================================================== 2 failed, 41 deselected in 0.40 seconds ================================================================================================

If i running my python run.py and I goes to Status Page and click in Projection I have the bellow answer from Flask.

127.0.0.1 - - [23/Aug/2019 11:59:36] "GET /status HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2019 11:59:36] "GET /favicon.ico HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2019 11:59:44] "GET /api/v1/movies/ HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2019 11:59:45] "GET /api/v1/movies/countries?countries=Australia HTTP/1.1" 200 -

21

Like we can see, Flash answer with HTTP/200 and Mongo also answer a correct thing.

So, why the Connection works fine but any other test stuck?

All the best,

JNetto

Looking the problem above, the pytest asking to the MongoDB movies from Kosovo Country, but they receive the first document. Looks like the query was: db.movies.findOne(). Take a look in this answer from pytest:

where 1 = len([{'_id': ObjectId('573a1390f29313caabcd4135'), 'awards': {'nominations': 0, 'text': '1 win.', 'wins': 1}, 'cast': ['Charles Kayser', 'John Ott'], 'countries': ['USA'], ...}])

Using my mongo Shell, I have the right answer from Mongo, like you can see beloow:

> db.movies.countDocuments({countries: "Kosovo"})
2

Ps.: I use count only for sampling propose.

So, looking the issue here, why the test sent the different query to the MongoDB ?

All the best,

JNetto

Hi Joao,

Have you done the section of Chapter 1 labeled “Ticket: Projection”? If you haven’t done so, the projection test as well as the others will not work as you have yet to modify the section for them in db.py file.

Kevin, thank you and I’m sorry. My mistake.

JNetto