Skip to content

Commit

Permalink
style: applied ruf formater and linter; added configuration for ruf
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kryukov authored and michaelkryukov committed Dec 29, 2023
1 parent 57f292f commit 6ac83c7
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 75 deletions.
179 changes: 107 additions & 72 deletions mongomock_motor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ class Wrapper(cls):
@property
def __class__(self):
return target

return Wrapper

return decorator


def with_async_methods(source, async_methods):
def decorator(cls):
for method_name in async_methods:

def make_wrapper(method_name):
async def wrapper(self, *args, **kwargs):
proxy_source = self.__dict__.get(f'_{cls.__name__}{source}')
return getattr(proxy_source, method_name)(*args, **kwargs)

return wrapper

setattr(cls, method_name, make_wrapper(method_name))
Expand All @@ -50,11 +54,13 @@ async def wrapper(self, *args, **kwargs):
def with_cursor_chaining_methods(source, chaining_methods):
def decorator(cls):
for method_name in chaining_methods:

def make_wrapper(method_name):
def wrapper(self, *args, **kwargs):
proxy_source = self.__dict__.get(f'_{cls.__name__}{source}')
getattr(proxy_source, method_name)(*args, **kwargs)
return self

return wrapper

setattr(cls, method_name, make_wrapper(method_name))
Expand All @@ -65,24 +71,27 @@ def wrapper(self, *args, **kwargs):


@masquerade_class('motor.motor_asyncio.AsyncIOMotorCursor')
@with_cursor_chaining_methods('__cursor', [
'add_option',
'allow_disk_use',
'collation',
'comment',
'hint',
'limit',
'max_await_time_ms',
'max_scan',
'max_time_ms',
'max',
'min',
'remove_option',
'skip',
'sort',
'where',
])
class AsyncCursor():
@with_cursor_chaining_methods(
'__cursor',
[
'add_option',
'allow_disk_use',
'collation',
'comment',
'hint',
'limit',
'max_await_time_ms',
'max_scan',
'max_time_ms',
'max',
'min',
'remove_option',
'skip',
'sort',
'where',
],
)
class AsyncCursor:
def __init__(self, cursor):
self.__cursor = cursor

Expand Down Expand Up @@ -111,7 +120,7 @@ async def to_list(self, *args, **kwargs):


@masquerade_class('motor.motor_asyncio.AsyncIOMotorLatentCommandCursor')
class AsyncLatentCommandCursor():
class AsyncLatentCommandCursor:
def __init__(self, cursor):
self.__cursor = cursor

Expand All @@ -134,39 +143,42 @@ async def to_list(self, *args, **kwargs):


@masquerade_class('motor.motor_asyncio.AsyncIOMotorCollection')
@with_async_methods('__collection', [
'bulk_write',
'count_documents',
'count', # deprecated
'create_index',
'create_indexes',
'delete_many',
'delete_one',
'drop_index',
'drop_indexes',
'drop',
'distinct',
'ensure_index',
'estimated_document_count',
'find_and_modify', # deprecated
'find_one_and_delete',
'find_one_and_replace',
'find_one_and_update',
'find_one',
'index_information',
'inline_map_reduce',
'insert_many',
'insert_one',
'map_reduce',
'options',
'reindex',
'rename',
'replace_one',
'save',
'update_many',
'update_one',
])
class AsyncMongoMockCollection():
@with_async_methods(
'__collection',
[
'bulk_write',
'count_documents',
'count', # deprecated
'create_index',
'create_indexes',
'delete_many',
'delete_one',
'drop_index',
'drop_indexes',
'drop',
'distinct',
'ensure_index',
'estimated_document_count',
'find_and_modify', # deprecated
'find_one_and_delete',
'find_one_and_replace',
'find_one_and_update',
'find_one',
'index_information',
'inline_map_reduce',
'insert_many',
'insert_one',
'map_reduce',
'options',
'reindex',
'rename',
'replace_one',
'save',
'update_many',
'update_one',
],
)
class AsyncMongoMockCollection:
def __init__(self, database, collection):
self.database = database
self.__collection = collection
Expand All @@ -191,14 +203,17 @@ def list_indexes(self, *args, **kwargs) -> AsyncCursor:


