More tests. Added try/catch around read/write_object.

This commit is contained in:
Alan Kligman 2013-07-13 12:41:10 -04:00
parent 3d00c85a27
commit cfd0932736
3 changed files with 148 additions and 109 deletions

View File

@ -23,10 +23,9 @@ var flags = 'FORMAT';
var fs = new IDBFS.FileSystem('local', flags);
fs.stat('/', function(error, result) {
console.log('stat /', error, result);
// indexedDB.deleteDatabase('local');
});
fs.stat('/tmp', function(error, result) {
console.log('stat /tmp', error, result);
});
/*
fs.mkdir('/tmp', function(error) {
if(error) throw error;

View File

@ -150,14 +150,18 @@ define(function(require) {
*/
function read_object(objectStore, id, callback) {
var getRequest = objectStore.get(id);
getRequest.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
getRequest.onerror = function onerror(error) {
callback(error);
};
try {
var getRequest = objectStore.get(id);
getRequest.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
getRequest.onerror = function onerror(error) {
callback(error);
};
} catch(error) {
callback(new EIO(error.message));
}
};
/*
@ -165,14 +169,18 @@ define(function(require) {
*/
function write_object(objectStore, object, id, callback) {
var putRequest = objectStore.put(object, id);
putRequest.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
putRequest.onerror = function onerror(error) {
callback(error);
};
try {
var putRequest = objectStore.put(object, id);
putRequest.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
putRequest.onerror = function onerror(error) {
callback(error);
};
} catch(error) {
callback(new EIO(error.message));
}
};
/*
@ -586,22 +594,21 @@ define(function(require) {
};
openRequest.onsuccess = function onsuccess(event) {
var db = event.target.result;
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function complete(error) {
that.db = db;
if(error) {
that.readyState = FS_ERROR;
deferred.reject(error);
} else {
that.readyState = FS_READY;
that.db = db;
deferred.resolve();
}
};
if(format) {
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
var clearRequest = files.clear();
clearRequest.onsuccess = function onsuccess(event) {
make_root_directory(files, complete);

View File

@ -1,9 +1,27 @@
var TEST_DATABASE_NAME = 'test';
var TEST_DATABASE_NAME = '__test';
var DEFAULT_TIMEOUT = 5000;
var next_id = 1;
var test_database_names = [];
window.onbeforeunload = function() {
test_database_names.forEach(function(name) {
indexedDB.deleteDatabase(name);
});
};
function mk_id(length) {
var text = '';
var tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for( var i=0; i < length; i++ )
text += tokens.charAt(Math.floor(Math.random() * tokens.length));
return text;
};
function mk_db_name() {
return TEST_DATABASE_NAME + next_id++;
var name = TEST_DATABASE_NAME + mk_id(5);
test_database_names.push(name);
return name;
};
describe("IDBFS", function() {
@ -16,7 +34,7 @@ describe("IDBFS", function() {
});
});
describe("new fs", function() {
describe("fs", function() {
beforeEach(function() {
this.db_name = mk_db_name();
this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT');
@ -31,34 +49,11 @@ describe("new fs", function() {
expect(typeof this.fs).toEqual('object');
});
var api_methods = [
"mkdir",
"rmdir",
"readdir",
"link",
"unlink",
"open",
"close",
"read",
"write",
"setxattr",
"getxattr",
"stat",
"fstat",
];
api_methods.forEach(function(method) {
it("has method " + method, function() {
expect(typeof this.fs[method]).toEqual('function');
});
});
it('should have a root directory', function() {
var complete = false;
var _error, _result;
var _result;
this.fs.stat('/', function(error, result) {
_error = error;
_result = result;
complete = true;
@ -85,76 +80,114 @@ describe('stat', function() {
delete this.fs;
});
describe('on non-existing path', function() {
it('should return an error', function() {
var complete = false;
var _error, _result;
it('should be a function', function() {
expect(typeof this.fs.stat).toEqual('function');
});
this.fs.stat('/tmp', function(error, result) {
_error = error;
_result = result;
it('should return an error if path does not exist', function() {
var complete = false;
var _error, _result;
complete = true;
});
this.fs.stat('/tmp', function(error, result) {
_error = error;
_result = result;
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toBeDefined();
});
complete = true;
});
it('should not return a result', function() {
var complete = false;
var _error, _result;
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
this.fs.stat('/tmp', function(error, result) {
_error = error;
_result = result;
complete = true;
});
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_result).not.toBeDefined();
});
runs(function() {
expect(_error).toBeDefined();
expect(_result).not.toBeDefined();
});
});
describe('on existing path', function() {
it('should return a stat object', function() {
var complete = false;
var _error, _result;
var that = this;
it('should return a stat object if path exists', function() {
var complete = false;
var _error, _result;
var that = this;
this.fs.mkdir('/tmp', function(error) {
if(error) throw error;
that.fs.stat('/tmp', function(error, result) {
_error = error;
_result = result;
that.fs.stat('/', function(error, result) {
_error = error;
_result = result;
complete = true;
});
});
complete = true;
});
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_result).toBeDefined();
expect(_result['dev']).toEqual(that.db_name);
expect(_result['nlinks']).toEqual(jasmine.any(Number));
expect(_result['atime']).toEqual(jasmine.any(Number));
expect(_result['mtime']).toEqual(jasmine.any(Number));
expect(_result['ctime']).toEqual(jasmine.any(Number));
});
runs(function() {
expect(_result).toBeDefined();
expect(_error).not.toBeDefined();
expect(_result['dev']).toEqual(that.db_name);
expect(_result['nlinks']).toEqual(jasmine.any(Number));
expect(_result['atime']).toEqual(jasmine.any(Number));
expect(_result['mtime']).toEqual(jasmine.any(Number));
expect(_result['ctime']).toEqual(jasmine.any(Number));
});
});
});
});
describe('mkdir', function() {
beforeEach(function() {
this.db_name = mk_db_name();
this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT');
});
afterEach(function() {
indexedDB.deleteDatabase(this.db_name);
delete this.fs;
});
it('should return an error if part of the parent path does not exist', function() {
var complete = false;
var _error;
var that = this;
that.fs.mkdir('/tmp/mydir', function(error) {
_error = error;
complete = true;
});
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toBeDefined();
});
});
it('should return with no result if successful', function() {
var complete = false;
var _error, _result, _stat;
var that = this;
that.fs.mkdir('/tmp', function(error, result) {
_error = error;
_result = result;
that.fs.stat('/tmp', function(error, result) {
_stat = result;
complete = true;
});
});
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).not.toBeDefined();
expect(_stat).toBeDefined();
});
});
});