Skip to content

Commit

Permalink
GEPS045 Fix Testcasegenerator for enhanced places
Browse files Browse the repository at this point in the history
  • Loading branch information
prculley committed Jul 24, 2020
1 parent f6a6e45 commit 7d5ecdc
Showing 1 changed file with 150 additions and 120 deletions.
270 changes: 150 additions & 120 deletions gramps/plugins/tool/testcasegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,29 @@ class TestcaseGenerator(tool.BatchTool):
EventType.MARR_SETTL,
EventType.CUSTOM])

PLACETYPES = [
"Unknown", # -1 original value
"Country", # 1
"State", # 2
"County", # 3
"City", # 4
"Parish", # 5
"Locality", # 6
"Street", # 7
"Province", # 8
"Region", # 9
"Department", # 10
"Neighborhood", # 11
"District", # 12
"Borough", # 13
"Municipality", # 14
"Town", # 15
"Village", # 16
"Hamlet", # 17
"Farm", # 18
"Building", # 19
"Number"] # 20

def __init__(self, dbstate, user, options_class, name, callback=None):
uistate = user.uistate
if uistate:
Expand Down Expand Up @@ -390,33 +413,38 @@ def run_tool(self, cli=False):
self.generate_data_errors(step)

if self.options_dict['persons']:
with self.progress(_('Generating testcases'),
_('Generating families'),
self.max_person_count) \
as self.progress_step:
self.person_count = 0

while True:
if not self.persons_todo:
pers_h = self.generate_person(0)
self.persons_todo.append(pers_h)
self.parents_todo.append(pers_h)
person_h = self.persons_todo.pop(0)
self.generate_family(person_h)
if _randint(0, 3) == 0:
self.generate_family(person_h)
if _randint(0, 7) == 0:
self.generate_family(person_h)
if self.person_count > self.max_person_count:
break
for child_h in self.parents_todo:
self.generate_parents(child_h)
if self.person_count > self.max_person_count:
break
self.generate_persons()

if not cli:
self.top.destroy()

def generate_persons(self):
"""This creates the persons and families"""
with DbTxn(_("Testcase generator step %d") % self.transaction_count,
self.db) as self.trans, \
self.progress(_('Generating testcases'), _('Generating families'),
self.max_person_count) \
as self.progress_step:
self.person_count = 0

while True:
if not self.persons_todo:
pers_h = self.generate_person(0)
self.persons_todo.append(pers_h)
self.parents_todo.append(pers_h)
person_h = self.persons_todo.pop(0)
self.generate_family(person_h)
if _randint(0, 3) == 0:
self.generate_family(person_h)
if _randint(0, 7) == 0:
self.generate_family(person_h)
if self.person_count > self.max_person_count:
break
for child_h in self.parents_todo:
self.generate_parents(child_h)
if self.person_count > self.max_person_count:
break

