package io.ddf.spark.content;


import com.google.common.collect.Lists;
import io.ddf.DDF;
import io.ddf.content.*;
import io.ddf.content.ViewHandler;
import io.ddf.content.ViewHandler.*;
import io.ddf.exception.DDFException;
import io.ddf.spark.BaseTest;
import junit.framework.Assert;
import org.junit.Test;

import java.util.List;

public class ViewHandlerTest extends BaseTest{

  @Test
  public void testRemoveColumns() throws DDFException {
    createTableAirline();

    DDF ddf = manager.sql2ddf("select * from airline", "SparkSQL");

    List<String> columns = Lists.newArrayList();
    columns.add("year");
    columns.add("month");
    columns.add("deptime");

    DDF newddf1 = ddf.VIEWS.removeColumn("year");
    DDF newddf2 = ddf.VIEWS.removeColumns("year", "deptime", "SparkSQL");
    DDF newddf3 = ddf.VIEWS.removeColumns(columns);

    Assert.assertEquals(28, newddf1.getNumColumns());
    Assert.assertEquals(27, newddf2.getNumColumns());
    Assert.assertEquals(26, newddf3.getNumColumns());
  }

  @Test
  public void testSubsettingWithGrep() throws DDFException {
    createTableAirline();

    DDF ddf = manager.sql2ddf("select * from airline", "SparkSQL");

    List<Column> columns = Lists.newArrayList();
    Column col = new Column();
    col.setName("origin");
    columns.add(col);

    Operator grep = new Operator();
    grep.setName(OperationName.grep);
    Expression[] operands = new Expression[2];
    StringVal val = new StringVal();
    val.setValue("IAD");
    operands[0] = val;
    operands[1] = col;
    grep.setOperarands(operands);

    DDF ddf2 = ddf.VIEWS.subset(columns, grep);
    Assert.assertEquals(2, ddf2.getNumRows());

  }

  @Test
  public void testSubsettingWithGrepIgnoreCase() throws DDFException {
    createTableAirline();

    DDF ddf = manager.sql2ddf("select * from airline", "SparkSQL");

    List<Column> columns = Lists.newArrayList();
    Column col = new Column();
    col.setName("origin");
    columns.add(col);

    Operator grep = new Operator();
    grep.setName(OperationName.grep_ic);
    Expression[] operands = new Expression[2];
    StringVal val = new StringVal();
    val.setValue("iad");
    operands[0] = val;
    operands[1] = col;
    grep.setOperarands(operands);

    DDF ddf2 = ddf.VIEWS.subset(columns, grep);
    Assert.assertEquals(2, ddf2.getNumRows());
  }
}

@@ -672,22 +672,8 @@
 %0A   
- DDF newddf1 =
  ddf
@@ -709,21 +709,57 @@
     
-DDF newddf2 =
+Assert.assertEquals(28, ddf.getNumColumns());%0A   
  ddf
@@ -815,61 +815,8 @@
 %22);%0A
-    DDF newddf3 = ddf.VIEWS.removeColumns(columns);%0A%0A
     
@@ -840,18 +840,14 @@
 ls(2
-8
+7
 , 
-new
 ddf
-1
 .get
@@ -869,54 +869,32 @@
     
-Assert.assertEquals(27, newddf2.getNum
+ddf.VIEWS.remove
 Columns(
 ));%0A
@@ -885,25 +885,31 @@
 moveColumns(
-)
+columns
 );%0A    Asser
@@ -931,15 +931,11 @@
 26, 
-new
 ddf
-3
 .get

fix viewHandlerTest