<commit_before>require('../support/test_helper.js');

var assert = require('assert');
var MapConfig = require('../../lib/windshaft/models/mapconfig');

describe('mapconfig filters', function() {

    var categoryWidgetMapConfig = {
        version: '1.5.0',
        layers: [
            {
                type: 'mapnik',
                options: {
                    sql: 'select * from populated_places_simple_reduced',
                    cartocss: '#layer0 { marker-fill: red; marker-width: 10; }',
                    cartocss_version: '2.0.1',
                    widgets: {
                        adm0name: {
                            type: 'aggregation',
                            options: {
                                aggregation: 'count',
                                column: 'adm0name'
                            }
                        }
                    }
                }
            }
        ]
    };

    describe('error', function() {
        var mapConfig = MapConfig.create(categoryWidgetMapConfig);

        it('fails to apply category filter if no params are used', function() {
            assert.throws(
                function() {
                    mapConfig.applyFilters({layers: [{
                        adm0name: {}
                    }]});
                },
                function(err) {
                    assert.equal(
                        err.message,
                        'Category filter expect to have at least one value in accept or reject params'
                    );
                    return true;
                }
            );
        });

        it('fails to apply category filter if no accept is empty', function() {
            assert.throws(
                function() {
                    mapConfig.applyFilters({layers: [{
                        adm0name: {
                            accept: []
                        }
                    }]});
                },
                function(err) {
                    assert.equal(
                        err.message,
                        'Category filter expect to have at least one value in accept or reject params'
                    );
                    return true;
                }
            );
        });

        it('fails to apply category filter if no reject is empty', function() {
            assert.throws(
                function() {
                    mapConfig.applyFilters({layers: [{
                        adm0name: {
                            reject: []
                        }
                    }]});
                },
                function(err) {
                    assert.equal(
                        err.message,
                        'Category filter expect to have at least one value in accept or reject params'
                    );
                    return true;
                }
            );
        });
    });

    describe('should return an object with filtered aggregations', function() {

        it('uses accept filter param', function() {
            var mapConfig = MapConfig.create(categoryWidgetMapConfig);
            var acceptFilterMapConfig = mapConfig.applyFilters({layers: [{
                adm0name: { // this is category filter associated to the aggregation widget
                    accept: ['Spain']
                }
            }]});

            var filteredList = acceptFilterMapConfig.getWidget(0, 'adm0name');
            assert.equal(filteredList.sql(),
                    "SELECT count(*) AS count, adm0name FROM" +
                    " (SELECT * FROM" +
                    " (select * from populated_places_simple_reduced) " +
                    "_cdb_category_filter WHERE adm0name IN ('Spain')) " +
                    "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
            );

            // check original mapconfig keeps it right
            var list = mapConfig.getWidget(0, 'adm0name');
            assert.equal(list.sql(),
                    "SELECT count(*) AS count, adm0name FROM" +
                    " (select * from populated_places_simple_reduced) " +
                    "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
            );
        });

        it('uses reject filter param', function() {
            var mapConfig = MapConfig.create(categoryWidgetMapConfig);

            var rejectFilterMapConfig = mapConfig.applyFilters({layers: [{
                adm0name: { // this is category filter associated to the aggregation widget
                    reject: ['Spain']
                }
            }]});
            var rejectFilterList = rejectFilterMapConfig.getWidget(0, 'adm0name');
            assert.equal(rejectFilterList.sql(),
                    "SELECT count(*) AS count, adm0name FROM" +
                    " (SELECT * FROM" +
                    " (select * from populated_places_simple_reduced) " +
                    "_cdb_category_filter WHERE adm0name NOT IN ('Spain')) " +
                    "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
            );

            // check original mapconfig keeps it right
            var list = mapConfig.getWidget(0, 'adm0name');
            assert.equal(list.sql(),
                    "SELECT count(*) AS count, adm0name FROM" +
                    " (select * from populated_places_simple_reduced) " +
                    "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
            );
        });

        it('uses accept and reject filter param', function() {
            var mapConfig = MapConfig.create(categoryWidgetMapConfig);

            var acceptAndRejectFilterMapConfig = mapConfig.applyFilters({layers: [{
                adm0name: { // this is category filter associated to the aggregation widget
                    reject: ['Spain'],
                    accept: ['USA']
                }
            }]});
            var acceptAndRejectFilterList = acceptAndRejectFilterMapConfig.getWidget(0, 'adm0name');

            assert.equal(acceptAndRejectFilterList.sql(),
                    "SELECT count(*) AS count, adm0name FROM" +
                    " (SELECT * FROM" +
                    " (select * from populated_places_simple_reduced) " +
                    "_cdb_category_filter WHERE adm0name IN ('USA') AND adm0name NOT IN ('Spain')) " +
                    "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
            );

            // check original mapconfig keeps it right
            var list = mapConfig.getWidget(0, 'adm0name');
            assert.equal(list.sql(),
                    "SELECT count(*) AS count, adm0name FROM" +
                    " (select * from populated_places_simple_reduced) " +
                    "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
            );
        });
    });

});
<commit_msg>Moves aggregation tests to their own _suite_<commit_after>require('../support/test_helper.js');

var assert = require('assert');
var MapConfig = require('../../lib/windshaft/models/mapconfig');

