package com.bizo.hive.serde.csv;

import java.util.List;
import java.util.Properties;

import org.apache.hadoop.hive.serde.serdeConstants;
import org.apache.hadoop.io.Text;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;


public final class CSVSerdeTest {
  private final CSVSerde csv = new CSVSerde();
  final Properties props = new Properties();  
  
  @Before
  public void setup() throws Exception {
    props.put(serdeConstants.LIST_COLUMNS, "a,b,c,d");
    props.put(serdeConstants.LIST_COLUMN_TYPES, "string,string,string,string");
  }
  
  @Test
  public void testDeserialize() throws Exception {
    csv.initialize(null, props);    
    final Text in = new Text("hello,\"yes, okay\",1,new\nline");
    
    final List<String> row = (List<String>) csv.deserialize(in);

    assertEquals("hello", row.get(0));
    assertEquals("yes, okay", row.get(1));
    assertEquals("1", row.get(2));
    assertEquals("new\nline", row.get(3));
  }
  
  
  @Test
  public void testDeserializeCustomSeparators() throws Exception {
    props.put("separatorChar", "\t");
    props.put("quoteChar", "'");
    
    csv.initialize(null, props);
    
    final Text in = new Text("hello\t'yes\tokay'\t1\tnew\nline");
    final List<String> row = (List<String>) csv.deserialize(in);
        
    assertEquals("hello", row.get(0));
    assertEquals("yes\tokay", row.get(1));    
    assertEquals("1", row.get(2));
    assertEquals("new\nline", row.get(3));
  }
  
  @Test
  public void testDeserializeCustomEscape() throws Exception {
    props.put("quoteChar", "'");
    props.put("escapeChar", "\\");
    
    csv.initialize(null, props);
    
    final Text in = new Text("hello,'yes\\'okay',1,new\nline");
    final List<String> row = (List<String>) csv.deserialize(in);
        
    assertEquals("hello", row.get(0));
    assertEquals("yes'okay", row.get(1));
    assertEquals("1", row.get(2));
    assertEquals("new\nline", row.get(3));
  }  
}

@@ -733,16 +733,18 @@
 kay%5C%22,1,
+%5C%22
 new%5Cnlin
@@ -736,32 +736,34 @@
 %5C%22,1,%5C%22new%5Cnline
+%5C%22
 %22);%0A    %0A    fin
@@ -1233,16 +1233,17 @@
 ay'%5Ct1%5Ct
+'
 new%5Cnlin
@@ -1235,32 +1235,33 @@
 '%5Ct1%5Ct'new%5Cnline
+'
 %22);%0A    final Li
@@ -1726,16 +1726,18 @@
 okay',1,
+%5C'
 new%5Cnlin
@@ -1737,16 +1737,18 @@
 ew%5Cnline
+%5C'
 %22);%0A    

Correct tests.