Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
upgrade to motor 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
amitnabarro committed Dec 24, 2018
1 parent 7623353 commit b97ca82
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-dateutil >= 2.5.3
python-dateutil >= 2.7.5
10 changes: 5 additions & 5 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-r ../requirements.txt

pytest==3.2.1
pytest-asyncio==0.6.0
coverage==4.4.1
motor==1.1
phonenumbers==8.8.0
pytest==4.0.2
pytest-asyncio==0.9.0
coverage==4.5.2
motor==2.0.0
phonenumbers==8.10.2



1 change: 0 additions & 1 deletion tbone/data/fields/phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def add_to_class(self, cls, name):
'''
Overrides the base class to add a PhoheNumberDescriptor rather than the standard FieldDescriptor
'''
self.name = name
self.model_class = cls
setattr(cls, name, PhoneNumberDescriptor(self))
self._bound = True
12 changes: 6 additions & 6 deletions tbone/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ async def check_reconnect_tries_and_wait(cls, reconnect_number, method_name):
await asyncio.sleep(timeout)

@classmethod
async def count(cls, db):
async def count(cls, db, filters={}):
for i in cls.connection_retries():
try:
result = await db[cls.get_collection_name()].count()
result = await db[cls.get_collection_name()].count_documents(filters)
return result
except ConnectionFailure as ex:
exceed = await cls.check_reconnect_tries_and_wait(i, 'count')
Expand Down Expand Up @@ -200,8 +200,8 @@ async def save(self, db=None):
for i in self.connection_retries():
try:
created = False if '_id' in data else True
result = await self.db[self.get_collection_name()].save(data)
self._id = result
result = await self.db[self.get_collection_name()].insert_one(data)
self._id = result.inserted_id
# emit post save
asyncio.ensure_future(post_save.send(
sender=self.__class__,
Expand All @@ -227,8 +227,8 @@ async def insert(self, db=None):
for i in self.connection_retries():
try:
created = False if '_id' in data else True
result = await db[self.get_collection_name()].insert(data)
self._id = result
result = await db[self.get_collection_name()].insert_one(data)
self._id = result.inserted_id
# emit post save
asyncio.ensure_future(post_save.send(
sender=self.__class__,
Expand Down
2 changes: 1 addition & 1 deletion tbone/dispatch/channels/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def create_channel(self, capacity=2**15, message_size=1024): # default si
if self._collection:
return
with (await MongoChannel.channel_lock):
if self.name not in (await self._db.collection_names()):
if self.name not in (await self._db.list_collection_names()):
self._collection = await self._db.create_collection(
self.name,
size=capacity * message_size,
Expand Down
2 changes: 1 addition & 1 deletion tbone/resources/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def list(self, *args, **kwargs):
cursor = self._meta.object_class.get_cursor(db=self.db, query=filters, projection=projection, sort=sort)
cursor.skip(offset)
cursor.limit(limit)
total_count = await cursor.count()
total_count = await self._meta.object_class.count(db=self.db, filters=filters)
object_list = await self._meta.object_class.find(cursor)
# serialize results
serialized_objects = await asyncio.gather(*[obj.serialize() for obj in object_list])
Expand Down
2 changes: 1 addition & 1 deletion tbone/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def db(request, create_database):
'''
Clears the database before every test so all tests start in a known db state
'''
colls = await create_database.collection_names()
colls = await create_database.list_collection_names()
for coll in colls:
await create_database.drop_collection(coll)
return create_database
13 changes: 9 additions & 4 deletions tests/db/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Meta:
m = M({'name': 'Toque'})
await m.save(db)
# verify defined namespace.name exists
names = await db.collection_names()
names = await db.list_collection_names()
assert 'kitchen.hats' in names

class M2(BaseModel):
Expand All @@ -36,7 +36,7 @@ class M2(BaseModel):
m = M2({'name': 'Toque'})
await m.save(db)
# verify default model class name exists
names = await db.collection_names()
names = await db.list_collection_names()
assert 'm2' in names


Expand Down Expand Up @@ -123,8 +123,8 @@ async def test_collection_pagination_and_sorting(request, db):
futures.append(m.save(db))
await asyncio.wait(futures)
# make sure the collection contains the right amount of documents
cursor = Number.get_cursor(db=db)
total_count = await cursor.count()
# cursor = Number.get_cursor(db=db)
total_count = await Number.count(db)
assert total_count == COUNT

# get objects from collection by size of page
Expand All @@ -141,6 +141,11 @@ async def test_collection_pagination_and_sorting(request, db):
assert numbers == sorted(numbers)


@pytest.mark.asyncio
async def test_collection_filter(request, db):
pass


@pytest.mark.asyncio
async def test_model_custom_methods(request, db):
''' Test custom methods for model updating implemented in the model class '''
Expand Down

0 comments on commit b97ca82

Please sign in to comment.