@masquerade_class('motor.motor_asyncio.AsyncIOMotorDatabase')
@with_async_methods('__database', [
'create_collection',
'dereference',
'drop_collection',
'list_collection_names',
'validate_collection',
])
class AsyncMongoMockDatabase():
@with_async_methods(
'__database',
[
'create_collection',
'dereference',
'drop_collection',
'list_collection_names',
'validate_collection',
],
)
class AsyncMongoMockDatabase:
def __init__(self, client, database, mock_build_info=None):
self.client = client
self.__database = database
Expand Down Expand Up @@ -234,7 +249,11 @@ async def command(self, *args, **kwargs):
raise
if isinstance(args[0], str) and args[0].lower() == 'buildinfo':
return self.__build_info
if isinstance(args[0], dict) and args[0] and list(args[0])[0].lower() == 'buildinfo':
if (
isinstance(args[0], dict)
and args[0]
and list(args[0])[0].lower() == 'buildinfo'
):
return self.__build_info
raise

Expand All @@ -252,15 +271,27 @@ def __getattr__(self, name):


@masquerade_class('motor.motor_asyncio.AsyncIOMotorClient')
@with_async_methods('__client', [
'drop_database',
'list_database_names',
'list_databases',
'server_info',
])
class AsyncMongoMockClient():
def __init__(self, *args, mock_build_info=None, mock_mongo_client=None, mock_io_loop=None, **kwargs):
self.__client = _patch_client_internals(mock_mongo_client or MongoClient(*args, **kwargs))
@with_async_methods(
'__client',
[
'drop_database',
'list_database_names',
'list_databases',
'server_info',
],
)
class AsyncMongoMockClient:
def __init__(
self,
*args,
mock_build_info=None,
mock_mongo_client=None,
mock_io_loop=None,
**kwargs,
):
self.__client = _patch_client_internals(
mock_mongo_client or MongoClient(*args, **kwargs)
)
self.__build_info = mock_build_info
self.__io_loop = mock_io_loop

Expand Down Expand Up @@ -290,7 +321,11 @@ def __getattr__(self, name):
@contextmanager
def enabled_gridfs_integration():
with ExitStack() as stack:
stack.enter_context(patch('gridfs.Database', (PyMongoDatabase, MongoMockDatabase)))
stack.enter_context(patch('gridfs.grid_file.Collection', (PyMongoDatabase, MongoMockCollection)))
stack.enter_context(
patch('gridfs.Database', (PyMongoDatabase, MongoMockDatabase))
)
stack.enter_context(
patch('gridfs.grid_file.Collection', (PyMongoDatabase, MongoMockCollection))
)
stack.enter_context(patch('gridfs.GridOutCursor', _create_grid_out_cursor))
yield
5 changes: 4 additions & 1 deletion mongomock_motor/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ def wrapper(data, *args, **kwargs):
return fn(data, *args, **kwargs)
except DuplicateKeyError as exc:
raise _provide_error_details(collection, data, exc)

return wrapper

collection._insert = with_enriched_duplicate_key_error(collection._insert)
collection._ensure_uniques = with_enriched_duplicate_key_error(collection._ensure_uniques)
collection._ensure_uniques = with_enriched_duplicate_key_error(
collection._ensure_uniques
)

return collection

Expand Down
4 changes: 4 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target-version = "py38"

[format]
quote-style = "single"
6 changes: 4 additions & 2 deletions tests/test_gridfs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import io

import pytest
from mongomock_motor import AsyncMongoMockClient, enabled_gridfs_integration
from motor.motor_asyncio import AsyncIOMotorGridFSBucket
import io

from mongomock_motor import AsyncMongoMockClient, enabled_gridfs_integration


@pytest.mark.anyio
Expand Down

0 comments on commit 6ac83c7

Please sign in to comment.