add tests with empty/non-existent database

This commit is contained in:
Konstantin Käfer
2011-06-27 19:39:41 +02:00
parent e643a5bbc0
commit 9f08ae3e26
2 changed files with 45 additions and 23 deletions

View File

@@ -35,9 +35,7 @@ function MBTiles(uri, callback) {
// Finds all mbtiles file in the filepath and returns their tilesource URI.
MBTiles.list = function(filepath, callback) {
if (typeof callback !== 'function') {
throw new Error('Callback required as second argument');
}
if (typeof callback !== 'function') callback = noop;
filepath = path.resolve(filepath);
fs.readdir(filepath, function(err, files) {
@@ -53,9 +51,7 @@ MBTiles.list = function(filepath, callback) {
// Finds an mbtiles file with the given ID in the filepath and returns a
// tilesource URI.
MBTiles.findID = function(filepath, id, callback) {
if (typeof callback !== 'function') {
throw new Error('Callback required as third argument');
}
if (typeof callback !== 'function') callback = noop;
filepath = path.resolve(filepath);
var file = path.join(filepath, id + '.mbtiles');
@@ -68,9 +64,7 @@ MBTiles.findID = function(filepath, id, callback) {
// Retrieve the schema of the current mbtiles database and inform the caller of
// whether the specified table exists.
MBTiles.prototype.exists = function(table, callback) {
if (typeof callback !== 'function') {
throw new Error('Callback required as second argument');
}
if (typeof callback !== 'function') callback = noop;
if (this.schema) {
return callback(null, _(this.schema).include(table));
@@ -91,9 +85,7 @@ MBTiles.prototype.exists = function(table, callback) {
// DB integrity check.
MBTiles.prototype.integrity = function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback required as first argument');
}
if (typeof callback !== 'function') callback = noop;
this.db.get('PRAGMA quick_check(1)', function(err, row) {
if (!(row && row.integrity_check && row.integrity_check === 'ok')) {
@@ -108,9 +100,7 @@ MBTiles.prototype.integrity = function(callback) {
// Sets the synchronous flag to OFF for (much) faster inserts.
// See http://www.sqlite3.org/pragma.html#pragma_synchronous
MBTiles.prototype.setup = function(callback) {
if (typeof callback !== 'function') {
throw new Error('Callback required as second argument');
}
if (typeof callback !== 'function') callback = noop;
fs.readFile(__dirname + '/schema.sql', 'utf8', function(err, sql) {
if (err) return callback(err);
@@ -286,8 +276,8 @@ MBTiles.prototype.getTile = function(x, y, z, callback) {
'zoom_level = ? AND tile_column = ? AND tile_row = ?',
z, x, y,
function(err, row) {
if (err) callback(err);
else if (!row || !row.tile_data) callback(new Error('Tile does not exist'));
if (!row || (err && err.errno == 1)) callback(new Error('Tile does not exist'));
else if (err) callback(err);
else callback(null, row.tile_data);
});
};
@@ -348,8 +338,8 @@ MBTiles.prototype.metadata = function(key, callback) {
this.db.get('SELECT value FROM metadata WHERE name = ?',
key,
function(err, row) {
if (err) callback(err);
else if (!row) callback(new Error('Key does not exist'));
if (!row || (err && err.errno == 1)) callback(new Error('Key does not exist'));
else if (err) callback(err);
else callback(null, row.value);
});
};
@@ -375,7 +365,7 @@ MBTiles.prototype.getInfo = function(callback) {
},
// Determine min/max zoom if needed
function(err) {
if (err) throw err;
if (err) return callback(err);
if (info.maxzoom !== undefined
&& info.minzoom !== undefined) return this();
@@ -389,7 +379,7 @@ MBTiles.prototype.getInfo = function(callback) {
zoomquery.finalize();
},
function(err, rows) {
if (err) throw err;
if (err) return callback(err);
if (rows) {
var zooms = _(rows).chain()
.reject(_.isUndefined)
@@ -402,7 +392,7 @@ MBTiles.prototype.getInfo = function(callback) {
},
// Determine bounds if needed
function(err) {
if (err) throw err;
if (err) return callback(err);
if (info.bounds) return this();
if (typeof info.minzoom === 'undefined') return this();