Ejemplo n.º 1
0
 def add_parents(child, depth, max_depth):
     if depth + 1 < max_depth:
         dad = RelatedPerson(child.name + '_dad', Gender.MALE)
         mom = RelatedPerson(child.name + '_mom', Gender.FEMALE)
         people.append(dad)
         people.append(mom)
         child.set_father(dad)
         child.set_mother(mom)
         add_parents(dad, depth + 1, max_depth)
         add_parents(mom, depth + 1, max_depth)
Ejemplo n.º 2
0
 def phase2(self, filename, logfile, log):
     ## Phase 2 testing
     row = 0
     errors = []
     bad_ids = set()
     f = filename
     for line in f:
         row += 1
         try:
             self.buffer.append(RawPersonRecord.make_from_line(line, row))
         except ValueError as e:
             errors.append(str(e))
     ## he wrote this
     for raw_person in self.buffer:
         try:
             if raw_person.id in self.people_index:
                  bad_ids.add(raw_person.id)
                  del self.people_index[raw_person.id]
             if raw_person.id in bad_ids:
                 raise RelatedPersonError("duplicate ID: {}".format(raw_person.id))
             ## i wrote this
             raw_person_gender = raw_person.gender
             correct_constant = Gender.get_gender(raw_person_gender)
             gender = correct_constant
             related_person = RelatedPerson(raw_person.id, raw_person.name, raw_person.gender)
             self.people_index[raw_person.id] = related_person
         except RelatedPersonError as e:
             errors.append("row {}: {}".format(raw_person.row, str(e)))
     if errors:
         if logfile != None:
             for rpe in errors:
                 log.logger.error(rpe)
         elif logfile == None:
             log.logger.error('- individual errors -')
             log.logger.error('\n'.join(errors))
Ejemplo n.º 3
0
    def test_remove_father_error(self):
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_father()
        self.assertIn('as it is not set', str(context.exception))

        self.child.father = RelatedPerson('test', 'M')
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_father()
        self.assertIn('not one of his children', str(context.exception))
Ejemplo n.º 4
0
    def test_remove_mother_error(self):
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_mother()
        self.assertIn('is not set and cannot be removed',
                      str(context.exception))

        self.child.mother = RelatedPerson('test', 'F')
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_mother()
        self.assertIn('not one of her children', str(context.exception))
Ejemplo n.º 5
0
    def phase2(self):
        # Phase 2:
        row = 0
        errors = []
        bad_ids = set()
        # todo: report failure to open and quit, or have file opened by command line parsing
        args = self.phase1()
        filename = args.infile

        with filename as f:
            for line in f:
                row += 1
                try:
                    self.buffer.append(
                        RawPersonRecord.make_from_line(line, row))
                except ValueError as e:
                    errors.append(str(e))

        # check IDs, genders, & create RelatedPersons
        for raw_person in self.buffer:
            try:
                # check for dupes
                if raw_person.id in self.people_index:
                    bad_ids.add(raw_person.id)
                    del self.people_index[raw_person.id]
                if raw_person.id in bad_ids:
                    raise RelatedPersonError("duplicate ID: {}".format(
                        raw_person.id))

                # todo: get and check gender
                gender = Gender.get_gender(raw_person.gender)

                # make RelatedPerson
                related_person = RelatedPerson(raw_person.id, raw_person.name,
                                               gender)
                self.people_index[raw_person.id] = related_person
            except RelatedPersonError as e:
                errors.append("row {}: {}".format(raw_person.row, str(e)))
                bad_ids.add(raw_person.id)

        if errors:
            # todo: write to output determined by command line input
            text_1 = '\n- individual errors -'
            text_2 = '\n'.join(errors)
            if args.outfile:
                with args.outfile as o:
                    o.write(text_1 + text_2)
            else:
                print(text_1, text_2)
Ejemplo n.º 6
0
    def phase2(self, filename):
        # Phase 2:
        row = 0
        errors = []
        bad_ids = set()
        # todo: report failure to open and quit, or have file opened by command line parsing
        with open(filename, 'r') as f:
            for line in f:
                row += 1
                try:
                    self.buffer.append(
                        RawPersonRecord.make_from_line(line, row))
                except ValueError as e:
                    errors.append(str(e))

        # check IDs, genders, & create RelatedPersons
        for raw_person in self.buffer:
            try:
                # check for dupes
                if raw_person.id in self.people_index:
                    bad_ids.add(raw_person.id)
                    del self.people_index[raw_person.id]
                if raw_person.id in bad_ids:
                    raise RelatedPersonError("duplicate ID: {}".format(
                        raw_person.id))

                # todo: get and check gender

                # make RelatedPerson
                related_person = RelatedPerson(raw_person.id, raw_person.name,
                                               gender)
                self.people_index[raw_person.id] = related_person
            except RelatedPersonError as e:
                errors.append("row {}: {}".format(raw_person.row, str(e)))

        if errors:
            # todo: write to output determined by command line input
            print('\n- individual errors -')
            print('\n'.join(errors))
