Projection: The return from the api was incorrect when searching by country

Hello team, i’m getting this error instead of validation code when i try to perform validation for Projection test. Can someone help me?
Thank you

Did the unit test case passed?

Kanika

No the test case didn’t pass. The call of “get_movies_by_country” returns an error when is attempted from test case to assert len(result) == 2, where result is the get_movies_by_country(countries)
The error is something as “typeerror object of type has no len()”

Panagiotis

I believe we can check the code around here.

Kanika

Would you like to include code here (It’s just one line the query)

Sure, you can share the code.

Kanika

Unfortunately i haven’t here a DB to test, but i think the query was as follows
(function get_movies_by_country in db.py)
return list(db.movies.find({“countries”: {"$all": countries}},{_id: 1, title: 1}).limit(1))

Panagiotis

You might want to test if this is the operator you want to use for the ticket requirement.

https://docs.mongodb.com/manual/reference/operator/aggregation/#array-expression-operators

This can help.

Kanika

I exhausted my three attempts with this and at the end i realized that i had the same query just as is at the detailed answer however i still had the same validation code “The return from the api was incorrect when searching by country” whether the failure of test cases was: “TypeError: object of type ‘TypeError’ has no len()” Why is this happening?
My solution:

query = {“countries”: {"$in": countries}} , {“title”: 1}
return list(db.movies.find(query))

is not the same as the suggested one?

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

Am i missing something? why i’m getting the error?

I am not very fluent in python but I would say that it is not the same. The method find takes 2 argument and you pass one. Since you get

It means you are not returning an array. It means your code crash the line (or expression) just before your return. Your assignment query = {…} , {…} probably does not what you think it does. If you split this assignment in 2 to separate the query from the projection and use find(query,projection) it might work.

1 Like

You are right, splitting that way it works Thank you