Mendeley importer: Fix issue with empty tags (#3018)

Also adds a test for this particular case and for importing tags in
general.
This commit is contained in:
Tom Najdek 2023-03-09 22:13:19 +01:00 committed by GitHub
parent a1d06066b2
commit c57c3c470e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 3 deletions

View file

@ -680,7 +680,10 @@ Zotero_Import_Mendeley.prototype._getDocumentTagsDB = async function (groupID) {
Zotero_Import_Mendeley.prototype._getDocumentTagsAPI = async function (documents) {
var map = new Map();
for (let doc of documents) {
const tags = [...(doc.tags || []).map(tag => ({ tag, type: 0 })), ...(doc.keywords || []).map(tag => ({ tag, type: 1 }))];
const tags = [
...(doc.tags || []).map(tag => ({ tag, type: 0 })),
...(doc.keywords || []).map(tag => ({ tag, type: 1 }))
].filter(t => t.tag && t.tag.trim());
map.set(doc.id, tags);
}
return map;

View file

@ -65,5 +65,33 @@
"8d2f262d-49b3-4dfc-8968-0bb71bcd92ea"
],
"year": 1987
},
{
"authored": false,
"confirmed": true,
"created": "2023-03-07T13:30:46.353Z",
"file_attached": false,
"hidden": false,
"id": "49426b47-d9ff-4ab6-ba2c-54e3608f56b8",
"client_data": "{\"desktop_id\":\"c7ec2737-044a-493b-9d94-d7f67be68765\"}",
"last_modified": "2023-03-07T13:30:46.025Z",
"private_publication": false,
"profile_id": "8dbf0832-8723-4c48-b532-20c0b7f6e01a",
"read": false,
"source": "something wrong",
"identifiers": {
"doi": "10.1111",
"pmid": "PMID: 11111111",
"arxiv": "1111.2222"
},
"starred": false,
"tags": ["", ""],
"keywords": [],
"title": "This one has empty tags and keywords",
"type": "journal",
"folder_uuids": [
"8d2f262d-49b3-4dfc-8968-0bb71bcd92ea"
],
"year": 1987
}
]

View file

@ -87,5 +87,30 @@
"filehash": "cc22c6611277df346ff8dc7386ba3880b2bafa15"
}
]
},
{
"authored": false,
"confirmed": true,
"created": "2021-11-04T11:53:10.353Z",
"file_attached": false,
"hidden": false,
"id": "cda0c150-bc85-4312-af2c-61a29b179595",
"client_data": "{\"desktop_id\":\"4308d8ec-e8ea-43fb-9d38-4e6628f7c10a\"}",
"last_modified": "2021-11-04T11:53:10.353Z",
"private_publication": false,
"profile_id": "8dbf0832-8723-4c48-b532-20c0b7f6e01a",
"read": false,
"starred": false,
"tags": [
"tag1",
"tag2"
],
"keywords": [
"keyword1",
"keyword2"
],
"title": "Has tags",
"type": "report",
"year": 2002
}
]

View file

@ -176,6 +176,11 @@ describe('Zotero_Import_Mendeley', function () {
.filter(item => item.libraryID == Zotero.Libraries.userLibraryID && !item.deleted)
.shift();
const withTags = (await Zotero.Relations
.getByPredicateAndObject('item', 'mendeleyDB:documentUUID', '4308d8ec-e8ea-43fb-9d38-4e6628f7c10a'))
.filter(item => item.libraryID == Zotero.Libraries.userLibraryID && !item.deleted)
.shift();
assert.equal(journal.getRelations()['mendeleyDB:remoteDocumentUUID'], '7fea3cb3-f97d-3f16-8fad-f59caaa71688');
assert.equal(journal.getField('title'), 'Foo Bar');
@ -197,6 +202,17 @@ describe('Zotero_Import_Mendeley', function () {
assert.equal(journal.getField('DOI'), '10.1111');
assert.sameMembers(journal.getField('extra').split('\n'), ['PMID: 11111111', 'arXiv: 1111.2222']);
// tags
assert.equal(withTags.getTags().length, 4);
assert.sameMembers(
withTags.getTags().filter(t => t.type === 1).map(t => t.tag),
['keyword1', 'keyword2']
);
assert.sameMembers(
withTags.getTags().filter(t => !t.type).map(t => t.tag),
['tag1', 'tag2']
);
// attachment & annotations
assert.lengthOf(withpdf.getAttachments(), 1);
assert.equal(pdf.parentID, withpdf.id);
@ -453,14 +469,14 @@ describe('Zotero_Import_Mendeley', function () {
assert.lengthOf(noNewItemHere, 0);
});
it("should handle empty creators", async () => {
it("should handle empty creators and tags", async () => {
setHTTPResponse(server, 'https://api.mendeley.com/', {
method: 'GET',
url: `documents?view=all&limit=500`,
status: 200,
headers: {},
json: JSON.parse(
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/items-empty-creators.json')
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/items-bad-data.json')
)
});
@ -486,6 +502,14 @@ describe('Zotero_Import_Mendeley', function () {
assert.equal(journalEmptyAuthors.getField('title'), 'This one has empty authors');
assert.equal(journalEmptyAuthors.getCreators().length, 0);
const journalEmptyTags = (await Zotero.Relations
.getByPredicateAndObject('item', 'mendeleyDB:documentUUID', 'c7ec2737-044a-493b-9d94-d7f67be68765'))
.filter(item => item.libraryID == Zotero.Libraries.userLibraryID && !item.deleted)
.shift();
assert.equal(journalEmptyTags.getField('title'), 'This one has empty tags and keywords');
assert.equal(journalEmptyTags.getTags().length, 0);
});
});
});