Ejemplo n.º 7
0
    def setUp(self):
        # create a few RelatedPersons
        self.child = RelatedPerson('kid', 'NA')
        self.mom = RelatedPerson('mom', 'f')
        self.dad = RelatedPerson('dad', 'm')

        # make a deep family history
        self.generations = 6
        self.people = people = []
        self.root_child = RelatedPerson('root_child', Gender.UNKNOWN)
        people.append(self.root_child)

        def add_parents(child, depth, max_depth):
            if depth + 1 < max_depth:
                dad = RelatedPerson(child.name + '_dad', Gender.MALE)
                mom = RelatedPerson(child.name + '_mom', Gender.FEMALE)
                people.append(dad)
                people.append(mom)
                child.set_father(dad)
                child.set_mother(mom)
                add_parents(dad, depth + 1, max_depth)
                add_parents(mom, depth + 1, max_depth)

        add_parents(self.root_child, 0, self.generations)
Ejemplo n.º 8
0
    def phase2(self, people_data):
        # Phase 2:
        row = 0
        errors = []
        bad_ids = set()
        for line in people_data:
            row += 1
            try:
                self.buffer.append(RawPersonRecord.make_from_line(line, row))
            except ValueError as e:
                errors.append(str(e))

        # check IDs, genders, & create RelatedPersons
        for raw_person in self.buffer:
            try:
                # check for dupes
                if raw_person.id in self.people_index:
                    bad_ids.add(raw_person.id)
                    del self.people_index[raw_person.id]
                if raw_person.id in bad_ids:
                    raise RelatedPersonError("duplicate ID: {}".format(
                        raw_person.id))

                # get and check gender
                gender = Gender.get_gender(raw_person.gender)

                # make RelatedPerson
                related_person = RelatedPerson(raw_person.id, raw_person.name,
                                               gender)
                self.people_index[raw_person.id] = related_person
            except RelatedPersonError as e:
                errors.append("row {}: {}".format(raw_person.row, str(e)))

        if errors:
            sys.stderr.write('\n- individual errors -\n')
            sys.stderr.write('\n'.join(errors))
            sys.stderr.write('\n')
Ejemplo n.º 9
0
class TestRelatedPerson(unittest.TestCase):
    def setUp(self):
        # create a few RelatedPersons
        self.child = RelatedPerson('kid', 'NA')
        self.mom = RelatedPerson('mom', 'f')
        self.dad = RelatedPerson('dad', 'm')

        # make a deep family history
        self.generations = 6
        self.people = people = []
        self.root_child = RelatedPerson('root_child', Gender.UNKNOWN)
        people.append(self.root_child)

        def add_parents(child, depth, max_depth):
            if depth + 1 < max_depth:
                dad = RelatedPerson(child.name + '_dad', Gender.MALE)
                mom = RelatedPerson(child.name + '_mom', Gender.FEMALE)
                people.append(dad)
                people.append(mom)
                child.set_father(dad)
                child.set_mother(mom)
                add_parents(dad, depth + 1, max_depth)
                add_parents(mom, depth + 1, max_depth)

        add_parents(self.root_child, 0, self.generations)

    def test_add_child_error(self):
        self.dad.gender = Gender.UNKNOWN
        with self.assertRaises(RelatedPersonError) as context:
            self.dad.add_child(self.child)
        self.assertRegex(str(context.exception),
                         "cannot add child.*with unknown gender")

    def test_remove_father(self):
        self.child.set_father(self.dad)
        self.child.remove_father()
        self.assertNotIn(self.child, self.dad.children)

    def test_ancestors_error(self):
        with self.assertRaises(RelatedPersonError) as context:
            self.root_child.ancestors(3, 2)
        self.assertRegex(str(context.exception),
                         "max_depth.*cannot be less than min_depth")

    def test_parents(self):
        pass

    def test_all_grandparents(self):
        all_grandparents = set(self.people).difference(
            [self.root_child], self.root_child.parents())
        self.assertEqual(self.root_child.all_grandparents(), all_grandparents)

    def test_all_ancestors(self):
        self.assertEqual(self.root_child.all_ancestors(), set(self.people[1:]))
Ejemplo n.º 10
0
 def test_get_related_persons_name():
     assert RelatedPerson.get_related_persons_name(
         RelatedPerson('test', 'NA')) == 'test'
     assert RelatedPerson.get_related_persons_name(None) == 'NA'
