michaelkryukov / mongomock_motor

Library for mocking AsyncIOMotorClient built on top of mongomock.
MIT License
102 stars 24 forks source link

AsyncMongoMockClient dosen't suport AsyncIOMotorGridFSBucket #41

Closed CaptainOfHacks closed 9 months ago

CaptainOfHacks commented 10 months ago

This simple test:


import pytest
from mongomock_motor import AsyncMongoMockClient
from motor.motor_asyncio import AsyncIOMotorGridFSBucket

@pytest.mark.asyncio
async def test_dummy():
    mongo_client = AsyncMongoMockClient()
    mongo_db = mongo_client["test_database"]
    grid_fs = AsyncIOMotorGridFSBucket(mongo_db)

Provide this error: self = <motor.motor_asyncio.AsyncIOMotorGridFSBucket object at 0x107e0d710> database = <motor.motor_asyncio.AsyncIOMotorDatabase object at 0x107b091d0> bucket_name = 'fs', chunk_size_bytes = 261120, write_concern = None read_preference = None, collection = None

def __init__(
    self,
    database,
    bucket_name="fs",
    chunk_size_bytes=DEFAULT_CHUNK_SIZE,
    write_concern=None,
    read_preference=None,
    collection=None,
):
    """Create a handle to a GridFS bucket.

    Raises :exc:`~pymongo.errors.ConfigurationError` if `write_concern`
    is not acknowledged.

    This class conforms to the `GridFS API Spec
    <https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst>`_
    for MongoDB drivers.

    :Parameters:
      - `database`: database to use.
      - `bucket_name` (optional): The name of the bucket. Defaults to 'fs'.
      - `chunk_size_bytes` (optional): The chunk size in bytes. Defaults
        to 255KB.
      - `write_concern` (optional): The
        :class:`~pymongo.write_concern.WriteConcern` to use. If ``None``
        (the default) db.write_concern is used.
      - `read_preference` (optional): The read preference to use. If
        ``None`` (the default) db.read_preference is used.
      - `collection` (optional): Deprecated, an alias for `bucket_name`
        that exists solely to provide backwards compatibility.

    .. versionchanged:: 3.0
       Removed support for the `disable_md5` parameter (to match the
       GridFSBucket class in PyMongo).
    .. versionchanged:: 2.1
       Added support for the `bucket_name`, `chunk_size_bytes`,
       `write_concern`, and `read_preference` parameters.
       Deprecated the `collection` parameter which is now an alias to
       `bucket_name` (to match the GridFSBucket class in PyMongo).
    .. versionadded:: 1.0

    .. mongodoc:: gridfs
    """
    # Preserve backwards compatibility of "collection" parameter
    if collection is not None:
        warnings.warn(
            'the "collection" parameter is deprecated, use "bucket_name" instead',
            DeprecationWarning,
            stacklevel=2,
        )
        bucket_name = collection

    db_class = create_class_with_framework(AgnosticDatabase, self._framework, self.__module__)

    if not isinstance(database, db_class):
        raise TypeError(
            "First argument to %s must be  MotorDatabase, not %r" % (self.__class__, database)
        )
  self.io_loop = database.get_io_loop()

E TypeError: 'AsyncIOMotorCollection' object is not callable

michaelkryukov commented 9 months ago

I've created PR with changes that should enable working with GridFS (see added test for the example). Can you check if your use case now works?