Missing methods on dictDB; added tests for setting struct

This commit is contained in:
Doug Blank 2013-11-12 20:09:04 -05:00
parent 3ef1c02155
commit 732743cec5
3 changed files with 46 additions and 2 deletions

View File

@ -991,3 +991,33 @@ class DictionaryDb(DbWriteBase, DbReadBase):
Get the transaction class associated with this database backend. Get the transaction class associated with this database backend.
""" """
return DictionaryTxn return DictionaryTxn
def get_from_name_and_handle(self, table_name, handle):
"""
Returns a gen.lib object (or None) given table_name and
handle.
Examples:
>>> self.get_from_name_and_handle("Person", "a7ad62365bc652387008")
>>> self.get_from_name_and_handle("Media", "c3434653675bcd736f23")
"""
if table_name in self._tables:
return self._tables[table_name]["handle_func"](handle)
return None
def get_from_name_and_gramps_id(self, table_name, gramps_id):
"""
Returns a gen.lib object (or None) given table_name and
Gramps ID.
Examples:
>>> self.get_from_name_and_gramps_id("Person", "I00002")
>>> self.get_from_name_and_gramps_id("Family", "F056")
>>> self.get_from_name_and_gramps_id("Media", "M00012")
"""
if table_name in self._tables:
return self._tables[table_name]["gramps_id_func"](gramps_id)
return None

View File

@ -26,6 +26,7 @@ from .. import (Person, Family, Event, Source, Place, Citation,
Repository, MediaObject, Note, Tag) Repository, MediaObject, Note, Tag)
from gramps.gen.merge.diff import import_as_dict, from_struct from gramps.gen.merge.diff import import_as_dict, from_struct
from gramps.cli.user import User from gramps.cli.user import User
from gramps.gen.merge.diff import *
class BaseCheck: class BaseCheck:
def test_from_struct(self): def test_from_struct(self):
@ -98,7 +99,7 @@ def generate_test(obj):
serialized = obj.__class__.from_struct(struct) serialized = obj.__class__.from_struct(struct)
def test(self): def test(self):
self.assertEqual(obj.serialize(), serialized) self.assertEqual(obj.serialize(), serialized)
name = "test_%s_%s" % (obj.__class__.__name__, obj.handle) name = "test_serialize_%s_%s" % (obj.__class__.__name__, obj.handle)
setattr(DatabaseCheck, name, test) setattr(DatabaseCheck, name, test)
#### ####
def test2(self): def test2(self):
@ -112,5 +113,18 @@ for table in db._tables.keys():
obj = db._tables[table]["handle_func"](handle) obj = db._tables[table]["handle_func"](handle)
generate_test(obj) generate_test(obj)
class StructTest(unittest.TestCase):
def test(self):
family = db.get_family_from_gramps_id("F0001")
s = Struct(family.to_struct(), db)
self.assertEqual(s["gramps_id"], "F0001")
s["gramps_id"] = "TEST"
self.assertEqual(s["gramps_id"], "TEST")
self.assertEqual(s["father_handle.primary_name.first_name"],
"Allen Carl")
s["father_handle.primary_name.first_name"] = "Edward"
self.assertEqual(s["father_handle.primary_name.first_name"],
"Edward")
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -372,7 +372,7 @@ class Struct(object):
part = path[p] part = path[p]
struct = self.getitem(part, struct) struct = self.getitem(part, struct)
if isinstance(struct, Struct): if isinstance(struct, Struct):
return struct.setitem_from_path(path[p+1:], value) return struct.setitem_from_path(path[p+1:] + [item], value)
if struct is None: if struct is None:
return None return None
# struct is set # struct is set