Ejemplo n.º 11
0
class TestRelatedPerson(unittest.TestCase):
    def setUp(self):
        # create a few RelatedPersons
        self.child = RelatedPerson('kid', 'NA')
        self.mom = RelatedPerson('mom', 'f')
        self.dad = RelatedPerson('dad', 'm')

        # make a deep family history
        self.generations = 6
        self.people = people = []
        self.root_child = RelatedPerson('root_child', Gender.UNKNOWN)
        people.append(self.root_child)

        def add_parents(child, depth, max_depth):
            if depth + 1 < max_depth:
                dad = RelatedPerson(child.name + '_dad', Gender.MALE)
                mom = RelatedPerson(child.name + '_mom', Gender.FEMALE)
                people.append(dad)
                people.append(mom)
                child.set_father(dad)
                child.set_mother(mom)
                add_parents(dad, depth + 1, max_depth)
                add_parents(mom, depth + 1, max_depth)

        add_parents(self.root_child, 0, self.generations)

    @staticmethod
    def test_get_related_persons_name():
        assert RelatedPerson.get_related_persons_name(
            RelatedPerson('test', 'NA')) == 'test'
        assert RelatedPerson.get_related_persons_name(None) == 'NA'

    def test_set_father(self):
        self.child.set_father(self.dad)
        self.assertEqual(self.child.father, self.dad)
        self.assertIn(self.child, self.dad.children)

    def test_set_father_error(self):
        self.dad.gender = Gender.FEMALE
        with self.assertRaises(RelatedPersonError) as context:
            self.child.set_father(self.dad)
        self.assertIn('is not male', str(context.exception))

    def test_set_mother(self):
        self.child.set_mother(self.mom)
        self.assertEqual(self.child.mother, self.mom)
        self.assertIn(self.child, self.mom.children)

    def test_set_mother_error(self):
        self.mom.gender = Gender.MALE
        with self.assertRaises(RelatedPersonError) as context:
            self.child.set_mother(self.mom)
        self.assertIn('is not female', str(context.exception))

    def test_remove_father(self):
        self.child.set_father(self.dad)
        self.child.remove_father()
        self.assertNotIn(self.child, self.dad.children)
        self.assertEqual(self.child.father, None)

    def test_remove_father_error(self):
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_father()
        self.assertIn('as it is not set', str(context.exception))

        self.child.father = RelatedPerson('test', 'M')
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_father()
        self.assertIn('not one of his children', str(context.exception))

    def test_remove_mother(self):
        self.child.set_mother(self.mom)
        self.child.remove_mother()
        self.assertNotIn(self.child, self.mom.children)
        self.assertEqual(self.child.mother, None)

    def test_remove_mother_error(self):
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_mother()
        self.assertIn('is not set and cannot be removed',
                      str(context.exception))

        self.child.mother = RelatedPerson('test', 'F')
        with self.assertRaises(RelatedPersonError) as context:
            self.child.remove_mother()
        self.assertIn('not one of her children', str(context.exception))

    def test_add_child(self):
        self.dad.add_child(self.child)
        self.assertIn(self.child, self.dad.children)
        self.assertEqual(self.child.father, self.dad)

        self.mom.add_child(self.child)
        self.assertIn(self.child, self.mom.children)
        self.assertEqual(self.child.mother, self.mom)

    def test_add_child_error(self):
        self.dad.gender = Gender.UNKNOWN
        with self.assertRaises(RelatedPersonError) as context:
            self.dad.add_child(self.child)
        self.assertRegex(str(context.exception),
                         "cannot add child.*with unknown gender")

        self.dad.gender = Gender.MALE
        self.child.gender = Gender.MALE
        self.child.set_father(self.dad)
        with self.assertRaises(RelatedPersonError) as context:
            self.child.add_child(self.dad)
        self.assertRegex(str(context.exception), "would create ancestor cycle")

        with self.assertRaises(RelatedPersonError) as context:
            self.child.add_child(self.child)
        self.assertRegex(str(context.exception),
                         "cannot add him/herself as a child")

    def test_ancestor(self):
        mindepth = 0
        maxdepth = 1
        self.assertEqual(
            self.root_child.ancestors(mindepth, maxdepth),
            {self.root_child, self.root_child.father, self.root_child.mother})

        mindepth = 1
        maxdepth = None
        self.assertEqual(self.root_child.ancestors(mindepth, maxdepth),
                         {self.root_child.father, self.root_child.mother})

    def test_ancestors_error(self):
        with self.assertRaises(RelatedPersonError) as context:
            self.root_child.ancestors(3, 2)
        self.assertRegex(str(context.exception),
                         "max_depth.*cannot be less than min_depth")

    def test_parents(self):
        self.assertEqual(self.root_child.parents(), set(self.people[1:3]))

    def test_grandparents(self):
        check = {
            self.root_child.father.father, self.root_child.father.mother,
            self.root_child.mother.father, self.root_child.mother.mother
        }
        self.assertEqual(self.root_child.grandparents(), check)

    def test_all_ancestors(self):
        self.assertEqual(self.root_child.all_ancestors(), set(self.people[1:]))

    def test_grandparents_and_earlier(self):
        all_grandparents = set(self.people).difference(
            [self.root_child], self.root_child.parents())
        self.assertEqual(self.root_child.grandparents_and_earlier(),
                         all_grandparents)