Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior of list_collection_names() with and without filter #872

Open
adrienballsonos opened this issue Jan 22, 2024 · 0 comments

Comments

@adrienballsonos
Copy link

Summary

When calling list_collection_names() with a filter, mongomock will list "created" and "non-created" collections as opposed to what it does when no filter is provided in which case only "created" collections are listed.

Here is a small test which exposes the isssue:

def test_list_collection_names():
    client = mongomock.MongoClient()
    db = client["my_db"]
    assert db.list_collection_names() == []  # OK
    # Run an update operation on a non existent collection
    db["my_collection"].update_many({}, {"$set": {"a": 1}})
    assert db.list_collection_names() == []  # OK
    assert db.list_collection_names(filter={"name": "my_collection"}) == []  # KO

This test produces the following output:

def test_list_collection_names():
        client = mongomock.MongoClient()
        db = client["my-db"]
        assert db.list_collection_names() == []
        db["non_existing_collection"].update_many({}, {"$set": {"a": 1}})
        assert db.list_collection_names() == []
>       assert db.list_collection_names(filter={"name": "non_existing_collection"}) == []
E       AssertionError: assert ['non_existing_collection'] == []
E         Left contains one more item: 'non_existing_collection'
E         Full diff:
E         - []
E         + ['non_existing_collection']

When looking at the underlying code, mongomock relies on self._get_created_collections() when no filter is provided (here), whereas it uses self._store._collections when a filter is passed (here). I would have expected self._get_created_collections() to be used in both cases.

The current behavior does not match the one of MongoDB.

Python version: 3.9.9
mongomock version: 4.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant