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

81 lines
3.1 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.
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
2019-12-24 01:19:08 +08:00
it('returns a valid geopackage database', function (done) {
assert.response(server, {
2016-05-11 00:42:23 +08:00
url: base_url,
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) {
2016-04-27 22:56:06 +08:00
assert.equal(res.statusCode, 200, res.body);
2019-12-24 01:19:08 +08:00
assert.equal(res.headers['content-type'], 'application/x-sqlite3; charset=utf-8');
assert.notEqual(res.headers['content-disposition'].indexOf(table_name + '.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) {
2016-04-27 22:56:06 +08:00
assert.equal(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, {
2016-05-11 00:42:23 +08:00
url: base_url,
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) {
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) {
assert.equal(err, null);
assert.equal(row.name, table_name);
});
assert.notEqual(sqr, undefined);
2016-05-11 00:01:50 +08:00
2019-12-24 01:19:08 +08:00
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);
2016-05-11 00:01:50 +08:00
2019-12-24 01:19:08 +08:00
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');
assert.equal(row.fid, undefined);
done();
});
assert.notEqual(dqr, undefined);
2016-05-11 00:01:50 +08:00
});
2016-04-27 22:56:06 +08:00
});
});
});
});