<commit_before>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// (C) Copyright 2002-2005 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// jhyde, 28 September, 2002
*/
package mondrian.rolap;

import junit.framework.TestCase;
import mondrian.olap.Connection;
import mondrian.olap.*;
import mondrian.rolap.agg.AggregationManager;
import mondrian.rolap.agg.CellRequest;
import mondrian.rolap.sql.SqlQuery;
import mondrian.test.TestContext;
import mondrian.util.DelegatingInvocationHandler;

import javax.sql.DataSource;
import java.lang.reflect.Proxy;
import java.sql.*;

/**
 * Unit test for {@link AggregationManager}.
 *
 * @author jhyde
 * @since 21 March, 2002
 * @version $Id$
 **/
public class TestAggregationManager extends TestCase {
    public TestAggregationManager(String name) {
        super(name);
    }

    public void testFemaleUnitSales() {
        CellRequest request = createRequest("Sales", "[Measures].[Unit Sales]", "customer", "gender", "F");
        final RolapAggregationManager aggMan = AggregationManager.instance();
        Object value = aggMan.getCellFromCache(request);
        assertNull(value); // before load, the cell is not found
        FastBatchingCellReader fbcr =
                new FastBatchingCellReader(getCube("Sales"));
        fbcr.recordCellRequest(request);
        fbcr.loadAggregations();
        value = aggMan.getCellFromCache(request); // after load, cell is found
        assertTrue(value instanceof Number);
        assertEquals(131558, ((Number) value).intValue());
    }

    /**
     * Tests that a request for ([Measures].[Unit Sales], [Gender].[F])
     * generates the correct SQL.
     */
    public void testFemaleUnitSalesSql() {
        CellRequest request = createRequest("Sales", "[Measures].[Unit Sales]", "customer", "gender", "F");
        final String pattern =
                "select `customer`.`gender` as `c0`," +
                " sum(`agg_l_03_sales_fact_1997`.`unit_sales`) as `m0` " +
                "from `customer` as `customer`," +
                " `agg_l_03_sales_fact_1997` as `agg_l_03_sales_fact_1997` " +
                "where `agg_l_03_sales_fact_1997`.`customer_id` = `customer`.`customer_id` " +
                "and `customer`.`gender` = 'F' " +
                "group by `customer`.`gender`";
        assertRequestSql(new CellRequest[] {request}, pattern, "select `customer`.`gender`");
    }

    // todo: test multiple values, (UNit Sales, State={CA,OR})

