/
main_test.go
104 lines (89 loc) · 2.07 KB
/
main_test.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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
)
func initConf() {
API1, ok := os.LookupEnv("API1")
if !ok {
panic("$API1 - enviromental variable not set")
}
tmpBody := `
---
website:
- api:
website: "dictionaryapi.com"
link: "https://www.dictionaryapi.com/api/v3/references/collegiate/json/"
apikey: "` + API1 + `"
- api:
website: "api.dictionaryapi.dev"
link: "https://api.dictionaryapi.dev/api/v2/entries/en/"
apikey: NULL
dictionary: "dictionary.json"
`
fmt.Println(tmpBody)
err := ioutil.WriteFile("./conf.yaml", []byte(tmpBody), 0644)
if err != nil {
log.Fatal(err)
}
}
func getLogistics() (*config, string, map[string][]string) {
initConf()
apiConf, defPath := getConfig()
dictFile := getDictConf(apiConf)
dictionary := getDict(defPath + "/" + dictFile)
return apiConf, dictFile, dictionary
}
func TestLocateDef(t *testing.T) {
apiConf, dictFile, dictionary := getLogistics()
var tests = []struct {
a string
o1 bool
}{
{"undefinablestring", true},
{"definable", false},
}
for _, tv := range tests {
t.Run(tv.a, func(t *testing.T) {
definition := locateDef(tv.a, apiConf, dictFile, dictionary)
ok := verifyDef(definition)
if ok != tv.o1 {
t.Errorf("got: %s is a (%v) def - want (%v)", tv.a, ok, tv.o1)
}
})
}
os.Remove("conf.yaml")
}
func TestCallAPI(t *testing.T) {
apiConf, _, _ := getLogistics()
var tests = []struct {
idx int
word string
o1 int
o2 int
}{
{0, "undefinableword", 0, 0},
{0, "definable", 1, 10},
{1, "undefinableword", 0, 0},
{1, "definable", 1, 10},
}
for _, tv := range tests {
website, link, apiKey := parseConfig(apiConf, tv.idx)
t.Run(website, func(t *testing.T) {
requestLink, err := parseRequest(tv.word, website, link, apiKey)
if err != nil {
t.Errorf("failed with a fatal error")
} else {
response := callAPI(website, requestLink)
if (len(response) >= tv.o1) && (len(response) <= tv.o2) {
return
}
t.Errorf("got %d - wanted %d/%d", len(response), tv.o1, tv.o2)
}
})
}
os.Remove("conf.yaml")
}