go-csv-table/gocsvtable_test.go
2025-03-30 15:29:43 -03:00

150 lines
3.3 KiB
Go

package gocsvtable
import (
"io"
"reflect"
"testing"
)
const testFile = "test.csv"
func TestOpenCSV(t *testing.T) {
gct := GoCSVtable{}
err := gct.OpenCSV(testFile)
if err != nil {
t.Errorf("\nError opening csv file:\n - %#v\n", err.Error())
}
}
func TestGetHeader(t *testing.T) {
const letters = 0
const numbers = 1
gct := GoCSVtable{}
gct.OpenCSV(testFile)
_, err := gct.GetHeader("Non Existent Field")
if err == nil {
t.Errorf("Getting header of non existent field should raise error.")
}
gotLettersCol, _ := gct.GetHeader("Letters")
if gotLettersCol != letters {
t.Errorf("Error reading correct 'Letters' column. Expected: '%v'. Got: '%v'.\n", letters, gotLettersCol)
}
gotNumbersCol, _ := gct.GetHeader("Numbers")
if gotNumbersCol != numbers {
t.Errorf("Error reading correct 'Numbers' column. Expected: '%v'. Got: '%v'.\n", numbers, gotNumbersCol)
}
}
func TestNext(t *testing.T) {
gct := GoCSVtable{}
gct.OpenCSV(testFile)
for row := 2; row < 5; row++ {
err := gct.Next()
if err != nil {
t.Errorf("Error passing to next row. CSV file have 5 rows, not %v", row)
}
}
// Try to go to different row, it should receive io.EOF
err := gct.Next()
if err != io.EOF {
t.Errorf("Error on Next(), row should be inexistent. Expected: '%v'; Got: %v", io.EOF, err)
}
}
func TestGetNumberOfRows(t *testing.T) {
gct := GoCSVtable{}
gct.OpenCSV(testFile)
expected := 4 // 1 Header + 4 Data
if gct.GetNumberOfRows() != expected {
t.Errorf("Error getting number of rows in file. Expected: '%v'. Got: '%v'", expected, gct.GetNumberOfRows())
}
}
func TestRead(t *testing.T) {
gct := GoCSVtable{}
gct.OpenCSV(testFile)
row := 2
expected := "b"
gct.Row = row
got, err := gct.Read("Letters")
if err != nil {
t.Errorf("%v", err.Error())
}
if got != expected {
t.Errorf("Error getting correct field on row '%v'. Expected: '%v'. Got: '%v'", row, expected, got)
}
expected = "2"
got, err = gct.Read("Numbers")
if err != nil {
t.Errorf("%v", err.Error())
}
if got != expected {
t.Errorf("Error getting correct field on row '%v'. Expected: '%v'. Got: '%v'", row, expected, got)
}
}
func TestReadRow(t *testing.T) {
gct := GoCSVtable{}
gct.OpenCSV(testFile)
row := 3
gct.Row = row
got, err := gct.ReadRow(row)
if err != nil {
t.Errorf("Error getting correct row: '%v'", err.Error())
}
col, err := gct.GetHeader("Letters")
if err != nil {
t.Errorf("%v", err.Error())
}
gotLetter := got[col]
expected := "c"
if gotLetter != expected {
t.Errorf("Error getting correct row. Expected: '[%v %v]'. Got: '%v'", expected, row, got)
}
col, err = gct.GetHeader("Numbers")
if err != nil {
t.Errorf("%v", err.Error())
}
gotNumber := got[col]
expected = "3"
if gotNumber != expected {
t.Errorf("Error getting correct row. Expected: '[%v %v]'. Got: '%v'", expected, row, got)
}
row = 7
got, err = gct.ReadRow(row)
if err == nil {
t.Errorf("Getting inexistent row should raise error. Value: '%v'. Error: '%v'", got, err)
}
}
func TestReadCol(t *testing.T) {
gct := GoCSVtable{}
gct.OpenCSV(testFile)
expected := []string{"a", "b", "c", "d"}
col := "Letters"
got, err := gct.ReadCol(col)
if err != nil {
t.Errorf("%v", err.Error())
}
if !reflect.DeepEqual(expected, got) {
t.Errorf("Error getting column. Expected: '%v'. Got: '%v'", expected, got)
}
}