Skip to content

Commit

Permalink
CSVRecord: docs + tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Aug 3, 2015
1 parent ac618d6 commit cf1bb23
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
15 changes: 10 additions & 5 deletions record/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

// CSVRecord is a CSV record, i.e. a line from a CSV file
//
// If created with a header its fields can be retrieved with their column name.
// In any case, one can use a "$N" field name, where N is the column index,
// starting at 0.
type CSVRecord struct {
header, record []string
}
Expand All @@ -32,8 +36,7 @@ func (r *CSVRecord) Find(field *ch.Field) (*ch.Const, error) {
// Column index

if name[0] == '$' {

index, err := strconv.ParseInt(name[1:], 10, 0)
index, err := strconv.ParseInt(name[1:], 10, 64)
if err != nil {
return nil, fmt.Errorf("Invalid column index %s: %s", name, err)
}
Expand All @@ -54,7 +57,7 @@ func (r *CSVRecord) Find(field *ch.Field) (*ch.Const, error) {
// AtIndex gets the value at the given index
func (r *CSVRecord) AtIndex(index int) (*ch.Const, error) {

if index < 0 || int(index) > len(r.record) {
if index < 0 || index > len(r.record) {
return nil, fmt.Errorf("index out of bounds %d", index)
}

Expand All @@ -67,8 +70,10 @@ func (r *CSVRecord) AtIndex(index int) (*ch.Const, error) {
return ch.ConstFromString(value), nil
}

// ColumnNameIndex searches the index of the column name into the header of
// this record. If no header, or a column not found, return -1
// ColumnNameIndex searches the index of the column name in this record’s
// header. If it doesn’t have a header or if the column wasn’t found, the
// method returns -1. The column name match is case-sensitive, the first
// matching one is used.
func (r *CSVRecord) ColumnNameIndex(name string) int {
for index, element := range r.header {
if element == name {
Expand Down
54 changes: 54 additions & 0 deletions record/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package record

import (
"testing"

ch "github.com/BatchLabs/charlatan"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewCSVRecordWithoutHeader(t *testing.T) {
c := NewCSVRecord([]string{"a", "b", "c"})
require.NotNil(t, c)
assert.Nil(t, c.header)
}

func TestFindNameWithoutHeader(t *testing.T) {
c := NewCSVRecord([]string{"a", "b", "c"})
require.NotNil(t, c)

_, err := c.Find(ch.NewField("foo"))
assert.NotNil(t, err)
}

func TestFindColumnIndex(t *testing.T) {
c := NewCSVRecord([]string{"a", "b", "c"})
require.NotNil(t, c)

_, err := c.Find(ch.NewField("$-1"))
assert.NotNil(t, err)

_, err = c.Find(ch.NewField("$42"))
assert.NotNil(t, err)

v, err := c.Find(ch.NewField("$1"))
assert.Nil(t, err)
assert.True(t, v.IsString())
assert.Equal(t, "b", v.AsString())
}

func TestFindColumnNamr(t *testing.T) {
c := NewCSVRecordWithHeader([]string{"a", "b", "c"}, []string{"id", "x", "y"})
require.NotNil(t, c)

_, err := c.Find(ch.NewField("yo"))
assert.NotNil(t, err)

_, err = c.Find(ch.NewField("xy"))
assert.NotNil(t, err)

v, err := c.Find(ch.NewField("y"))
assert.Nil(t, err)
assert.Equal(t, "c", v.AsString())
}

0 comments on commit cf1bb23

Please sign in to comment.