Ejemplo n.º 1
0
func TestCSVInputReadsBad(t *testing.T) {
	fp := test_util.OpenFileFromString(bad)
	defer fp.Close()
	defer os.Remove(fp.Name())

	opts := &CSVInputOptions{
		HasHeader: true,
		Seperator: ',',
		ReadFrom:  fp,
	}

	input, _ := NewCSVInput(opts)
	expected := make([][]string, len(strings.Split(bad, "\n"))-1)
	expected[0] = []string{"1", "2", ""}
	expected[1] = []string{"4", "5", "6"}
	expected[2] = []string{"7", "8", ""}
	expected[3] = []string{"9", "", "10"}
	expected[4] = []string{"11", "12", "13", "14"}
	expected[5] = []string{"foo,bar", `boo,\"far`, ","}
	expected[6] = []string{`'foo`, `bar'`, `'"'`, `'"'`}
	expected[7] = []string{"test\n", "multi-line", ""}

	for counter := 0; counter < len(expected); counter++ {
		row := input.ReadRecord()
		if !reflect.DeepEqual(row, expected[counter]) {
			t.Errorf("ReadRecord() = %v, want %v", row, expected[counter])
		}
	}
}
Ejemplo n.º 2
0
func LoadTestDataAndExecuteQuery(t *testing.T, testData string, sqlString string) (map[int]map[string]interface{}, []string) {
	storage := NewSQLite3Storage(storageOpts)
	fp := test_util.OpenFileFromString(testData)

	opts := &inputs.CSVInputOptions{
		HasHeader: true,
		Seperator: ',',
		ReadFrom:  fp,
	}

	input, _ := inputs.NewCSVInput(opts)
	defer fp.Close()
	defer os.Remove(fp.Name())
	defer storage.Close()

	storage.LoadInput(input)

	rows, rowsErr := storage.ExecuteSQLString(sqlString)

	if rowsErr != nil {
		t.Fatalf(rowsErr.Error())
	}

	cols, colsErr := rows.Columns()

	if colsErr != nil {
		t.Fatalf(colsErr.Error())
	}

	rowNumber := 0
	result := make(map[int]map[string]interface{})
	rawResult := make([]interface{}, len(cols))
	dest := make([]interface{}, len(cols))

	for i := range cols {
		dest[i] = &rawResult[i]
	}

	for rows.Next() {
		scanErr := rows.Scan(dest...)

		if scanErr != nil {
			t.Fatalf(scanErr.Error())
		}

		result[rowNumber] = make(map[string]interface{})
		for i, raw := range rawResult {
			result[rowNumber][cols[i]] = raw
		}
		rowNumber++
	}

	return result, cols
}
Ejemplo n.º 3
0
func NewTestCSVInput() (input inputs.Input, fp *os.File) {
	fp = test_util.OpenFileFromString(simpleCSV)

	opts := &inputs.CSVInputOptions{
		HasHeader: true,
		Seperator: ',',
		ReadFrom:  fp,
	}

	newInput, _ := inputs.NewCSVInput(opts)
	return newInput, fp
}
Ejemplo n.º 4
0
func TestCSVInputReadsHeader(t *testing.T) {
	fp := test_util.OpenFileFromString(simple)
	defer fp.Close()
	defer os.Remove(fp.Name())

	opts := &CSVInputOptions{
		HasHeader: true,
		Seperator: ',',
		ReadFrom:  fp,
	}

	input, _ := NewCSVInput(opts)
	expected := []string{"a", "b", "c"}

	if !reflect.DeepEqual(input.Header(), expected) {
		t.Errorf("Header() = %v, want %v", input.Header(), expected)
	}
}
Ejemplo n.º 5
0
func TestCSVInputReadsSimple(t *testing.T) {
	fp := test_util.OpenFileFromString(simple)
	defer fp.Close()
	defer os.Remove(fp.Name())

	opts := &CSVInputOptions{
		HasHeader: true,
		Seperator: ',',
		ReadFrom:  fp,
	}

	input, _ := NewCSVInput(opts)
	expected := make([][]string, len(strings.Split(simple, "\n"))-1)
	expected[0] = []string{"1", "2", "3"}
	expected[1] = []string{"4", "5", "6"}

	for counter := 0; counter < len(expected); counter++ {
		row := input.ReadRecord()
		if !reflect.DeepEqual(row, expected[counter]) {
			t.Errorf("ReadRecord() = %v, want %v", row, expected[counter])
		}
	}
}