def generate_data_errors(self, step):
"""This generates errors in the database to test src/plugins/tool/Check
The module names correspond to the checking methods in
Expand Down Expand Up @@ -1318,13 +1346,17 @@ def create_all_possible_citations(self, c_h_list, name, message):

place = Place()
place.set_title(message)
# Place
place.add_citation(_choice(c_h_list))
# Place : Name
pname = PlaceName(value='All Attribute Test')
pname.add_citation(_choice(c_h_list))
place.add_name(pname)
ptype = PlaceType(value=PlaceType.BOROUGH)
# Place : Type
ptype = PlaceType("Borough")
ptype.add_citation(_choice(c_h_list))
place.add_type(ptype)
# Place : Attribute
place.add_attribute(att)
# Place : MediaRef
mref = MediaRef()
Expand All @@ -1337,7 +1369,8 @@ def create_all_possible_citations(self, c_h_list, name, message):
att.add_citation(_choice(c_h_list))
mref.add_attribute(att)
place.add_media_reference(mref)
place.add_attribute(att)
# Place : EventRef
place.add_event_ref(eref)
self.db.add_place(place, self.trans)

ref = Repository()
Expand Down Expand Up @@ -1560,85 +1593,83 @@ def generate_family(self, person1_h):
if person2_h and _randint(0, 2) > 0:
self.parents_todo.append(person2_h)

with DbTxn(_("Testcase generator step %d") % self.transaction_count,
self.db) as self.trans:
self.transaction_count += 1
fam = Family()
self.add_defaults(fam)
if person1_h:
fam.set_father_handle(person1_h)
if person2_h:
fam.set_mother_handle(person2_h)
self.transaction_count += 1
fam = Family()
self.add_defaults(fam)
if person1_h:
fam.set_father_handle(person1_h)
if person2_h:
fam.set_mother_handle(person2_h)

# Avoid adding the same event more than once to the same family
event_set = set()
# Avoid adding the same event more than once to the same family
event_set = set()

# Generate at least one family event with a probability of 75%
if _randint(0, 3) > 0:
(dummy, eref) = self.rand_family_event(None)
fam.add_event_ref(eref)
event_set.add(eref.get_reference_handle())
# Generate at least one family event with a probability of 75%
if _randint(0, 3) > 0:
(dummy, eref) = self.rand_family_event(None)
fam.add_event_ref(eref)
event_set.add(eref.get_reference_handle())

# generate some more events with a lower probability
while _randint(0, 3) == 1:
(dummy, eref) = self.rand_family_event(None)
if eref.get_reference_handle() in event_set:
continue
fam.add_event_ref(eref)
event_set.add(eref.get_reference_handle())

# generate some more events with a lower probability
while _randint(0, 3) == 1:
(dummy, eref) = self.rand_family_event(None)
if eref.get_reference_handle() in event_set:
continue
# some shared events
if self.generated_events:
while _randint(0, 5) == 1:
typeval = EventType.UNKNOWN
while int(typeval) not in self.FAMILY_EVENTS:
e_h = _choice(self.generated_events)
typeval = self.db.get_event_from_handle(e_h).get_type()
if e_h in event_set:
break
eref = EventRef()
self.fill_object(eref)
eref.set_reference_handle(e_h)
fam.add_event_ref(eref)
event_set.add(eref.get_reference_handle())

# some shared events
if self.generated_events:
while _randint(0, 5) == 1:
typeval = EventType.UNKNOWN
while int(typeval) not in self.FAMILY_EVENTS:
e_h = _choice(self.generated_events)
typeval = self.db.get_event_from_handle(e_h).get_type()
if e_h in event_set:
break
eref = EventRef()
self.fill_object(eref)
eref.set_reference_handle(e_h)
fam.add_event_ref(eref)
event_set.add(e_h)
event_set.add(e_h)

fam_h = self.db.add_family(fam, self.trans)
self.generated_families.append(fam_h)
fam = self.db.commit_family(fam, self.trans)
if person1_h:
person1 = self.db.get_person_from_handle(person1_h)
person1.add_family_handle(fam_h)
self.db.commit_person(person1, self.trans)
if person2_h:
person2 = self.db.get_person_from_handle(person2_h)
person2.add_family_handle(fam_h)
self.db.commit_person(person2, self.trans)
fam_h = self.db.add_family(fam, self.trans)
self.generated_families.append(fam_h)
fam = self.db.commit_family(fam, self.trans)
if person1_h:
person1 = self.db.get_person_from_handle(person1_h)
person1.add_family_handle(fam_h)
self.db.commit_person(person1, self.trans)
if person2_h:
person2 = self.db.get_person_from_handle(person2_h)
person2.add_family_handle(fam_h)
self.db.commit_person(person2, self.trans)

lastname = person1.get_primary_name().get_surname()
lastname = person1.get_primary_name().get_surname()

for i in range(0, _randint(1, 10)):
if self.person_count > self.max_person_count:
break
if alive_in_year:
child_h = self.generate_person(
None, lastname,
alive_in_year=alive_in_year +
_randint(16 + 2 * i, 30 + 2 * i))
else:
child_h = self.generate_person(None, lastname)
(born, died) = self.person_dates[child_h]
alive_in_year = born
fam = self.db.get_family_from_handle(fam_h)
child_ref = ChildRef()
child_ref.set_reference_handle(child_h)
self.fill_object(child_ref)
fam.add_child_ref(child_ref)
self.db.commit_family(fam, self.trans)
child = self.db.get_person_from_handle(child_h)
child.add_parent_family_handle(fam_h)
self.db.commit_person(child, self.trans)
if _randint(0, 3) > 0:
self.persons_todo.append(child_h)
for i in range(0, _randint(1, 10)):
if self.person_count > self.max_person_count:
break
if alive_in_year:
child_h = self.generate_person(
None, lastname,
alive_in_year=alive_in_year +
_randint(16 + 2 * i, 30 + 2 * i))
else:
child_h = self.generate_person(None, lastname)
(born, died) = self.person_dates[child_h]
alive_in_year = born
fam = self.db.get_family_from_handle(fam_h)
child_ref = ChildRef()
child_ref.set_reference_handle(child_h)
self.fill_object(child_ref)
fam.add_child_ref(child_ref)
self.db.commit_family(fam, self.trans)
child = self.db.get_person_from_handle(child_h)
child.add_parent_family_handle(fam_h)
self.db.commit_person(child, self.trans)
if _randint(0, 3) > 0:
self.persons_todo.append(child_h)

def generate_parents(self, child_h):
""" Add parents to a person, if not present already"""
Expand Down Expand Up @@ -1666,28 +1697,26 @@ def generate_parents(self, child_h):
if _randint(0, 2) > 1:
self.parents_todo.append(person2_h)

with DbTxn(_("Testcase generator step %d") % self.transaction_count,
self.db) as self.trans:
self.transaction_count += 1
fam = Family()
self.add_defaults(fam)
fam.set_father_handle(person1_h)
fam.set_mother_handle(person2_h)
child_ref = ChildRef()
child_ref.set_reference_handle(child_h)
self.fill_object(child_ref)
fam.add_child_ref(child_ref)
fam_h = self.db.add_family(fam, self.trans)
self.generated_families.append(fam_h)
fam = self.db.commit_family(fam, self.trans)
person1 = self.db.get_person_from_handle(person1_h)
person1.add_family_handle(fam_h)
self.db.commit_person(person1, self.trans)
person2 = self.db.get_person_from_handle(person2_h)
person2.add_family_handle(fam_h)
self.db.commit_person(person2, self.trans)
child.add_parent_family_handle(fam_h)
self.db.commit_person(child, self.trans)
self.transaction_count += 1
fam = Family()
self.add_defaults(fam)
fam.set_father_handle(person1_h)
fam.set_mother_handle(person2_h)
child_ref = ChildRef()
child_ref.set_reference_handle(child_h)
self.fill_object(child_ref)
fam.add_child_ref(child_ref)
fam_h = self.db.add_family(fam, self.trans)
self.generated_families.append(fam_h)
fam = self.db.commit_family(fam, self.trans)
person1 = self.db.get_person_from_handle(person1_h)
person1.add_family_handle(fam_h)
self.db.commit_person(person1, self.trans)
person2 = self.db.get_person_from_handle(person2_h)
person2.add_family_handle(fam_h)
self.db.commit_person(person2, self.trans)
child.add_parent_family_handle(fam_h)
self.db.commit_person(child, self.trans)

def generate_tags(self):
""" Make up some odd tags """
Expand Down Expand Up @@ -2085,7 +2114,7 @@ def generate_place(self):
# skip some levels in the place hierarchy
continue
place = Place()
place.set_type(PlaceType(type_num))
place.set_type(self.PLACETYPES[type_num])
if parent_handle is not None:
self.add_parent_place(place, parent_handle)
if type_num > 1 and _randint(1, 3) == 1:
Expand All @@ -2094,6 +2123,7 @@ def generate_place(self):
if parent_handle is not None:
self.add_parent_place(place, parent_handle)
self.fill_object(place)
place.group = place.get_type().get_probable_group()
self.db.add_place(place, self.trans)
parent_handle = place.get_handle()
self.generated_places.append(place.get_handle())
Expand Down

0 comments on commit 7d5ecdc

Please sign in to comment.