have Document.load() return None if no such doc is found in the db

This commit is contained in:
Noah Levitt 2017-03-02 10:31:29 -08:00
parent aa177f94b8
commit 07bc01cbbd
3 changed files with 13 additions and 4 deletions

View File

@ -162,9 +162,14 @@ class Document(dict, object):
'''
Retrieves a document from the database, by primary key.
'''
if pk is None:
return None
doc = cls(rr)
doc[doc.pk_field] = pk
doc.refresh()
try:
doc.refresh()
except KeyError:
return None
return doc
@classmethod

View File

@ -3,7 +3,7 @@ import codecs
setuptools.setup(
name='doublethink',
version='0.2.0.dev68',
version='0.2.0.dev69',
packages=['doublethink'],
classifiers=[
'Programming Language :: Python :: 2.7',

View File

@ -232,11 +232,16 @@ def test_orm_pk(rr):
def table_create(cls, rethinker):
rethinker.table_create(cls.table, primary_key='not_id').run()
with pytest.raises(Exception):
NonstandardPrimaryKey.load(rr, 'no_such_thing')
with pytest.raises(Exception):
NonstandardPrimaryKey.load(rr, 'no_such_thing')
NonstandardPrimaryKey.table_ensure(rr)
assert NonstandardPrimaryKey.load(rr, None) is None
assert NonstandardPrimaryKey.load(rr, 'no_such_thing') is None
# new empty doc
f = NonstandardPrimaryKey(rr, {})
f.save()
@ -245,8 +250,7 @@ def test_orm_pk(rr):
assert f.not_id == f.pk_value
assert len(f.keys()) == 1
with pytest.raises(KeyError):
NonstandardPrimaryKey.load(rr, 'no_such_thing')
assert NonstandardPrimaryKey.load(rr, 'no_such_thing') is None
# new doc with (only) primary key
d = NonstandardPrimaryKey(rr, {'not_id': 1})