test_entity_occurrence.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from django.core.exceptions import ObjectDoesNotExist
  2. from .manager_case import ManagerTestCase
  3. from .factories import (
  4. EntityFactory, EntityKindFactory,
  5. TextSegmentFactory, RelationFactory,
  6. EntityOccurrenceFactory
  7. )
  8. from iepy.data.models import TextSegment
  9. class TestEntityOccurrences(ManagerTestCase):
  10. def setUp(self):
  11. self.k_person = EntityKindFactory(name='person')
  12. self.k_location = EntityKindFactory(name='location')
  13. self.e_john = EntityFactory(key='john', kind=self.k_person)
  14. self.e_bob = EntityFactory(key='bob', kind=self.k_person)
  15. self.e_argentina = EntityFactory(key='argentina', kind=self.k_location)
  16. self.e_germany = EntityFactory(key='germany', kind=self.k_location)
  17. self.e_australia = EntityFactory(key='australia', kind=self.k_location)
  18. self.person_location_relation = RelationFactory(
  19. left_entity_kind=self.k_person,
  20. right_entity_kind=self.k_location
  21. )
  22. def create_segment_with_eos(self, entities):
  23. segment = TextSegmentFactory()
  24. doc = segment.document
  25. offset = 0
  26. eos = []
  27. for entity in entities:
  28. eo = EntityOccurrenceFactory(
  29. document=doc, entity=entity,
  30. offset=offset, offset_end=offset+1
  31. )
  32. eos.append(eo)
  33. offset += 2
  34. segment.entity_occurrences.add(eo)
  35. return segment, eos
  36. def test_delete_removes_one_evidences(self):
  37. segment, eos = self.create_segment_with_eos([self.e_john, self.e_argentina])
  38. evidences_before = segment.get_evidences_for_relation(self.person_location_relation)
  39. self.assertEqual(len(list(evidences_before)), 1)
  40. eo = eos[0]
  41. eo.delete()
  42. del segment._hydrated_eos # Erase segment cache
  43. evidences_after = segment.get_evidences_for_relation(self.person_location_relation)
  44. self.assertEqual(len(list(evidences_after)), 0)
  45. def test_delete_removes_multiple_evidences(self):
  46. segment, eos = self.create_segment_with_eos([
  47. self.e_john, self.e_bob, self.e_argentina, self.e_germany
  48. ])
  49. evidences_before = segment.get_evidences_for_relation(self.person_location_relation)
  50. self.assertEqual(len(list(evidences_before)), 4) # each person with each location
  51. eo = eos[0]
  52. eo.delete()
  53. del segment._hydrated_eos # Erase segment cache
  54. evidences_after = segment.get_evidences_for_relation(self.person_location_relation)
  55. self.assertEqual(len(list(evidences_after)), 2) # only bob with each location
  56. def test_delete_eo_removes_segment(self):
  57. # We create a segment with only two entity occurrences,
  58. # deleting one should delete the segment as well
  59. segment, eos = self.create_segment_with_eos([
  60. self.e_john, self.e_argentina
  61. ])
  62. self.assertIsNotNone(TextSegment.objects.get(pk=segment.id))
  63. self.e_john.delete()
  64. with self.assertRaises(ObjectDoesNotExist):
  65. TextSegment.objects.get(pk=segment.id)
  66. def test_delete_eo_does_not_removes_segment(self):
  67. # We create a segment with only two entity occurrences,
  68. # deleting one should delete the segment as well
  69. segment, eos = self.create_segment_with_eos([
  70. self.e_john, self.e_bob, self.e_argentina, self.e_germany
  71. ])
  72. self.assertIsNotNone(TextSegment.objects.get(pk=segment.id))
  73. self.e_john.delete()
  74. self.assertIsNotNone(TextSegment.objects.get(pk=segment.id))