def setUp(self): self.family = Family() self.family.save() self.daughter = Node(Person.objects.create(name='daughter', gender='F', family_id=self.family.id)) self.person = Node(Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) self.mum = Node(Person.objects.create(name='mum', gender='F', family_id=self.family.id)) self.dad = Node(Person.objects.create(name='dad', gender='M', family_id=self.family.id)) self.grandma = Node(Person.objects.create(name='grandma', gender='F', family_id=self.family.id)) self.grandpa = Node(Person.objects.create(name='grandpa', gender='M', family_id=self.family.id)) self.great_grandma = Node(Person.objects.create(name='great grandma', gender='F', family_id=self.family.id)) self.daughter.relations.append((RAISED_BY, self.person)) self.person.relations.append((RAISED, self.daughter)) self.person.relations.append((RAISED_BY, self.mum)) self.mum.relations.append((RAISED, self.person)) self.mum.relations.append((RAISED_BY, self.grandma)) self.mum.relations.append((RAISED_BY, self.grandpa)) self.person.relations.append((RAISED_BY, self.dad)) self.dad.relations.append((RAISED, self.person)) self.grandma.relations.append((RAISED, self.mum)) self.grandma.relations.append((PARTNERED, self.grandpa)) self.grandpa.relations.append((PARTNERED, self.grandma)) self.grandpa.relations.append((RAISED, self.mum)) self.grandma.relations.append((RAISED_BY, self.great_grandma)) self.great_grandma.relations.append((RAISED, self.grandma))
class BiographyTestCase(TestCase): # pragma: no cover ''' This defines all the tests for all model logic for a Biography ''' def setUp(self): self.family = Family() self.family.save() super(BiographyTestCase, self).setUp() def test_biography_can_be_written_in_non_latic_characters(self): """ Tests that a biography name can be written in non-latin characters """ #Traditional Chinese person = Person(name='nonlatin', gender='M', email='*****@*****.**', family_id=self.family.id, biography='傳記') person.save() #Simplified Chinese person.biography = '传记' person.save() #Polish person.biography = 'zabójstwo' person.save()
def test_get_relation_name_not_authenticated(self): family = Family() family.save() User.objects.create_user(email='*****@*****.**', password='******', name='Margaret Knight', family=family) person = Person.objects.create(name='patient zero', gender='M', family_id=family.id) wife = Person.objects.create(name='wife', gender='F', family_id=family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/relation_name/{0}/{1}/'.format(person.id, wife.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content)
def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Phil Collins', family=self.family) self.person = Person(name='Phil Collins', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) self.gallery2 = Gallery.objects.create(title="test_gallery2", family_id=self.family.id) self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user( email='*****@*****.**', password='******', name='Phillip Bailey', family=self.family2) self.person2 = Person(name='Phillip Bailey', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() self.test_image = os.path.join(settings.BASE_DIR, 'gallery/tests/test_image.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination)
def setUp(self): self.family = Family() self.family.save() self.person = Person.objects.create(name='patient zero', gender='M', family_id=self.family.id) self.wife = Person.objects.create(name='wife', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.wife, to_person=self.person, relation_type=PARTNERED) self.son = Person.objects.create(name='son', gender='M', family_id=self.family.id) Relation.objects.create(from_person=self.person, to_person=self.son, relation_type=RAISED) self.daughter = Person.objects.create(name='daughter', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.person, to_person=self.daughter, relation_type=RAISED) self.mum = Person.objects.create(name='mum', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.mum, to_person=self.person, relation_type=RAISED) self.dad = Person.objects.create(name='dad', gender='M', family_id=self.family.id) Relation.objects.create(from_person=self.dad, to_person=self.person, relation_type=RAISED) self.grandma = Person.objects.create(name='grandma', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.grandma, to_person=self.mum, relation_type=RAISED) self.grandson = Person.objects.create(name='grandson', gender='M', family_id=self.family.id) Relation.objects.create(from_person=self.son, to_person=self.grandson, relation_type=RAISED)
def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Matt Tong', family=self.family) self.person = Person(name='Matt Tong', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.new_person = Person(name='Taka Hirose', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.new_person.save() self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Herman Li', family=self.family2) self.person2 = Person(name='Herman Li', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() super(InviteEmailApiTestCase, self).setUp() self.invite = EmailConfirmation( email_address='*****@*****.**', person_id=self.person.id, user_who_invited_person_id=self.user.id, sent=timezone.now()) self.invite.save()
def test_get_ancestors_single_person(self): ''' Tests get ancestors when only one person ''' another_family = Family() another_family.save() person = Person.objects.create(name='patient zero a', gender='M', hierarchy_score=100, family_id=another_family.id) result, relations = tree_service.get_ancestors(person) self.assertEqual(1, len(result))
def test_get_relation_name_not_available_to_another_family(self): family = Family() family.save() family2 = Family() family2.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Margaret Knight', family=family2) person = Person.objects.create(name='patient zero', gender='M', family_id=family.id) wife = Person.objects.create(name='wife', gender='F', family_id=family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=user) url = '/api/relation_name/{0}/{1}/'.format(person.id, wife.id) response = client.get(url, format='json') self.assertFalse(b"Wife" in response.content)
def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Grace Hopper', family_id = self.family.id) self.person = Person(name='Grace Hopper', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save()
def test_get_navigable_relations(self): my_family = Family() my_family.save() person = Person.objects.create(name='patient zero', gender='M',hierarchy_score=100, family_id=my_family.id) wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=my_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M',hierarchy_score=101, family_id=my_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter', gender='F',hierarchy_score=101, family_id=my_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) paths_by_person = Relation.objects.get_navigable_relations(my_family.id) self.assertEqual(3, len(paths_by_person[person.id])) self.assertEqual(1, len(paths_by_person[son.id])) self.assertEqual(Relation, type(paths_by_person[son.id][0]))
def test_add_related_partner(self): ''' Tests the _add_related function adds in a partner ''' another_family = Family() another_family.save() grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=100, family_id=another_family.id) grandad = Person.objects.create(name='grandad', gender='M', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=grandad, relation_type=PARTNERED) list_of_people_by_hierachy = {} list_of_people_by_hierachy[100] = [] list_of_people_by_hierachy[100].append(grandma) people_included = {} people_included[grandma.id] = grandma people_by_id = {grandma.id: grandma, grandad.id: grandad} relations_by_person = Relation.objects.get_navigable_relations( another_family.id) tree_service._add_related(grandma, people_by_id, list_of_people_by_hierachy, people_included, relations_by_person) self.assertEqual(grandma.id, list_of_people_by_hierachy[100][0].id) self.assertEqual(grandad.id, list_of_people_by_hierachy[100][1].id)
def test_add_related_son(self): ''' Tests the _add_related function adds in a son ''' another_family = Family() another_family.save() mother = Person.objects.create(name='mother', gender='F', hierarchy_score=100, family_id=another_family.id) son = Person.objects.create(name='son', gender='M', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=mother, to_person=son, relation_type=RAISED_BY) list_of_people_by_hierachy = {} list_of_people_by_hierachy[100] = [] list_of_people_by_hierachy[100].append(mother) people_included = {} people_included[mother.id] = mother people_by_id = {mother.id: mother, son.id: son} relations_by_person = Relation.objects.get_navigable_relations( another_family.id) tree_service._add_related(mother, people_by_id, list_of_people_by_hierachy, people_included, relations_by_person) self.assertEqual(mother.id, list_of_people_by_hierachy[100][0].id) self.assertEqual(son.id, list_of_people_by_hierachy[100][1].id)
class PersonTestCase(TestCase): ''' This defines all the tests for all model logic for a Person ''' def setUp(self): self.family = Family() self.family.save() super(PersonTestCase, self).setUp() def test_update_user_when_email_changed(self): ''' Tests that a user is updated when a person's email is modified ''' user = User.objects.create(email='*****@*****.**', password='******', name='John Wong') person = Person(name='John Wong', gender='M', email='*****@*****.**', family_id=self.family.id, language='pl', user_id=user.id) person.save() person.email = '*****@*****.**' person.create_update_user() self.assertEqual(1, User.objects.filter(email='*****@*****.**').count()) self.assertEqual(0, User.objects.filter(email='*****@*****.**').count()) self.assertEqual(person.user_id, User.objects.get(email='*****@*****.**').id) self.assertEqual(person.family_id, User.objects.get(email='*****@*****.**').family_id) self.assertEqual('pl', User.objects.get(email='*****@*****.**').language) def test_person_name_can_be_in_non_latic_characters(self): ''' Tests that a users name can be written in non-latin characters ''' #Traditional Chinese person = Person(name='實驗', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Simplified Chinese person = Person(name='实验', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Polish person = Person(name='kiełbasa', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() def test_hierachy_score_set_for_adding_child(self): ''' Ensure that hierachy score is set when adding the child of a parent the relation is not passed to the function ''' parent = Person(name='parent', gender='M', hierarchy_score = 100, family_id=self.family.id) parent.save() child = Person(name='child', gender='M', family_id=self.family.id) child.save() relation = Relation(from_person_id = parent.id, to_person_id = child.id, relation_type = RAISED) relation.save() child.set_hierarchy_score() self.assertEqual(101, child.hierarchy_score) def test_hierachy_score_set_for_adding_child_with_relation_as_param(self): ''' Ensure that hierachy score is set when adding the child of a parent the relation is passed to the function ''' parent = Person(name='parent', gender='M', hierarchy_score = 100, family_id=self.family.id) parent.save() child = Person(name='child', gender='M', family_id=self.family.id) child.save() relation = Relation(from_person_id = parent.id, to_person_id = child.id, relation_type = RAISED) child.set_hierarchy_score(relation) self.assertEqual(101, child.hierarchy_score) def test_hierachy_score_set_for_adding_parent(self): ''' Ensure that hierachy score is set when adding the parent of a child the relation is not passed to the function ''' parent = Person(name='parent', gender='M', family_id=self.family.id) parent.save() child = Person(name='child', gender='M', hierarchy_score = 100, family_id=self.family.id) child.save() relation = Relation(from_person_id = parent.id, to_person_id = child.id, relation_type = RAISED) relation.save() parent.set_hierarchy_score() self.assertEqual(99, parent.hierarchy_score) def test_hierachy_score_set_for_adding_parent_with_relation_as_param(self): ''' Ensure that hierachy score is set when adding the parent of a child the relation is passed to the function ''' parent = Person(name='parent', gender='M', family_id=self.family.id) parent.save() child = Person(name='child', gender='M', hierarchy_score = 100, family_id=self.family.id) child.save() relation = Relation(from_person_id = parent.id, to_person_id = child.id, relation_type = RAISED) parent.set_hierarchy_score(relation) self.assertEqual(99, parent.hierarchy_score) def test_hierachy_score_set_for_adding_partner(self): ''' Ensure that hierachy score is set when adding a partner ''' husband = Person(name='husband', gender='M', family_id=self.family.id) husband.save() wife = Person(name='wife', gender='F', hierarchy_score = 75, family_id=self.family.id) wife.save() relation = Relation(from_person_id = husband.id, to_person_id = wife.id, relation_type = PARTNERED) relation.save() husband.set_hierarchy_score() self.assertEqual(75, husband.hierarchy_score) def test_geocode_address_UK(self): ''' Tests that the correct longitude and latitude are returned for a UK location ''' person = Person.objects.create(name='Kate Bush', gender='F', address='Bexleyheath, England', family_id=self.family.id) person.geocode_address() self.assertEqual(51.45, round(person.latitude,2)) self.assertEqual(0.14, round(person.longitude,2)) def test_geocode_address_China(self): ''' Tests that the correct longitude and latitude are returned for a location in China ''' person = Person.objects.create(name='Jackie Chan', gender='M', address='扯旗山 香港', family_id=self.family.id) person.geocode_address() self.assertEqual(22.28, round(person.latitude,2)) self.assertEqual(114.15, round(person.longitude,2)) def test_geocode_address_using_backup_UK(self): ''' Tests that the correct longitude and latitude are returned for a UK address using the backup geocoding service ''' person = Person.objects.create(name='Brian Blessed', gender='M', address='Mexborough, Yorkshire', family_id=self.family.id) person._geocode_address_using_backup() self.assertEqual(53.5, round(person.latitude,1)) self.assertEqual(-1.3, round(person.longitude,1)) def test_geocode_address_using_backup_China(self): ''' Tests that the correct longitude and latitude are returned for a location in China using the backup geocoding service ''' person = Person.objects.create(name='Sammo Hung', gender='M', address='星光大道 香港', family_id=self.family.id) person._geocode_address_using_backup() self.assertEqual(22.3, round(person.latitude,1)) self.assertEqual(114.2, round(person.longitude,1)) def test_set_hires_photo(self): ''' Tests that the function correctly sets sets the photo field on a person and converts an image. ''' from django.conf import settings #Copy test image to media area import shutil import os shutil.copy2(os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') person = Person(name='陳港生', gender='M', family_id=self.family.id) person.set_hires_photo('large_test_image.jpg') self.assertEqual('profile_photos/large_test_image.jpg', person.photo) #Check this image is valid image = Image.open(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') image.verify() width, height = image.size self.assertEqual(500, width) self.assertEqual(343, height) #maintains aspect ratio self.assertEqual('profile_photos/large_test_image.jpg', person.photo) #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') def test_crop_and_resize_photo(self): ''' Tests that the function correctly sets two thumbnails of correct size ''' from django.conf import settings #Copy test image to media area import shutil import os shutil.copy2(os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') person = Person(name='譚詠麟', gender='M', family_id=self.family.id) person.photo = 'profile_photos/large_test_image.jpg' person.crop_and_resize_photo(50, 50, 20, 20, 800) #Check small thumbnail is valid small = Image.open(settings.MEDIA_ROOT + str(person.small_thumbnail)) small.verify() width, height = small.size self.assertEqual(80, width) self.assertEqual(80, height) #Check large thumbnail is valid large = Image.open(settings.MEDIA_ROOT + str(person.large_thumbnail)) large.verify() width, height = large.size self.assertEqual(200, width) self.assertEqual(200, height) #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg')
def setUp(self): self.family = Family() self.family.save() super(PersonTestCase, self).setUp()
class PathToNameMapper3rdGenTestCase(TestCase): # pragma: no cover ''' This defines all the tests for the Path to name mapper ''' def setUp(self): self.family = Family() self.family.save() def test_maternal_great_grandmother(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) mum = Node( Person.objects.create(name='mum', gender='F', family_id=self.family.id)) maternal_grandmother = Node( Person.objects.create(name='maternal_grandmother', gender='F', family_id=self.family.id)) maternal_great_grandmother = Node( Person.objects.create(name='maternal_great_grandmother', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, maternal_great_grandmother) path.add_node(mum, RAISED_BY) path.add_node(maternal_grandmother, RAISED_BY) path.add_node(maternal_great_grandmother, RAISED_BY) result = get_name(path) self.assertEqual('Maternal Great Grandmother', result[0]) def test_paternal_great_grandfather(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) dad = Node( Person.objects.create(name='dad', gender='M', family_id=self.family.id)) paternal_grandmother = Node( Person.objects.create(name='paternal_grandmother', gender='F', family_id=self.family.id)) paternal_great_grandfather = Node( Person.objects.create(name='paternal_great_grandfather', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, paternal_great_grandfather) path.add_node(dad, RAISED_BY) path.add_node(paternal_grandmother, RAISED_BY) path.add_node(paternal_great_grandfather, RAISED_BY) result = get_name(path) self.assertEqual('Paternal Great Grandfather', result[0]) def test_great_grandfather(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) parent = Node( Person.objects.create(name='dad', gender='O', family_id=self.family.id)) paternal_grandmother = Node( Person.objects.create(name='paternal_grandmother', gender='F', family_id=self.family.id)) great_grandfather = Node( Person.objects.create(name='great_grandfather', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, great_grandfather) path.add_node(parent, RAISED_BY) path.add_node(paternal_grandmother, RAISED_BY) path.add_node(great_grandfather, RAISED_BY) result = get_name(path) self.assertTrue('Paternal Great Grandfather' in result) self.assertTrue('Maternal Great Grandfather' in result) def test_step_grandparent(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) mother = Node( Person.objects.create(name='mother', gender='F', family_id=self.family.id)) grandparent = Node( Person.objects.create(name='grandparent', gender='F', family_id=self.family.id)) step_grandparent = Node( Person.objects.create(name='step_grandparent', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, step_grandparent) path.add_node(mother, RAISED_BY) path.add_node(grandparent, RAISED_BY) path.add_node(step_grandparent, PARTNERED) result = get_name(path) self.assertEqual([], result) def test_mothers_elder_sister(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) mother = Node( Person.objects.create(name='mother', gender='F', family_id=self.family.id, birth_year=1990)) grandparent = Node( Person.objects.create(name='grandparent', gender='F', family_id=self.family.id)) aunt = Node( Person.objects.create(name='aunt', gender='F', family_id=self.family.id, birth_year=1988)) path = Path() path.set_goals(person, aunt) path.add_node(mother, RAISED_BY) path.add_node(grandparent, RAISED_BY) path.add_node(aunt, RAISED) result = get_name(path) self.assertTrue("Mother's Elder Sister" in result) self.assertTrue("Mother's Sister" in result) def test_fathers_younger_brother(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) father = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id, birth_year=1990)) grandparent = Node( Person.objects.create(name='grandparent', gender='F', family_id=self.family.id)) uncle = Node( Person.objects.create(name='uncle', gender='M', family_id=self.family.id, birth_year=1991)) path = Path() path.set_goals(person, uncle) path.add_node(father, RAISED_BY) path.add_node(grandparent, RAISED_BY) path.add_node(uncle, RAISED) result = get_name(path) self.assertTrue("Father's Younger Brother" in result) self.assertTrue("Father's Brother" in result) def test_fathers_sister(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) father = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id, birth_year=1990)) grandparent = Node( Person.objects.create(name='grandparent', gender='F', family_id=self.family.id)) aunt = Node( Person.objects.create(name='aunt', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, aunt) path.add_node(father, RAISED_BY) path.add_node(grandparent, RAISED_BY) path.add_node(aunt, RAISED) result = get_name(path) self.assertTrue("Father's Elder Sister" in result) self.assertTrue("Father's Younger Sister" in result) self.assertTrue("Father's Sister" in result) def test_elder_sisters_husband(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id, birth_year=1990)) father = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id)) sister = Node( Person.objects.create(name='sister', gender='F', family_id=self.family.id, birth_year=1989)) sisters_husband = Node( Person.objects.create(name='sisters_husband', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, sisters_husband) path.add_node(father, RAISED_BY) path.add_node(sister, RAISED) path.add_node(sisters_husband, PARTNERED) result = get_name(path) self.assertTrue("Elder Sibling's Husband" in result) def test_siblings_partner_(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) parent = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id)) sibling = Node( Person.objects.create(name='sibling', gender='O', family_id=self.family.id)) siblings_partner = Node( Person.objects.create(name='siblings_partner', gender='O', family_id=self.family.id)) path = Path() path.set_goals(person, siblings_partner) path.add_node(parent, RAISED_BY) path.add_node(sibling, RAISED) path.add_node(siblings_partner, PARTNERED) result = get_name(path) self.assertTrue("Elder Sibling's Husband" in result) self.assertTrue("Younger Sibling's Husband" in result) self.assertTrue("Elder Sibling's Wife" in result) self.assertTrue("Younger Sibling's Wife" in result) def test_brothers_daughter(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) parent = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id)) brother = Node( Person.objects.create(name='brother', gender='M', family_id=self.family.id)) brothers_daughter = Node( Person.objects.create(name='brothers_daughter', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, brothers_daughter) path.add_node(parent, RAISED_BY) path.add_node(brother, RAISED) path.add_node(brothers_daughter, RAISED) result = get_name(path) self.assertTrue("Brother's Daughter" in result) def test_sisters_child(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) parent = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id)) sister = Node( Person.objects.create(name='sister', gender='F', family_id=self.family.id)) sisters_child = Node( Person.objects.create(name='sisters_child', gender='O', family_id=self.family.id)) path = Path() path.set_goals(person, sisters_child) path.add_node(parent, RAISED_BY) path.add_node(sister, RAISED) path.add_node(sisters_child, RAISED) result = get_name(path) self.assertTrue("Sister's Daughter" in result) self.assertTrue("Sister's Son" in result) def test_wifes_younger_sister(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) wife = Node( Person.objects.create(name='wife', gender='F', family_id=self.family.id, birth_year=1990)) wifes_mother = Node( Person.objects.create(name='wifes_mother', gender='F', family_id=self.family.id)) wifes_sister = Node( Person.objects.create(name='wifes_sister', gender='F', family_id=self.family.id, birth_year=1998)) path = Path() path.set_goals(person, wifes_sister) path.add_node(wife, PARTNERED) path.add_node(wifes_mother, RAISED_BY) path.add_node(wifes_sister, RAISED) result = get_name(path) self.assertTrue("Wife's Younger Sister" in result) def test_husbands_elder_sister(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) husband = Node( Person.objects.create(name='husband', gender='M', family_id=self.family.id, birth_year=1990)) husbands_mother = Node( Person.objects.create(name='husbands_mother', gender='F', family_id=self.family.id)) husbands_sister = Node( Person.objects.create(name='husbands_sister', gender='F', family_id=self.family.id, birth_year=1988)) path = Path() path.set_goals(person, husbands_sister) path.add_node(husband, PARTNERED) path.add_node(husbands_mother, RAISED_BY) path.add_node(husbands_sister, RAISED) result = get_name(path) self.assertTrue("Husband's Elder Sister" in result) def test_partners_brother(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) partner = Node( Person.objects.create(name='partner', gender='O', family_id=self.family.id)) partners_mother = Node( Person.objects.create(name='partner_mother', gender='F', family_id=self.family.id)) partners_brother = Node( Person.objects.create(name='partner_brother', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, partners_brother) path.add_node(partner, PARTNERED) path.add_node(partners_mother, RAISED_BY) path.add_node(partners_brother, RAISED) result = get_name(path) self.assertTrue("Wife's Elder Brother" in result) self.assertTrue("Wife's Younger Brother" in result) self.assertTrue("Husband's Elder Brother" in result) self.assertTrue("Husband's Younger Brother" in result) def test_childs_mother_in_law(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) child = Node( Person.objects.create(name='child', gender='O', family_id=self.family.id)) childs_partner = Node( Person.objects.create(name='childs_partner', gender='O', family_id=self.family.id)) childs_partners_mother = Node( Person.objects.create(name='childs_partners_mother', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, childs_partners_mother) path.add_node(child, RAISED) path.add_node(childs_partner, PARTNERED) path.add_node(childs_partners_mother, RAISED_BY) result = get_name(path) self.assertTrue("Child's Mother In Law" in result) def test_female_great_grandchild_male_lineage(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) child = Node( Person.objects.create(name='child', gender='M', family_id=self.family.id)) grandchild = Node( Person.objects.create(name='grandchild', gender='M', family_id=self.family.id)) great_grandchild = Node( Person.objects.create(name='great_grandchild', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, great_grandchild) path.add_node(child, RAISED) path.add_node(grandchild, RAISED) path.add_node(great_grandchild, RAISED) result = get_name(path) self.assertTrue("Son's Granddaughter" in result) def test_male_great_grandchild_non_other_lineage(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) child = Node( Person.objects.create(name='child', gender='O', family_id=self.family.id)) grandchild = Node( Person.objects.create(name='grandchild', gender='M', family_id=self.family.id)) great_grandchild = Node( Person.objects.create(name='great_grandchild', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, great_grandchild) path.add_node(child, RAISED) path.add_node(grandchild, RAISED) path.add_node(great_grandchild, RAISED) result = get_name(path) self.assertTrue("Son's Grandson" in result) self.assertTrue("Daughter's Grandson" in result) def test_mothers_sisters_husband(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) mother = Node( Person.objects.create(name='mother', gender='F', family_id=self.family.id)) grandmother = Node( Person.objects.create(name='grandmother', gender='F', family_id=self.family.id)) aunt = Node( Person.objects.create(name='aunt', gender='F', family_id=self.family.id)) aunt_husband = Node( Person.objects.create(name='aunt husband', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, aunt_husband) path.add_node(mother, RAISED_BY) path.add_node(grandmother, RAISED_BY) path.add_node(aunt, RAISED) path.add_node(aunt_husband, PARTNERED) result = get_name(path) self.assertTrue("Mother's Sister's Husband" in result) def test_fathers_elder_sisters_husband(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) father = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id, birth_year=1992)) grandmother = Node( Person.objects.create(name='grandmother', gender='F', family_id=self.family.id)) aunt = Node( Person.objects.create(name='aunt', gender='F', family_id=self.family.id, birth_year=1990)) aunt_husband = Node( Person.objects.create(name='aunt husband', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, aunt_husband) path.add_node(father, RAISED_BY) path.add_node(grandmother, RAISED_BY) path.add_node(aunt, RAISED) path.add_node(aunt_husband, PARTNERED) result = get_name(path) self.assertEqual(["Father's Elder Sister's Husband"], result) def test_aunt_uncle_partner(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) parent = Node( Person.objects.create(name='parent', gender='O', family_id=self.family.id)) grandmother = Node( Person.objects.create(name='grandmother', gender='F', family_id=self.family.id)) aunt_uncle = Node( Person.objects.create(name='aunt_uncle', gender='OF', family_id=self.family.id)) aunt_uncle_partner = Node( Person.objects.create(name='aunt_uncle partner', gender='O', family_id=self.family.id)) path = Path() path.set_goals(person, aunt_uncle_partner) path.add_node(parent, RAISED_BY) path.add_node(grandmother, RAISED_BY) path.add_node(aunt_uncle, RAISED) path.add_node(aunt_uncle_partner, PARTNERED) result = get_name(path) self.assertTrue("Mother's Sister's Husband" in result) self.assertTrue("Mother's Brother's Wife" in result) self.assertTrue("Father's Elder Sister's Husband" in result) self.assertTrue("Father's Younger Brother's Wife" in result) def test_maternal_male_elder_cousin(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id, birth_year=2000)) mother = Node( Person.objects.create(name='mother', gender='F', family_id=self.family.id)) grandmother = Node( Person.objects.create(name='grandmother', gender='F', family_id=self.family.id)) aunt = Node( Person.objects.create(name='aunt', gender='F', family_id=self.family.id)) aunt_son = Node( Person.objects.create(name='aunt husband', gender='M', family_id=self.family.id, birth_year=1998)) path = Path() path.set_goals(person, aunt_son) path.add_node(mother, RAISED_BY) path.add_node(grandmother, RAISED_BY) path.add_node(aunt, RAISED) path.add_node(aunt_son, RAISED) result = get_name(path) self.assertTrue("Maternal Elder Male Cousin" in result) def test_paternal_cousin(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id, birth_year=2000)) father = Node( Person.objects.create(name='father', gender='M', family_id=self.family.id)) grandmother = Node( Person.objects.create(name='grandmother', gender='F', family_id=self.family.id)) aunt = Node( Person.objects.create(name='aunt', gender='F', family_id=self.family.id)) cousin = Node( Person.objects.create(name='aunt husband', gender='O', family_id=self.family.id)) path = Path() path.set_goals(person, cousin) path.add_node(father, RAISED_BY) path.add_node(grandmother, RAISED_BY) path.add_node(aunt, RAISED) path.add_node(cousin, RAISED) result = get_name(path) self.assertTrue("Paternal Elder Male Cousin" in result) self.assertTrue("Paternal Younger Male Cousin" in result) self.assertTrue("Paternal Elder Female Cousin" in result) self.assertTrue("Paternal Younger Female Cousin" in result) def test_maternal_younger_cousin(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id, birth_year=2000)) mother = Node( Person.objects.create(name='mother', gender='F', family_id=self.family.id)) grandmother = Node( Person.objects.create(name='grandmother', gender='F', family_id=self.family.id)) aunt = Node( Person.objects.create(name='aunt', gender='F', family_id=self.family.id)) cousin = Node( Person.objects.create(name='aunt husband', gender='O', family_id=self.family.id, birth_year=2005)) path = Path() path.set_goals(person, cousin) path.add_node(mother, RAISED_BY) path.add_node(grandmother, RAISED_BY) path.add_node(aunt, RAISED) path.add_node(cousin, RAISED) result = get_name(path) self.assertTrue("Maternal Younger Male Cousin" in result) self.assertTrue("Maternal Younger Female Cousin" in result)
def setUp(self): self.family = Family() self.family.save() super(RelationTestCase, self).setUp()
class TreeRelationSuggestionService(TestCase): # pragma: no cover ''' This defines all the tests for the relation suggestion service ''' def setUp(self): self.family = Family() self.family.save() def test_suggests_partner_of_parent(self): ''' Tests service suggests that darth vader is the father of luke skywalker given that padme and darth were partnered and padme is lukes mother ''' luke = Person.objects.create(name='luke skywalker', gender='M', family_id=self.family.id) darth = Person.objects.create(name='darth vader', gender='M', family_id=self.family.id) padme = Person.objects.create(name='padmé amidala', gender='F', family_id=self.family.id) Relation.objects.create(from_person=padme, to_person=darth, relation_type=PARTNERED) Relation.objects.create(from_person=padme, to_person=luke, relation_type=RAISED) suggestions = relation_suggestion_service.get_relation_suggestions( luke) #I am your father self.assertEqual(luke.id, suggestions[0].from_person_id) self.assertEqual(darth.id, suggestions[0].to_person_id) #NOOOO! self.assertEqual(RAISED_BY, suggestions[0].relation_type) suggested_relation, suggested_person = relation_suggestion_service.get_first_relation_suggestion( luke) self.assertEqual('darth vader', suggested_person.name) self.assertEqual(RAISED_BY, suggested_relation.relation_type) def test_suggests_child_of_partner(self): ''' Tests service suggests that leia organa is the child of darth vader given that padme and darth were partnered and padme raise leia ''' darth = Person.objects.create(name='darth vader', gender='M', family_id=self.family.id) padme = Person.objects.create(name='padmé amidala', gender='F', family_id=self.family.id) leia = Person.objects.create(name='leia organa', gender='F', family_id=self.family.id) Relation.objects.create(from_person=padme, to_person=darth, relation_type=PARTNERED) Relation.objects.create(from_person=padme, to_person=leia, relation_type=RAISED) suggestions = relation_suggestion_service.get_relation_suggestions( darth) #He is our last hope #No there is another self.assertEqual(darth.id, suggestions[0].from_person_id) self.assertEqual(leia.id, suggestions[0].to_person_id) self.assertEqual(RAISED, suggestions[0].relation_type) def test_suggests_parent_of_son(self): ''' Tests service suggests that padmé amidala and darth were partners give both raise luke skywalker ''' darth = Person.objects.create(name='darth vader', gender='M', family_id=self.family.id) padme = Person.objects.create(name='padmé amidala', gender='F', family_id=self.family.id) luke = Person.objects.create(name='luke skywalker', gender='F', family_id=self.family.id) Relation.objects.create(from_person=darth, to_person=luke, relation_type=RAISED) Relation.objects.create(from_person=padme, to_person=luke, relation_type=RAISED) suggestions = relation_suggestion_service.get_relation_suggestions( darth) #Where is Padmé? #You killed her. #NOOOO! self.assertEqual(darth.id, suggestions[0].from_person_id) self.assertEqual(padme.id, suggestions[0].to_person_id) self.assertEqual(PARTNERED, suggestions[0].relation_type) def test_does_not_suggest_existing_relations(self): ''' Tests service does not suggest existing relations ''' darth = Person.objects.create(name='darth vader', gender='M', family_id=self.family.id) padme = Person.objects.create(name='padmé amidala', gender='F', family_id=self.family.id) luke = Person.objects.create(name='luke skywalker', gender='F', family_id=self.family.id) leia = Person.objects.create(name='leia organa', gender='F', family_id=self.family.id) Relation.objects.create(from_person=darth, to_person=luke, relation_type=RAISED) Relation.objects.create(from_person=padme, to_person=darth, relation_type=RAISED) Relation.objects.create(from_person=padme, to_person=luke, relation_type=RAISED) Relation.objects.create(from_person=padme, to_person=leia, relation_type=RAISED) Relation.objects.create(from_person=darth, to_person=luke, relation_type=RAISED) Relation.objects.create(from_person=darth, to_person=leia, relation_type=RAISED) suggestions = relation_suggestion_service.get_relation_suggestions( darth) self.assertEqual(0, len(suggestions))
class RelationTestCase(TestCase): ''' This defines all the tests for all model logic for a Relation ''' def setUp(self): self.family = Family() self.family.save() super(RelationTestCase, self).setUp() def test_raised_by_relation_resolves_to_raised(self): ''' Tests when a relation that a child is raised by parent, it resolves to parent raised child ''' parent = Person(name="parent", gender="F", family_id=self.family.id) parent.save() child = Person(name="child", gender="O", family_id=self.family.id) child.save() relation = Relation(from_person_id = child.id, to_person_id = parent.id, relation_type = RAISED_BY) relation.normalise() self.assertEqual(RAISED, relation.relation_type) self.assertEqual(parent.id, relation.from_person_id) self.assertEqual(child.id, relation.to_person_id) def test_partnered_male_to_female_resolves_to_female_to_male(self): ''' Tests when a male partners a female, it resolves to females partners a male ''' male = Person(name="male", gender="M", family_id=self.family.id) male.save() female = Person(name="female", gender="F", family_id=self.family.id) female.save() relation = Relation(from_person_id = male.id, to_person_id = female.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(male.id, relation.to_person_id) def test_partnered_other_to_female_resolves_to_female_to_other(self): ''' Tests when an other gender partners a female, it resolves to females partners an other gender ''' other = Person(name="other", gender="O", family_id=self.family.id) other.save() female = Person(name="female", gender="F", family_id=self.family.id) female.save() relation = Relation(from_person_id = other.id, to_person_id = female.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(other.id, relation.to_person_id) def test_partnered_other_to_male_resolves_to_male_to_other(self): ''' Tests when a male partners an other gender, it resolves to other partners a male ''' other = Person(name="other", gender="O", family_id=self.family.id) other.save() male = Person(name="male", gender="M", family_id=self.family.id) male.save() relation = Relation(from_person_id = other.id, to_person_id = male.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(male.id, relation.from_person_id) self.assertEqual(other.id, relation.to_person_id) def test_partnered_female_to_male_stays_the_same(self): ''' Tests when a female partners a male, the relationship does not change ''' female = Person(name="female", gender="F", family_id=self.family.id) female.save() male = Person(name="male", gender="M", family_id=self.family.id) male.save() relation = Relation(from_person_id = female.id, to_person_id = male.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(male.id, relation.to_person_id) def test_existing_relations_get_replaced(self): ''' Tests that when a relations is added between two people, it replaces any existing relations between them ''' existing1 = Person(name="existing1", gender="F", family_id=self.family.id) existing1.save() existing2 = Person(name="existing2", gender="F", family_id=self.family.id) existing2.save() relation = Relation(from_person_id = existing1.id, to_person_id = existing2.id, relation_type = RAISED) relation.save() new_relation = Relation(from_person_id = existing1.id, to_person_id = existing2.id, relation_type = PARTNERED) new_relation.save() self.assertEqual(1, Relation.objects.filter(from_person_id = existing1.id, to_person_id = existing2.id).count()) self.assertEqual(PARTNERED, Relation.objects.get(from_person_id = existing1.id, to_person_id = existing2.id).relation_type) def test_create_inverted_relation(self): ''' Tests an inverted relationship is correctly created when using manager function ''' from_person = Person(name="from_person", gender="F", family_id=self.family.id) from_person.save() to_person = Person(name="to_person", gender="F", family_id=self.family.id) to_person.save() relation = Relation(from_person_id = from_person.id, to_person_id = to_person.id, relation_type = RAISED) inverted = Relation.objects._create_inverted_relation(relation) self.assertEqual(from_person.id, inverted.to_person_id) self.assertEqual(to_person.id, inverted.from_person_id) self.assertEqual(RAISED_BY, inverted.relation_type) def test_get_navigable_relations(self): my_family = Family() my_family.save() person = Person.objects.create(name='patient zero', gender='M',hierarchy_score=100, family_id=my_family.id) wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=my_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M',hierarchy_score=101, family_id=my_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter', gender='F',hierarchy_score=101, family_id=my_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) paths_by_person = Relation.objects.get_navigable_relations(my_family.id) self.assertEqual(3, len(paths_by_person[person.id])) self.assertEqual(1, len(paths_by_person[son.id])) self.assertEqual(Relation, type(paths_by_person[son.id][0]))
class TreeServiceTestCase(TestCase): # pragma: no cover ''' This defines all the tests for the tree service ''' def setUp(self): self.family = Family() self.family.save() def test_get_related_data(self): ''' Tests the get_related function. ''' person = Person.objects.create(name='patient zero', gender='M', hierarchy_score=100, family_id=self.family.id) person.save() wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=self.family.id) wife.save() wife_to_person = Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) wife_to_person.save() son = Person.objects.create(name='son', gender='M', hierarchy_score=101, family_id=self.family.id) son.save() person_to_son = Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) person_to_son.save() daughter = Person.objects.create(name='daughter', gender='F', hierarchy_score=101, family_id=self.family.id) daughter.save() person_to_daughter = Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) person_to_daughter.save() mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=self.family.id) mum.save() mum_to_person = Relation.objects.create(from_person=mum, to_person=person, relation_type=RAISED) mum_to_person.save() dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=self.family.id) dad.save() dad_to_person = Relation.objects.create(from_person=dad, to_person=person, relation_type=RAISED) dad_to_person.save() grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=self.family.id) grandma.save() grandma_to_mum = Relation.objects.create(from_person=grandma, to_person=mum, relation_type=RAISED) grandma_to_mum.save() grandson = Person.objects.create(name='grandson', gender='M', hierarchy_score=102, family_id=self.family.id) grandson.save() son_to_grandson = Relation.objects.create(from_person=son, to_person=grandson, relation_type=RAISED) son_to_grandson.save() related_data = tree_service.get_related_data(person) self.assertEqual(related_data.people_upper[0].id, mum.id) self.assertEqual(related_data.people_upper[1].id, dad.id) self.assertEqual(len(list(related_data.people_upper)), 2) #raw query sets don't have a count function self.assertEqual(related_data.people_same_level[0].id, wife.id) self.assertEqual(related_data.people_lower[0].id, daughter.id) self.assertEqual(related_data.people_lower[1].id, son.id) self.assertEqual(len(list(related_data.people_lower)), 2) self.assertEqual(len(list(related_data.relations)), 5) def test_get_path_relations(self): ''' Tests the _get_path_relations function ''' person = Person.objects.create(name='patient zero', gender='M', hierarchy_score=100, family_id=self.family.id) person.save() wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=self.family.id) wife_to_person = Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M', hierarchy_score=101, family_id=self.family.id) person_to_son = Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) grandson = Person.objects.create(name='grandson', gender='M', hierarchy_score=102, family_id=self.family.id) son_to_grandson = Relation.objects.create(from_person=son, to_person=grandson, relation_type=RAISED) relations_by_person = {} relations_by_person[person.id] = [] relations_by_person[person.id].append(wife_to_person) relations_by_person[person.id].append(person_to_son) relations_by_person[son.id] = [] relations_by_person[son.id].append(son_to_grandson) route = [person.id, son.id, grandson.id] path_relations = tree_service._get_path_relations( route, relations_by_person) self.assertEqual(person_to_son.id, path_relations[0].id) self.assertEqual(son_to_grandson.id, path_relations[1].id) def test_add_related_partner(self): ''' Tests the _add_related function adds in a partner ''' another_family = Family() another_family.save() grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=100, family_id=another_family.id) grandad = Person.objects.create(name='grandad', gender='M', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=grandad, relation_type=PARTNERED) list_of_people_by_hierachy = {} list_of_people_by_hierachy[100] = [] list_of_people_by_hierachy[100].append(grandma) people_included = {} people_included[grandma.id] = grandma people_by_id = {grandma.id: grandma, grandad.id: grandad} relations_by_person = Relation.objects.get_navigable_relations( another_family.id) tree_service._add_related(grandma, people_by_id, list_of_people_by_hierachy, people_included, relations_by_person) self.assertEqual(grandma.id, list_of_people_by_hierachy[100][0].id) self.assertEqual(grandad.id, list_of_people_by_hierachy[100][1].id) def test_add_related_son(self): ''' Tests the _add_related function adds in a son ''' another_family = Family() another_family.save() mother = Person.objects.create(name='mother', gender='F', hierarchy_score=100, family_id=another_family.id) son = Person.objects.create(name='son', gender='M', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=mother, to_person=son, relation_type=RAISED_BY) list_of_people_by_hierachy = {} list_of_people_by_hierachy[100] = [] list_of_people_by_hierachy[100].append(mother) people_included = {} people_included[mother.id] = mother people_by_id = {mother.id: mother, son.id: son} relations_by_person = Relation.objects.get_navigable_relations( another_family.id) tree_service._add_related(mother, people_by_id, list_of_people_by_hierachy, people_included, relations_by_person) self.assertEqual(mother.id, list_of_people_by_hierachy[100][0].id) self.assertEqual(son.id, list_of_people_by_hierachy[100][1].id) def test_get_whole_tree(self): ''' Tests the get_whole_tree function. ''' another_family = Family() another_family.save() person = Person.objects.create(name='patient zero', gender='M', hierarchy_score=100, family_id=another_family.id) wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter', gender='F', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=mum, to_person=person, relation_type=RAISED) dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=dad, to_person=person, relation_type=RAISED) grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=mum, relation_type=RAISED) uncle = Person.objects.create(name='uncle', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=uncle, relation_type=RAISED) cousin = Person.objects.create(name='cousin', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=uncle, to_person=cousin, relation_type=RAISED) distant_nephew = Person.objects.create(name='distant_nephew', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=cousin, to_person=distant_nephew, relation_type=RAISED) result, relations = tree_service.get_whole_tree(another_family.id) self.assertEqual(1, len(result[98])) self.assertEqual(grandma.id, result[98][0].id) self.assertEqual(3, len(result[99])) self.assertEqual(mum.id, result[99][0].id) self.assertEqual(dad.id, result[99][1].id) self.assertEqual(uncle.id, result[99][2].id) self.assertEqual(3, len(result[100])) self.assertEqual(person.id, result[100][0].id) self.assertEqual(wife.id, result[100][1].id) self.assertEqual(cousin.id, result[100][2].id) self.assertEqual(3, len(result[101])) #Not sure how to maintain Female before Male #self.assertEqual(daughter.id, result[101][0].id) #self.assertEqual(son.id, result[101][1].id) self.assertEqual(distant_nephew.id, result[101][2].id) def test_get_descendants(self): ''' Tests the test_get_descendants function. ''' another_family = Family() another_family.save() person = Person.objects.create(name='patient zero d', gender='M', hierarchy_score=100, family_id=another_family.id) wife = Person.objects.create(name='wife d', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son d', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter d', gender='F', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) mum = Person.objects.create(name='mum d', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=mum, to_person=person, relation_type=RAISED) dad = Person.objects.create(name='dad d', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=dad, to_person=person, relation_type=RAISED) grandma = Person.objects.create(name='grandma d', gender='F', hierarchy_score=98, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=mum, relation_type=RAISED) uncle = Person.objects.create(name='uncle d', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=uncle, relation_type=RAISED) cousin = Person.objects.create(name='cousin d', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=uncle, to_person=cousin, relation_type=RAISED) distant_nephew = Person.objects.create(name='distant_nephew d', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=cousin, to_person=distant_nephew, relation_type=RAISED) result, relations = tree_service.get_descendants(grandma) self.assertEqual(1, len(result[98])) self.assertEqual(grandma.id, result[98][0].id) self.assertEqual(2, len(result[99])) self.assertEqual(mum.id, result[99][0].id) self.assertEqual(uncle.id, result[99][1].id) self.assertEqual(2, len(result[100])) self.assertEqual(person.id, result[100][0].id) self.assertEqual(cousin.id, result[100][1].id) self.assertEqual(3, len(result[101])) self.assertTrue(son in result[101]) self.assertTrue(daughter in result[101]) self.assertTrue(distant_nephew in result[101]) def test_get_ancestors(self): ''' Tests the test_get_descendants function. ''' another_family = Family() another_family.save() person = Person.objects.create(name='patient zero a', gender='M', hierarchy_score=100, family_id=another_family.id) wife = Person.objects.create(name='wife a', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son a', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) Relation.objects.create(from_person=wife, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter a', gender='F', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) mum = Person.objects.create(name='mum a', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=mum, to_person=person, relation_type=RAISED) dad = Person.objects.create(name='dad a', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=dad, to_person=person, relation_type=RAISED) grandma = Person.objects.create(name='grandma a', gender='F', hierarchy_score=98, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=mum, relation_type=RAISED) wifes_dad = Person.objects.create(name='wifes_dad a', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=wifes_dad, to_person=wife, relation_type=RAISED) uncle = Person.objects.create(name='uncle a', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=uncle, relation_type=RAISED) cousin = Person.objects.create(name='cousin a', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=uncle, to_person=cousin, relation_type=RAISED) distant_nephew = Person.objects.create(name='distant_nephew a', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=cousin, to_person=distant_nephew, relation_type=RAISED) result, relations = tree_service.get_ancestors(son) self.assertEqual(1, len(result[101])) self.assertTrue(son in result[101]) self.assertEqual(2, len(result[100])) self.assertTrue(person in result[100]) self.assertTrue(wife in result[100]) self.assertEqual(3, len(result[99])) self.assertTrue(mum in result[99]) self.assertTrue(wifes_dad in result[99]) self.assertTrue(dad in result[99]) self.assertEqual(1, len(result[98])) self.assertEqual(grandma.id, result[98][0].id) def test_get_ancestors_single_person(self): ''' Tests get ancestors when only one person ''' another_family = Family() another_family.save() person = Person.objects.create(name='patient zero a', gender='M', hierarchy_score=100, family_id=another_family.id) result, relations = tree_service.get_ancestors(person) self.assertEqual(1, len(result)) def test_get_shortest_path(self): ''' Tests the get_related_path function. ''' another_family = Family() another_family.save() person = Person.objects.create(name='patient zero', gender='M', hierarchy_score=100, family_id=another_family.id) wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter', gender='F', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=mum, to_person=person, relation_type=RAISED) dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=dad, to_person=person, relation_type=RAISED) grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=mum, relation_type=RAISED) aunt = Person.objects.create(name='aunt', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=aunt, relation_type=RAISED) cousin = Person.objects.create(name='cousin', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=aunt, to_person=cousin, relation_type=RAISED) other_cousin = Person.objects.create(name='other_cousin', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=aunt, to_person=other_cousin, relation_type=RAISED) distant_nephew = Person.objects.create(name='distant_nephew', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=cousin, to_person=distant_nephew, relation_type=RAISED) people, relations = tree_service.get_shortest_path( other_cousin, person) self.assertEqual(person.id, people[0].id) self.assertEqual(mum.id, people[1].id) self.assertEqual(grandma.id, people[2].id) self.assertEqual(aunt.id, people[3].id) self.assertEqual(other_cousin.id, people[4].id) self.assertEqual(RAISED_BY, relations[0].relation_type) self.assertEqual(RAISED_BY, relations[1].relation_type) self.assertEqual(RAISED, relations[2].relation_type) self.assertEqual(RAISED, relations[3].relation_type)
class PathToNameMapperTestCase(TestCase): # pragma: no cover ''' This defines all the tests for the Path to name mapper ''' def setUp(self): self.family = Family() self.family.save() def test_1_relation_apart(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) mum = Node( Person.objects.create(name='mum', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, mum) path.add_node(mum, RAISED_BY) result = get_name(path) self.assertEqual('Mother', result[0]) def test_maternal_grandmother(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) mum = Node( Person.objects.create(name='mum', gender='F', family_id=self.family.id)) paternal_grandmother = Node( Person.objects.create(name='paternal_grandmother', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, paternal_grandmother) path.add_node(mum, RAISED_BY) path.add_node(paternal_grandmother, RAISED_BY) result = get_name(path) self.assertEqual('Maternal Grandmother', result[0]) def test_paternal_grandfather(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) dad = Node( Person.objects.create(name='dad', gender='M', family_id=self.family.id)) paternal_grandfather = Node( Person.objects.create(name='paternal_grandfather', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, paternal_grandfather) path.add_node(dad, RAISED_BY) path.add_node(paternal_grandfather, RAISED_BY) result = get_name(path) self.assertEqual('Paternal Grandfather', result[0]) def test_generic_grandparent(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) parent = Node( Person.objects.create(name='parent', gender='O', family_id=self.family.id)) grandparent = Node( Person.objects.create(name='grandparent', gender='O', family_id=self.family.id)) path = Path() path.set_goals(person, grandparent) path.add_node(parent, RAISED_BY) path.add_node(grandparent, RAISED_BY) result = get_name(path) self.assertTrue('Paternal Grandfather' in result) self.assertTrue('Paternal Grandmother' in result) self.assertTrue('Maternal Grandfather' in result) self.assertTrue('Maternal Grandmother' in result) def test_stepmother(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) dad = Node( Person.objects.create(name='dad', gender='M', family_id=self.family.id)) stepmother = Node( Person.objects.create(name='stepmother', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, stepmother) path.add_node(dad, RAISED_BY) path.add_node(stepmother, PARTNERED) result = get_name(path) self.assertEqual('Stepmother', result[0]) def test_father_in_law_wifes_side(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id)) wife = Node( Person.objects.create(name='wife', gender='F', family_id=self.family.id)) father_in_law = Node( Person.objects.create(name='father_in_law', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, father_in_law) path.add_node(wife, PARTNERED) path.add_node(father_in_law, RAISED_BY) result = get_name(path) self.assertEqual("Wife's Father", result[0]) def test_elder_sister(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id, birth_year=1982)) dad = Node( Person.objects.create(name='dad', gender='M', family_id=self.family.id)) sister = Node( Person.objects.create(name='sister', gender='F', family_id=self.family.id, birth_year=1980)) path = Path() path.set_goals(person, sister) path.add_node(dad, RAISED_BY) path.add_node(sister, RAISED) result = get_name(path) self.assertEqual("Elder Sister", result[0]) def test_brother_unknown_age(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id, birth_year=1982)) dad = Node( Person.objects.create(name='dad', gender='M', family_id=self.family.id)) brother = Node( Person.objects.create(name='brother', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, brother) path.add_node(dad, RAISED_BY) path.add_node(brother, RAISED) result = get_name(path) self.assertTrue("Elder Brother" in result) self.assertTrue("Younger Brother" in result) def test_younger_sibling(self): person = Node( Person.objects.create(name='patient zero', gender='M', family_id=self.family.id, birth_year=1982)) dad = Node( Person.objects.create(name='dad', gender='M', family_id=self.family.id)) sibling = Node( Person.objects.create(name='sibling', gender='O', family_id=self.family.id, birth_year=1990)) path = Path() path.set_goals(person, sibling) path.add_node(dad, RAISED_BY) path.add_node(sibling, RAISED) result = get_name(path) self.assertTrue("Younger Brother" in result) self.assertTrue("Younger Sister" in result) def test_stepson(self): person = Node( Person.objects.create(name='patient zero', gender='F', family_id=self.family.id)) partner = Node( Person.objects.create(name='partner', gender='F', family_id=self.family.id)) stepson = Node( Person.objects.create(name='stepson', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, stepson) path.add_node(partner, PARTNERED) path.add_node(stepson, RAISED) result = get_name(path) self.assertTrue("Stepson" in result) def test_daughter_in_law(self): person = Node( Person.objects.create(name='patient zero', gender='F', family_id=self.family.id)) son = Node( Person.objects.create(name='son', gender='M', family_id=self.family.id)) daughter_in_law = Node( Person.objects.create(name='daughter in law', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, daughter_in_law) path.add_node(son, RAISED) path.add_node(daughter_in_law, PARTNERED) result = get_name(path) self.assertTrue("Daughter In Law" in result) def test_grandson_daughters_side(self): person = Node( Person.objects.create(name='patient zero', gender='F', family_id=self.family.id)) daughter = Node( Person.objects.create(name='daughter', gender='F', family_id=self.family.id)) grandson = Node( Person.objects.create(name='grandson', gender='M', family_id=self.family.id)) path = Path() path.set_goals(person, grandson) path.add_node(daughter, RAISED) path.add_node(grandson, RAISED) result = get_name(path) self.assertTrue("Grandson Daughter's Side" in result) def test_granddaughter_unknown_side(self): person = Node( Person.objects.create(name='patient zero', gender='F', family_id=self.family.id)) child = Node( Person.objects.create(name='child', gender='O', family_id=self.family.id)) granddaughter = Node( Person.objects.create(name='granddaughter', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, granddaughter) path.add_node(child, RAISED) path.add_node(granddaughter, RAISED) result = get_name(path) self.assertTrue("Granddaughter Daughter's Side" in result) self.assertTrue("Granddaughter Son's Side" in result)
def setUp(self): self.family = Family() self.family.save() self.family2 = Family() self.family2.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Edith Clarke', family=self.family) self.person1 = Person(name='Edith Clarke', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person1.save() self.person2 = Person(name='someone else', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person2.save() self.person3 = Person(name='another person', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person3.save() self.relation1 = Relation.objects.create(from_person=self.person1, to_person=self.person2, relation_type=PARTNERED) self.relation1.save() self.relation2 = Relation.objects.create(from_person=self.person1, to_person=self.person3, relation_type=RAISED) self.relation2.save() self.otherFamilyUser = User.objects.create_user( email='*****@*****.**', password='******', name='Another Family', family=self.family2) self.otherFamilyPerson = Person(name='another family', gender='O', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user.id) self.otherFamilyPerson.save() self.otherFamilyPerson2 = Person(name='another family 2', gender='O', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user.id) self.otherFamilyPerson2.save() self.relation3 = Relation.objects.create( from_person=self.otherFamilyPerson, to_person=self.otherFamilyPerson2, relation_type=PARTNERED) self.relation3.save() super(RelationApiTestCase, self).setUp()
class RelationApiTestCase(TestCase): ''' Tests for the relation API ''' def setUp(self): self.family = Family() self.family.save() self.family2 = Family() self.family2.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Edith Clarke', family=self.family) self.person1 = Person(name='Edith Clarke', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person1.save() self.person2 = Person(name='someone else', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person2.save() self.person3 = Person(name='another person', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person3.save() self.relation1 = Relation.objects.create(from_person=self.person1, to_person=self.person2, relation_type=PARTNERED) self.relation1.save() self.relation2 = Relation.objects.create(from_person=self.person1, to_person=self.person3, relation_type=RAISED) self.relation2.save() self.otherFamilyUser = User.objects.create_user( email='*****@*****.**', password='******', name='Another Family', family=self.family2) self.otherFamilyPerson = Person(name='another family', gender='O', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user.id) self.otherFamilyPerson.save() self.otherFamilyPerson2 = Person(name='another family 2', gender='O', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user.id) self.otherFamilyPerson2.save() self.relation3 = Relation.objects.create( from_person=self.otherFamilyPerson, to_person=self.otherFamilyPerson2, relation_type=PARTNERED) self.relation3.save() super(RelationApiTestCase, self).setUp() def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.get('/api/relation/', format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_list(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) response = client.get('/api/relation/', format='json') relations = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(relations)) self.assertNotEqual(self.relation3.id, relations[0]["id"]) self.assertNotEqual(self.relation3.id, relations[1]["id"]) json.loads(response.content) def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/relation/{0}/'.format(self.relation1.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_retrieve(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/{0}/'.format(self.relation1.id) response = client.get(url, format='json') relation = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.relation1.id, relation["id"]) json.loads(response.content) def test_retrieve_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/{0}/'.format(self.relation3.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_delete_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/relation/{0}/'.format(self.relation1.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_delete_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/{0}/'.format(self.relation3.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_delete(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/{0}/'.format(self.relation1.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) count = Relation.objects.filter(pk=self.relation1.id).count() self.assertEqual(0, count) json.loads(response.content) def test_create(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': self.person2.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') relation = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertNotEqual(relation, None) json.loads(response.content) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/relation/' data = { 'from_person_id': self.person2.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.otherFamilyUser) url = '/api/relation/' data = { 'from_person_id': self.person2.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create_invalid_parameter(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': 'invalid parameter', 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_invalid_relation_type(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': 'invalid parameter', 'to_person_id': self.person3.id, 'relation_type': 50000 } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_related_to_self(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': self.person3.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content)
class RelationTestCase(TestCase): # pragma: no cover ''' This defines all the tests for all model logic for a Relation ''' def setUp(self): self.family = Family() self.family.save() super(RelationTestCase, self).setUp() def test_raised_by_relation_resolves_to_raised(self): ''' Tests when a relation that a child is raised by parent, it resolves to parent raised child ''' parent = Person(name="parent", gender="F", family_id=self.family.id) parent.save() child = Person(name="child", gender="O", family_id=self.family.id) child.save() relation = Relation(from_person_id = child.id, to_person_id = parent.id, relation_type = RAISED_BY) relation.normalise() self.assertEqual(RAISED, relation.relation_type) self.assertEqual(parent.id, relation.from_person_id) self.assertEqual(child.id, relation.to_person_id) def test_partnered_male_to_female_resolves_to_female_to_male(self): ''' Tests when a male partners a female, it resolves to females partners a male ''' male = Person(name="male", gender="M", family_id=self.family.id) male.save() female = Person(name="female", gender="F", family_id=self.family.id) female.save() relation = Relation(from_person_id = male.id, to_person_id = female.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(male.id, relation.to_person_id) def test_partnered_other_to_female_resolves_to_female_to_other(self): ''' Tests when an other gender partners a female, it resolves to females partners an other gender ''' other = Person(name="other", gender="O", family_id=self.family.id) other.save() female = Person(name="female", gender="F", family_id=self.family.id) female.save() relation = Relation(from_person_id = other.id, to_person_id = female.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(other.id, relation.to_person_id) def test_partnered_other_to_male_resolves_to_male_to_other(self): ''' Tests when a male partners an other gender, it resolves to other partners a male ''' other = Person(name="other", gender="O", family_id=self.family.id) other.save() male = Person(name="male", gender="M", family_id=self.family.id) male.save() relation = Relation(from_person_id = other.id, to_person_id = male.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(male.id, relation.from_person_id) self.assertEqual(other.id, relation.to_person_id) def test_partnered_female_to_male_stays_the_same(self): ''' Tests when a female partners a male, the relationship does not change ''' female = Person(name="female", gender="F", family_id=self.family.id) female.save() male = Person(name="male", gender="M", family_id=self.family.id) male.save() relation = Relation(from_person_id = female.id, to_person_id = male.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(male.id, relation.to_person_id) def test_existing_relations_get_replaced(self): ''' Tests that when a relations is added between two people, it replaces any existing relations between them ''' existing1 = Person(name="existing1", gender="F", family_id=self.family.id) existing1.save() existing2 = Person(name="existing2", gender="F", family_id=self.family.id) existing2.save() relation = Relation(from_person_id = existing1.id, to_person_id = existing2.id, relation_type = RAISED) relation.save() new_relation = Relation(from_person_id = existing1.id, to_person_id = existing2.id, relation_type = PARTNERED) new_relation.save() self.assertEqual(1, Relation.objects.filter(from_person_id = existing1.id, to_person_id = existing2.id).count()) self.assertEqual(PARTNERED, Relation.objects.get(from_person_id = existing1.id, to_person_id = existing2.id).relation_type) def test_create_inverted_relation(self): ''' Tests an inverted relationship is correctly created when using manager function ''' from_person = Person(name="from_person", gender="F", family_id=self.family.id) from_person.save() to_person = Person(name="to_person", gender="F", family_id=self.family.id) to_person.save() relation = Relation(from_person_id = from_person.id, to_person_id = to_person.id, relation_type = RAISED) inverted = Relation.objects._create_inverted_relation(relation) self.assertEqual(from_person.id, inverted.to_person_id) self.assertEqual(to_person.id, inverted.from_person_id) self.assertEqual(RAISED_BY, inverted.relation_type) def test_get_navigable_relations(self): my_family = Family() my_family.save() person = Person.objects.create(name='patient zero', gender='M',hierarchy_score=100, family_id=my_family.id) wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=my_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M',hierarchy_score=101, family_id=my_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter', gender='F',hierarchy_score=101, family_id=my_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) paths_by_person = Relation.objects.get_navigable_relations(my_family.id) self.assertEqual(3, len(paths_by_person[person.id])) self.assertEqual(1, len(paths_by_person[son.id])) self.assertEqual(Relation, type(paths_by_person[son.id][0]))
class PersonImageProcessingTestCase(TestCase): # pragma: no cover ''' This defines all the tests for all model logic for a Person ''' def setUp(self): self.family = Family() self.family.save() def test_set_profile_image_crop_rotate_resize(self): ''' Tests that the function correctly sets sets the photo field on a person and converts an image. ''' from django.conf import settings path = settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg' #Copy test image to media area import shutil import os shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), path) person = Person(name='陳港生', gender='M', family_id=self.family.id) person.save() person.set_profile_image_crop_rotate_resize(path, 10, 20, 200, 200, 90, test=True) #Check small image is valid small_image = Image.open(settings.MEDIA_ROOT + str(person.small_thumbnail)) small_image.verify() width, height = small_image.size self.assertEqual(80, width) self.assertEqual(80, height) #Check large image is valid large_thumbnail = Image.open(settings.MEDIA_ROOT + str(person.large_thumbnail)) large_thumbnail.verify() width, height = large_thumbnail.size self.assertEqual(200, width) self.assertEqual(200, height) #Clear up mess afterwards os.remove(path) person.remove_local_images() person.remove_remote_images() def test_set_profile_image_crop_rotate_resize_replaces_existing(self): ''' Tests that the function correctly sets sets the photo field on a person and converts an image. ''' from django.conf import settings path = settings.MEDIA_ROOT + 'profile_photos/large_test_image1.jpg' path2 = settings.MEDIA_ROOT + 'profile_photos/large_test_image2.jpg' #Copy test image to media area import shutil import os shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), path) person = Person(name='陳港生', gender='M', family_id=self.family.id) person.set_profile_image_crop_rotate_resize(path, 10, 20, 200, 200, 90) shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), path2) person = Person(name='陳港生', gender='M', family_id=self.family.id) person.set_profile_image_crop_rotate_resize(path2, 10, 20, 200, 200, 90)
def setUp(self): self.family = Family() self.family.save() super(BiographyTestCase, self).setUp()
class JWTAuthTest(TestCase): ''' Tests JWT auth ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Grace Hopper', family_id = self.family.id) self.person = Person(name='Grace Hopper', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() def test_jwt_auth_and_refresh_token_created_on_correct_auth_details(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') auth_details = { 'email': '*****@*****.**', 'password': '******' } response = client.post('/api/auth/obtain_token/', auth_details, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) access_token = json.loads(response.content)["access"] refresh_token = json.loads(response.content)["refresh"] auth_token = { 'refresh': refresh_token } # Sleep to ensure new token is different time.sleep(1) refresh_response = client.post('/api/auth/refresh_token/', auth_token, format='json') refresh_token = json.loads(refresh_response.content)["access"] self.assertEqual(refresh_response.status_code, status.HTTP_200_OK) self.assertNotEqual(refresh_token, access_token) # Check verify token new_auth_token ={# 'token': refresh_token } verify_new_token_response = client.post('/api/auth/verify_token/', new_auth_token, format='json') self.assertEqual(verify_new_token_response.status_code, status.HTTP_200_OK) # Check ip not locked locked_response = client.get('/api/auth/is_locked/', format='json') self.assertEqual(b'false', locked_response.content) def test_jwt_fails_on_auth_incorrect_password(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') payload = { 'email': '*****@*****.**', 'password': '******' } response = client.post('/api/auth/obtain_token/', payload, format='json') self.assertNotEqual(response.status_code, status.HTTP_200_OK) def test_verify_fails_on_invalid_token(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') invalid_auth_token ={# 'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImp0aSI6IjM1ODU0ODc3LWQyZjQtNDIxZS04ZDI5LWY3YTgxNTk3NzdhYyIsImlhdCI6MTU1NDM4NzU4NCwiZXhwIjoxNTU0MzkxMTg0fQ.yIr0TMbalatx7alU1TMGIxxaelqquMJfz3m4H7AA9v4' } verify_old_token_response = client.post('/api/auth/verify_token/', invalid_auth_token, format='json') self.assertNotEqual(verify_old_token_response.status_code, status.HTTP_200_OK) def test_account_locks_out_on_multiple_invalid_login_attempts(self): self.signal_was_called = False def handler(sender, **kwargs): self.signal_was_called = True user_locked_out.connect(handler) user = User.objects.create_user(email='*****@*****.**', password='******', name='Adele Goldberg', family_id = self.family.id) person = Person(name='Adele Goldberg', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=user.id) person.save() # 127.0.0.1 is whitelisted client = APIClient(HTTP_X_REAL_IP='127.0.0.2') wrong_auth_details = { 'email': '*****@*****.**', 'password': '******' } for x in range(0, 6): client.post('/api/auth/obtain_token/', wrong_auth_details, format='json') correct_auth_details = { 'email': '*****@*****.**', 'password': '******' } final_response = client.post('/api/auth/obtain_token/', correct_auth_details, format='json') self.assertNotEqual(final_response.status_code, status.HTTP_200_OK) self.assertTrue(self.signal_was_called) # Check ip locked locked_response = client.get('/api/auth/is_locked/', format='json') self.assertEqual(b'true', locked_response.content) user_locked_out.disconnect(handler) def test_api_docs_loads(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) response = client.get('/api/docs/') self.assertEqual(response.status_code, status.HTTP_200_OK) def test_api_schema_loads(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) response = client.get('/api/schema/') self.assertEqual(response.status_code, status.HTTP_200_OK)
class BiographyTestCase(TestCase): ''' This defines all the tests for all model logic for a Biography ''' def setUp(self): self.family = Family() self.family.save() super(BiographyTestCase, self).setUp() def test_biography_can_be_written_in_non_latic_characters(self): """ Tests that a biography name can be written in non-latin characters """ person = Person(name='nonlatin', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Traditional Chinese traditional_chinese = Biography(person_id = person.id, language = 'zh-hk', content='傳記') traditional_chinese.save() #Simplified Chinese simplified_chinese = Biography(person_id = person.id, language = 'zh-cn', content='传记') simplified_chinese.save() #Polish polish = Biography(person_id = person.id, language = 'pl', content='zabójstwo') polish.save() def test_get_biography(self): ''' Tests get_biography ''' person = Person(name='test_get_biography', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() english = Biography(person_id = person.id, language = 'en', content='biography in english') english.save() polish = Biography(person_id = person.id, language = 'pl', content='biography in polish') polish.save() simplified_chinese = Biography(person_id = person.id, language = 'zh-cn', content='biography in simplified chinese') simplified_chinese.save() #Test when requested language passed in biog = Biography.objects.get_biography(person_id = person.id, requested_language = 'pl') self.assertEqual(polish.id, biog.id) #Test when default language passed in and no requested language biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-cn') self.assertEqual(simplified_chinese.id, biog.id) #Test when requested language specified but biography does not exist biog = Biography.objects.get_biography(person_id = person.id, requested_language = 'zh-hk') self.assertEqual(None, biog) #Test when default language exists biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-cn') self.assertEqual(simplified_chinese.id, biog.id) #Test english returned when default language does not exist biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-hk') self.assertEqual(english.id, biog.id) def test_get_biography_no_requested_no_default_no_english(self): ''' Tests get_biography when no default, requested or english language exists, function returns whatever it can ''' person = Person(name='no_requested_no_default_no_english', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() polish = Biography(person_id = person.id, language = 'pl', content='biography in polish') polish.save() biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-hk') self.assertEqual(polish.id, biog.id) def test_get_biography_no_biographies_at_all(self): ''' Tests get_biography when no biographies exist at all ''' person = Person(name='test_get_biography_no_biographies_at_all', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() biog = Biography.objects.get_biography(person_id = person.id, requested_language = 'zh-hk', default_language = 'zh-hk') self.assertEqual(None, biog)
class InviteEmailApiTestCase(TestCase): ''' Tests for the Invite Email API ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Matt Tong', family=self.family) self.person = Person(name='Matt Tong', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.new_person = Person(name='Taka Hirose', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.new_person.save() self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Herman Li', family=self.family2) self.person2 = Person(name='Herman Li', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() super(InviteEmailApiTestCase, self).setUp() self.invite = EmailConfirmation( email_address='*****@*****.**', person_id=self.person.id, user_who_invited_person_id=self.user.id, sent=timezone.now()) self.invite.save() def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email/{0}/'.format(self.person.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_retrieve(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/{0}/'.format(self.person.id) response = client.get(url, format='json') invite = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.invite.person_id, invite["person_id"]) json.loads(response.content) def test_retrieve_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/invite_email/{0}/'.format(self.person.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) invite = json.loads(response.content) self.assertEqual(self.new_person.id, invite["person_id"]) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create_invalid_person_id(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': '', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_create_user_already_exists(self): self.new_person.user = self.user self.new_person.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_no_email(self): self.new_person.email = '' self.new_person.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_invite_exists_same_email(self): EmailConfirmation.objects.create(email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_invite_exists_different_email(self): EmailConfirmation.objects.create(email_address='*****@*****.**', person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) invite = json.loads(response.content) self.assertEqual(self.new_person.id, invite["person_id"]) json.loads(response.content) def test_confirmation_partial_update(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) data = { 'password': '******', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.new_person = Person.objects.get(pk=self.new_person.id) # Check user assigned newUser = User.objects.get(name=self.new_person.name) self.assertIsNotNone(newUser) json.loads(response.content) def test_confirmation_partial_update_block_ip_after_unsuccessful_attempts( self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.2') url = '/api/invite_email_confirmation/{0}/'.format('invalid_key') data = { 'password': '******', } for x in range(0, 6): response = client.patch(url, data, format='json') self.assertNotEqual(response.status_code, status.HTTP_200_OK) # Check ip blocked after multiple failed attempts with correct key url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_confirmation_partial_update_password_too_short(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) data = { 'password': '******', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_confirmation_partial_update_email_address_mismatch(self): invite = EmailConfirmation.objects.create( email_address='*****@*****.**', person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) data = { 'password': '******', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_confirmation_retrieve(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'*****@*****.**' in response.content) self.assertTrue(b'Taka Hirose' in response.content) json.loads(response.content) def test_confirmation_retrieve_block_ip_after_unsuccessful_attempt(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.19') url = '/api/invite_email_confirmation/{0}/'.format('not_a_proper_key') for x in range(0, 6): response = client.get(url, format='json') self.assertNotEqual(response.status_code, status.HTTP_200_OK) # Check ip blocked after multiple failed attempts with correct key url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_get_shortest_path(self): ''' Tests the get_related_path function. ''' another_family = Family() another_family.save() person = Person.objects.create(name='patient zero', gender='M', hierarchy_score=100, family_id=another_family.id) wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=another_family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter', gender='F', hierarchy_score=101, family_id=another_family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=mum, to_person=person, relation_type=RAISED) dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=dad, to_person=person, relation_type=RAISED) grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=mum, relation_type=RAISED) aunt = Person.objects.create(name='aunt', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=grandma, to_person=aunt, relation_type=RAISED) cousin = Person.objects.create(name='cousin', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=aunt, to_person=cousin, relation_type=RAISED) other_cousin = Person.objects.create(name='other_cousin', gender='F', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=aunt, to_person=other_cousin, relation_type=RAISED) distant_nephew = Person.objects.create(name='distant_nephew', gender='M', hierarchy_score=99, family_id=another_family.id) Relation.objects.create(from_person=cousin, to_person=distant_nephew, relation_type=RAISED) people, relations = tree_service.get_shortest_path( other_cousin, person) self.assertEqual(person.id, people[0].id) self.assertEqual(mum.id, people[1].id) self.assertEqual(grandma.id, people[2].id) self.assertEqual(aunt.id, people[3].id) self.assertEqual(other_cousin.id, people[4].id) self.assertEqual(RAISED_BY, relations[0].relation_type) self.assertEqual(RAISED_BY, relations[1].relation_type) self.assertEqual(RAISED, relations[2].relation_type) self.assertEqual(RAISED, relations[3].relation_type)
def setUp(self): self.family = Family() self.family.save()
class SolverTestCase(TestCase): # pragma: no cover ''' This defines all the tests for the solver class ''' def setUp(self): self.family = Family() self.family.save() self.person = Person.objects.create(name='patient zero', gender='M', family_id=self.family.id) self.wife = Person.objects.create(name='wife', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.wife, to_person=self.person, relation_type=PARTNERED) self.son = Person.objects.create(name='son', gender='M', family_id=self.family.id) Relation.objects.create(from_person=self.person, to_person=self.son, relation_type=RAISED) self.daughter = Person.objects.create(name='daughter', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.person, to_person=self.daughter, relation_type=RAISED) self.mum = Person.objects.create(name='mum', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.mum, to_person=self.person, relation_type=RAISED) self.dad = Person.objects.create(name='dad', gender='M', family_id=self.family.id) Relation.objects.create(from_person=self.dad, to_person=self.person, relation_type=RAISED) self.grandma = Person.objects.create(name='grandma', gender='F', family_id=self.family.id) Relation.objects.create(from_person=self.grandma, to_person=self.mum, relation_type=RAISED) self.grandson = Person.objects.create(name='grandson', gender='M', family_id=self.family.id) Relation.objects.create(from_person=self.son, to_person=self.grandson, relation_type=RAISED) def test_load_data(self): ''' Tests load data creates correct arrays ''' solver = Solver() solver.load_data(self.family.id) self.assertTrue(len(solver.nodes) > 0) def test_find_path(self): ''' Tests finds path from one node to another ''' solver = Solver() solver.load_data(self.family.id) solver.find_path(self.grandson.id, self.grandma.id) titles = ",".join(solver.result.titles) self.assertEqual("Father,Father,Mother,Mother", titles) self.assertEqual(-4, solver.result.generation) self.assertEqual(0, solver.result.age_diff)
class PersonTestCase(TestCase): # pragma: no cover ''' This defines all the tests for all model logic for a Person ''' def setUp(self): self.family = Family() self.family.save() super(PersonTestCase, self).setUp() def test_update_user_when_email_changed(self): ''' Tests that a user is updated when a person's email is modified ''' user = User.objects.create(email='*****@*****.**', password='******', name='John Wong') person = Person(name='John Wong', gender='M', email='*****@*****.**', family_id=self.family.id, language='pl', user_id=user.id) person.save() person.email = '*****@*****.**' person.create_update_user() self.assertEqual( 1, User.objects.filter(email='*****@*****.**').count()) self.assertEqual( 0, User.objects.filter(email='*****@*****.**').count()) self.assertEqual( person.user_id, User.objects.get(email='*****@*****.**').id) self.assertEqual( person.family_id, User.objects.get(email='*****@*****.**').family_id) self.assertEqual( 'pl', User.objects.get(email='*****@*****.**').language) def test_person_name_can_be_in_non_latic_characters(self): ''' Tests that a users name can be written in non-latin characters ''' #Traditional Chinese person = Person(name='實驗', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Simplified Chinese person = Person(name='实验', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Polish person = Person(name='kiełbasa', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() def test_hierachy_score_set_for_adding_child(self): ''' Ensure that hierachy score is set when adding the child of a parent the relation is not passed to the function ''' parent = Person(name='parent', gender='M', hierarchy_score=100, family_id=self.family.id) parent.save() child = Person(name='child', gender='M', family_id=self.family.id) child.save() relation = Relation(from_person_id=parent.id, to_person_id=child.id, relation_type=RAISED) relation.save() child.set_hierarchy_score() self.assertEqual(101, child.hierarchy_score) def test_hierachy_score_set_for_adding_child_with_relation_as_param(self): ''' Ensure that hierachy score is set when adding the child of a parent the relation is passed to the function ''' parent = Person(name='parent', gender='M', hierarchy_score=100, family_id=self.family.id) parent.save() child = Person(name='child', gender='M', family_id=self.family.id) child.save() relation = Relation(from_person_id=parent.id, to_person_id=child.id, relation_type=RAISED) child.set_hierarchy_score(relation) self.assertEqual(101, child.hierarchy_score) def test_hierachy_score_set_for_adding_parent(self): ''' Ensure that hierachy score is set when adding the parent of a child the relation is not passed to the function ''' parent = Person(name='parent', gender='M', family_id=self.family.id) parent.save() child = Person(name='child', gender='M', hierarchy_score=100, family_id=self.family.id) child.save() relation = Relation(from_person_id=parent.id, to_person_id=child.id, relation_type=RAISED) relation.save() parent.set_hierarchy_score() self.assertEqual(99, parent.hierarchy_score) def test_hierachy_score_set_for_adding_parent_with_relation_as_param(self): ''' Ensure that hierachy score is set when adding the parent of a child the relation is passed to the function ''' parent = Person(name='parent', gender='M', family_id=self.family.id) parent.save() child = Person(name='child', gender='M', hierarchy_score=100, family_id=self.family.id) child.save() relation = Relation(from_person_id=parent.id, to_person_id=child.id, relation_type=RAISED) parent.set_hierarchy_score(relation) self.assertEqual(99, parent.hierarchy_score) def test_hierachy_score_set_for_adding_partner(self): ''' Ensure that hierachy score is set when adding a partner ''' husband = Person(name='husband', gender='M', family_id=self.family.id) husband.save() wife = Person(name='wife', gender='F', hierarchy_score=75, family_id=self.family.id) wife.save() relation = Relation(from_person_id=husband.id, to_person_id=wife.id, relation_type=PARTNERED) relation.save() husband.set_hierarchy_score() self.assertEqual(75, husband.hierarchy_score) def test_geocode_address_UK(self): ''' Tests that the correct longitude and latitude are returned for a UK location ''' person = Person.objects.create(name='Kate Bush', gender='F', address='Bexleyheath, England', family_id=self.family.id) person.geocode_address() self.assertEqual(51.45, round(person.latitude, 2)) self.assertEqual(0.14, round(person.longitude, 2)) def test_geocode_address_China(self): ''' Tests that the correct longitude and latitude are returned for a location in China ''' person = Person.objects.create(name='Jackie Chan', gender='M', address='扯旗山 香港', family_id=self.family.id) person.geocode_address() self.assertEqual(22.28, round(person.latitude, 2)) self.assertEqual(114.15, round(person.longitude, 2)) def test_geocode_address_using_backup_UK(self): ''' Tests that the correct longitude and latitude are returned for a UK address using the backup geocoding service ''' location = geocoder._geocode_address_using_backup( 'Mexborough, Yorkshire') self.assertEqual(53.5, round(location.latitude, 1)) self.assertEqual(-1.3, round(location.longitude, 1)) def test_geocode_address_using_backup_China(self): ''' Tests that the correct longitude and latitude are returned for a location in China using the backup geocoding service ''' location = geocoder._geocode_address_using_backup('星光大道 香港') self.assertEqual(22.3, round(location.latitude, 1)) self.assertEqual(114.2, round(location.longitude, 1)) def test_set_hires_photo(self): ''' Tests that the function correctly sets sets the photo field on a person and converts an image. ''' from django.conf import settings #Copy test image to media area import shutil import os shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') person = Person(name='陳港生', gender='M', family_id=self.family.id) person.set_hires_photo('large_test_image.jpg') self.assertEqual('profile_photos/large_test_image.jpg', person.photo) #Check this image is valid image = Image.open(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') image.verify() width, height = image.size self.assertEqual(500, width) self.assertEqual(343, height) #maintains aspect ratio self.assertEqual('profile_photos/large_test_image.jpg', person.photo) #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') def test_crop_and_resize_photo(self): ''' Tests that the function correctly sets two thumbnails of correct size ''' from django.conf import settings #Copy test image to media area import shutil import os shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') person = Person(name='譚詠麟', gender='M', family_id=self.family.id) person.photo = 'profile_photos/large_test_image.jpg' person.crop_and_resize_photo(50, 50, 20, 20) #Check small thumbnail is valid small = Image.open(settings.MEDIA_ROOT + str(person.small_thumbnail)) small.verify() width, height = small.size self.assertEqual(80, width) self.assertEqual(80, height) #Check large thumbnail is valid large = Image.open(settings.MEDIA_ROOT + str(person.large_thumbnail)) large.verify() width, height = large.size self.assertEqual(200, width) self.assertEqual(200, height) #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') def test_rotate_photo(self): ''' Tests that the function correctly rotates a photo ''' from django.conf import settings #Copy test image to media area import shutil import os shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') person = Person(name='譚詠麟', gender='M', family_id=self.family.id) person.photo = 'profile_photos/large_test_image.jpg' person.rotate_photo(90) #Check rotated photo is valid rotated_photo = Image.open(settings.MEDIA_ROOT + str(person.photo)) rotated_photo.verify() #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + str(person.photo))
class ImageApiTestCase(TestCase): ''' Tests for the Image API ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Phil Collins', family=self.family) self.person = Person(name='Phil Collins', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) self.test_image = os.path.join(settings.BASE_DIR, 'gallery/tests/test_image.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save() #Tag person 1 in image self.tag = Tag.objects.create(image=self.image, person=self.person, x1=1, x2=2, y1=3, y2=4) self.gallery2 = Gallery.objects.create(title="test_gallery2", family_id=self.family.id) self.test_image2 = os.path.join(settings.BASE_DIR, 'gallery/tests/test_image.jpg') self.test_image2_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery2.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery2.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image2, self.test_image2_destination) self.image2 = Image(gallery=self.gallery2, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery2.id), '/test_image.jpg' ])) self.image2.save() self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user( email='*****@*****.**', password='******', name='Phillip Bailey', family=self.family2) self.person2 = Person(name='Phillip Bailey', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() super(ImageApiTestCase, self).setUp() def tearDown(self): self.image.delete_local_image_files() threading.Thread(target=self.image.delete_remote_image_files).start() self.image2.delete_local_image_files() threading.Thread(target=self.image2.delete_remote_image_files).start() # Delete any updates try: self.image = Image.objects.get(id=self.image.id) self.image.delete_local_image_files() threading.Thread( target=self.image.delete_remote_image_files).start() except: pass try: self.image2 = Image.objects.get(id=self.image2.id) self.image2.delete_local_image_files() threading.Thread( target=self.image2.delete_remote_image_files).start() except: pass try: os.remove(self.test_image_destination) except: pass try: os.remove(self.test_image2_destination) except: pass def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.get('/api/image/?page=1', format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_list_page1(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') # Check this works with JWT token auth_details = { 'email': '*****@*****.**', 'password': '******' } auth_response = client.post('/api/auth/obtain_token/', auth_details, format='json') token = json.loads(auth_response.content)["access"] client.credentials(HTTP_AUTHORIZATION='Bearer ' + token) response = client.get('/api/image/?page=1', format='json') # Check it contains both images self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.image.thumbnail).encode() in response.content) self.assertTrue( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_list_page1_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') # Login with other user auth_details = { 'email': '*****@*****.**', 'password': '******' } auth_response = client.post('/api/auth/obtain_token/', auth_details, format='json') token = json.loads(auth_response.content)["access"] client.credentials(HTTP_AUTHORIZATION='Bearer ' + token) response = client.get('/api/image/?page=1', format='json') # Check it contains neither images self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse( str(self.image.thumbnail).encode() in response.content) self.assertFalse( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_list_by_gallery(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/?gallery_id={0}'.format(self.gallery2.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse( str(self.image.thumbnail).encode() in response.content) self.assertTrue( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_list_by_tagged_person(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/?person_id={0}'.format(self.person.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.image.thumbnail).encode() in response.content) self.assertFalse( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/{0}/'.format(self.image.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) os.remove(self.test_image_destination) os.remove(self.test_image2_destination) def test_retrieve(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.image.thumbnail).encode() in response.content) json.loads(response.content) def test_retrieve_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/image/{0}/'.format(self.image.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create(self): ''' test that we can upload a file ''' client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/' with open(self.test_image, 'rb') as fp: data = { 'picture': fp, 'gallery_id': self.gallery.id, } response = client.post(url, data) # Check image loads image_id = json.loads(response.content)['id'] image = Image.objects.get(id=image_id) image.delete_local_image_files() image.delete_remote_image_files() self.assertEqual(200, response.status_code) self.assertEqual('test_image', image.title) self.assertTrue('Phil Collins', image.uploaded_by.name) json.loads(response.content) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/' with open(self.test_image, 'rb') as fp: data = { 'picture': fp, 'gallery_id': self.gallery.id, } response = client.post(url, data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/image/' with open(self.test_image, 'rb') as fp: data = { 'picture': fp, 'gallery_id': self.gallery.id, } response = client.post(url, data) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_destroy(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) count = Image.objects.filter(id=self.image.id).count() self.assertEqual(0, count) json.loads(response.content) def test_destroy_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/{0}/'.format(self.image.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_destroy_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/image/{0}/'.format(self.image.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_partial_update(self): self.image.upload_files_to_s3() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'new title' in response.content) self.assertTrue(b'new description' in response.content) self.assertTrue(b'10' in response.content) self.assertTrue(b'20' in response.content) json.loads(response.content) def test_partial_update_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_partial_update_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_partial_update_invalid_title(self): self.image.upload_files_to_s3() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': ' ', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_partial_update_optional_data_missing(self): self.image.upload_files_to_s3() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'new title' in response.content) self.assertTrue(b'new description' in response.content) self.assertTrue(b'"longitude":0.0' in response.content) self.assertTrue(b'"latitude":0.0' in response.content) json.loads(response.content)
def test_get_relation_name(self): family = Family() family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Margaret Knight', family=family) person = Person.objects.create(name='patient zero', gender='M', family_id=family.id) wife = Person.objects.create(name='wife', gender='F', family_id=family.id) Relation.objects.create(from_person=wife, to_person=person, relation_type=PARTNERED) son = Person.objects.create(name='son', gender='M', family_id=family.id) Relation.objects.create(from_person=person, to_person=son, relation_type=RAISED) daughter = Person.objects.create(name='daughter', gender='F', family_id=family.id) Relation.objects.create(from_person=person, to_person=daughter, relation_type=RAISED) mum = Person.objects.create(name='mum', gender='F', family_id=family.id) Relation.objects.create(from_person=mum, to_person=person, relation_type=RAISED) dad = Person.objects.create(name='dad', gender='M', family_id=family.id) Relation.objects.create(from_person=dad, to_person=person, relation_type=RAISED) grandma = Person.objects.create(name='grandma', gender='F', family_id=family.id) Relation.objects.create(from_person=grandma, to_person=mum, relation_type=RAISED) grandson = Person.objects.create(name='grandson', gender='M', family_id=family.id) Relation.objects.create(from_person=son, to_person=grandson, relation_type=RAISED) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=user) url = '/api/relation_name/{0}/{1}/'.format(person.id, grandson.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b"syun1" in response.content)