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

105 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2016-04-27 22:56:06 +08:00
require('../../helper');
var server = require('../../../lib/server')();
2016-04-27 22:56:06 +08:00
var assert = require('../../support/assert');
var sqlite = require('sqlite3');
2019-12-24 01:19:08 +08:00
var fs = require('fs');
2016-04-27 22:56:06 +08:00
2019-12-24 01:19:08 +08:00
describe('geopackage query', function () {
2016-04-27 22:56:06 +08:00
// Default name, cartodb-query, fails because of the hyphen.
2019-12-26 23:10:41 +08:00
var tableName = 'a_gpkg_table';
var baseUrl = '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=gpkg&filename=' + tableName;
2016-04-27 22:56:06 +08:00
2019-12-24 01:19:08 +08:00
it('returns a valid geopackage database', function (done) {
assert.response(server, {
2019-12-26 23:10:41 +08:00
url: baseUrl,
2019-12-24 01:19:08 +08:00
headers: { host: 'vizzuality.cartodb.com' },
2016-04-27 22:56:06 +08:00
method: 'GET'
2019-12-24 01:19:08 +08:00
}, { }, function (err, res) {
2019-12-26 23:10:41 +08:00
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 200, res.body);
assert.strictEqual(res.headers['content-type'], 'application/x-sqlite3; charset=utf-8');
2019-12-26 23:10:41 +08:00
assert.notEqual(res.headers['content-disposition'].indexOf(tableName + '.gpkg'), -1);
2016-04-27 22:56:06 +08:00
var db = new sqlite.Database(':memory:', res.body);
2019-12-24 01:19:08 +08:00
var qr = db.get('PRAGMA database_list', function (err) {
2019-12-26 21:01:18 +08:00
assert.strictEqual(err, null);
2016-04-27 22:56:06 +08:00
done();
});
assert.notEqual(qr, undefined);
});
});
it('works with a long list of skipfields', function (done) {
const longUrl = baseUrl + `&skipfields=the_geom_webmercator,${'a'.repeat(300)}`;
assert.response(server, {
url: longUrl,
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
}, { }, function (err, res) {
assert.ifError(err);
assert.strictEqual(res.statusCode, 200, res.body);
assert.strictEqual(res.headers['content-type'], 'application/x-sqlite3; charset=utf-8');
assert.notEqual(res.headers['content-disposition'].indexOf(tableName + '.gpkg'), -1);
var db = new sqlite.Database(':memory:', res.body);
var qr = db.get('PRAGMA database_list', function (err) {
assert.strictEqual(err, null);
done();
});
assert.notEqual(qr, undefined);
});
});
2019-12-24 01:19:08 +08:00
it('gets database and geopackage schema', function (done) {
assert.response(server, {
2019-12-26 23:10:41 +08:00
url: baseUrl,
2019-12-24 01:19:08 +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'
2019-12-24 01:19:08 +08:00
}, { }, function (err, res) {
2019-12-26 23:10:41 +08:00
assert.ifError(err);
2016-05-11 00:01:50 +08:00
var tmpfile = '/tmp/a_geopackage_file.gpkg';
try {
2019-12-24 01:19:08 +08:00
fs.writeFileSync(tmpfile, res.body, 'binary');
} catch (err) {
return done(err);
2016-05-11 00:01:50 +08:00
}
2016-04-27 22:56:06 +08:00
2019-12-24 01:19:08 +08:00
var db = new sqlite.Database(tmpfile, function (err) {
if (err) {
return done(err);
}
2016-05-11 00:01:50 +08:00
2019-12-24 01:19:08 +08:00
db.serialize(function () {
var schemaQuery = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
var sqr = db.get(schemaQuery, function (err, row) {
2019-12-26 23:43:12 +08:00
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(err, null);
2019-12-26 23:10:41 +08:00
assert.strictEqual(row.name, tableName);
2019-12-24 01:19:08 +08:00
});
assert.notEqual(sqr, undefined);
2016-05-11 00:01:50 +08:00
2019-12-26 23:43:12 +08:00
var gpkgQuery = 'SELECT table_name FROM gpkg_contents';
2019-12-24 01:19:08 +08:00
var gqr = db.get(gpkgQuery, function (err, row) {
2019-12-26 23:43:12 +08:00
assert.ifError(err);
assert.strictEqual(row.table_name, tableName);
2019-12-26 21:01:18 +08:00
assert.strictEqual(err, null);
2019-12-24 01:19:08 +08:00
});
assert.notEqual(gqr, undefined);
2016-05-11 00:01:50 +08:00
2019-12-26 23:10:41 +08:00
var dataQuery = 'SELECT * FROM ' + tableName + ' order by cartodb_id';
2019-12-24 01:19:08 +08:00
var dqr = db.get(dataQuery, function (err, row) {
2019-12-26 23:43:12 +08:00
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(row.cartodb_id, 1);
assert.strictEqual(row.name, 'Hawai');
assert.strictEqual(row.fid, undefined);
2019-12-24 01:19:08 +08:00
done();
});
assert.notEqual(dqr, undefined);
2016-05-11 00:01:50 +08:00
});
2016-04-27 22:56:06 +08:00
});
});
});
});