From 3987e83b7a967d9a3df28641c8fd9c2c3aed462d Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Wed, 18 May 2016 12:34:51 +0200 Subject: [PATCH] Add tests for query rewriter with filters --- test/unit/cartodb/overviews_query_rewriter.js | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/test/unit/cartodb/overviews_query_rewriter.js b/test/unit/cartodb/overviews_query_rewriter.js index ac3039c6..dd6b0798 100644 --- a/test/unit/cartodb/overviews_query_rewriter.js +++ b/test/unit/cartodb/overviews_query_rewriter.js @@ -523,4 +523,89 @@ describe('Overviews query rewriter', function() { var expected_sql = "SELECT * FROM table1"; assertSameSql(overviews_sql, expected_sql); }); + + it('generates query with filters', function(){ + var sql = "SELECT ST_Transform(the_geom, 3857) the_geom_webmercator, cartodb_id, name\ + FROM (SELECT *\ + FROM (select * from table1) _camshaft_category_filter\ + WHERE name IN ($escape_0$X$escape_0$)) _cdb_analysis_query"; + var data = { + overviews: { + table1: { + 0: { table: 'table1_ov0' }, + 1: { table: 'table1_ov1' }, + 2: { table: 'table1_ov2' }, + 3: { table: 'table1_ov3' } + } + }, + filters: { name_filter: { type: 'category', column: 'name', params: { accept: [ 'X' ] } } }, + unfiltered_query: 'SELECT * FROM table1' + }; + var overviews_sql = overviewsQueryRewriter.query(sql, data); + var expected_sql = "\ + SELECT * FROM (WITH\ + _vovw_scale AS ( SELECT ZoomLevel() AS _vovw_z )\ + SELECT * FROM (\ + SELECT * FROM table1_ov0, _vovw_scale WHERE _vovw_z = 0\ + UNION ALL\ + SELECT * FROM table1_ov1, _vovw_scale WHERE _vovw_z = 1\ + UNION ALL\ + SELECT * FROM table1_ov2, _vovw_scale WHERE _vovw_z = 2\ + UNION ALL\ + SELECT * FROM table1_ov3, _vovw_scale WHERE _vovw_z = 3\ + UNION ALL\ + SELECT * FROM table1, _vovw_scale WHERE _vovw_z > 3\ + ) AS _vovw_table1) _camshaft_category_filter\ + WHERE name IN ($escape_0$X$escape_0$)\ + "; + assertSameSql(overviews_sql, expected_sql); + }); + + it('generates query with filters for specific zoom level', function(){ + var sql = "SELECT ST_Transform(the_geom, 3857) the_geom_webmercator, cartodb_id, name\ + FROM (SELECT *\ + FROM (select * from table1) _camshaft_category_filter\ + WHERE name IN ($escape_0$X$escape_0$)) _cdb_analysis_query"; + var data = { + overviews: { + table1: { + 0: { table: 'table1_ov0' }, + 1: { table: 'table1_ov1' }, + 2: { table: 'table1_ov2' }, + 3: { table: 'table1_ov3' } + } + }, + filters: { name_filter: { type: 'category', column: 'name', params: { accept: [ 'X' ] } } }, + unfiltered_query: 'SELECT * FROM table1', + filter_stats: { unfiltered_rows: 1000, filtered_rows: 900 } + }; + var overviews_sql = overviewsQueryRewriter.query(sql, data, { zoom_level: 2 }); + var expected_sql = "\ + SELECT * FROM (SELECT * FROM table1_ov2) _camshaft_category_filter\ + WHERE name IN ($escape_0$X$escape_0$)\ + "; + assertSameSql(overviews_sql, expected_sql); + }); + + it('does not generates query with aggressive filtering', function(){ + var sql = "SELECT ST_Transform(the_geom, 3857) the_geom_webmercator, cartodb_id, name\ + FROM (SELECT *\ + FROM (select * from table1) _camshaft_category_filter\ + WHERE name IN ($escape_0$X$escape_0$)) _cdb_analysis_query"; + var data = { + overviews: { + table1: { + 0: { table: 'table1_ov0' }, + 1: { table: 'table1_ov1' }, + 2: { table: 'table1_ov2' }, + 3: { table: 'table1_ov3' } + } + }, + filters: { name_filter: { type: 'category', column: 'name', params: { accept: [ 'X' ] } } }, + unfiltered_query: 'SELECT * FROM table1', + filter_stats: { unfiltered_rows: 1000, filtered_rows: 10 } + }; + var overviews_sql = overviewsQueryRewriter.query(sql, data); + assert.equal(overviews_sql, sql); + }); });