go-hep.org/x/hep@v0.38.1/hbook/ntup/ntcsv/ntcsv_test.go (about) 1 // Copyright ©2017 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ntcsv_test 6 7 import ( 8 "reflect" 9 "testing" 10 11 "go-hep.org/x/hep/hbook/ntup/ntcsv" 12 ) 13 14 func TestOpen(t *testing.T) { 15 for _, test := range []struct { 16 name string 17 query string 18 opts []ntcsv.Option 19 }{ 20 {"testdata/simple.csv", `var1, var2, var3`, 21 []ntcsv.Option{ 22 ntcsv.Comma(';'), 23 ntcsv.Comment('#'), 24 }, 25 }, 26 {"testdata/simple.csv", `var1, var2, var3`, 27 []ntcsv.Option{ 28 ntcsv.Comma(';'), 29 ntcsv.Comment('#'), 30 ntcsv.Columns("var1", "var2", "var3"), 31 }, 32 }, 33 {"testdata/simple.csv", `v1, v2, v3`, 34 []ntcsv.Option{ 35 ntcsv.Comma(';'), 36 ntcsv.Comment('#'), 37 ntcsv.Columns("v1", "v2", "v3"), 38 }, 39 }, 40 {"testdata/simple-comma.csv", `var1, var2, var3`, 41 []ntcsv.Option{ 42 ntcsv.Comma(','), 43 ntcsv.Comment('#'), 44 }, 45 }, 46 {"testdata/simple-with-comment.csv", `var1, var2, var3`, 47 []ntcsv.Option{ 48 ntcsv.Comma(';'), 49 ntcsv.Comment('#'), 50 }, 51 }, 52 {"testdata/simple-with-comment.csv", `v1, v2, v3`, 53 []ntcsv.Option{ 54 ntcsv.Comma(';'), 55 ntcsv.Comment('#'), 56 ntcsv.Columns("v1", "v2", "v3"), 57 }, 58 }, 59 {"testdata/simple-with-header.csv", `i, f, str`, 60 []ntcsv.Option{ 61 ntcsv.Header(), 62 ntcsv.Comma(';'), 63 ntcsv.Comment('#'), 64 }, 65 }, 66 {"testdata/simple-with-header.csv", `i64, f64, str`, 67 []ntcsv.Option{ 68 ntcsv.Header(), 69 ntcsv.Columns("i64", "f64", "str"), 70 ntcsv.Comma(';'), 71 ntcsv.Comment('#'), 72 }, 73 }, 74 {"https://codeberg.org/go-hep/hep/raw/branch/main/hbook/ntup/ntcsv/testdata/simple-with-comment.csv", `v1, v2, v3`, 75 []ntcsv.Option{ 76 ntcsv.Comma(';'), 77 ntcsv.Comment('#'), 78 ntcsv.Columns("v1", "v2", "v3"), 79 }, 80 }, 81 {"https://codeberg.org/go-hep/hep/raw/branch/main/hbook/ntup/ntcsv/testdata/simple-with-header.csv", `i64, f64, str`, 82 []ntcsv.Option{ 83 ntcsv.Header(), 84 ntcsv.Columns("i64", "f64", "str"), 85 ntcsv.Comma(';'), 86 ntcsv.Comment('#'), 87 }, 88 }, 89 } { 90 t.Run(test.name, func(t *testing.T) { 91 testCSV(t, test.name, test.query, test.opts...) 92 }) 93 } 94 } 95 96 func testCSV(t *testing.T, name, query string, opts ...ntcsv.Option) { 97 nt, err := ntcsv.Open(name, opts...) 98 if err != nil { 99 t.Fatalf("%s: error opening n-tuple: %v", name, err) 100 } 101 defer func() { 102 err = nt.DB().Close() 103 if err != nil { 104 t.Fatal(err) 105 } 106 }() 107 108 type dataType struct { 109 i int64 110 f float64 111 s string 112 } 113 114 var got []dataType 115 err = nt.Scan( 116 query, 117 func(i int64, f float64, s string) error { 118 got = append(got, dataType{i, f, s}) 119 return nil 120 }, 121 ) 122 if err != nil { 123 t.Fatalf("%s: error scanning: %v", name, err) 124 } 125 126 want := []dataType{ 127 {0, 0, "str-0"}, 128 {1, 1, "str-1"}, 129 {2, 2, "str-2"}, 130 {3, 3, "str-3"}, 131 {4, 4, "str-4"}, 132 {5, 5, "str-5"}, 133 {6, 6, "str-6"}, 134 {7, 7, "str-7"}, 135 {8, 8, "str-8"}, 136 {9, 9, "str-9"}, 137 } 138 if !reflect.DeepEqual(got, want) { 139 t.Fatalf("%s: got=\n%v\nwant=\n%v\n", name, got, want) 140 } 141 }