|
| 1 | +import pytest |
| 2 | +from gramps.gen.lib.json_utils import data_to_object |
| 3 | + |
| 4 | +from gramps_webapi.api import util |
| 5 | +from gramps_webapi.const import PRIMARY_GRAMPS_OBJECTS |
| 6 | + |
| 7 | + |
| 8 | +def _test_complete_gramps_object_dict(obj_dict): |
| 9 | + util.complete_gramps_object_dict(obj_dict) |
| 10 | + # this will raise an exception if the object dict is not valid |
| 11 | + data_to_object(obj_dict) |
| 12 | + |
| 13 | + |
| 14 | +def test_complete_gramps_object_dict_empty(): |
| 15 | + """Test with empty dictionaries for each primary object""" |
| 16 | + for class_name in PRIMARY_GRAMPS_OBJECTS: |
| 17 | + if class_name == "Family": |
| 18 | + continue |
| 19 | + obj_dict = {"_class": class_name} |
| 20 | + try: |
| 21 | + _test_complete_gramps_object_dict(obj_dict) |
| 22 | + except: |
| 23 | + pytest.fail(f"Failed to complete {class_name} object dict") |
| 24 | + raise |
| 25 | + |
| 26 | + |
| 27 | +def test_complete_gramps_object_dict_nested(): |
| 28 | + """Test with nested objects that need completion.""" |
| 29 | + # Test a Person with incomplete Name object |
| 30 | + person_dict = { |
| 31 | + "_class": "Person", |
| 32 | + "gender": 0, |
| 33 | + "primary_name": {"_class": "Name", "first_name": "John"}, |
| 34 | + } |
| 35 | + _test_complete_gramps_object_dict(person_dict) |
| 36 | + |
| 37 | + # Test an Event with incomplete Place reference |
| 38 | + event_dict = {"_class": "Event", "place": {"_class": "PlaceRef", "ref": "abcd1234"}} |
| 39 | + _test_complete_gramps_object_dict(event_dict) |
| 40 | + |
| 41 | + |
| 42 | +def test_complete_gramps_object_dict_lists(): |
| 43 | + """Test with objects containing lists of other objects.""" |
| 44 | + # Test Person with attribute list |
| 45 | + person_dict = { |
| 46 | + "_class": "Person", |
| 47 | + "attribute_list": [ |
| 48 | + {"_class": "Attribute", "type": "Birth", "value": "Hospital"} |
| 49 | + ], |
| 50 | + } |
| 51 | + _test_complete_gramps_object_dict(person_dict) |
| 52 | + |
| 53 | + |
| 54 | +def test_complete_gramps_object_dict_secondary_objects(): |
| 55 | + """Test with various secondary objects that aren't in PRIMARY_GRAMPS_OBJECTS.""" |
| 56 | + secondary_objects = [ |
| 57 | + "Date", |
| 58 | + "Address", |
| 59 | + "Location", |
| 60 | + "Attribute", |
| 61 | + "Surname", |
| 62 | + "Name", |
| 63 | + "PlaceRef", |
| 64 | + "MediaRef", |
| 65 | + "EventRef", |
| 66 | + "Url", |
| 67 | + ] |
| 68 | + |
| 69 | + for class_name in secondary_objects: |
| 70 | + obj_dict = {"_class": class_name} |
| 71 | + try: |
| 72 | + _test_complete_gramps_object_dict(obj_dict) |
| 73 | + except: |
| 74 | + pytest.fail(f"Failed to complete {class_name} object dict") |
| 75 | + raise |
| 76 | + |
| 77 | + |
| 78 | +def test_complete_gramps_object_dict_with_data(): |
| 79 | + """Test with dictionaries containing partial data.""" |
| 80 | + obj_dict = { |
| 81 | + "_class": "Person", |
| 82 | + "gender": 1, # Female |
| 83 | + "primary_name": { |
| 84 | + "_class": "Name", |
| 85 | + "first_name": "Jane", |
| 86 | + "surname_list": [{"_class": "Surname", "surname": "Doe"}], |
| 87 | + }, |
| 88 | + } |
| 89 | + _test_complete_gramps_object_dict(obj_dict) |
| 90 | + |
| 91 | + # The dictionary should now be complete and can be converted to a Person object |
| 92 | + assert obj_dict["_class"] == "Person" |
| 93 | + assert obj_dict["gender"] == 1 |
| 94 | + assert "attribute_list" in obj_dict |
| 95 | + assert "address_list" in obj_dict |
| 96 | + assert "event_ref_list" in obj_dict |
| 97 | + |
| 98 | + |
| 99 | +def test_complete_gramps_object_dict_non_gramps_dict(): |
| 100 | + """Test with dictionaries that are not Gramps objects.""" |
| 101 | + # Dictionary without _class should be returned unchanged |
| 102 | + obj_dict = {"name": "Test", "value": 123} |
| 103 | + result = util.complete_gramps_object_dict(obj_dict.copy()) |
| 104 | + assert result == obj_dict |
0 commit comments