|
8 | 8 | remove_author_honorifics, |
9 | 9 | ) |
10 | 10 | from openlibrary.catalog.utils import InvalidLanguage |
11 | | -from openlibrary.core.models import Author |
| 11 | +from openlibrary.core.models import Author, AuthorRemoteIdConflictError |
12 | 12 |
|
13 | 13 |
|
14 | 14 | @pytest.fixture |
@@ -137,12 +137,103 @@ def test_author_wildcard_match_with_no_matches_creates_author_with_wildcard( |
137 | 137 | new_author_name = import_author(author) |
138 | 138 | assert author["name"] == new_author_name["name"] |
139 | 139 |
|
140 | | - def test_first_match_priority_name_and_dates(self, mock_site): |
| 140 | + def test_first_match_ol_key(self, mock_site): |
141 | 141 | """ |
142 | | - Highest priority match is name, birth date, and death date. |
| 142 | + Highest priority match is OL key. |
143 | 143 | """ |
144 | | - self.add_three_existing_authors(mock_site) |
145 | 144 |
|
| 145 | + # Author with VIAF |
| 146 | + author = { |
| 147 | + "name": "William H. Brewer", |
| 148 | + "key": "/authors/OL3A", |
| 149 | + "type": {"key": "/type/author"}, |
| 150 | + "remote_ids": {"viaf": "12345678"}, |
| 151 | + } |
| 152 | + |
| 153 | + # Another author with VIAF |
| 154 | + author_different_key = { |
| 155 | + "name": "William Brewer", |
| 156 | + "key": "/authors/OL4A", |
| 157 | + "type": {"key": "/type/author"}, |
| 158 | + "remote_ids": {"viaf": "87654321"}, |
| 159 | + } |
| 160 | + |
| 161 | + mock_site.save(author) |
| 162 | + mock_site.save(author_different_key) |
| 163 | + |
| 164 | + # Look for exact match on OL ID, regardless of other fields. |
| 165 | + searched_author = { |
| 166 | + "name": "William H. Brewer", |
| 167 | + "key": "/authors/OL4A", |
| 168 | + } |
| 169 | + found = import_author(searched_author) |
| 170 | + assert found.key == author_different_key["key"] |
| 171 | + |
| 172 | + def test_conflicting_ids_cause_error(self, mock_site): |
| 173 | + # Author with VIAF |
| 174 | + author = { |
| 175 | + "name": "William H. Brewer", |
| 176 | + "key": "/authors/OL3A", |
| 177 | + "type": {"key": "/type/author"}, |
| 178 | + "remote_ids": {"viaf": "12345678"}, |
| 179 | + } |
| 180 | + |
| 181 | + # Another author with VIAF |
| 182 | + author_different_key = { |
| 183 | + "name": "William Brewer", |
| 184 | + "key": "/authors/OL4A", |
| 185 | + "type": {"key": "/type/author"}, |
| 186 | + "remote_ids": {"viaf": "87654321"}, |
| 187 | + } |
| 188 | + |
| 189 | + mock_site.save(author) |
| 190 | + mock_site.save(author_different_key) |
| 191 | + |
| 192 | + # Author with differing ID |
| 193 | + searched_author = { |
| 194 | + "name": "William H. Brewer", |
| 195 | + "key": "/authors/OL4A", |
| 196 | + "remote_ids": {"viaf": "12345678"}, |
| 197 | + } |
| 198 | + with pytest.raises(AuthorRemoteIdConflictError): |
| 199 | + import_author(searched_author) |
| 200 | + |
| 201 | + def test_second_match_remote_identifier(self, mock_site): |
| 202 | + """ |
| 203 | + Next highest priority match is any other remote identifier, such as VIAF, Goodreads ID, Amazon ID, etc. |
| 204 | + """ |
| 205 | + |
| 206 | + # Author with VIAF |
| 207 | + author = { |
| 208 | + "name": "William H. Brewer", |
| 209 | + "key": "/authors/OL3A", |
| 210 | + "type": {"key": "/type/author"}, |
| 211 | + "remote_ids": {"viaf": "12345678"}, |
| 212 | + } |
| 213 | + |
| 214 | + # Another author with VIAF |
| 215 | + author_different_viaf = { |
| 216 | + "name": "William Brewer", |
| 217 | + "key": "/authors/OL4A", |
| 218 | + "type": {"key": "/type/author"}, |
| 219 | + "remote_ids": {"viaf": "87654321"}, |
| 220 | + } |
| 221 | + |
| 222 | + mock_site.save(author) |
| 223 | + mock_site.save(author_different_viaf) |
| 224 | + |
| 225 | + # Look for exact match on VIAF, regardless of name field. |
| 226 | + searched_author = { |
| 227 | + "name": "William Brewer", |
| 228 | + "remote_ids": {"viaf": "12345678"}, |
| 229 | + } |
| 230 | + found = import_author(searched_author) |
| 231 | + assert found.key == author["key"] |
| 232 | + |
| 233 | + def test_third_match_priority_name_and_dates(self, mock_site): |
| 234 | + """ |
| 235 | + Next highest priority match is name, birth date, and death date. |
| 236 | + """ |
146 | 237 | # Exact name match with no birth or death date |
147 | 238 | author = { |
148 | 239 | "name": "William H. Brewer", |
@@ -202,7 +293,7 @@ def test_non_matching_birth_death_creates_new_author(self, mock_site): |
202 | 293 | assert isinstance(found, dict) |
203 | 294 | assert found["death_date"] == searched_and_not_found_author["death_date"] |
204 | 295 |
|
205 | | - def test_second_match_priority_alternate_names_and_dates(self, mock_site): |
| 296 | + def test_match_priority_alternate_names_and_dates(self, mock_site): |
206 | 297 | """ |
207 | 298 | Matching, as a unit, alternate name, birth date, and death date, get |
208 | 299 | second match priority. |
|
0 commit comments