Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize references between places and events, and add test coverage. #50

Merged
merged 2 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions betty/ancestry.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,15 @@ def __init__(self, entity_id: str, name: str = None):
Entity.__init__(self, entity_id)
self._name = name
self._coordinates = None
self._events = set()

def handle_event_addition(event: Event):
event.place = self

def handle_event_removal(event: Event):
event.place = None

self._events = EventHandlingSet(
handle_event_addition, handle_event_removal)

@property
def label(self) -> str:
Expand All @@ -210,7 +218,7 @@ def coordinates(self, coordinates: Coordinates):
self._coordinates = coordinates

@property
def events(self):
def events(self) -> Iterable:
return self._events


Expand Down Expand Up @@ -261,8 +269,13 @@ def place(self) -> Optional[Place]:
return self._place

@place.setter
def place(self, place: Place):
def place(self, place: Optional[Place]):
previous_place = self._place
self._place = place
if previous_place is not None:
previous_place.events.remove(self)
if place is not None:
place.events.add(self)

@property
def type(self):
Expand Down
4 changes: 1 addition & 3 deletions betty/gramps.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ def _parse_event(places: Dict[str, Place], element: Element) -> Tuple[str, Event
# Parse the event place.
place_handle = xpath1(element, './ns:place/@hlink')
if place_handle:
place = places[place_handle]
event.place = place
place.events.add(event)
event.place = places[place_handle]

return handle, event
23 changes: 22 additions & 1 deletion betty/tests/test_ancestry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from betty.ancestry import EventHandlingSet, Person, Family
from betty.ancestry import EventHandlingSet, Person, Family, Event, Place


class EventHandlingSetTest(TestCase):
Expand Down Expand Up @@ -109,3 +109,24 @@ def test_children_should_sync_references(self):
sut.children.remove(child)
self.assertEquals([], list(sut.children))
self.assertEquals(None, child.descendant_family)


class PlaceTest(TestCase):
def test_events_should_sync_references(self):
event = Event('1', Event.Type.BIRTH)
sut = Place('1')
sut.events.add(event)
self.assertIn(event, sut.events)
self.assertEquals(sut, event.place)


class EventTest(TestCase):
def test_place_should_sync_references(self):
place = Place('1')
sut = Event('1', Event.Type.BIRTH)
sut.place = place
self.assertEquals(place, sut.place)
self.assertIn(sut, place.events)
sut.place = None
self.assertEquals(None, sut.place)
self.assertNotIn(sut, place.events)
8 changes: 7 additions & 1 deletion betty/tests/test_gramps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def test_place_should_include_coordinates(self):
self.assertEquals('52.366667', place.coordinates.latitude)
self.assertEquals('4.9', place.coordinates.longitude)

def test_place_should_include_events(self):
place = self.ancestry.places['P0000']
event = self.ancestry.events['E0000']
self.assertIn(event, place.events)


class ParsePersonTest(GrampsTestCase):
def test_person_should_include_individual_name(self):
Expand Down Expand Up @@ -55,7 +60,8 @@ def test_family_should_include_children(self):
class ParseEventTest(GrampsTestCase):
def test_event_should_include_place(self):
event = self.ancestry.events['E0000']
self.assertEquals('P0000', event.place.id)
place = self.ancestry.places['P0000']
self.assertEquals(place, event.place)

def test_event_should_include_date(self):
event = self.ancestry.events['E0000']
Expand Down