
package org.eclipse.birt.tests.data.engine.regression;

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.data.engine.api.querydefn.BaseDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.ComputedColumn;
import org.eclipse.birt.data.engine.api.querydefn.OdaDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Regression test case description:</b>
 * <p>
 * The function Total.isTopNPercent( expr, pct ) is different from
 * specification.
 * </p>
 * <b>Test description:</b>
 * <p>
 * Creating a Table with two INT columns A and B
 * </p>
 * <p>
 * Then import "Total.isTopNPercent( expr, pct )" to the column binding, and pct =
 * range [0,100] instead of [0,1] compare the result set with golen file.
 * </p>
 */

public class Regression_132519 extends APITestCase
{

	String callsTableName;

	protected void setUp( ) throws Exception
	{
		super.setUp( );

		prepareDataSet( new DataSourceInfo( ConfigText
				.getString( "Api.TestData4.TableName" ), ConfigText
				.getString( "Api.TestData4.TableSQL" ), ConfigText
				.getString( "Api.TestData4.TestDataFileName" ) ) );

		callsTableName = ConfigText.getString( "Api.TestData4.TableName" );
	}

	/*
	 * @see org.eclipse.birt.data.engine.api.APITestCase#getDataSourceInfo()
	 */
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestData4.TableName" ), ConfigText
				.getString( "Api.TestData4.TableSQL" ), ConfigText
				.getString( "Api.TestData4.TestDataFileName" ) );
	}

	public void testTotal_IsTopNPercent( ) throws Exception
	{
		String testSQL = "select " + callsTableName + ".A " + ", "
				+ callsTableName + ".B " + "from " + callsTableName;

		( (OdaDataSetDesign) this.dataSet ).setQueryText( testSQL );
		// String value = "16";

		String[] ccName = new String[]{"Actual"};
		String[] ccExpr = new String[]{"Total.isTopNPercent(dataSetRow.A,49)"};
		for ( int i = 0; i < ccName.length; i++ )
		{
			ComputedColumn computedColumn = new ComputedColumn( ccName[i],
					ccExpr[i], DataType.ANY_TYPE );
			( (BaseDataSetDesign) this.dataSet )
					.addComputedColumn( computedColumn );
		}

		String[] bindingNameRow = new String[3];
		bindingNameRow[0] = "ROW_A";
		bindingNameRow[1] = "ROW_B";
		bindingNameRow[2] = "ROW_NUM1";
		ScriptExpression[] bindingExprRow = new ScriptExpression[]{

		new ScriptExpression( "dataSetRow.A" ),
				new ScriptExpression( "dataSetRow.B" ),
				new ScriptExpression( "dataSetRow.Actual" )};

		String[] columnStr = new String[]{"id", "amount1", "amount2"};

		QueryDefinition qd = this.createQuery( null, null, null, null, null,
				null, null, null, null, bindingNameRow, bindingExprRow );
		// Generate a group
		String outputStr = getOutputStrForGroupTest( 30, qd, 0, bindingNameRow,
				columnStr );
		// use DtE output framework
		testPrint( outputStr );
		// golden file compare
		this.checkOutputFile( );
	}
}