2015-03-09 18:25:49 +00:00
|
|
|
describe("Support Functions for Unit Testing", function() {
|
|
|
|
describe("resetDB", function() {
|
2021-06-24 10:07:33 +00:00
|
|
|
it("should restore the DB to factory settings", async function () {
|
|
|
|
await Zotero.DB.queryAsync("CREATE TABLE testTable (foo INTEGER PRIMARY KEY)");
|
|
|
|
assert.isTrue(await Zotero.DB.tableExists('testTable'));
|
|
|
|
await resetDB({
|
2015-09-29 08:07:26 +00:00
|
|
|
thisArg: this
|
|
|
|
});
|
2021-06-24 10:07:33 +00:00
|
|
|
assert.isFalse(await Zotero.DB.tableExists('testTable'));
|
2015-04-15 05:32:40 +00:00
|
|
|
});
|
2015-03-09 18:25:49 +00:00
|
|
|
});
|
2015-03-24 04:52:36 +00:00
|
|
|
describe("loadSampleData", function() {
|
|
|
|
it("should load data from file", function() {
|
|
|
|
let data = loadSampleData('journalArticle');
|
|
|
|
assert.isObject(data, 'loaded data object');
|
|
|
|
assert.isNotNull(data);
|
|
|
|
assert.isAbove(Object.keys(data).length, 0, 'data object is not empty');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("populateDBWithSampleData", function() {
|
2015-06-01 03:59:15 +00:00
|
|
|
it("should populate database with data", Zotero.Promise.coroutine(function* () {
|
2015-03-24 04:52:36 +00:00
|
|
|
let data = loadSampleData('journalArticle');
|
2015-06-01 03:59:15 +00:00
|
|
|
yield populateDBWithSampleData(data);
|
2015-03-24 04:52:36 +00:00
|
|
|
|
|
|
|
let skipFields = ['id', 'itemType', 'creators']; // Special comparisons
|
|
|
|
|
|
|
|
for (let itemName in data) {
|
|
|
|
let item = data[itemName];
|
|
|
|
assert.isAbove(item.id, 0, 'assigned new item ID');
|
|
|
|
|
2015-06-01 03:59:15 +00:00
|
|
|
let zItem = yield Zotero.Items.getAsync(item.id);
|
2015-03-24 04:52:36 +00:00
|
|
|
assert.ok(zItem, 'inserted item into database');
|
|
|
|
|
|
|
|
// Compare item type
|
|
|
|
assert.equal(item.itemType, Zotero.ItemTypes.getName(zItem.itemTypeID), 'inserted item has the same item type');
|
|
|
|
|
|
|
|
// Compare simple properties
|
|
|
|
for (let prop in item) {
|
|
|
|
if (skipFields.indexOf(prop) != -1) continue;
|
|
|
|
|
|
|
|
// Using base-mapped fields
|
2015-06-07 22:38:00 +00:00
|
|
|
let field = zItem.getField(prop, false, true);
|
|
|
|
if (prop === "accessDate") field = Zotero.Date.sqlToISO8601(field);
|
|
|
|
assert.equal(field, item[prop], 'inserted item property has the same value as sample data');
|
2015-03-24 04:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.creators) {
|
|
|
|
// Compare creators
|
|
|
|
for (let i=0; i<item.creators.length; i++) {
|
|
|
|
let creator = item.creators[i];
|
|
|
|
let zCreator = zItem.getCreator(i);
|
|
|
|
assert.ok(zCreator, 'creator was added to item');
|
2015-06-01 03:59:15 +00:00
|
|
|
assert.equal(creator.firstName, zCreator.firstName, 'first names match');
|
|
|
|
assert.equal(creator.lastName, zCreator.lastName, 'last names match');
|
2015-03-24 04:52:36 +00:00
|
|
|
assert.equal(creator.creatorType, Zotero.CreatorTypes.getName(zCreator.creatorTypeID), 'creator types match');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-01 03:59:15 +00:00
|
|
|
}));
|
|
|
|
it("should populate items with tags", Zotero.Promise.coroutine(function* () {
|
|
|
|
let data = yield populateDBWithSampleData({
|
2015-03-24 04:52:36 +00:00
|
|
|
itemWithTags: {
|
|
|
|
itemType: "journalArticle",
|
|
|
|
tags: [
|
|
|
|
{ tag: "automatic tag", type: 0 },
|
|
|
|
{ tag: "manual tag", type: 1}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-01 03:59:15 +00:00
|
|
|
let zItem = yield Zotero.Items.getAsync(data.itemWithTags.id);
|
2015-03-24 04:52:36 +00:00
|
|
|
assert.ok(zItem, 'inserted item with tags into database');
|
|
|
|
|
2015-06-01 03:59:15 +00:00
|
|
|
|
2015-03-24 04:52:36 +00:00
|
|
|
let tags = data.itemWithTags.tags;
|
|
|
|
for (let i=0; i<tags.length; i++) {
|
2016-04-21 15:07:16 +00:00
|
|
|
let tagID = Zotero.Tags.getID(tags[i].tag);
|
2015-03-24 04:52:36 +00:00
|
|
|
assert.ok(tagID, '"' + tags[i].tag + '" tag was inserted into the database');
|
2015-06-01 03:59:15 +00:00
|
|
|
assert.ok(zItem.hasTag(tags[i].tag), '"' + tags[i].tag + '" tag was assigned to item');
|
2015-03-24 04:52:36 +00:00
|
|
|
}
|
2015-06-01 03:59:15 +00:00
|
|
|
}));
|
2015-03-24 04:52:36 +00:00
|
|
|
});
|
|
|
|
describe("generateAllTypesAndFieldsData", function() {
|
|
|
|
it("should generate all types and fields data", function() {
|
|
|
|
let data = generateAllTypesAndFieldsData();
|
|
|
|
assert.isObject(data, 'created data object');
|
|
|
|
assert.isNotNull(data);
|
|
|
|
assert.isAbove(Object.keys(data).length, 0, 'data object is not empty');
|
|
|
|
});
|
|
|
|
it("all types and fields sample data should be up to date", function() {
|
|
|
|
assert.deepEqual(loadSampleData('allTypesAndFields'), generateAllTypesAndFieldsData());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("generateItemJSONData", function() {
|
2015-06-01 03:59:15 +00:00
|
|
|
it("item JSON data should be up to date", Zotero.Promise.coroutine(function* () {
|
2015-03-24 04:52:36 +00:00
|
|
|
let oldData = loadSampleData('itemJSON'),
|
2015-06-01 03:59:15 +00:00
|
|
|
newData = yield generateItemJSONData();
|
2015-03-24 04:52:36 +00:00
|
|
|
|
|
|
|
assert.isObject(newData, 'created data object');
|
|
|
|
assert.isNotNull(newData);
|
|
|
|
assert.isAbove(Object.keys(newData).length, 0, 'data object is not empty');
|
|
|
|
|
|
|
|
// Ignore data that is not stable, but make sure it is set
|
|
|
|
let ignoreFields = ['dateAdded', 'dateModified', 'key'];
|
|
|
|
for (let itemName in oldData) {
|
|
|
|
for (let i=0; i<ignoreFields.length; i++) {
|
|
|
|
let field = ignoreFields[i]
|
|
|
|
if (oldData[itemName][field] !== undefined) {
|
|
|
|
assert.isDefined(newData[itemName][field], field + ' is set');
|
|
|
|
delete oldData[itemName][field];
|
|
|
|
delete newData[itemName][field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-01 03:59:15 +00:00
|
|
|
|
2015-03-24 04:52:36 +00:00
|
|
|
assert.deepEqual(oldData, newData);
|
2015-06-01 03:59:15 +00:00
|
|
|
}));
|
2015-03-24 04:52:36 +00:00
|
|
|
});
|
2015-06-01 03:59:15 +00:00
|
|
|
// describe("generateCiteProcJSExportData", function() {
|
|
|
|
// let citeURL = Zotero.Prefs.get("export.citePaperJournalArticleURL");
|
|
|
|
// before(function () {
|
|
|
|
// Zotero.Prefs.set("export.citePaperJournalArticleURL", true);
|
|
|
|
// });
|
|
|
|
// after(function() {
|
|
|
|
// Zotero.Prefs.set("export.citePaperJournalArticleURL", citeURL);
|
|
|
|
// });
|
2015-03-24 04:52:36 +00:00
|
|
|
|
2015-06-01 03:59:15 +00:00
|
|
|
// it("all citeproc-js export data should be up to date", Zotero.Promise.coroutine(function* () {
|
|
|
|
// let oldData = loadSampleData('citeProcJSExport'),
|
|
|
|
// newData = yield generateCiteProcJSExportData();
|
|
|
|
|
|
|
|
// assert.isObject(newData, 'created data object');
|
|
|
|
// assert.isNotNull(newData);
|
|
|
|
// assert.isAbove(Object.keys(newData).length, 0, 'citeproc-js export object is not empty');
|
|
|
|
|
|
|
|
// // Ignore item ID
|
|
|
|
// for (let itemName in oldData) {
|
|
|
|
// delete oldData[itemName].id;
|
|
|
|
// }
|
|
|
|
// for (let itemName in newData) {
|
|
|
|
// delete newData[itemName].id;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// assert.deepEqual(oldData, newData, 'citeproc-js export data has not changed');
|
|
|
|
// }));
|
|
|
|
// });
|
2015-03-24 04:52:36 +00:00
|
|
|
describe("generateTranslatorExportData", function() {
|
2015-06-01 03:59:15 +00:00
|
|
|
it("legacy mode data should be up to date", Zotero.Promise.coroutine(function* () {
|
2015-03-24 04:52:36 +00:00
|
|
|
let oldData = loadSampleData('translatorExportLegacy'),
|
2015-06-01 03:59:15 +00:00
|
|
|
newData = yield generateTranslatorExportData(true);
|
2015-03-24 04:52:36 +00:00
|
|
|
|
|
|
|
assert.isObject(newData, 'created data object');
|
|
|
|
assert.isNotNull(newData);
|
|
|
|
assert.isAbove(Object.keys(newData).length, 0, 'translator export object is not empty');
|
|
|
|
|
|
|
|
// Ignore data that is not stable, but make sure it is set
|
|
|
|
let ignoreFields = ['itemID', 'dateAdded', 'dateModified', 'uri', 'key'];
|
|
|
|
for (let itemName in oldData) {
|
|
|
|
for (let i=0; i<ignoreFields.length; i++) {
|
|
|
|
let field = ignoreFields[i]
|
|
|
|
if (oldData[itemName][field] !== undefined) {
|
|
|
|
assert.isDefined(newData[itemName][field], field + ' is set');
|
|
|
|
delete oldData[itemName][field];
|
|
|
|
delete newData[itemName][field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual(oldData, newData, 'translator export data has not changed');
|
2015-06-01 03:59:15 +00:00
|
|
|
}));
|
|
|
|
it("data should be up to date", Zotero.Promise.coroutine(function* () {
|
2015-03-24 04:52:36 +00:00
|
|
|
let oldData = loadSampleData('translatorExport'),
|
2015-06-01 03:59:15 +00:00
|
|
|
newData = yield generateTranslatorExportData();
|
2015-03-24 04:52:36 +00:00
|
|
|
|
|
|
|
assert.isObject(newData, 'created data object');
|
|
|
|
assert.isNotNull(newData);
|
|
|
|
assert.isAbove(Object.keys(newData).length, 0, 'translator export object is not empty');
|
|
|
|
|
|
|
|
// Ignore data that is not stable, but make sure it is set
|
|
|
|
let ignoreFields = ['dateAdded', 'dateModified', 'uri'];
|
|
|
|
for (let itemName in oldData) {
|
|
|
|
for (let i=0; i<ignoreFields.length; i++) {
|
|
|
|
let field = ignoreFields[i]
|
|
|
|
if (oldData[itemName][field] !== undefined) {
|
|
|
|
assert.isDefined(newData[itemName][field], field + ' is set');
|
|
|
|
delete oldData[itemName][field];
|
|
|
|
delete newData[itemName][field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual(oldData, newData, 'translator export data has not changed');
|
2015-06-01 03:59:15 +00:00
|
|
|
}));
|
2015-03-24 04:52:36 +00:00
|
|
|
});
|
2015-03-09 18:25:49 +00:00
|
|
|
});
|