Skip to content

Commit

Permalink
Added support for with_options method for Frames.
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyjb committed Jan 16, 2020
1 parent 62957be commit 458930c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pyvenv.cfg
.tox
.cache
.pytest_cache/
.pip-selfcheck.json
29 changes: 26 additions & 3 deletions mongoframes/frames.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import contextmanager

from blinker import signal
from bson.objectid import ObjectId
from copy import deepcopy
Expand Down Expand Up @@ -430,12 +432,12 @@ def count(cls, filter=None, **kwargs):

if filter:
return cls.get_collection().count_documents(
to_refs(filter),
to_refs(filter),
**kwargs
)
else:
return cls.get_collection().estimated_document_count(**kwargs)

@classmethod
def ids(cls, filter=None, **kwargs):
"""Return a list of Ids for documents matching the filter"""
Expand Down Expand Up @@ -779,7 +781,11 @@ def stop_listening(cls, event, func):
@classmethod
def get_collection(cls):
"""Return a reference to the database collection for the class"""
return getattr(cls.get_db(), cls._collection)
return getattr(
cls,
'_collection_context',
getattr(cls.get_db(), cls._collection)
)

@classmethod
def get_db(cls):
Expand All @@ -788,6 +794,23 @@ def get_db(cls):
return getattr(cls._client, cls._db)
return cls._client.get_default_database()

@classmethod
@contextmanager
def with_options(cls, **options):
existing_context = getattr(cls, '_collection_context', None)

try:
collection = getattr(cls.get_db(), cls._collection)
cls._collection_context = collection.with_options(**options)
yield cls._collection_context

finally:
if existing_context is None:
del cls._collection_context

else:
cls._collection_context = existing_context


class SubFrame(_BaseFrame):
"""
Expand Down
1 change: 0 additions & 1 deletion pip-selfcheck.json

This file was deleted.

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.3.4',
version='1.3.5',
description='A fast unobtrusive MongoDB ODM for Python',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down Expand Up @@ -84,7 +84,7 @@
install_requires=[
'blinker>=1.4',
'Faker>=0.7.18',
'pymongo>=3.8'
'pymongo>=3.9.0'
],

# List additional groups of dependencies here (e.g. development
Expand Down
23 changes: 22 additions & 1 deletion tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from time import sleep
from unittest.mock import Mock

from pymongo import ReadPreference
from mongoframes import *

from tests.fixtures import *
Expand Down Expand Up @@ -749,4 +750,24 @@ def test_flattern_projection():
'name': True,
'inventory.gold': True,
'inventory.secret_draw.gold': True
}
}

def test_with_options():
"""Flattern projection"""

collection = Dragon.get_collection()

with Dragon.with_options(read_preference=ReadPreference.SECONDARY):
assert Dragon.get_collection().read_preference \
== ReadPreference.SECONDARY

with Dragon.with_options(
read_preference=ReadPreference.PRIMARY_PREFERRED):

assert Dragon.get_collection().read_preference \
== ReadPreference.PRIMARY_PREFERRED

assert Dragon.get_collection().read_preference \
== ReadPreference.SECONDARY

assert collection.read_preference == ReadPreference.PRIMARY

0 comments on commit 458930c

Please sign in to comment.