UNPKG

@yaga/cordova-plugin-spatialite

Version:

A Spatialite plugin for cordova

312 lines (292 loc) 12.6 kB
mocha.setup('bdd'); document.addEventListener('deviceready', function () { mocha.run(); }); /// Tests var TEST_DATA = 'It works', TEST_DB = 'test', TEST_TABLE = 'test', TEST_SPARTIAL_TABLE = 'spatial-test', TEST_POINT = 'POINT(123.45 543.21)', TEST_POLYGON = 'POLYGON((100 500, 100 600, 200 600, 200 500, 100 500))'; describe('Spatialite ', function () { 'use strict'; var testDb; it('should have a spatialite Class on window', function () { if (!window.spatialitePlugin) { throw new Error('Not found'); } }); it('should open a database', function (done) { testDb = window.spatialitePlugin.openDatabase({ name: 'test' }, function () { return done(); }, done); }); it('should do a simple select', function (done) { testDb.executeSql('SELECT "All right" as test;', [], function (response) { if (response.rows.length !== 1) { return done(new Error('Wrong number of rows')); } if (response.rows.item(0).test !== 'All right') { return done(new Error('Wrong content')); } return done(); }, done); }); describe('Test Queries', function () { var handle; before(function (done) { spatialitePlugin.openDatabase({name: TEST_DB}, function(h){ handle = h; return done(); }, done); }); function testFactory(name, sql, cb) { it(name, function (done) { handle.executeSql(sql, [], function (result) { var message = cb(result); if (message) { window.console.error('Error with', name, result); return done(new Error(message)); } return done(); }, done); }); } testFactory( 'should create a table within test database', 'CREATE TABLE IF NOT EXISTS "' + TEST_TABLE + '" (a INTEGER, b VARCHAR(255));', function (result) { return ''; } ); testFactory( 'should write data to table', 'INSERT INTO "' + TEST_TABLE + '" (a, b) VALUES (123, "' + TEST_DATA + '");', function (result) { if (result.rows.length !== 0 || result.rowsAffected !== 1) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should read created data from table', 'SELECT * FROM "' + TEST_TABLE + '" WHERE a=123;', function (result) { if (result.rows.length !== 1 || result.rows.item(0).a !== 123 || result.rows.item(0).b !== TEST_DATA) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should delete created data from table', 'DELETE FROM "' + TEST_TABLE + '" WHERE a=123;', function (result) { if (result.rows.length !== 0 || result.rowsAffected !== 1) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should not read created data anymore from table', 'SELECT * FROM "' + TEST_TABLE + '" WHERE a=123;', function (result) { if (result.rows.length !== 0) { return 'Wrong result-set returned'; } return ''; }); it('should drop database again', function (done) { spatialitePlugin.deleteDatabase(TEST_DB, function (status) { if (status !== 'OK') { return done(new Error('Wrong status after deleting: ' + status)); } return done(); }, function (message) { return done(new Error('Error while deleting Database: ' + message)); }); }); describe('Spatialite', function () { describe('Selects', function () { testFactory( 'should get geos version', 'SELECT geos_version() as geosv;', function (result) { if (result.rows.length !== 1) { return 'No geos installed'; } console.log('Geos version:' + result.rows.item(0).geosv); return ''; }); testFactory( 'should get proj4 version', 'SELECT proj4_version() as projv;', function (result) { if (result.rows.length !== 1) { return 'No PROJ4 installed'; } console.log('PROJ4 version:' + result.rows.item(0).projv); return ''; }); testFactory( 'should get sqlite version', 'SELECT sqlite_version() as sqlitev;', function (result) { if (result.rows.length !== 1) { return 'No SQLite installed'; } console.log('SQLite version:' + result.rows.item(0).sqlitev); return ''; }); testFactory( 'should get spartialite version', 'SELECT spatialite_version() as spatialitev;', function (result) { if (result.rows.length !== 1) { return 'No spartialite installed'; } console.log('spartialite version:' + result.rows.item(0).spatialitev); return ''; }); testFactory( 'should parse point from WKT', 'SELECT AsText(GeomFromText("' + TEST_POINT + '")) as c;', function (result) { if (result.rows.length !== 1) { return 'Wrong response'; } if (result.rows.item(0).c !== TEST_POINT) { return 'This is not the expected WKT'; } return ''; }); testFactory( 'should parse point from WKT to geoJSON', 'SELECT AsGeoJSON(GeomFromText("' + TEST_POINT + '")) AS geojson;', function (result) { if (result.rows.length !== 1) { return 'Wrong response'; } console.log('GEOJSON: ', result); try { var json=JSON.parse(result.rows.item(0).geojson); } catch (e) { return 'Error while parsing JSON: ' + e.message; } return ''; }); testFactory( 'should parse polygon from WKT', 'SELECT AsText(GeomFromText("' + TEST_POLYGON + '")) as c;', function (result) { if (result.rows.length !== 1) { return 'Wrong response'; } if (result.rows.item(0).c !== TEST_POLYGON) { return 'This is not the expected WKT'; } return ''; }); }); testFactory( 'should create a spatial table within test database', 'CREATE TABLE IF NOT EXISTS "' + TEST_SPARTIAL_TABLE + '" (a INTEGER, b VARCHAR(255), c BLOB);', function (result) { return ''; }); testFactory( 'should write data to table', 'INSERT INTO "' + TEST_SPARTIAL_TABLE + '" (a, b) VALUES (123, "' + TEST_DATA + '");', function (result) { if (result.rows.length !== 0 || result.rowsAffected !== 1) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should read created data from table', 'SELECT * FROM "' + TEST_SPARTIAL_TABLE + '" WHERE a=123;', function (result) { if (result.rows.length !== 1 || result.rows.item(0).a !== 123 || result.rows.item(0).b !== TEST_DATA) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should write a spatial point to table', 'INSERT INTO "' + TEST_SPARTIAL_TABLE + '" (a, b, c) VALUES (1234, "' + TEST_DATA + '", GeomFromText("' + TEST_POINT + '"));', function (result) { if (result.rows.length !== 0 || result.rowsAffected !== 1) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should write a spatial polygon to table', 'INSERT INTO "' + TEST_SPARTIAL_TABLE + '" (a, b, c) VALUES (1234, "' + TEST_DATA + '", GeomFromText("' + TEST_POLYGON + '"));', function (result) { if (result.rowsAffected !== 1) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should get spatial data from table', 'SELECT a, b, AsText(c) FROM "' + TEST_SPARTIAL_TABLE + '" WHERE a=1234;', function (result) { if (result.rows.length !== 2) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should write another spatial point to table', 'INSERT INTO "' + TEST_SPARTIAL_TABLE + '" (a, b, c) VALUES (1234, "' + TEST_DATA + '", GeomFromText("POINT(10000 10000)"));', function (result) { if (result.rowsAffected !== 1) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should read created geometries with INTERSECTS', 'SELECT a, b, AsText(c) FROM "' + TEST_SPARTIAL_TABLE + '" WHERE INTERSECTS(GeomFromText("' + TEST_POLYGON + '"), c) ;', function (result) { if (result.rows.length !== 3) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should drop all data', 'DELETE FROM "' + TEST_SPARTIAL_TABLE + '";', function (result) { if (result.rowsAffected !== 4) { return 'Wrong result-set returned'; } return ''; }); testFactory( 'should drop the whole table', 'DROP TABLE "' + TEST_SPARTIAL_TABLE + '";', function (result) { if (result.rowsAffected !== 4) { return 'Wrong result-set returned'; } return ''; }); it('should drop database again', function (done) { spatialitePlugin.deleteDatabase(TEST_DB, function (status) { if (status !== 'OK') { return done(new Error('Wrong status after deleting: ' + status)); } return done(); }, function (message) { return done(new Error('Error while deleting Database: ' + message)); }); }); }); }); });