    /**
     * Test a batch containing multiple measures:
     *   (store_state=CA, gender=F, measure=[UNit Sales])
     *   (store_state=CA, gender=M, measure=[Store Sales])
     *   (store_state=OR, gender=M, measure=[Unit Sales])
     */
    public void testMultipleMeasures() {
        CellRequest[] requests = new CellRequest[] {
            createRequest("Sales", "[Measures].[Unit Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"F", "CA"}),
            createRequest("Sales", "[Measures].[Store Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"M", "CA"}),
            createRequest("Sales", "[Measures].[Unit Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"F", "OR"})};
        final String pattern =
                "select `customer`.`gender` as `c0`," +
                " `store`.`store_state` as `c1`," +
                " sum(`agg_l_05_sales_fact_1997`.`unit_sales`) as `m0`," +
                " sum(`agg_l_05_sales_fact_1997`.`store_sales`) as `m1` " +
                "from `customer` as `customer`," +
                " `agg_l_05_sales_fact_1997` as `agg_l_05_sales_fact_1997`," +
                " `store` as `store` " +
                "where `agg_l_05_sales_fact_1997`.`customer_id` = `customer`.`customer_id`" +
                " and `agg_l_05_sales_fact_1997`.`store_id` = `store`.`store_id`" +
                " and `store`.`store_state` in ('CA', 'OR') " +
                "group by `customer`.`gender`, `store`.`store_state`";
        assertRequestSql(requests, pattern, "select `customer`.`gender`");
    }

    /**
     */
    private CellRequest createMultipleMeasureCellRequest() {
        String cube = "Sales";
        String measure = "[Measures].[Unit Sales]";
        String table = "store";
        String column = "store_state";
        String value = "CA";
        final Connection connection = TestContext.instance().getFoodMartConnection(false);
        final boolean fail = true;
        Cube salesCube = connection.getSchema().lookupCube(cube, fail);
        Member storeSqftMeasure = salesCube.getSchemaReader(null).getMemberByUniqueName(
                Util.explode(measure), fail);
        RolapStar.Measure starMeasure = RolapStar.getStarMeasure(storeSqftMeasure);
        CellRequest request = new CellRequest(starMeasure);
        final RolapStar star = starMeasure.getStar();
        final RolapStar.Column storeTypeColumn = star.lookupColumn(
                table, column);
        request.addConstrainedColumn(storeTypeColumn, value);
        return request;
    }

    // todo: test unrestricted column, (Unit Sales, Gender=*)<commit_msg>MONDRIAN: Fix test which was assuming that aggregate tables were switched on.<commit_after>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// (C) Copyright 2002-2005 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// jhyde, 28 September, 2002
*/
package mondrian.rolap;

import junit.framework.TestCase;
import mondrian.olap.Connection;
import mondrian.olap.*;
import mondrian.rolap.agg.AggregationManager;
import mondrian.rolap.agg.CellRequest;
import mondrian.rolap.sql.SqlQuery;
import mondrian.test.TestContext;
import mondrian.util.DelegatingInvocationHandler;

import javax.sql.DataSource;
import java.lang.reflect.Proxy;
import java.sql.*;

/**
 * Unit test for {@link AggregationManager}.
 *
 * @author jhyde
 * @since 21 March, 2002
 * @version $Id$
 **/
public class TestAggregationManager extends TestCase {
    public TestAggregationManager(String name) {
        super(name);
    }

    public void testFemaleUnitSales() {
        CellRequest request = createRequest("Sales", "[Measures].[Unit Sales]", "customer", "gender", "F");
        final RolapAggregationManager aggMan = AggregationManager.instance();
        Object value = aggMan.getCellFromCache(request);
        assertNull(value); // before load, the cell is not found
        FastBatchingCellReader fbcr =
                new FastBatchingCellReader(getCube("Sales"));
        fbcr.recordCellRequest(request);
        fbcr.loadAggregations();
        value = aggMan.getCellFromCache(request); // after load, cell is found
        assertTrue(value instanceof Number);
        assertEquals(131558, ((Number) value).intValue());
    }

    /**
     * Tests that a request for ([Measures].[Unit Sales], [Gender].[F])
     * generates the correct SQL.
     */
    public void testFemaleUnitSalesSql() {
        CellRequest request = createRequest("Sales", "[Measures].[Unit Sales]", "customer", "gender", "F");
        final String pattern =
                "select `customer`.`gender` as `c0`," +
                " sum(`sales_fact_1997`.`unit_sales`) as `m0` " +
                "from `customer` as `customer`, `sales_fact_1997` as `sales_fact_1997` " +
                "where `sales_fact_1997`.`customer_id` = `customer`.`customer_id` " +
                "and `customer`.`gender` = 'F' " +
                "group by `customer`.`gender`";
        assertRequestSql(new CellRequest[] {request}, pattern, "select `customer`.`gender`");
    }

    /**
     * As {@link #testFemaleUnitSalesSql()}, but with aggregate tables switched
     * on.
     *
     * TODO: Enable this test.
     */
    private void _testFemaleUnitSalesSql_withAggs() {
        CellRequest request = createRequest("Sales", "[Measures].[Unit Sales]", "customer", "gender", "F");
        final String pattern =
                "select `customer`.`gender` as `c0`," +
                " sum(`agg_l_03_sales_fact_1997`.`unit_sales`) as `m0` " +
                "from `customer` as `customer`," +
                " `agg_l_03_sales_fact_1997` as `agg_l_03_sales_fact_1997` " +
                "where `agg_l_03_sales_fact_1997`.`customer_id` = `customer`.`customer_id` " +
                "and `customer`.`gender` = 'F' " +
                "group by `customer`.`gender`";
        assertRequestSql(new CellRequest[] {request}, pattern, "select `customer`.`gender`");
    }

    /**
     * Test a batch containing multiple measures:
     *   (store_state=CA, gender=F, measure=[UNit Sales])
     *   (store_state=CA, gender=M, measure=[Store Sales])
     *   (store_state=OR, gender=M, measure=[Unit Sales])
     */
    public void testMultipleMeasures() {
        CellRequest[] requests = new CellRequest[] {
            createRequest("Sales", "[Measures].[Unit Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"F", "CA"}),
            createRequest("Sales", "[Measures].[Store Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"M", "CA"}),
            createRequest("Sales", "[Measures].[Unit Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"F", "OR"})};
        final String pattern =
                "select `customer`.`gender` as `c0`," +
                " `store`.`store_state` as `c1`," +
                " sum(`sales_fact_1997`.`unit_sales`) as `m0`," +
                " sum(`sales_fact_1997`.`store_sales`) as `m1` " +
                "from `customer` as `customer`," +
                " `sales_fact_1997` as `sales_fact_1997`," +
                " `store` as `store` " +
                "where `sales_fact_1997`.`customer_id` = `customer`.`customer_id` " +
                "and `sales_fact_1997`.`store_id` = `store`.`store_id` " +
                "and `store`.`store_state` in ('CA', 'OR') " +
                "group by `customer`.`gender`, `store`.`store_state`";
        assertRequestSql(requests, pattern, "select `customer`.`gender`");
    }

    /**
     * As {@link #testMultipleMeasures()}, but with aggregate tables switched
     * on.
     *
     * TODO: Enable this test.
     */
    private void _testMultipleMeasures_withAgg() {
        CellRequest[] requests = new CellRequest[] {
            createRequest("Sales", "[Measures].[Unit Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"F", "CA"}),
            createRequest("Sales", "[Measures].[Store Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"M", "CA"}),
            createRequest("Sales", "[Measures].[Unit Sales]",
                    new String[] {"customer", "store"},
                    new String[] {"gender", "store_state"},
                    new String[] {"F", "OR"})};
        final String pattern =
                "select `customer`.`gender` as `c0`," +
                " `store`.`store_state` as `c1`," +
                " sum(`agg_l_05_sales_fact_1997`.`unit_sales`) as `m0`," +
                " sum(`agg_l_05_sales_fact_1997`.`store_sales`) as `m1` " +
                "from `customer` as `customer`," +
                " `agg_l_05_sales_fact_1997` as `agg_l_05_sales_fact_1997`," +
                " `store` as `store` " +
                "where `agg_l_05_sales_fact_1997`.`customer_id` = `customer`.`customer_id`" +
                " and `agg_l_05_sales_fact_1997`.`store_id` = `store`.`store_id`" +
                " and `store`.`store_state` in ('CA', 'OR') " +
                "group by `customer`.`gender`, `store`.`store_state`";
        assertRequestSql(requests, pattern, "select `customer`.`gender`");
    }

    /**
     */
    private CellRequest createMultipleMeasureCellRequest() {
        String cube = "Sales";
        String measure = "[Measures].[Unit Sales]";
        String table = "store";
        String column = "store_state";
        String value = "CA";
        final Connection connection =
                TestContext.instance().getFoodMartConnection(false);
        final boolean fail = true;
        Cube salesCube = connection.getSchema().lookupCube(cube, fail);
        Member storeSqftMeasure = salesCube.getSchemaReader(null)
                .getMemberByUniqueName(Util.explode(measure), fail);
        RolapStar.Measure starMeasure =
                RolapStar.getStarMeasure(storeSqftMeasure);
        CellRequest request = new CellRequest(starMeasure);
        final RolapStar star = starMeasure.getStar();
        final RolapStar.Column storeTypeColumn = star.lookupColumn(
                table, column);
        request.addConstrainedColumn(storeTypeColumn, value);
        return request;
    }

    // todo: test unrestricted column, (Unit Sales, Gender=*)
{'fraction_non_alphanumeric': 0.14167453343983735, 'fraction_numerical': 0.016701764577735822, 'mean_word_length': 2.4655259184700555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7554942', 'n_tokens_mistral': 4209, 'n_tokens_neox': 3842, 'n_words': 929}