github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/csv_test.go (about)

     1  package util
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  )
     9  
    10  type SampleSubStruct struct {
    11  	Sub1  string `csv:"this"`
    12  	NoCSV string
    13  }
    14  
    15  type SampleStruct struct {
    16  	FieldA    string          `csv:"fieldA"`
    17  	FieldInt  int             `csv:"fieldB"`
    18  	FieldBool bool            `csv:"boolean"`
    19  	Sub1      SampleSubStruct `csv:"sub1"`
    20  	Sub2      SampleSubStruct
    21  	NoCSV     string
    22  }
    23  
    24  func TestGetCSVFieldsAndValues(t *testing.T) {
    25  	Convey("With a struct with csv struct tags", t, func() {
    26  		Convey("getting csv fields should return the right fields flattened out", func() {
    27  			fields := getCSVFields(reflect.TypeOf(SampleStruct{}))
    28  			So(fields, ShouldResemble, []string{"fieldA", "fieldB", "boolean", "this", "this"})
    29  		})
    30  		Convey("csv values are printed correctly for a given sample struct", func() {
    31  			s := SampleStruct{
    32  				FieldA:    "hello",
    33  				FieldInt:  1,
    34  				FieldBool: true,
    35  				Sub1:      SampleSubStruct{"this", "print"},
    36  				Sub2:      SampleSubStruct{"another", "no"},
    37  				NoCSV:     "hi",
    38  			}
    39  			values := getCSVValues(s)
    40  			So(values, ShouldResemble, []string{"hello", "1", "true", "this", "another"})
    41  		})
    42  	})
    43  }
    44  
    45  func TestConvertCSVToRecord(t *testing.T) {
    46  	Convey("with a list of sample structs", t, func() {
    47  		s := SampleStruct{
    48  			FieldA:    "hello",
    49  			FieldInt:  1,
    50  			FieldBool: true,
    51  			Sub1:      SampleSubStruct{"this", "print"},
    52  			Sub2:      SampleSubStruct{"another", "no"},
    53  			NoCSV:     "hi",
    54  		}
    55  		s2 := SampleStruct{
    56  			FieldA:    "world",
    57  			FieldInt:  1,
    58  			FieldBool: true,
    59  			Sub1:      SampleSubStruct{"mongo", "db"},
    60  			Sub2:      SampleSubStruct{"ever", "green"},
    61  			NoCSV:     "hi",
    62  		}
    63  		samples := []SampleStruct{s, s2}
    64  		Convey("passing in a struct to convert to a record that is not a list errors", func() {
    65  			_, err := convertDataToCSVRecord(s)
    66  			So(err, ShouldNotBeNil)
    67  		})
    68  		Convey("passing in a valid set of structs should convert the record", func() {
    69  			strings, err := convertDataToCSVRecord(samples)
    70  			So(err, ShouldBeNil)
    71  			So(len(strings), ShouldEqual, 3)
    72  			So(strings[0], ShouldResemble, []string{"fieldA", "fieldB", "boolean", "this", "this"})
    73  			So(strings[1], ShouldResemble, []string{"hello", "1", "true", "this", "another"})
    74  			So(strings[2], ShouldResemble, []string{"world", "1", "true", "mongo", "ever"})
    75  		})
    76  	})
    77  }