|
8 | 8 | from poster2json.ror import ( |
9 | 9 | RorClient, |
10 | 10 | _enrich_affiliation_item, |
| 11 | + coerce_person_affiliations, |
| 12 | + dedupe_person_affiliations, |
11 | 13 | enrich_persons, |
12 | 14 | ) |
13 | 15 |
|
@@ -94,6 +96,82 @@ def test_cache_records_negative_lookups(tmp_path, monkeypatch): |
94 | 96 | assert client.lookup("Unknown Inst") is None |
95 | 97 |
|
96 | 98 |
|
| 99 | +def test_coerce_bare_string_affiliation_to_list(): |
| 100 | + # The schema-violating shape reported in the field: affiliation is a string. |
| 101 | + persons = [{ |
| 102 | + "name": "Tedla, Saron", |
| 103 | + "affiliation": "Oregon Health & Science University, School of Medicine, Portland, OR, USA", |
| 104 | + }] |
| 105 | + out = coerce_person_affiliations(persons) |
| 106 | + assert out[0]["affiliation"] == [ |
| 107 | + "Oregon Health & Science University, School of Medicine, Portland, OR, USA" |
| 108 | + ] |
| 109 | + |
| 110 | + |
| 111 | +def test_coerce_single_object_to_list(): |
| 112 | + persons = [{"name": "A", "affiliation": {"name": "MIT"}}] |
| 113 | + out = coerce_person_affiliations(persons) |
| 114 | + assert out[0]["affiliation"] == [{"name": "MIT"}] |
| 115 | + |
| 116 | + |
| 117 | +def test_coerce_drops_null_empty_and_junk(): |
| 118 | + persons = [ |
| 119 | + {"name": "A", "affiliation": None}, |
| 120 | + {"name": "B", "affiliation": ""}, |
| 121 | + {"name": "C", "affiliation": []}, |
| 122 | + {"name": "D", "affiliation": [" ", None, 5, {"foo": "bar"}]}, |
| 123 | + {"name": "E"}, |
| 124 | + ] |
| 125 | + out = coerce_person_affiliations(persons) |
| 126 | + assert "affiliation" not in out[0] |
| 127 | + assert "affiliation" not in out[1] |
| 128 | + assert "affiliation" not in out[2] |
| 129 | + assert "affiliation" not in out[3] # all items were junk |
| 130 | + assert "affiliation" not in out[4] |
| 131 | + |
| 132 | + |
| 133 | +def test_coerce_filters_blank_list_items_but_keeps_valid(): |
| 134 | + persons = [{"name": "A", "affiliation": ["Stanford", " ", {"name": "MIT"}, {}]}] |
| 135 | + out = coerce_person_affiliations(persons) |
| 136 | + assert out[0]["affiliation"] == ["Stanford", {"name": "MIT"}] |
| 137 | + |
| 138 | + |
| 139 | +def test_dedupe_identical_objects(): |
| 140 | + # The other reported shape: same ROR object listed twice (Jalili / UCSD). |
| 141 | + ucsd = { |
| 142 | + "name": "University of California San Diego", |
| 143 | + "schemeUri": "https://ror.org/", |
| 144 | + "affiliationIdentifier": "https://ror.org/0168r3w48", |
| 145 | + "affiliationIdentifierScheme": "ROR", |
| 146 | + } |
| 147 | + persons = [{"name": "Jalili, Jalil", "affiliation": [dict(ucsd), dict(ucsd)]}] |
| 148 | + out = dedupe_person_affiliations(persons) |
| 149 | + assert out[0]["affiliation"] == [ucsd] |
| 150 | + |
| 151 | + |
| 152 | +def test_dedupe_identical_strings(): |
| 153 | + persons = [{"name": "A", "affiliation": ["MIT", "mit", "MIT "]}] |
| 154 | + out = dedupe_person_affiliations(persons) |
| 155 | + assert out[0]["affiliation"] == ["MIT"] |
| 156 | + |
| 157 | + |
| 158 | +def test_dedupe_prefers_identified_entry_over_bare_name(): |
| 159 | + persons = [{"name": "A", "affiliation": [ |
| 160 | + "Stanford University", |
| 161 | + {"name": "Stanford University", "affiliationIdentifier": "https://ror.org/00f54p054"}, |
| 162 | + ]}] |
| 163 | + out = dedupe_person_affiliations(persons) |
| 164 | + assert out[0]["affiliation"] == [ |
| 165 | + {"name": "Stanford University", "affiliationIdentifier": "https://ror.org/00f54p054"} |
| 166 | + ] |
| 167 | + |
| 168 | + |
| 169 | +def test_dedupe_keeps_distinct_affiliations_and_order(): |
| 170 | + persons = [{"name": "A", "affiliation": ["MIT", "Stanford", "MIT"]}] |
| 171 | + out = dedupe_person_affiliations(persons) |
| 172 | + assert out[0]["affiliation"] == ["MIT", "Stanford"] |
| 173 | + |
| 174 | + |
97 | 175 | def test_strip_trailing_country(): |
98 | 176 | from poster2json.ror import _strip_trailing_country |
99 | 177 |
|
|
0 commit comments