require('../support/test_helper');

var _ = require('underscore');
var assert = require('../support/assert');
var TestClient = require('../support/test_client');
var rendererOptions = global.environment.renderer;

var testQueryRewriter = {
    query: function(query, data) {
        var table = (data && data.table) || 'test_table';
        return 'SELECT * FROM ' + table;
    },
    style: function(style) {
        return style;
    }
};

describe('server_gettile', function() {

    var IMAGE_EQUALS_TOLERANCE_PER_MIL = 2;

    function imageCompareFn(fixture, done) {
        return function(err, tile) {
            if (err) {
                return done(err);
            }
            assert.imageEqualsFile(tile, './test/fixtures/' + fixture, IMAGE_EQUALS_TOLERANCE_PER_MIL, done);
        };
    }

    it("should by default not rewrite queries",  function(done){
        var mapConfig = TestClient.singleLayerMapConfig('SELECT * FROM _vovw_12_test_table');
        new TestClient(mapConfig)
            .getTile(11, 1002, 772, imageCompareFn('_vovw_12_test_table_11_1002_772.png', done));
    });

    it("should rewrite queries",  function(done){
        var options = {
            mapnik: {
                mapnik: _.extend({}, rendererOptions.mapnik, { queryRewriter: testQueryRewriter })
            }
        };
        var mapConfig = TestClient.singleLayerMapConfig('SELECT * FROM _vovw_12_test_table');
        new TestClient(mapConfig, options)
            .getTile(11, 1002, 772, imageCompareFn('test_table_11_1002_772.png', done));

    });

    it("should rewrite queries with passed data",  function(done){
        var options = {
            mapnik: {
                mapnik: _.extend({}, rendererOptions.mapnik, { queryRewriter: testQueryRewriter })
            }
        };
        var mapConfig = TestClient.singleLayerMapConfig('SELECT * FROM test_table');
        mapConfig.layers[0].options.query_rewriter_data = {
            table: '_vovw_12_test_table'
        };
        new TestClient(mapConfig, options)
            .getTile(11, 1002, 772, imageCompareFn('test_table_11_1002_772.png', done));
    });

});

require('../support/test_helper');

var _ = require('underscore');
var assert = require('../support/assert');
var TestClient = require('../support/test_client');
var rendererOptions = global.environment.renderer;

var testQueryRewriter = {
    query: function(query, data) {
        var table = (data && data.table) || 'test_table';
        return 'SELECT * FROM ' + table;
    },
    style: function(style) {
        return style;
    }
};

describe('server_gettile', function() {

    var IMAGE_EQUALS_TOLERANCE_PER_MIL = 2;

    function imageCompareFn(fixture, done) {
        return function(err, tile) {
            if (err) {
                return done(err);
            }
            assert.imageEqualsFile(tile, './test/fixtures/' + fixture, IMAGE_EQUALS_TOLERANCE_PER_MIL, done);
        };
    }

    it("should by default not rewrite queries",  function(done){
        var mapConfig = TestClient.singleLayerMapConfig('SELECT * FROM _vovw_12_test_table');
        new TestClient(mapConfig)
            .getTile(11, 1002, 772, imageCompareFn('_vovw_12_test_table_11_1002_772.png', done));
    });

    it("should rewrite queries",  function(done){
        var options = {
            mapnik: {
                mapnik: _.extend({}, rendererOptions.mapnik, { queryRewriter: testQueryRewriter })
            }
        };
        var mapConfig = TestClient.singleLayerMapConfig('SELECT * FROM _vovw_12_test_table');
        new TestClient(mapConfig, options)
            .getTile(11, 1002, 772, imageCompareFn('test_table_11_1002_772.png', done));

    });

    it("should rewrite queries with passed data",  function(done){
        var options = {
            mapnik: {
                mapnik: _.extend({}, rendererOptions.mapnik, { queryRewriter: testQueryRewriter })
            }
        };
        var mapConfig = TestClient.singleLayerMapConfig('SELECT * FROM test_table');
        mapConfig.layers[0].options.query_rewrite_data = {
            table: '_vovw_12_test_table'
        };
        new TestClient(mapConfig, options)
            .getTile(11, 1002, 772, imageCompareFn('_vovw_12_test_table_11_1002_772.png', done));
    });

});

Fix query rewriter tests