Skip to content

Commit b00d2ff

Browse files
committed
tests and fixes for document
1 parent 6435735 commit b00d2ff

File tree

2 files changed

+480
-22
lines changed

2 files changed

+480
-22
lines changed

aiorethink/document.py

+28-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class _MetaDocument(_MetaFieldContainer):
2020

2121
def __init__(cls, name, bases, classdict):
22-
cls._set_tablename()
22+
cls._tablename = cls._get_tablename()
2323

2424
# make sure that the following runs only for subclasses of Document.
2525
# There's no really nice way to do this AFAIK, because 'Document' is
@@ -37,12 +37,9 @@ class Document(FieldContainer, metaclass = _MetaDocument):
3737
Non-obvious customization:
3838
cls._table_create_options dict with extra kwargs for rethinkdb.table_create
3939
"""
40-
_tablename = None # customize with _set_tablename
40+
_tablename = None # customize with _get_tablename - don't set this attr
4141
_table_create_options = None # dict with extra kwargs for rethinkdb.table_create
4242

43-
pkey = None # alias for whichever field is the primary key. set
44-
# automatically for subclasses upon class creation
45-
4643

4744
def __init__(self, **kwargs):
4845
"""Makes a Document object, but does not save it yet to the database.
@@ -62,15 +59,15 @@ def __init__(self, **kwargs):
6259
###########################################################################
6360

6461
@classmethod
65-
def _set_tablename(cls):
62+
def _get_tablename(cls):
6663
"""Override this in subclasses if you want anything other than a table
6764
name automatically derived from the class name using
6865
inflection.tableize().
6966
7067
Mind the dangers of further subclassing, and of using the same table
7168
for different Document classes.
7269
"""
73-
cls._tablename = inflection.tableize(cls.__name__)
70+
return inflection.tableize(cls.__name__)
7471

7572

7673
@classmethod
@@ -392,24 +389,33 @@ async def __aiter__(self):
392389

393390

394391
async def __anext__(self):
395-
try:
396-
msg = await self.cursor.next()
397-
except r.ReqlCursorEmpty:
398-
raise StopAsyncIteration
392+
while True:
393+
try:
394+
msg = await self.cursor.next()
395+
except r.ReqlCursorEmpty:
396+
raise StopAsyncIteration
399397

400-
doc = self.doc
398+
if "new_val" not in msg:
399+
continue
401400

402-
# update doc
403-
if msg["new_val"] == None:
404-
doc._stored_in_db = False
405-
else:
406-
changed_fields = {k: v for k, v in msg["new_val"].items()
407-
if k not in doc or v != doc.get_dbvalue(k)}
408-
for k, v in changed_fields.items():
409-
doc_key = doc.get_key_for_dbkey(k)
410-
doc.set_dbvalue(doc_key, v, mark_updated = False)
401+
doc = self.doc
402+
403+
# update doc and return changed fields
404+
if msg["new_val"] == None:
405+
doc._stored_in_db = False
406+
return doc, None, msg
407+
else:
408+
changed_fields = {k: v for k, v in msg["new_val"].items()
409+
if k not in doc or v != doc.get_dbvalue(k)}
410+
411+
if not changed_fields:
412+
continue
413+
414+
for k, v in changed_fields.items():
415+
doc_key = doc.get_key_for_dbkey(k)
416+
doc.set_dbvalue(doc_key, v, mark_updated = False)
411417

412-
return doc, list(changed_fields.keys()), msg
418+
return doc, list(changed_fields.keys()), msg
413419

414420

415421
async def aiter_changes(self, conn = None):

0 commit comments

Comments
 (0)