"use strict";

var mapnik = require('../');
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;

var dbname = 'node-mapnik-tmp-postgis-test-db';

var postgis_registered = false;
var hasPostgisAvailable = false;

describe('mapnik.VectorTile postgis.input', function() {

    before(function(done) {
        // Check that the postgis plugin is available
        mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'postgis.input'));
        postgis_registered = mapnik.datasources().indexOf('postgis') > -1;

        if (postgis_registered) {
            // Check if postgres is available
            spawn('psql', ['--version'])
                .on('error', function(code, signal) {
                    console.warn('psql --version could not be executed.');
                    return done();
                })
                .on('exit', function(code, signal) {
                    if (code !== 0) {
                        console.warn('psql --version returned ' + code);
                        return done();
                    }
                    // Drop the test database if it exists
                    spawn('dropdb', ['--if-exists', dbname])
                        .on('exit', function(code, signal) {
                            if (code !== 0) {
                                console.warn('dropdb --if-exists ' + dbname + ' returned ' + code);
                                return done();
                            }
                            // Create the test database
                            spawn('createdb', ['-T', 'template_postgis', dbname])
                                .on('exit', function(code, signal) {
                                    if (code !== 0) {
                                        console.warn('createdb -T template_postgis ' + dbname + 'returned ' + code);
                                        return done();
                                    }
                                    hasPostgisAvailable = true;
                                    return done();
                                });
                        })
                });
        } else {
            console.warn('postgis input datasource not registered.');
            return done();
        }
    });

    it('passes variables to replace tokens in query', function(done) {
        if (!hasPostgisAvailable) {
            this.skip('postgis not available');
        }

        spawn('psql', ['-q', '-f', './test/data/postgis-create-db-and-tables.sql', dbname])
            .on('exit', function(code, signal) {
                assert.equal(code, 0, 'could not load data in postgis');
                var map = new mapnik.Map(256, 256);
                map.loadSync('./test/data/postgis_datasource_tokens_query.xml');

                var opts = {};
                opts.variables = { "fieldid": 2 };
                map.render(new mapnik.VectorTile(0, 0, 0), opts, function(err, vtile) {
                    if (err) throw err;
                    assert(!vtile.empty());
                    var out = JSON.parse(vtile.toGeoJSON(0));
                    assert.equal(out.type,'FeatureCollection');
                    assert.equal(out.features.length,1);
                    assert.equal(out.features[0].properties.gid, 2);
                    var coords = out.features[0].geometry.coordinates;
                    var expected_coords = [-2.0, 2.0]; // From DB, GeomFromEWKT('SRID=4326;POINT(-2 2)')
                    assert.ok(Math.abs(coords[0] - expected_coords[0]) < 0.3);
                    assert.ok(Math.abs(coords[1] - expected_coords[1]) < 0.3);
                    done();
                });
            });
    });

    it('Correctly handles bool values', function(done) {
        if (!hasPostgisAvailable) {
            this.skip('postgis not available');
        }

        spawn('psql', ['-q', '-f', './test/data/postgis-create-db-and-tables.sql', dbname])
            .on('exit', function(code, signal) {
                assert.equal(code, 0, 'could not load data in postgis');
                var map = new mapnik.Map(256, 256);
                map.loadSync('./test/data/postgis_datasource_bool_query.xml');

                var opts = {};
                opts.variables = { "fieldid": 2 };
                map.render(new mapnik.VectorTile(0, 0, 0), opts, function(err, vtile) {
                    assert.ok(!err,err);
                    assert(!vtile.empty());
                    var out = JSON.parse(vtile.toGeoJSON(0));
                    assert.equal(out.type,'FeatureCollection');
                    assert.equal(out.features.length,1);
                    assert.strictEqual(out.features[0].properties.data, 0);
                    assert.strictEqual(out.features[0].properties.status2, false);
                    done();
                });
            });

    });

});

@@ -3789,18 +3789,47 @@
 les 
-bool
+same
  value
+ but different type propertie
 s', 
@@ -3944,230 +3944,8 @@
  %7D%0A%0A
-        spawn('psql', %5B'-q', '-f', './test/data/postgis-create-db-and-tables.sql', dbname%5D)%0A            .on('exit', function(code, signal) %7B%0A                assert.equal(code, 0, 'could not load data in postgis');%0A        
     
@@ -3980,32 +3980,24 @@
 (256, 256);%0A
-        
         map.
@@ -4060,98 +4060,8 @@
 );%0A%0A
-                var opts = %7B%7D;%0A                opts.variables = %7B %22fieldid%22: 2 %7D;%0A        
     
@@ -4099,36 +4099,34 @@
 rTile(0, 0, 0), 
-opts
+%7B%7D
 , function(err, 
@@ -4142,32 +4142,24 @@
             
-        
 assert.ok(!e
@@ -4175,32 +4175,24 @@
             
-    
-    
 assert(!vtil
@@ -4199,32 +4199,24 @@
 e.empty());%0A
-        
             
@@ -4265,32 +4265,24 @@
             
- 
-       
 assert.equal
@@ -4321,32 +4321,24 @@
             
-      
-  
 assert.equal
@@ -4358,32 +4358,24 @@
 .length,1);%0A
-        
             
@@ -4438,32 +4438,24 @@
             
-   
-     
 assert.stric
@@ -4501,32 +4501,24 @@
 s2, false);%0A
-        
             
@@ -4537,36 +4537,11 @@
     
-        %7D);%0A            %7D);%0A
+%7D);
 %0A   

Simplify MVT feature group test