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 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_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_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_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)
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))
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 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 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)
class PathToNameMapper4thGenTestCase(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_grandmothers_sister(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)) grandmother = Node( Person.objects.create(name='grandmother', gender='F', family_id=self.family.id)) great_grandmother = Node( Person.objects.create(name='great_grandmother', gender='F', family_id=self.family.id)) great_aunt = Node( Person.objects.create(name='great_aunt', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, great_aunt) path.add_node(mum, RAISED_BY) path.add_node(grandmother, RAISED_BY) path.add_node(great_grandmother, RAISED_BY) path.add_node(great_aunt, RAISED) result = get_name(path) self.assertEqual(1, len(result)) self.assertEqual("Maternal Grandmother's Sister", result[0]) def test_maternal_grandfathers_elder_brother(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)) grandfather = Node( Person.objects.create(name='grandfather', gender='M', family_id=self.family.id, birth_year=1950)) great_grandmother = Node( Person.objects.create(name='great_grandmother', gender='F', family_id=self.family.id)) great_uncle = Node( Person.objects.create(name='great_uncle', gender='M', family_id=self.family.id, birth_year=1949)) path = Path() path.set_goals(person, great_uncle) path.add_node(mum, RAISED_BY) path.add_node(grandfather, RAISED_BY) path.add_node(great_grandmother, RAISED_BY) path.add_node(great_uncle, RAISED) result = get_name(path) self.assertEqual(1, len(result)) self.assertEqual("Maternal Grandfather's Elder Brother", result[0]) def test_paternal_grandfathers_younger_brothers_wife(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)) grandfather = Node( Person.objects.create(name='grandfather', gender='M', family_id=self.family.id, birth_year=1950)) great_grandmother = Node( Person.objects.create(name='great_grandmother', gender='F', family_id=self.family.id)) great_uncle = Node( Person.objects.create(name='great_uncle', gender='M', family_id=self.family.id, birth_year=1951)) great_aunt = Node( Person.objects.create(name='great_aunt', gender='F', family_id=self.family.id)) path = Path() path.set_goals(person, great_aunt) path.add_node(dad, RAISED_BY) path.add_node(grandfather, RAISED_BY) path.add_node(great_grandmother, RAISED_BY) path.add_node(great_uncle, RAISED) path.add_node(great_aunt, PARTNERED) result = get_name(path) self.assertEqual(1, len(result)) self.assertEqual("Paternal Grandfather's Younger Brother's Wife", result[0]) def test_unknownlineage_grandparents_siblings_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)) grandparent = Node( Person.objects.create(name='grandparent', gender='O', family_id=self.family.id)) great_grandparent = Node( Person.objects.create(name='great_grandparent', gender='O', family_id=self.family.id)) grandparents_sibling = Node( Person.objects.create(name='grandparent_sibling', gender='O', family_id=self.family.id)) grandparents_siblings_partner = Node( Person.objects.create(name='grandparents_siblings_partner', gender='O', family_id=self.family.id)) path = Path() path.set_goals(person, grandparents_siblings_partner) path.add_node(parent, RAISED_BY) path.add_node(grandparent, RAISED_BY) path.add_node(great_grandparent, RAISED_BY) path.add_node(grandparents_sibling, RAISED) path.add_node(grandparents_siblings_partner, PARTNERED) result = get_name(path) self.assertEqual(10, len(result)) self.assertTrue( "Paternal Grandfather's Younger Brother's Wife" in result) self.assertTrue( "Paternal Grandfather's Elder Brother's Wife" in result) self.assertTrue("Maternal Grandmother's Sister's Husband" in 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 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]))
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_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)
class PathTestCase(TestCase): # pragma: no cover ''' This defines all the tests for the solver class ''' 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)) def test_add_nodes_and_duplicate(self): path = Path() path.set_goals(self.person, self.grandma) path.add_node(self.mum, RAISED_BY) self.assertEqual(1, len(path.steps)) self.assertEqual(2, len(path.nodes)) path.add_node(self.grandma, RAISED_BY) self.assertEqual(2, len(path.steps)) self.assertEqual(3, len(path.nodes)) self.assertTrue(path.success) duplicate = path.duplicate() path.add_node(self.grandpa, PARTNERED) self.assertEqual(2, len(duplicate.steps)) self.assertEqual(3, len(duplicate.nodes)) self.assertTrue(duplicate.success) def test_create_next_level_paths(self): path = Path() path.set_goals(self.person, self.great_grandma) path.add_node(self.mum, RAISED_BY) new_paths = path.create_next_level_paths() self.assertEqual(2, len(new_paths)) self.assertEqual(2, len(new_paths[0].steps)) self.assertEqual(2, len(new_paths[1].steps)) def test_step_title_mother(self): path = Path() path.set_goals(self.person, self.mum) path.add_node(self.mum, RAISED_BY) self.assertEqual("Mother", path.steps[0].step_title()) def test_step_title_father(self): path = Path() path.set_goals(self.person, self.dad) path.add_node(self.dad, RAISED_BY) self.assertEqual("Father", path.steps[0].step_title()) def test_step_title_husband(self): path = Path() path.set_goals(self.grandma, self.grandpa) path.add_node(self.grandpa, PARTNERED) self.assertEqual("Husband", path.steps[0].step_title()) def test_step_title_wife(self): path = Path() path.set_goals(self.grandpa, self.grandma) path.add_node(self.grandma, PARTNERED) self.assertEqual("Wife", path.steps[0].step_title()) def test_step_title_son(self): path = Path() path.set_goals(self.mum, self.person) path.add_node(self.person, RAISED) self.assertEqual("Son", path.steps[0].step_title()) def test_step_title_daughter(self): path = Path() path.set_goals(self.person, self.daughter) path.add_node(self.daughter, RAISED) self.assertEqual("Daughter", path.steps[0].step_title())
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 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 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')
class GalleryApiTestCase(TestCase): ''' Tests for the Gallery 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.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 test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.get('/api/gallery/', format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 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/gallery/', format='json') # Check it contains both galleries self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.gallery.title).encode() in response.content) self.assertTrue(str(self.gallery2.title).encode() in 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/gallery/', format='json') # Check it contains neither galleries self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(str(self.gallery.title).encode() in response.content) self.assertFalse(str(self.gallery2.title).encode() in response.content) def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/gallery/{0}/'.format(self.gallery.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_retrieve(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/{0}/'.format(self.gallery.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.gallery.title).encode() in 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/gallery/{0}/'.format(self.gallery.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_partial_update(self): shutil.copy2(self.test_image, self.test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) image.save() image2 = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) image2.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/{0}/'.format(self.gallery.id) data = { 'family_id': self.family2.id, # try to switch families 'title': 'new title', 'description': 'new description', 'thumbnail_id': image2.id, } response = client.patch(url, data, format='json') gallery = Gallery.objects.get(id=self.gallery.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual('new title', gallery.title) self.assertEqual(self.family.id, gallery.family_id) self.assertTrue(b'new title' in response.content) self.assertTrue(b'new description' in response.content) self.assertTrue( str(image2.thumbnail) in response.content.decode("utf-8")) image.delete_local_image_files() image.delete_remote_image_files() image2.delete_local_image_files() image2.delete_remote_image_files() def test_partial_update_remove_thumbnail(self): shutil.copy2(self.test_image, self.test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) image.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/{0}/'.format(self.gallery.id) data = { 'thumbnail_id': '', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue('"thumbnail":null' in response.content.decode("utf-8")) def test_partial_update_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/gallery/{0}/'.format(self.gallery.id) data = { 'title': 'new title', 'description': 'new description', 'thumbnail': 'test_image.jpg', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_partial_update_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) data = { 'title': 'new title', 'description': 'new description', 'thumbnail': 'test_image.jpg', } url = '/api/gallery/{0}/'.format(self.gallery.id) response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_create(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/' data = { 'family_id': self.family2.id, # try to switch families 'title': 'new gallery title', 'description': 'new gallery description', } response = client.post(url, data, format='json') gallery = Gallery.objects.get(title='new gallery title') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual('new gallery title', gallery.title) self.assertEqual(self.family.id, gallery.family_id) self.assertTrue(b'new gallery title' in response.content) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/gallery/'.format(self.gallery.id) data = { 'title': 'new title', 'description': 'new description', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_create_no_title(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/' data = { 'title': '', 'description': 'new gallery description', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)