Add tests for Object.get_field
* tests for get_field, with and without join * fix bug in Handle * fix issues in get_field
This commit is contained in:
parent
1ced0ac405
commit
dfc0808167
@ -35,7 +35,7 @@ class HandleClass(str):
|
||||
"Event": Event,
|
||||
"Place": Place,
|
||||
"Source": Source,
|
||||
"MediaObject": MediaObject,
|
||||
"Media": MediaObject,
|
||||
"Repository": Repository,
|
||||
"Note": Note,
|
||||
"Citation": Citation,
|
||||
|
@ -220,9 +220,14 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
|
||||
results = []
|
||||
while todo:
|
||||
parent, current, chain, path_to = todo.pop()
|
||||
#print("expand:", parent.__class__.__name__,
|
||||
# current.__class__.__name__,
|
||||
# chain,
|
||||
# path_to)
|
||||
keep_going = True
|
||||
p = 0
|
||||
while p < len(chain) and keep_going:
|
||||
#print("while:", p, chain, chain[p])
|
||||
part = chain[p]
|
||||
if hasattr(current, part): # attribute
|
||||
current = getattr(current, part)
|
||||
@ -235,7 +240,11 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
|
||||
path_to.append(part)
|
||||
else: # else branch! in middle, split paths
|
||||
for i in range(len(current)):
|
||||
todo.append([self, current, [str(i)] + chain[p:], path_to])
|
||||
#print("split :", self.__class__.__name__,
|
||||
# current.__class__.__name__,
|
||||
# [str(i)] + chain[p:],
|
||||
# path_to[:-len(chain[p:])])
|
||||
todo.append([self, current, [str(i)] + chain[p:], path_to[:-len(chain[p:])]])
|
||||
current = None
|
||||
keep_going = False
|
||||
else: # part not found on this self
|
||||
@ -249,11 +258,20 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
|
||||
obj = ptype.join(db, current)
|
||||
if part == "self":
|
||||
current = obj
|
||||
path_to = []
|
||||
#print("split :", obj.__class__.__name__,
|
||||
# current.__class__.__name__,
|
||||
# chain[p + 1:],
|
||||
# path_to[p + 1:])
|
||||
todo.append([obj, current, chain[p + 1:], chain[p + 1:]])
|
||||
elif obj:
|
||||
current = getattr(obj, part)
|
||||
if current:
|
||||
path_to = []
|
||||
todo.append([obj, current, chain[p + 1:], path_to])
|
||||
#print("split :", obj.__class__.__name__,
|
||||
# current.__class__.__name__,
|
||||
# chain[p + 1:],
|
||||
# path_to[p:])
|
||||
todo.append([obj, current, chain[p + 1:], chain[p:]])
|
||||
current = None
|
||||
keep_going = False
|
||||
else:
|
||||
|
68
gramps/gen/lib/test/field_test.py
Normal file
68
gramps/gen/lib/test/field_test.py
Normal file
@ -0,0 +1,68 @@
|
||||
#
|
||||
# Gramps - a GTK+/GNOME based genealogy program
|
||||
#
|
||||
# Copyright (C) 2016 Gramps Development Team
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
|
||||
""" Tests for using database fields """
|
||||
|
||||
import unittest
|
||||
|
||||
from gramps.plugins.database.dictionarydb import DictionaryDb
|
||||
|
||||
from ..import (Person, Surname, Name, NameType, Family, FamilyRelType,
|
||||
Event, EventType, Source, Place, PlaceName, Citation, Date,
|
||||
Repository, RepositoryType, MediaObject, Note, NoteType,
|
||||
StyledText, StyledTextTag, StyledTextTagType, Tag,
|
||||
ChildRef, ChildRefType, Attribute, MediaRef, AttributeType,
|
||||
Url, UrlType, Address, EventRef, EventRoleType, RepoRef,
|
||||
FamilyRelType, LdsOrd, MediaRef, PersonRef, PlaceType,
|
||||
SrcAttribute, SrcAttributeType)
|
||||
|
||||
class FieldBaseTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
db = DictionaryDb()
|
||||
db.load(None)
|
||||
with db.get_transaction_class()("Test", db) as trans:
|
||||
# Add some people:
|
||||
person1 = Person()
|
||||
person1.primary_name = Name()
|
||||
person1.primary_name.surname_list.append(Surname())
|
||||
person1.primary_name.surname_list[0].surname = "Smith"
|
||||
person1.gramps_id = "I0001"
|
||||
db.add_person(person1, trans) # person gets a handle
|
||||
|
||||
# Add some families:
|
||||
family1 = Family()
|
||||
family1.father_handle = person1.handle
|
||||
family1.gramps_id = "F0001"
|
||||
db.add_family(family1, trans)
|
||||
self.db = db
|
||||
|
||||
def test_field_access01(self):
|
||||
person = self.db.get_person_from_gramps_id("I0001")
|
||||
self.assertEqual(person.get_field("primary_name.surname_list.0.surname"),
|
||||
"Smith")
|
||||
|
||||
def test_field_join01(self):
|
||||
family = self.db.get_family_from_gramps_id("F0001")
|
||||
self.assertEqual(family.get_field("father_handle.primary_name.surname_list.0.surname", self.db),
|
||||
"Smith")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Loading…
Reference in New Issue
Block a user