| 1 |
package dstream |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestStreamCSV1(t *testing.T) { |
| 12 |
|
| 13 |
x1 := []interface{}{ |
| 14 |
[]float64{1, 2, 3}, |
| 15 |
[]float64{4, 5}, |
| 16 |
} |
| 17 |
x2 := []interface{}{ |
| 18 |
[]float64{34, 19, 45}, |
| 19 |
[]float64{46, 44}, |
| 20 |
} |
| 21 |
x3 := []interface{}{ |
| 22 |
[]string{"Argentina", "Canada", "India"}, |
| 23 |
[]string{"Mexico", "Egypt"}, |
| 24 |
} |
| 25 |
dat := [][]interface{}{x1, x2, x3} |
| 26 |
na := []string{"Id", "Age", "Country"} |
| 27 |
ex := NewFromArrays(dat, na) |
| 28 |
|
| 29 |
var bbuf bytes.Buffer |
| 30 |
bbuf.Write([]byte("Id,Country,Pop,Age\n")) |
| 31 |
bbuf.Write([]byte("1,Argentina,29,34\n")) |
| 32 |
bbuf.Write([]byte("2,Canada,17,19\n")) |
| 33 |
bbuf.Write([]byte("3,India,234,45\n")) |
| 34 |
bbuf.Write([]byte("4,Mexico,94,46\n")) |
| 35 |
bbuf.Write([]byte("5,Egypt,89,44\n")) |
| 36 |
|
| 37 |
rdr := bytes.NewReader(bbuf.Bytes()) |
| 38 |
|
| 39 |
|
| 40 |
types := []VarType{ |
| 41 |
{"Id", Float64}, |
| 42 |
{"Age", Float64}, |
| 43 |
{"Country", String}, |
| 44 |
} |
| 45 |
da := FromCSV(rdr).SetTypes(types).ChunkSize(3).HasHeader().Done() |
| 46 |
|
| 47 |
|
| 48 |
if !EqualReport(ex, da, true) { |
| 49 |
t.Fail() |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
da.Reset() |
| 54 |
if !EqualReport(ex, da, true) { |
| 55 |
t.Fail() |
| 56 |
} |
| 57 |
|
| 58 |
if da.NumObs() != 5 { |
| 59 |
t.Fail() |
| 60 |
} |
| 61 |
if da.NumVar() != 3 { |
| 62 |
t.Fail() |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
f, msg := checkPosName(da) |
| 67 |
if !f { |
| 68 |
print(msg) |
| 69 |
t.Fail() |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestStreamCSV2(t *testing.T) { |
| 74 |
|
| 75 |
x1 := []interface{}{ |
| 76 |
[]float64{1, 2, 3}, |
| 77 |
[]float64{4, 5}, |
| 78 |
} |
| 79 |
x2 := []interface{}{ |
| 80 |
[]float64{34, 19, 45}, |
| 81 |
[]float64{46, 44}, |
| 82 |
} |
| 83 |
dat := [][]interface{}{x1, x2} |
| 84 |
na := []string{"Id", "Age"} |
| 85 |
ex := NewFromArrays(dat, na) |
| 86 |
|
| 87 |
var bbuf bytes.Buffer |
| 88 |
bbuf.Write([]byte("Id,Age\n")) |
| 89 |
bbuf.Write([]byte("1,34\n")) |
| 90 |
bbuf.Write([]byte("2,19\n")) |
| 91 |
bbuf.Write([]byte("3,45\n")) |
| 92 |
bbuf.Write([]byte("4,46\n")) |
| 93 |
bbuf.Write([]byte("5,44\n")) |
| 94 |
|
| 95 |
rdr := bytes.NewReader(bbuf.Bytes()) |
| 96 |
|
| 97 |
types := []VarType{ |
| 98 |
{"Id", Float64}, |
| 99 |
{"Age", Float64}, |
| 100 |
} |
| 101 |
|
| 102 |
da := FromCSV(rdr).SetTypes(types).ChunkSize(3).HasHeader().Done() |
| 103 |
|
| 104 |
|
| 105 |
if !EqualReport(ex, da, true) { |
| 106 |
t.Fail() |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
da.Reset() |
| 111 |
if !EqualReport(ex, da, true) { |
| 112 |
t.Fail() |
| 113 |
} |
| 114 |
|
| 115 |
if da.NumObs() != 5 { |
| 116 |
msg := fmt.Sprintf("Incorrect number of observations (found %d, expected 3)\n", da.NumObs()) |
| 117 |
os.Stderr.WriteString(msg) |
| 118 |
t.Fail() |
| 119 |
} |
| 120 |
if da.NumVar() != 2 { |
| 121 |
msg := fmt.Sprintf("Incorrect number of variables (found %d, expected 3)\n", da.NumVar()) |
| 122 |
os.Stderr.WriteString(msg) |
| 123 |
t.Fail() |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
f, msg := checkPosName(da) |
| 128 |
if !f { |
| 129 |
print(msg) |
| 130 |
t.Fail() |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestStreamCSV3(t *testing.T) { |
| 135 |
|
| 136 |
x1 := []interface{}{ |
| 137 |
[]string{"1", "2", "3"}, |
| 138 |
[]string{"4", "5"}, |
| 139 |
} |
| 140 |
x2 := []interface{}{ |
| 141 |
[]string{"34", "19", "45"}, |
| 142 |
[]string{"46", "44"}, |
| 143 |
} |
| 144 |
dat := [][]interface{}{x1, x2} |
| 145 |
na := []string{"Id", "Age"} |
| 146 |
ex := NewFromArrays(dat, na) |
| 147 |
|
| 148 |
var bbuf bytes.Buffer |
| 149 |
bbuf.Write([]byte("Id,Age\n")) |
| 150 |
bbuf.Write([]byte("1,34\n")) |
| 151 |
bbuf.Write([]byte("2,19\n")) |
| 152 |
bbuf.Write([]byte("3,45\n")) |
| 153 |
bbuf.Write([]byte("4,46\n")) |
| 154 |
bbuf.Write([]byte("5,44\n")) |
| 155 |
|
| 156 |
rdr := bytes.NewReader(bbuf.Bytes()) |
| 157 |
|
| 158 |
types := []VarType{ |
| 159 |
{"Id", String}, |
| 160 |
{"Age", String}, |
| 161 |
} |
| 162 |
|
| 163 |
da := FromCSV(rdr).SetTypes(types).ChunkSize(3).HasHeader().Done() |
| 164 |
|
| 165 |
|
| 166 |
if !EqualReport(ex, da, true) { |
| 167 |
t.Fail() |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
da.Reset() |
| 172 |
if !EqualReport(ex, da, true) { |
| 173 |
t.Fail() |
| 174 |
} |
| 175 |
|
| 176 |
if da.NumObs() != 5 { |
| 177 |
msg := fmt.Sprintf("Incorrect number of observations (found %d, expected 3)\n", da.NumObs()) |
| 178 |
os.Stderr.WriteString(msg) |
| 179 |
t.Fail() |
| 180 |
} |
| 181 |
if da.NumVar() != 2 { |
| 182 |
msg := fmt.Sprintf("Incorrect number of variables (found %d, expected 3)\n", da.NumVar()) |
| 183 |
os.Stderr.WriteString(msg) |
| 184 |
t.Fail() |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
f, msg := checkPosName(da) |
| 189 |
if !f { |
| 190 |
print(msg) |
| 191 |
t.Fail() |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func TestCSVWriter1(t *testing.T) { |
| 196 |
data1 := `id,v1,v2,v3 |
| 197 |
1,2,3,4 |
| 198 |
1,3,4,5 |
| 199 |
2,4,5,6 |
| 200 |
3,5,6,7 |
| 201 |
3,99,99,99 |
| 202 |
3,100,101,102 |
| 203 |
4,200,201,202 |
| 204 |
` |
| 205 |
|
| 206 |
r := strings.NewReader(data1) |
| 207 |
|
| 208 |
types := []VarType{ |
| 209 |
{"id", Float64}, |
| 210 |
{"v1", Float64}, |
| 211 |
{"v2", Float64}, |
| 212 |
{"v3", Float64}, |
| 213 |
} |
| 214 |
|
| 215 |
ds := FromCSV(r).SetTypes(types).ChunkSize(2).HasHeader().Done() |
| 216 |
|
| 217 |
var buf bytes.Buffer |
| 218 |
fm := map[string]string{"v1": "%.1f"} |
| 219 |
err := ToCSV(ds).SetWriter(&buf).FloatFmt("%.0f").Formats(fm).Done() |
| 220 |
if err != nil { |
| 221 |
panic(err) |
| 222 |
} |
| 223 |
|
| 224 |
es := `id,v1,v2,v3 |
| 225 |
1,2.0,3,4 |
| 226 |
1,3.0,4,5 |
| 227 |
2,4.0,5,6 |
| 228 |
3,5.0,6,7 |
| 229 |
3,99.0,99,99 |
| 230 |
3,100.0,101,102 |
| 231 |
4,200.0,201,202 |
| 232 |
` |
| 233 |
|
| 234 |
if es != buf.String() { |
| 235 |
t.Fail() |
| 236 |
} |
| 237 |
} |