<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.
// Copyright (C) 2004-2007 <NAME> and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
 */
package mondrian.olap;

import mondrian.test.FoodMartTestCase;
import mondrian.olap.fun.*;

import java.util.*;

/**
 * Tests a customized MDX Parser.
 *
 * @author <NAME>
 * @version $Id$
 */
public class CustomizedParserTest extends FoodMartTestCase {

    Parser p = new Parser();   
    public CustomizedParserTest(String name) {
        super(name);
    }

    CustomizedFunctionTable getCustomizedFunctionTable(Set<String> funNameSet) {
        Set<FunDef> specialFunctions = new HashSet<FunDef>();
        ParenthesesFunDef specialPerentheseFun = new ParenthesesFunDef(Category.Numeric);
        specialFunctions.add(specialPerentheseFun);

        CustomizedFunctionTable cftab = 
            new CustomizedFunctionTable(funNameSet, specialFunctions);        
        cftab.init();
        return cftab;
    }

    private String wrapExpr(String expr) {
        return "with member [Measures].[Foo] as " +
        expr +
        "\n select from [Sales]";
    }

    private void checkErrorMsg(Throwable e, String expectedErrorMsg) {
        String actualMsg = e.getMessage();
        if (e.getCause() != null) {
            actualMsg = e.getCause().getMessage();
        }
        assertEquals(fold(expectedErrorMsg), actualMsg);        
    }

    private Query getParsedQueryForExpr(CustomizedFunctionTable cftab, String expr) {
        String mdx = wrapExpr(expr);
        Query q = p.parseInternal(getConnection(), mdx, false, cftab, false);
        return q;
    }
    
    public void testAddition() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);
        
        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                    "([Measures].[Store Cost] + [Measures].[Unit Sales])");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }        
    }

    public void testSubtraction() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("-");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                "([Measures].[Store Cost] - [Measures].[Unit Sales])");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }
    }

    public void testSingleMultiplication() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("*");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                "[Measures].[Store Cost] * [Measures].[Unit Sales]");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }             
    }


    public void testMultipleMultiplication() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("*");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                    "([Measures].[Store Cost] * [Measures].[Unit Sales] * [Measures].[Store Sales])");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }                
    }

    public void testLiterals() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab, "([Measures].[Store Cost] + 10)");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }
    }

    public void testMultiplicationFail() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab, "([Measures].[Store Cost] * [Measures].[Unit Sales])");
            q.resolve(q.createValidator(cftab));
            // Shouldn't reach here
            fail();
        } catch (Throwable e) {
            checkErrorMsg(e,
            "Mondrian Error:No function matches signature '<Member> * <Member>'");            
        }
    }

    public void testMixingAttributesFail() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab, "([Measures].[Store Cost] + [Customers].[Name])");
            q.resolve(q.createValidator(cftab));<commit_msg>MONDRIAN: add a missing object test to CustomizedParser test<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.
// Copyright (C) 2004-2007 <NAME> and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
 */
package mondrian.olap;

import mondrian.test.FoodMartTestCase;
import mondrian.olap.fun.*;

import java.util.*;

/**
 * Tests a customized MDX Parser.
 *
 * @author <NAME>
 * @version $Id$
 */
public class CustomizedParserTest extends FoodMartTestCase {

    Parser p = new Parser();   
    public CustomizedParserTest(String name) {
        super(name);
    }

    CustomizedFunctionTable getCustomizedFunctionTable(Set<String> funNameSet) {
        Set<FunDef> specialFunctions = new HashSet<FunDef>();
        ParenthesesFunDef specialPerentheseFun = new ParenthesesFunDef(Category.Numeric);
        specialFunctions.add(specialPerentheseFun);

        CustomizedFunctionTable cftab = 
            new CustomizedFunctionTable(funNameSet, specialFunctions);        
        cftab.init();
        return cftab;
    }

    private String wrapExpr(String expr) {
        return "with member [Measures].[Foo] as " +
        expr +
        "\n select from [Sales]";
    }

    private void checkErrorMsg(Throwable e, String expectedErrorMsg) {
        String actualMsg = e.getMessage();
        if (e.getCause() != null) {
            actualMsg = e.getCause().getMessage();
        }
        assertEquals(fold(expectedErrorMsg), actualMsg);        
    }

    private Query getParsedQueryForExpr(CustomizedFunctionTable cftab, String expr) {
        String mdx = wrapExpr(expr);
        Query q = p.parseInternal(getConnection(), mdx, false, cftab, false);
        return q;
    }
    
    public void testAddition() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);
        
        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                    "([Measures].[Store Cost] + [Measures].[Unit Sales])");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }        
    }

    public void testSubtraction() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("-");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                "([Measures].[Store Cost] - [Measures].[Unit Sales])");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }
    }

    public void testSingleMultiplication() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("*");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                "[Measures].[Store Cost] * [Measures].[Unit Sales]");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }             
    }


    public void testMultipleMultiplication() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("*");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab,
                    "([Measures].[Store Cost] * [Measures].[Unit Sales] * [Measures].[Store Sales])");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }                
    }

    public void testLiterals() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab, "([Measures].[Store Cost] + 10)");
            q.resolve(q.createValidator(cftab));
        } catch (Throwable e) {
            fail(e.getMessage());
        }
    }

    public void testMissingObjectFail() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab, "([Measures].[Store Cost] + [Measures].[Unit Salese])");
            q.resolve(q.createValidator(cftab));
            // Shouldn't reach here
            fail();
        } catch (Throwable e) {
            checkErrorMsg(e,
            "Mondrian Error:MDX object '[Measures].[Unit Salese]' not found in cube 'Sales'");            
        }
    }
    
    public void testMultiplicationFail() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab, "([Measures].[Store Cost] * [Measures].[Unit Sales])");
            q.resolve(q.createValidator(cftab));
            // Shouldn't reach here
            fail();
        } catch (Throwable e) {
            checkErrorMsg(e,
            "Mondrian Error:No function matches signature '<Member> * <Member>'");            
        }
    }

    public void testMixingAttributesFail() {
        Set<String> functionNameSet = new HashSet<String>();
        functionNameSet.add("+");
        CustomizedFunctionTable cftab = getCustomizedFunctionTable(functionNameSet);

        try {
            Query q = 
                getParsedQueryForExpr(cftab, "([Measures].[Store Cost] + [Customers].[Name])");
            q.resolve(q.createValidator(cftab));
{'fraction_non_alphanumeric': 0.11461836998706339, 'fraction_numerical': 0.0017248814144027599, 'mean_word_length': 2.250911129800953, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '7655120', 'n_tokens_mistral': 3278, 'n_tokens_neox': 3147, 'n_words': 675}