mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-22 12:01:15 -04:00
Avoid the need to manually select Evopedia and ZIM test files to run unit tests.
It should help to automate unit testing in #119
This commit is contained in:
parent
15a4c04e62
commit
7bb77c8e36
10
tests.html
10
tests.html
@ -39,11 +39,11 @@
|
||||
|
||||
</head>
|
||||
<body>
|
||||
Please select all the files inside the archive wikipedia_small_2010-08-14 (included with source code) : wikipedia_*.dat, metadata.txt etc<br />
|
||||
<input type="file" id="evopediaArchiveFiles" multiple /><br />
|
||||
Please select wikipedia_en_ray_charles_2015-06.zim file (included with source code too)<br />
|
||||
<input type="file" id="zimArchiveFiles" multiple /><br />
|
||||
<input type="button" id="runTests" value="Run tests" />
|
||||
Depending on the browser you use, you should launch these unit tests differently : <br/>
|
||||
<ul>
|
||||
<li>Open this page from a file:/// URL for Firefox (for a reason I have to find, there's an error when running from an http://localhost URL)</li>
|
||||
<li>Open this page from a http:// URL for Chromium/Chrome (through a local webserver, for example), because it does not allow to run a web worker from a file:/// URL</li>
|
||||
</ul>
|
||||
<div id="qunit"></div>
|
||||
</body>
|
||||
</html>
|
@ -21,14 +21,80 @@
|
||||
*/
|
||||
define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geometry'],
|
||||
function($, evopediaTitle, evopediaArchive, zimArchive, zimDirEntry, util, geometry) {
|
||||
|
||||
var localEvopediaArchive;
|
||||
var localZimArchive;
|
||||
|
||||
// Due to security restrictions in the browsers,
|
||||
// we can not read directly the files and run the unit tests
|
||||
// The user has to select them manually, then launch the tests
|
||||
$('#runTests').on('click', function(e) {
|
||||
runTests();
|
||||
|
||||
/**
|
||||
* Make an HTTP request for a Blob and return a Promise
|
||||
*
|
||||
* @param {String} url URL to download from
|
||||
* @param {String} name Name to give to the Blob instance
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function makeBlobRequest(url, name) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', url);
|
||||
xhr.onload = function () {
|
||||
if (this.status >= 200 && this.status < 300) {
|
||||
var blob = new Blob([xhr.response], {type: 'application/octet-stream'});
|
||||
blob.name = name;
|
||||
resolve(blob);
|
||||
} else {
|
||||
reject({
|
||||
status: this.status,
|
||||
statusText: xhr.statusText
|
||||
});
|
||||
}
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
reject({
|
||||
status: this.status,
|
||||
statusText: xhr.statusText
|
||||
});
|
||||
};
|
||||
xhr.responseType = 'blob';
|
||||
xhr.send();
|
||||
});
|
||||
}
|
||||
|
||||
// Let's try to download the Evopedia and ZIM files
|
||||
var evopediaArchiveFiles = new Array();
|
||||
var zimArchiveFiles = new Array();
|
||||
|
||||
var blob1 = makeBlobRequest('tests/wikipedia_small_2010-08-14/wikipedia_00.dat', 'wikipedia_00.dat');
|
||||
var blob2 = makeBlobRequest('tests/wikipedia_small_2010-08-14/titles.idx', 'titles.idx');
|
||||
var blob3 = makeBlobRequest('tests/wikipedia_small_2010-08-14/metadata.txt', 'metadata.txt');
|
||||
var blob4 = makeBlobRequest('tests/wikipedia_small_2010-08-14/math.idx', 'math.idx');
|
||||
var blob5 = makeBlobRequest('tests/wikipedia_small_2010-08-14/math.dat', 'math.dat');
|
||||
var blob6 = makeBlobRequest('tests/wikipedia_small_2010-08-14/coordinates_01.idx', 'coordinates_01.idx');
|
||||
var blob7 = makeBlobRequest('tests/wikipedia_small_2010-08-14/coordinates_02.idx', 'coordinates_02.idx');
|
||||
var blob8 = makeBlobRequest('tests/wikipedia_small_2010-08-14/coordinates_03.idx', 'coordinates_03.idx');
|
||||
var blob9 = makeBlobRequest('tests/wikipedia_en_ray_charles_2015-06.zim', 'wikipedia_en_ray_charles_2015-06.zim');
|
||||
Promise.all([blob1, blob2, blob3, blob4, blob5, blob6, blob7, blob8, blob9])
|
||||
.then(function(values) {
|
||||
evopediaArchiveFiles.push(values[0]);
|
||||
evopediaArchiveFiles.push(values[1]);
|
||||
evopediaArchiveFiles.push(values[2]);
|
||||
evopediaArchiveFiles.push(values[3]);
|
||||
evopediaArchiveFiles.push(values[4]);
|
||||
evopediaArchiveFiles.push(values[5]);
|
||||
evopediaArchiveFiles.push(values[6]);
|
||||
evopediaArchiveFiles.push(values[7]);
|
||||
zimArchiveFiles.push(values[8]);
|
||||
}).then(function() {
|
||||
// Create a localEvopediaArchive and a localZimArchive from selected files, in order to run the following tests
|
||||
localEvopediaArchive = new evopediaArchive.LocalArchive();
|
||||
localEvopediaArchive.initializeFromArchiveFiles(evopediaArchiveFiles, function(archive) {
|
||||
var zimFile = zimArchiveFiles[0];
|
||||
localZimArchive = new zimArchive.ZIMArchive(zimFile, null, function (zimArchive) {
|
||||
runTests();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
var runTests = function() {
|
||||
|
||||
module("environment");
|
||||
@ -36,20 +102,12 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom
|
||||
equal("test", "test", "QUnit is properly configured");
|
||||
});
|
||||
|
||||
test("check archive files are selected", function() {
|
||||
var evopediaArchiveFiles = document.getElementById('evopediaArchiveFiles').files;
|
||||
test("check archive files are read", function() {
|
||||
ok(evopediaArchiveFiles && evopediaArchiveFiles[0] && evopediaArchiveFiles[0].size > 0, "First archive file set and not empty");
|
||||
ok(evopediaArchiveFiles.length >= 5, "At least 5 files are selected");
|
||||
var zimArchiveFiles = document.getElementById('zimArchiveFiles').files;
|
||||
ok(zimArchiveFiles && zimArchiveFiles[0] && zimArchiveFiles[0].size > 0, "ZIM file set and not empty");
|
||||
ok(evopediaArchiveFiles.length >= 5, "At least 5 files are read");
|
||||
ok(zimArchiveFiles && zimArchiveFiles[0] && zimArchiveFiles[0].size > 0, "ZIM file read and not empty");
|
||||
});
|
||||
|
||||
// Create a localEvopediaArchive and a localZimArchive from selected files, in order to run the following tests
|
||||
var localEvopediaArchive = new evopediaArchive.LocalArchive();
|
||||
localEvopediaArchive.initializeFromArchiveFiles(document.getElementById('evopediaArchiveFiles').files);
|
||||
var zimFile = document.getElementById('zimArchiveFiles').files[0];
|
||||
var localZimArchive = new zimArchive.ZIMArchive(zimFile);
|
||||
|
||||
module("evopedia_title_search_and_read");
|
||||
asyncTest("check getTitlesStartingAtOffset 0", function() {
|
||||
expect(4);
|
||||
@ -448,7 +506,6 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom
|
||||
asyncTest("article '(The Night Time Is) The Right Time' correctly redirects to 'Night Time Is the Right Time'", function() {
|
||||
expect(6);
|
||||
localZimArchive.getTitleByName("(The_Night_Time_Is)_The_Right_Time.html", function(title) {
|
||||
console.log(title);
|
||||
ok(title !== null, "Title found");
|
||||
ok(title.isRedirect(), "Title is a redirect.");
|
||||
equal(title.name(), "(The Night Time Is) The Right Time", "Correct redirect title name.");
|
||||
|
@ -183,12 +183,18 @@ define(['normalize_string', 'geometry', 'title', 'util', 'titleIterators', 'q'],
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @callback callbackLocalArchive
|
||||
* @param {LocalArchive} localArchive Ready-to-use LocalArchive instance
|
||||
*/
|
||||
|
||||
/**
|
||||
* Read the metadata file, in order to populate its values in the current
|
||||
* instance
|
||||
* @param {File} file metadata.txt file
|
||||
* @param {callbackLocalArchive} callback Callback called when the metadata file is read
|
||||
*/
|
||||
LocalArchive.prototype.readMetadataFile = function(file) {
|
||||
LocalArchive.prototype.readMetadataFile = function(file, callback) {
|
||||
var currentLocalArchiveInstance = this;
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
@ -208,6 +214,7 @@ define(['normalize_string', 'geometry', 'title', 'util', 'titleIterators', 'q'],
|
||||
else {
|
||||
currentLocalArchiveInstance._normalizedTitles = true;
|
||||
}
|
||||
callback(currentLocalArchiveInstance);
|
||||
};
|
||||
reader.readAsText(file);
|
||||
};
|
||||
@ -215,8 +222,9 @@ define(['normalize_string', 'geometry', 'title', 'util', 'titleIterators', 'q'],
|
||||
/**
|
||||
* Initialize the localArchive from given archive files
|
||||
* @param {type} archiveFiles
|
||||
* @param {callbackLocalArchive} callback Callback called when the LocalArchive is initialized
|
||||
*/
|
||||
LocalArchive.prototype.initializeFromArchiveFiles = function(archiveFiles) {
|
||||
LocalArchive.prototype.initializeFromArchiveFiles = function(archiveFiles, callback) {
|
||||
var dataFileRegex = /^wikipedia_(\d\d).dat$/;
|
||||
var coordinateFileRegex = /^coordinates_(\d\d).idx$/;
|
||||
this._dataFiles = new Array();
|
||||
@ -225,7 +233,7 @@ define(['normalize_string', 'geometry', 'title', 'util', 'titleIterators', 'q'],
|
||||
var file = archiveFiles[i];
|
||||
if (file) {
|
||||
if (file.name === "metadata.txt") {
|
||||
this.readMetadataFile(file);
|
||||
this.readMetadataFile(file, callback);
|
||||
}
|
||||
else if (file.name === "titles.idx") {
|
||||
this._titleFile = file;
|
||||
|
@ -32,28 +32,37 @@ define(['zimfile', 'zimDirEntry', 'util'],
|
||||
* @property {String} _language Language of the content
|
||||
*/
|
||||
|
||||
/**
|
||||
* @callback callbackZIMArchive
|
||||
* @param {ZIMArchive} zimArchive Ready-to-use ZIMArchive
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Creates a ZIM archive object to access the ZIM file at the given path in the given storage.
|
||||
* This constructor can also be used with a single File parameter.
|
||||
*
|
||||
* @param {StorageFirefoxOS|StoragePhoneGap|File} storage Storage (in this case, the path must be given) or File (path must be omitted)
|
||||
* @param {StorageFirefoxOS|StoragePhoneGap|Blob} storage Storage (in this case, the path must be given) or File (path must be omitted)
|
||||
* @param {String} path
|
||||
* @param {callbackZIMArchive} callbackReady
|
||||
*/
|
||||
function ZIMArchive(storage, path) {
|
||||
function ZIMArchive(storage, path, callbackReady) {
|
||||
var that = this;
|
||||
that._file = null;
|
||||
that._language = ""; //@TODO
|
||||
if (storage && storage instanceof File && !path) {
|
||||
// The constructor has been called with a single File parameter
|
||||
if (storage && storage instanceof Blob && !path) {
|
||||
// The constructor has been called with a single File/Blob parameter
|
||||
zimfile.fromFile(storage).then(function(file) {
|
||||
that._file = file;
|
||||
callbackReady(that);
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log(storage);
|
||||
storage.get(path).then(function(file) {
|
||||
return zimfile.fromFile(file).then(function(file) {
|
||||
that._file = file;
|
||||
callbackReady(that);
|
||||
});
|
||||
}, function(error) {
|
||||
alert("Error reading ZIM file " + path + ": " + error);
|
||||
|
Loading…
x
Reference in New Issue
Block a user