-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
50 lines (45 loc) · 1.1 KB
/
csv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2021 Wrangle Ltd
package testutils
import (
"bytes"
"encoding/csv"
"math/rand"
"strconv"
"strings"
)
func BuildRawCSV(numCol, numRow int) [][]string {
columns := []string{"id"}
for i := 0; i < numCol-1; i++ {
columns = append(columns, BrokenRandomAlphaNumericString(5))
}
rawCSV := [][]string{columns}
for i := 0; i < numRow; i++ {
row := []string{strconv.Itoa(i + 1)}
for j := 0; j < numCol-1; j++ {
row = append(row, BrokenRandomAlphaNumericString(rand.Intn(20)+1))
}
rawCSV = append(rawCSV, row)
}
return rawCSV
}
func ModifiedCSV(orig [][]string, mPercent int) [][]string {
res := [][]string{}
for i := 0; i < len(orig); i++ {
row := append([]string{}, orig[i]...)
if rand.Intn(100) <= mPercent {
j := rand.Intn(len(row))
row[j] = BrokenRandomAlphaNumericString(5)
}
res = append(res, row)
}
return res
}
func RawCSVReader(r [][]string) *csv.Reader {
l := []string{}
for _, o := range r {
l = append(l, strings.Join(o, ","))
}
buf := bytes.NewBuffer([]byte(strings.Join(l, "\n")))
return csv.NewReader(buf)
}