def test_gen_file(self): user = User(username="******") user.save() tree = Tree(title="Test", creator=user) tree.save() spouse_1_legal_name = LegalName(first_name="Betty", tree=tree) spouse_1_legal_name.save() spouse_1 = Person(legal_name=spouse_1_legal_name, gender='Female', tree=tree) spouse_1.save() spouse_2_legal_name = LegalName(first_name="Kyle", tree=tree) spouse_2_legal_name.save() spouse_2 = Person(legal_name=spouse_2_legal_name, gender='Male', tree=tree) spouse_2.save() child_legal_name = LegalName(first_name="Symphony", tree=tree) child_legal_name.save() child = Person(legal_name=child_legal_name, gender="Unknown", tree=tree) child.save() marriage_date = datetime.datetime(2000, 12, 1) divorce_date = datetime.datetime(2100, 12, 2) partnership = Partnership( marriage_date=marriage_date, divorce_date=divorce_date, marital_status=Partnership.MaritalStatus.MARRIED, tree=tree) partnership.save() partnership.children.add(child) partnership.save() person_partnership_1 = PersonPartnership(person=spouse_1, partnership=partnership) person_partnership_1.save() person_partnership_2 = PersonPartnership(person=spouse_2, partnership=partnership) person_partnership_2.save() root = gedcom_generator.generate_file(tree) parser = Parser() parser.parse([]) expected = parser.get_root_element() head_element, submitter_element = gedcom_generator.gen_head_and_submitter( tree) expected.add_child_element(head_element) expected.add_child_element(submitter_element) ptr, spouse_1_element = gedcom_generator.gen_individual(spouse_1) expected.add_child_element(spouse_1_element) ptr, spouse_2_element = gedcom_generator.gen_individual(spouse_2) expected.add_child_element(spouse_2_element) ptr, child_element = gedcom_generator.gen_individual(child) expected.add_child_element(child_element) ptr, family = gedcom_generator.gen_family(partnership) expected.add_child_element(family) self.assertTrue(gedcom_helpers.element_equals(root, expected))
def test_to_gedcom_string(): # From string case_1 = """0 @I5@ INDI 1 NAME First /Last/ 1 SEX M 1 BIRT 2 DATE 1 JAN 1900 2 PLAC Kirkland, King, Washington, USA 3 MAP 4 LATI N47.680663 4 LONG W122.234319 """ gedcom_parser = Parser() gedcom_parser.parse([(a + '\n').encode('utf-8-sig') for a in case_1.splitlines()]) case_1_string_array = case_1.splitlines() gedcom_string = gedcom_parser.to_gedcom_string(True) gedcom_string_array = gedcom_string.splitlines() # Check number of lines assert len(case_1_string_array) == len(gedcom_string_array) == 9 # Check each line for i in range(len(case_1_string_array)): assert case_1_string_array[i] == gedcom_string_array[i] # Check whole file string assert gedcom_string == case_1 # From file case_2 = "" with open('tests/files/Musterstammbaum.ged', 'rb') as gedcom_stream: for line in gedcom_stream: case_2 += line.decode('utf-8-sig') gedcom_parser.parse_file('tests/files/Musterstammbaum.ged') case_2_string_array = case_2.splitlines() gedcom_string = gedcom_parser.to_gedcom_string(True) gedcom_string_array = gedcom_string.splitlines() # Check number of lines assert len(case_2_string_array) == len(gedcom_string_array) == 396 # Check each line for i in range(len(case_2_string_array)): assert case_2_string_array[i] == gedcom_string_array[i] # Check whole file string assert gedcom_string == case_2
def test_parse_from_string(): case_1 = """0 @I5@ INDI 1 NAME First /Last/ 1 SEX M 1 BIRT 2 DATE 1 JAN 1900 2 PLAC Kirkland, King, Washington, USA 3 MAP 4 LATI N47.680663 4 LONG W122.234319 """ gedcom_parser = Parser() gedcom_parser.parse([(a + '\n').encode('utf-8-sig') for a in case_1.splitlines()]) element_1 = gedcom_parser.get_root_child_elements()[0] assert isinstance(element_1, IndividualElement) assert element_1.get_tag() == 'INDI' assert element_1.get_pointer() == '@I5@' element_1_children = element_1.get_child_elements() assert len(element_1_children) == 3 assert element_1_children[0].get_tag() == 'NAME' assert element_1_children[1].get_tag() == 'SEX' assert element_1_children[2].get_tag() == 'BIRT' case_2 = """0 @F28@ FAM 1 HUSB @I80@ 1 WIFE @I81@ 1 CHIL @I9@ 2 _FREL Natural 2 _MREL Natural 1 CHIL @I84@ 2 _FREL Natural 2 _MREL Natural 1 CHIL @I85@ 2 _FREL Natural 2 _MREL Natural """ gedcom_parser.parse([(a + '\n').encode('utf-8-sig') for a in case_2.splitlines()]) element_2 = gedcom_parser.get_root_child_elements()[0] assert element_2.get_tag() == 'FAM' assert element_2.get_pointer() == '@F28@' element_2_children = element_2.get_child_elements() assert len(element_2_children) == 5 assert element_2_children[0].get_tag() == 'HUSB' assert element_2_children[1].get_tag() == 'WIFE' assert element_2_children[2].get_tag() == 'CHIL' assert element_2_children[3].get_value() == '@I84@'
def get_root_element(f): parser = Parser() parser.parse(f, strict=False) root = parser.get_root_element() return root