CartoDB-SQL-API/test/acceptance/export/geopackage.js

80 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-04-27 22:56:06 +08:00
require('../../helper');
var server = require('../../../app/server')();
2016-04-27 22:56:06 +08:00
var assert = require('../../support/assert');
var sqlite = require('sqlite3');
2016-05-11 00:01:50 +08:00
var fs = require('fs');
2016-04-27 22:56:06 +08:00
describe('geopackage query', function(){
// Default name, cartodb-query, fails because of the hyphen.
2016-05-11 00:42:23 +08:00
var table_name = 'a_gpkg_table';
var base_url = '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=gpkg&filename=' + table_name;
2016-04-27 22:56:06 +08:00
it('returns a valid geopackage database', function(done){
assert.response(server, {
2016-05-11 00:42:23 +08:00
url: base_url,
2016-04-27 22:56:06 +08:00
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res) {
assert.equal(res.statusCode, 200, res.body);
assert.equal(res.headers["content-type"], "application/x-sqlite3; charset=utf-8");
assert.notEqual(res.headers["content-disposition"].indexOf(table_name + ".gpkg"), -1);
var db = new sqlite.Database(':memory:', res.body);
2016-05-11 00:42:23 +08:00
var qr = db.get("PRAGMA database_list", function(err) {
2016-04-27 22:56:06 +08:00
assert.equal(err, null);
done();
});
assert.notEqual(qr, undefined);
});
});
it('gets database and geopackage schema', function(done){
assert.response(server, {
2016-05-11 00:42:23 +08:00
url: base_url,
2016-04-27 22:56:06 +08:00
headers: {host: 'vizzuality.cartodb.com'},
2016-05-11 00:01:50 +08:00
encoding: 'binary',
2016-04-27 22:56:06 +08:00
method: 'GET'
},{ }, function(res) {
2016-05-11 00:01:50 +08:00
var tmpfile = '/tmp/a_geopackage_file.gpkg';
try {
fs.writeFileSync(tmpfile, res.body, 'binary');
} catch(err) {
return done(err);
}
2016-04-27 22:56:06 +08:00
2016-05-11 00:01:50 +08:00
var db = new sqlite.Database(tmpfile, function(err) {
if(!!err) {
return done(err);
}
db.serialize(function() {
var schemaQuery = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
var sqr = db.get(schemaQuery, function(err, row) {
assert.equal(err, null);
assert.equal(row.name, table_name);
});
assert.notEqual(sqr, undefined);
var gpkgQuery = "SELECT table_name FROM gpkg_contents";
var gqr = db.get(gpkgQuery, function(err, row) {
assert.equal(row.table_name, table_name);
assert.equal(err, null);
});
assert.notEqual(gqr, undefined);
var dataQuery = "SELECT * FROM " + table_name + " order by cartodb_id";
var dqr = db.get(dataQuery, function(err, row) {
assert.equal(err, null);
assert.equal(row.cartodb_id, 1);
assert.equal(row.name, 'Hawai');
done();
});
assert.notEqual(dqr, undefined);
});
2016-04-27 22:56:06 +08:00
});
});
});
});