Getting error while executing pytest -m projection

I have tried a lot but every time getting the same error. Although i have completed my assignment and got the validation got from status page but when runngin pytest -m projection getting following error

C:\Users\bilal.khan\M220P\mflix-python>pytest -m projection
============================= test session starts =============================
platform win32 – Python 3.7.1, pytest-4.0.0, py-1.7.0, pluggy-0.8.0
rootdir: C:\Users\bilal.khan\M220P\mflix-python, inifile:
plugins: remotedata-0.3.1, openfiles-0.3.0, flask-0.10.0, doctestplus-0.2.0, arraydiff-0.2
collected 43 items / 41 deselected

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 TypeError: object of type ‘ServerSelectionTimeoutError’ has no len()

tests\test_projection.py:15: TypeError
_____________________ 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) == 2421

E TypeError: object of type ‘ServerSelectionTimeoutError’ has no len()

tests\test_projection.py:22: TypeError
================== 2 failed, 41 deselected in 61.00 seconds ===================

Hi… any resolution for this error… I am getting the same error and in result not getting the validation for projection from status page. Please let me know. Thanks.

Please do not post the answer of an assignment.

I used the same line in db.py. But still getting the error Projection: The return from the api was incorrect when searching by country

@jjlop3z, of course you had to edit the db.py file. The description of the work we have to do it:

Implement the get_movies_by_country method in db.py to search movies by country and use projection to return the title field.

And please, [NOTICE] Please do not post potential answers in the forums.

@Bilal_Muhammad_77476, please [NOTICE] Please do not post potential answers in the forums.

Having a similar issue.
Before i found this post, i came across almost the same solutioin as the op unfortunateley posted in clear text.
When i run my code snippet “standalone” (outside the mflix application), i’m getting the results expected in the
unittests and the result length is as expected.
The unit tests are failing with the same error as posted above, but the search returns the movies found, but only the
text informatioin, no preview of the movie posters.
I’m returning - as in the original stub - the result of the query as a list:
return list(result).
Any hint would be appreciated.
Regards
Thomas

All the posted answers are almost correct. Please reread the task description:

Task

Implement the get_movies_by_country method in db.pyto search movies by country and use projection to return the title field.

And listen again to Lecture - Basic Reads, paying attention to the _id explications.

Please don’t share your findings as it will give away the answer.

I think there is something wrong in unit tests. If you check the movies collection, you get zero results for {“countries”:[‘Kosovo’]}, while unit test is expecting 2.

Similarly for {“countries”:[‘Russia’,‘Japan’]} there are only 2 results, while unit test is expecting 1237.

The “test_basic_country_search_shape_db” in test_projections.py expecting an _id column which is again incorrect as per requirement.

Any advice?

My advise is to verify your code.

Hi @Afzal_53899,

I am checking. :slight_smile:

Kanika

Hi @Afzal_53899,

Please check your query. It seems incorrect.
I will recommend re-watch the “Basics Reads” lecture again. Let me know if you have any questions.

Edit: Let me share how I debugged the issue, I checked the type of the field “countries” and I can see it is an array.
Now, we have different ways to query on an array field. (Query an Array) One of them is:

Match an array- which will match the exact element of an array like: { countries: [ "Kosovo"] } which will match all documents containing field “countries” which has exact one value and that is Kosovo.

Another one is:
Query an Array for an Element: which will match all documents having at least one value in the query.

Kanika

I’am also facing same problem when i run in notebook of query …it will getting result but i can’t test in projection…it shows above error only

Hi @mahalaxmi_77395,

Please share the query you are using.

Kanika

list(db.movies.find({“countries”: countries}, {“title”: 1}))

Maybe can be useful for someone else:

I was getting this pytest error:

E TypeError: object of type ‘ServerSelectionTimeoutError’ has no len()

while I hadn’t assigned the database connection info in the secion [TEST] on .ini file (same data from [PROD]).

A tip for those who are not getting the correct code validation in the status page for Projection ticket:

The original return statement of get_movies_by_country() function on db.py file comes with a “limit(1)”, but this ticket wants a query for countrIES list. Don’t just adapt the original return, made yourself.

even after trying array type: i am getting this
"Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information."