Pytest -m projection failure

Hi All,

I am executing pytest -m projection from below directory :
C:\MongoDB Learnings\M220\mflix-python>

Getting below error :

============================= test session starts =============================
platform win32 – Python 3.7.4, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: C:\MongoDB Learnings\M220\mflix-python, inifile: pytest.ini
plugins: remotedata-0.3.2, openfiles-0.4.0, flask-0.11.0, doctestplus-0.4.0, arraydiff-0.3
collected 43 items

tests\ FF [100%]

================================== FAILURES ===================================
________________________ test_basic_country_search_db _________________________

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

def test_basic_country_search_db(client):
    countries = ['Kosovo']
    result = get_movies_by_country(countries)
  assert len(result) == 2

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

tests\ TypeError
_____________________ test_basic_country_search_shape_db ______________________

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

def test_basic_country_search_shape_db(client):
    countries = ['Russia', 'Japan']
    result = get_movies_by_country(countries)
  assert len(result) == 1237

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

tests\ TypeError
============================= 41 tests deselected =============================
=================== 2 failed, 41 deselected in 0.77 seconds ===================

my code for get_movies_by_country function in is as below :
return list(db.movies.find({“countries”: {"$in": countries}}, {title:1, _id:1}))

Similar command when executed from Mongo Shell gives proper result
MongoDB Enterprise mflix-shard-0:PRIMARY> db.movies.find({countries: {$in: [‘Kosovo’]}}, {title:1, _id:1})
Output as below :
{ “_id” : ObjectId(“573a13eff29313caabdd7dca”), “title” : “Sworn Virgin” }
{ “_id” : ObjectId(“573a13faf29313caabdec74f”), “title” : “Babai” }

MongoDB Enterprise mflix-shard-0:PRIMARY> db.movies.find({countries: {$in: [‘Russia’, ‘Japan’]}}, {title:1, _id:1}).count()

When the query from mongo shell works fine and gives right result then why pytest -m projection is failing while asserting.

Any help would be much appreciated

The error

means your function does not return a list. Since you do return list(expression) it means the code crashes the line before the return statement or while evaluating expression. In situation like than, when I don’t really know where it crashes, I move up the evaluation of expression. In the code above I would create 2 temporary variables query = … and project = … and use them in place of expression. Sometimes it helps me where is my problem.

Thanks a lot for the guidance.
Below is the code i wrote after your suggestion and it worked for me.
def get_movies_by_country(countries):
query = {“countries”:{"$in": countries}}
project = {“title”:1, “_id”:1}
return list(db.movies.find(query,project))
except Exception as e:
return e

1 Like