mirror of
https://github.com/internetarchive/warcprox.git
synced 2025-01-18 13:22:09 +01:00
have Document.load() return None if no such doc is found in the db
This commit is contained in:
parent
aa177f94b8
commit
07bc01cbbd
@ -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
|
||||
|
2
setup.py
2
setup.py
@ -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',
|
||||
|
@ -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})
|
||||
|
Loading…
x
Reference in New Issue
Block a user