Pytest -m paging fails on test_supports_paging_by_genre

UI status validation for paging is working, I got the code to work with the Ticket too. However the pytest fails, see below. Appears as if some data was changed (test is hardcoded) !?

$ pytest -m paging
=============================================================== test session starts ===============================================================
platform darwin – Python 3.7.3, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: ############/mflix-python, inifile:
plugins: flask-0.10.0
collected 43 items

tests/ .F. [100%]

==================================================================== FAILURES =====================================================================
__________________________________________________________ test_supports_paging_by_genre __________________________________________________________

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

def test_supports_paging_by_genre(client):
    filter = {'genres': ['History']}
    (movies0, results0) = get_movies(filter, 0, 20)
    assert len(list(movies0)) == 20
    assert results0 == 999
    assert movies0[0].get('title') == 'Braveheart'
    last_page = int(999 / 20)
    (movies2, results2) = get_movies(filter, last_page, 20)
    assert len(list(movies2)) == 19
  assert movies2[0].get('title') == 'Only the Dead'

E AssertionError: assert ‘Korengal’ == ‘Only the Dead’
E - Korengal
E + Only the Dead

tests/ AssertionError
=============================================================== 40 tests deselected ===============================================================
================================================ 1 failed, 2 passed, 40 deselected in 7.58 seconds ================================================

Please make sure you have sorted the results.


Thanks for getting back. The integration tests/UI tests work, also the code seems to match the answer given on your “See detailed answer” page. Here’s the code I’ve and AFAIU sorting should be applied.

def get_movies(filters, page, movies_per_page):
Returns a cursor to a list of movie documents.

Based on the page number and the number of movies per page, the result may
be skipped and limited.

The `filters` from the API are passed to the `build_query_sort_project`
method, which constructs a query, sort, and projection, and then that query
is executed by this method (`get_movies`).

Returns 2 elements in a tuple: (movies, total_num_movies)
query, sort, project = build_query_sort_project(filters)
if project:
    cursor = db.movies.find(query, project).sort(sort)
    cursor = db.movies.find(query).sort(sort)

total_num_movies = 0
if page == 0:
    total_num_movies = db.movies.count_documents(query)
Ticket: Paging

Before this method returns back to the API, use the "movies_per_page"
argument to decide how many movies get displayed per page. The "page"
argument will decide which page

Paging can be implemented by using the skip() and limit() methods against
the Pymongo cursor.

# TODO: Paging
# Use the cursor to only return the movies that belong on the current page.
movies = cursor.limit(movies_per_page).skip(page*movies_per_page)

return (list(movies), total_num_movies)

Hi @Nikhil_10418,

Give me some time. I am looking into this.


Hi @Nikhil_10418,

Can you please try adding sorting for _id field as well in ASCENDING order?


  1. Around line: 15 where import pymongo is there, replace by this:

    from pymongo import MongoClient, DESCENDING, ASCENDING 
  2. In function build_query_sort_project, in genres elif condition, add the below line:

    sort = [("tomatoes.viewer.numReviews", DESCENDING), ("_id",ASCENDING)]

like this:

   elif "genres" in filters:

        Ticket: Text and Subfield Search

        Given a genre in the "filters" object, construct a query that
        searches MongoDB for movies with that genre.

        # TODO: Text and Subfield Search
        # Construct a query that will search for the chosen genre.
        query = {<YOUR QUERY>}
        sort = [("tomatoes.viewer.numReviews", DESCENDING), ("_id",ASCENDING)]

Let me know if this works.


1 Like