Skip to content

FEATURE: Fix cursor.count() usage — removed in pymongo 4.x, replace with count_documents() #36

Description

@DewaldOosthuizen

Summary

cursor.count() was removed in pymongo 4.0. The helper functions user_exist() and verify_user() in app.py call users.find({...}).count(), which raises AttributeError at runtime against a real MongoDB instance. The tests pass only because the cursor is fully mocked.

Background

The repo pins pymongo==4.7.2 (requirements.txt line 4), making this a latent runtime crash. Every registration and login request will fail when connected to an actual database, which fundamentally breaks the tutorial's purpose.

Affected Areas

  • web/app.pyuser_exist() function (uses .count())
  • web/app.pyverify_user() function (uses .count())
  • web/tests/test_app.py — mock cursor sets cursor.count.return_value (must be updated)

Recommended Fix

# Before
def user_exist(username):
    return users.find({"Username": username}).count() > 0

# After
def user_exist(username):
    return users.count_documents({"Username": username}) > 0

Update test helpers to set mock_users.count_documents.return_value accordingly and remove the .count mock attribute.

Acceptance Criteria

  • cursor.count() replaced with count_documents() in all helpers
  • Test mocks updated to patch count_documents instead of cursor .count
  • All tests pass
  • Manually verified against a live MongoDB 6+ instance

Complexity Estimate

S — small code change but requires updating tests and verifying end-to-end.

Priority

High — causes an AttributeError crash on every register and login against a real database.


Auto-identified by workspace issue-logger
Category: dependency upgrade
Complexity: S
Repository: DewaldOosthuizen/python_rest_tutorial

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdependenciesPull requests that update a dependency fileenhancementNew feature or requestpythonPull requests that update python code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions