From dfc080816795ef0b4562cd2383539c12e2984bdf Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Sun, 17 Jan 2016 12:07:31 -0500 Subject: [PATCH] Add tests for Object.get_field * tests for get_field, with and without join * fix bug in Handle * fix issues in get_field --- gramps/gen/lib/handle.py | 2 +- gramps/gen/lib/primaryobj.py | 24 +++++++++-- gramps/gen/lib/test/field_test.py | 68 +++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 gramps/gen/lib/test/field_test.py diff --git a/gramps/gen/lib/handle.py b/gramps/gen/lib/handle.py index 2e3b37ed5..b82e47d34 100644 --- a/gramps/gen/lib/handle.py +++ b/gramps/gen/lib/handle.py @@ -35,7 +35,7 @@ class HandleClass(str): "Event": Event, "Place": Place, "Source": Source, - "MediaObject": MediaObject, + "Media": MediaObject, "Repository": Repository, "Note": Note, "Citation": Citation, diff --git a/gramps/gen/lib/primaryobj.py b/gramps/gen/lib/primaryobj.py index 1c48f2e64..0d44eaf3b 100644 --- a/gramps/gen/lib/primaryobj.py +++ b/gramps/gen/lib/primaryobj.py @@ -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: diff --git a/gramps/gen/lib/test/field_test.py b/gramps/gen/lib/test/field_test.py new file mode 100644 index 000000000..92d959942 --- /dev/null +++ b/gramps/gen/lib/test/field_test.py @@ -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()