-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbIO_test.go
More file actions
104 lines (97 loc) · 2.79 KB
/
dbIO_test.go
File metadata and controls
104 lines (97 loc) · 2.79 KB
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
// Tests dbIO functions which do not access sql database
package dbIO
import (
"strings"
"testing"
)
func TestEscapeChars(t *testing.T) {
// Tests escapeChars (in upload.go) function using raw strings
matches := []struct {
input string
expected string
}{
{"N/A", "NA"},
{"Na", "NA"},
{"black_footed_ferret", `black\_footed\_ferret`},
{"weasel: 'Fred'", `weasel: \'Fred\'`},
{`badger\ "Reggie" `, `badger- \"Reggie\" `},
}
for _, i := range matches {
actual := escapeChars(i.input)
if actual != i.expected {
t.Errorf("Actual excaped value %s is not equal to expected: %s", actual, i.expected)
}
}
}
func getReturnString() (string, int) {
// Returns expected result of FormatMap and FormatSlice
l := 4
s := `('1','Weasel','15'),('2','stoat','9'),('3','egret','NA'),('4','black\_footed\_ferret','20')`
return s, l
}
func TestFormatMap(t *testing.T) {
// Tests FormatMap (in upload.go)
expected, exlen := getReturnString()
values := map[string][]string{
"a": {"1", "Weasel", "15"},
"b": {"2", "stoat", "9"},
"c": {"3", "egret", "na"},
"d": {"4", "black_footed_ferret", "20"},
}
actual, aclen := FormatMap(values)
if aclen != exlen {
t.Errorf("Actual length from map %d is not equal to expected: %d", aclen, exlen)
}
// Compare individual elements to account for random order of map
a := strings.Split(actual, "),(")
e := strings.Split(expected, "),(")
for _, i := range a {
i = strings.Replace(i, "(", "", -1)
i = strings.Replace(i, ")", "", -1)
id := strings.Split(i, ",")[0]
for _, j := range e {
j = strings.Replace(j, "(", "", -1)
j = strings.Replace(j, ")", "", -1)
if id == strings.Split(j, ",")[0] {
if i != j {
t.Errorf("Actual string from map %s is not equal to expected: %s", i, j)
}
break
}
}
}
}
func TestFormatSlice(t *testing.T) {
// Tests FormatSLice (in upload.go)
expected, exlen := getReturnString()
values := [][]string{
{"1", "Weasel", "15"},
{"2", "stoat", "9"},
{"3", "egret", "na"},
{"4", "black_footed_ferret", "20"},
}
actual, aclen := FormatSlice(values)
if aclen != exlen {
t.Errorf("Actual length from slice %d is not equal to expected: %d", aclen, exlen)
} else if actual != expected {
t.Errorf("Actual string from slice %s is not equal to expected: %s", actual, expected)
}
}
func TestAddApostrophes(t *testing.T) {
// Tests addApostrophes function (in extract.go)
matches := []struct {
input string
expected string
}{
{"1,Weasel,15", "'1','Weasel','15'"},
{"2,stoat,9", "'2','stoat','9'"},
{"3,egret,NA", "'3','egret','NA'"},
{`4,black\_footed\_ferret,20`, `'4','black\_footed\_ferret','20'`},
}
for _, i := range matches {
actual := addApostrophes(i.input)
if actual != i.expected {
t.Errorf("Actual apostrophe string %s is not equal to expected: %s", actual, i.expected)
}
}
}