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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package breader
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
)
var testfile = "testdata.tsv"
func TestUnprocessedText(t *testing.T) {
var text []string
fn := func(line string) (interface{}, bool, error) {
return line, true, nil
}
reader, err := NewBufferedReader(testfile, 2, 4, fn)
if err != nil {
t.Error(err)
return
}
for chunk := range reader.Ch {
if chunk.Err != nil {
t.Error(chunk.Err)
return
}
for _, data := range chunk.Data {
text = append(text, data.(string))
}
}
originalText, err := readFileText(testfile)
if err != nil {
t.Error(err)
return
}
if strings.Join(text, "") != originalText {
t.Error("text unmatch")
}
}
func TestProcessedText(t *testing.T) {
type Slice []string
fn := func(line string) (interface{}, bool, error) {
line = strings.TrimRight(line, "\n")
if line == "" || line[0] == '#' {
return "", false, nil
}
items := strings.Split(line, "\t")
if len(items) != 2 {
return items, false, nil
}
return Slice(items), true, nil
}
reader, err := NewBufferedReader(testfile, 2, 4, fn)
if err != nil {
t.Error(err)
return
}
n := 0
for chunk := range reader.Ch {
if chunk.Err != nil {
t.Error(chunk.Err)
return
}
for _, data := range chunk.Data {
// switch reflect.TypeOf(data).Kind() {
// case reflect.Slice:
// s := reflect.ValueOf(data)
// items := make([]string, s.Len())
// for i := 0; i < s.Len(); i++ {
// items[i] = s.Index(i).String()
// }
// fmt.Println(items)
// n++
// }
fmt.Println(data.(Slice))
n++
}
}
if n != 9 {
t.Error("testing TestProcessedText failed")
}
}
func TestCancellation(t *testing.T) {
reader, err := NewBufferedReader(testfile, 1, 1, DefaultFunc)
if err != nil {
t.Error(err)
return
}
// note that range is bufferd. using range will be failed
// for chunk := range reader.Ch {
LOOP:
for {
select {
case chunk := <-reader.Ch:
if chunk.Err != nil {
t.Log(chunk.Err)
return
}
reader.Cancel()
break LOOP
default:
}
}
}
func TestProcessedTextReturnObject(t *testing.T) {
type string2int struct {
id string
value int
}
fn := func(line string) (interface{}, bool, error) {
line = strings.TrimRight(line, "\n")
if line == "" || line[0] == '#' {
return nil, false, nil
}
items := strings.Split(line, "\t")
if len(items) != 2 {
return nil, false, nil
}
if items[0] == "" || items[1] == "" {
return nil, false, nil
}
id := items[0]
value, err := strconv.Atoi(items[1])
if err != nil {
return nil, false, err
}
return string2int{id, value}, true, nil
}
reader, err := NewBufferedReader(testfile, 2, 4, fn)
if err != nil {
t.Error(err)
return
}
n := 0
for chunk := range reader.Ch {
if chunk.Err != nil {
t.Error(chunk.Err)
return
}
for _, data := range chunk.Data {
_ = data.(string2int)
n++
}
}
if n != 7 {
t.Error("testing TestProcessedTextReturnObject failed")
}
}
func readFileText(file string) (string, error) {
fh, err := os.Open(file)
defer fh.Close()
if err != nil {
return "", err
}
bs, _ := ioutil.ReadAll(fh)
return string(bs), nil
}
|