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();

View File

@@ -2,13 +2,17 @@ var fs = require('fs');
var assert = require('assert');
var MBTiles = require('..');
var fixtures = {
plain_1: __dirname + '/fixtures/plain_1.mbtiles',
plain_2: __dirname + '/fixtures/plain_2.mbtiles',
plain_3: __dirname + '/fixtures/plain_3.mbtiles',
plain_4: __dirname + '/fixtures/plain_4.mbtiles'
plain_4: __dirname + '/fixtures/plain_4.mbtiles',
non_existent: __dirname + '/fixtures/non_existent.mbtiles'
};
try { fs.unlink(fixtures.non_existent); } catch (err) {}
exports['get metadata'] = function(beforeExit) {
var completion = {};
@@ -211,3 +215,31 @@ exports['get grids from file without interaction'] = function(beforeExit) {
assert.equal(status.error, 14);
});
};
exports['get tiles from non-existent file'] = function(beforeExit) {
var status = {
success: 0,
error: 0
};
var mbtiles = new MBTiles(fixtures.non_existent);
mbtiles.getTile(1, 0, 0, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(0, 0, -1, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(0, -1, 0, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(1, 8, 3, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(-3, 0, 2, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(2, 3, 18, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(0, 0, 4, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(3, 8, 4, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(4, 8, 4, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(5, 8, 4, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(13, 4, 4, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(0, 14, 4, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(0, 7, 3, yieldsError(status, 'error', 'Tile does not exist'));
mbtiles.getTile(6, 2, 3, yieldsError(status, 'error', 'Tile does not exist'));
beforeExit(function() {
assert.equal(status.success, 0);
assert.equal(status.error, 14);
});
};