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.
|
Retrieves a document from the database, by primary key.
|
||||||
'''
|
'''
|
||||||
|
if pk is None:
|
||||||
|
return None
|
||||||
doc = cls(rr)
|
doc = cls(rr)
|
||||||
doc[doc.pk_field] = pk
|
doc[doc.pk_field] = pk
|
||||||
doc.refresh()
|
try:
|
||||||
|
doc.refresh()
|
||||||
|
except KeyError:
|
||||||
|
return None
|
||||||
return doc
|
return doc
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
2
setup.py
2
setup.py
@ -3,7 +3,7 @@ import codecs
|
|||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name='doublethink',
|
name='doublethink',
|
||||||
version='0.2.0.dev68',
|
version='0.2.0.dev69',
|
||||||
packages=['doublethink'],
|
packages=['doublethink'],
|
||||||
classifiers=[
|
classifiers=[
|
||||||
'Programming Language :: Python :: 2.7',
|
'Programming Language :: Python :: 2.7',
|
||||||
|
@ -232,11 +232,16 @@ def test_orm_pk(rr):
|
|||||||
def table_create(cls, rethinker):
|
def table_create(cls, rethinker):
|
||||||
rethinker.table_create(cls.table, primary_key='not_id').run()
|
rethinker.table_create(cls.table, primary_key='not_id').run()
|
||||||
|
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
NonstandardPrimaryKey.load(rr, 'no_such_thing')
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
NonstandardPrimaryKey.load(rr, 'no_such_thing')
|
NonstandardPrimaryKey.load(rr, 'no_such_thing')
|
||||||
|
|
||||||
NonstandardPrimaryKey.table_ensure(rr)
|
NonstandardPrimaryKey.table_ensure(rr)
|
||||||
|
|
||||||
|
assert NonstandardPrimaryKey.load(rr, None) is None
|
||||||
|
assert NonstandardPrimaryKey.load(rr, 'no_such_thing') is None
|
||||||
|
|
||||||
# new empty doc
|
# new empty doc
|
||||||
f = NonstandardPrimaryKey(rr, {})
|
f = NonstandardPrimaryKey(rr, {})
|
||||||
f.save()
|
f.save()
|
||||||
@ -245,8 +250,7 @@ def test_orm_pk(rr):
|
|||||||
assert f.not_id == f.pk_value
|
assert f.not_id == f.pk_value
|
||||||
assert len(f.keys()) == 1
|
assert len(f.keys()) == 1
|
||||||
|
|
||||||
with pytest.raises(KeyError):
|
assert NonstandardPrimaryKey.load(rr, 'no_such_thing') is None
|
||||||
NonstandardPrimaryKey.load(rr, 'no_such_thing')
|
|
||||||
|
|
||||||
# new doc with (only) primary key
|
# new doc with (only) primary key
|
||||||
d = NonstandardPrimaryKey(rr, {'not_id': 1})
|
d = NonstandardPrimaryKey(rr, {'not_id': 1})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user