From 2ebce91ecf96486859d389e343c9e7d2e3aa66d4 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Mon, 23 Mar 2015 23:52:36 -0500 Subject: [PATCH] Add -g flag to runtests.sh to generate test data Add functions to generate sample data for various formats * Zotero Web API JSON (Zotero.Item::toJSON) * CiteProc-JS JSON * Export translator JSON * Direct serialization of Zotero.Item fields Add a way to load sample data into DB from JSON Add tests for loading sample data into DB Add tests for automatically generated data This will help us make sure that field mappings and data formats don't change --- test/components/zotero-unit.js | 3 + test/content/runtests.js | 78 +++++++- test/content/support.js | 315 ++++++++++++++++++++++++++++++++- test/runtests.sh | 39 ++-- test/tests/support.js | 182 ++++++++++++++++++- 5 files changed, 593 insertions(+), 24 deletions(-) diff --git a/test/components/zotero-unit.js b/test/components/zotero-unit.js index a1d3cc6854..2928cbdbd4 100644 --- a/test/components/zotero-unit.js +++ b/test/components/zotero-unit.js @@ -33,6 +33,9 @@ ZoteroUnit.prototype = { handle:function(cmdLine) { this.tests = cmdLine.handleFlagWithParam("test", false); this.noquit = cmdLine.handleFlag("noquit", false); + this.makeTestData = cmdLine.handleFlag("makeTestData", false); + this.noquit = !this.makeTestData && this.noquit; + this.runTests = !this.makeTestData; }, dump:function(x) { diff --git a/test/content/runtests.js b/test/content/runtests.js index 1b3d8dc9b7..7e3ddb1b37 100644 --- a/test/content/runtests.js +++ b/test/content/runtests.js @@ -1,17 +1,17 @@ -Components.utils.import("resource://gre/modules/FileUtils.jsm"); Components.utils.import("resource://gre/modules/osfile.jsm"); Components.utils.import("resource://zotero/q.js"); var EventUtils = Components.utils.import("resource://zotero-unit/EventUtils.jsm"); var ZoteroUnit = Components.classes["@mozilla.org/commandlinehandler/general-startup;1?type=zotero-unit"]. - getService(Components.interfaces.nsISupports). - wrappedJSObject; + getService(Components.interfaces.nsISupports). + wrappedJSObject; + var dump = ZoteroUnit.dump; function quit(failed) { // Quit with exit status if(!failed) { - OS.File.writeAtomic(FileUtils.getFile("ProfD", ["success"]).path, Uint8Array(0)); + OS.File.writeAtomic(OS.Path.join(OS.Constants.Path.profileDir, "success"), new Uint8Array(0)); } if(!ZoteroUnit.noquit) { Components.classes['@mozilla.org/toolkit/app-startup;1']. @@ -20,6 +20,72 @@ function quit(failed) { } } +if (ZoteroUnit.makeTestData) { + let dataPath = getTestDataDirectory().path; + + Zotero.Prefs.set("export.citePaperJournalArticleURL", true); + + let dataFiles = [ + { + name: 'allTypesAndFields', + func: generateAllTypesAndFieldsData + }, + { + name: 'itemJSON', + func: generateItemJSONData, + args: [null] + }, + { + name: 'citeProcJSExport', + func: generateCiteProcJSExportData + }, + { + name: 'translatorExportLegacy', + func: generateTranslatorExportData, + args: [true] + }, + { + name: 'translatorExport', + func: generateTranslatorExportData, + args: [false] + } + ]; + let p = Q.resolve(); + for (let i=0; i m.toLowerCase()); // not all-caps words + } + + itemFields[name] = value; + } + + let creatorTypes = Zotero.CreatorTypes.getTypesForItemType(itemTypes[i].id), + creators = itemFields.creators = []; + for (let j = 0; j < creatorTypes.length; j++) { + let typeName = creatorTypes[j].name; + creators.push({ + creatorType: typeName, + firstName: typeName + 'First', + lastName: typeName + 'Last' + }); + } + } + + return data; +} + +/** + * Populates the database with sample items + * The field values should be in the form exactly as they would appear in Zotero + */ +function populateDBWithSampleData(data) { + Zotero.DB.beginTransaction(); + + for (let itemName in data) { + let item = data[itemName]; + let zItem = new Zotero.Item(item.itemType); + for (let itemField in item) { + if (itemField == 'itemType') continue; + + if (itemField == 'creators') { + let creators = item[itemField]; + for (let i=0; i&2 <