describe('mapconfig filters', function() {

    describe('aggregations', function() {

        var categoryWidgetMapConfig = {
            version: '1.5.0',
            layers: [
                {
                    type: 'mapnik',
                    options: {
                        sql: 'select * from populated_places_simple_reduced',
                        cartocss: '#layer0 { marker-fill: red; marker-width: 10; }',
                        cartocss_version: '2.0.1',
                        widgets: {
                            adm0name: {
                                type: 'aggregation',
                                options: {
                                    aggregation: 'count',
                                    column: 'adm0name'
                                }
                            }
                        }
                    }
                }
            ]
        };

        describe('errors', function() {
            var mapConfig = MapConfig.create(categoryWidgetMapConfig);

            it('fails to apply category filter if no params are used', function() {
                assert.throws(
                    function() {
                        mapConfig.applyFilters({layers: [{
                            adm0name: {}
                        }]});
                    },
                    function(err) {
                        assert.equal(
                            err.message,
                            'Category filter expects at least one array in accept or reject params'
                        );
                        return true;
                    }
                );
            });

            it('fails to apply category filter if accept is empty', function() {
                assert.throws(
                    function() {
                        mapConfig.applyFilters({layers: [{
                            adm0name: {
                                accept: []
                            }
                        }]});
                    },
                    function(err) {
                        assert.equal(
                            err.message,
                            'Category filter expects to have at least one value in accept or reject arrays'
                        );
                        return true;
                    }
                );
            });

            it('fails to apply category filter if reject is empty', function() {
                assert.throws(
                    function() {
                        mapConfig.applyFilters({layers: [{
                            adm0name: {
                                reject: []
                            }
                        }]});
                    },
                    function(err) {
                        assert.equal(
                            err.message,
                            'Category filter expects to have at least one value in accept or reject arrays'
                        );
                        return true;
                    }
                );
            });
        });

        describe('queries with filters', function() {

            it('uses accept filter param', function() {
                var mapConfig = MapConfig.create(categoryWidgetMapConfig);
                var acceptFilterMapConfig = mapConfig.applyFilters({layers: [{
                    adm0name: { // this is category filter associated to the aggregation widget
                        accept: ['Spain']
                    }
                }]});

                var acceptFilterAggregation = acceptFilterMapConfig.getWidget(0, 'adm0name');
                assert.equal(acceptFilterAggregation.sql(),
                        "SELECT count(*) AS count, adm0name FROM" +
                        " (SELECT * FROM" +
                        " (select * from populated_places_simple_reduced) " +
                        "_cdb_category_filter WHERE adm0name IN ('Spain')) " +
                        "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
                );

                // check original mapconfig keeps it right
                var aggregation = mapConfig.getWidget(0, 'adm0name');
                assert.equal(aggregation.sql(),
                        "SELECT count(*) AS count, adm0name FROM" +
                        " (select * from populated_places_simple_reduced) " +
                        "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
                );
            });

            it('uses reject filter param', function() {
                var mapConfig = MapConfig.create(categoryWidgetMapConfig);

                var rejectFilterMapConfig = mapConfig.applyFilters({layers: [{
                    adm0name: { // this is a category filter associated to the aggregation widget
                        reject: ['Spain']
                    }
                }]});
                var rejectFilterAggregation = rejectFilterMapConfig.getWidget(0, 'adm0name');
                assert.equal(rejectFilterAggregation.sql(),
                        "SELECT count(*) AS count, adm0name FROM" +
                        " (SELECT * FROM" +
                        " (select * from populated_places_simple_reduced) " +
                        "_cdb_category_filter WHERE adm0name NOT IN ('Spain')) " +
                        "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
                );

                // check original mapconfig keeps it right
                var aggregation = mapConfig.getWidget(0, 'adm0name');
                assert.equal(aggregation.sql(),
                        "SELECT count(*) AS count, adm0name FROM" +
                        " (select * from populated_places_simple_reduced) " +
                        "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
                );
            });

            it('uses accept and reject filter param', function() {
                var mapConfig = MapConfig.create(categoryWidgetMapConfig);

                var acceptAndRejectFilterMapConfig = mapConfig.applyFilters({layers: [{
                    adm0name: { // this is a category filter associated to the aggregation widget
                        reject: ['Spain'],
                        accept: ['USA']
                    }
                }]});
                var acceptAndRejectFilterAggregation = acceptAndRejectFilterMapConfig.getWidget(0, 'adm0name');

                assert.equal(acceptAndRejectFilterAggregation.sql(),
                        "SELECT count(*) AS count, adm0name FROM" +
                        " (SELECT * FROM" +
                        " (select * from populated_places_simple_reduced) " +
                        "_cdb_category_filter WHERE adm0name IN ('USA') AND adm0name NOT IN ('Spain')) " +
                        "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
                );

                // check original mapconfig keeps it right
                var aggregation = mapConfig.getWidget(0, 'adm0name');
                assert.equal(aggregation.sql(),
                        "SELECT count(*) AS count, adm0name FROM" +
                        " (select * from populated_places_simple_reduced) " +
                        "_cdb_aggregation GROUP BY adm0name ORDER BY count DESC"
                );
            });
        });

    });

});

{'fraction_non_alphanumeric': 0.08816801829902267, 'fraction_numerical': 0.006238303181534623, 'mean_word_length': 1.1403352618305889, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1348427', 'n_tokens_mistral': 3437, 'n_tokens_neox': 3080, 'n_words': 814}