-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
152 lines (129 loc) · 3.03 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
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
package main
import (
"errors"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetAirportCodes(t *testing.T) {
assert := assert.New(t)
var tests = []struct {
input string
expected []string
}{
{"LJU STN GLA", []string{"LJU", "STN", "GLA"}},
{" LJU STN GLA ", []string{"LJU", "STN", "GLA"}},
{"EGSS,STN,EIDW", []string{"EGSS", "STN", "EIDW"}},
{" EGSS, STN, EIDW ", []string{"EGSS", "STN", "EIDW"}},
{"STN", []string{"STN"}},
{"lJu sTN GLa", []string{"LJU", "STN", "GLA"}},
{"", []string{}},
{" ", []string{}},
{", , ", []string{}},
}
for _, test := range tests {
output := GetAirportCodes(test.input)
assert.Equal(test.expected, output)
}
}
type errReader int
func (errReader) Read(p []byte) (n int, err error) {
return 0, errors.New("test error")
}
func TestLoadAirports(t *testing.T) {
assert := assert.New(t)
tests := []struct {
r io.Reader
errExpected bool
}{
{strings.NewReader(""), true}, // empty string
{strings.NewReader(`[{"ICAO":"OM11","IATA":"","Name":"Abu Dhabi Northeast Airport"}]`), false},
{&strings.Reader{}, true}, // nil reader
{errReader(0), true}, // nil reader
}
for _, test := range tests {
env := Env{}
err := env.LoadAirports(test.r)
if test.errExpected {
assert.NotNil(err)
} else {
assert.Nil(err)
}
}
}
func TestFindAirport(t *testing.T) {
assert := assert.New(t)
tests := []struct {
arpts []Airport
code string
want Airport
errExpected bool
}{
{
[]Airport{{"EGLL", "LHR", "London Heathrow"}}, "EGLL", Airport{"EGLL", "LHR", "London Heathrow"}, false,
},
{
[]Airport{{"EGLL", "LHR", "London Heathrow"}}, "LHR", Airport{"EGLL", "LHR", "London Heathrow"}, false,
},
{
[]Airport{}, "toolongcode", Airport{}, true, // code too long
},
{
[]Airport{}, "22", Airport{}, true, // code too short
},
{
[]Airport{{"EGLL", "LHR", "London Heathrow"}}, "JFK", Airport{}, true, // inexistent airport
},
}
for _, test := range tests {
env := Env{}
env.Airports = test.arpts
out, err := env.FindAirport(test.code)
if test.errExpected {
assert.NotNil(err)
} else {
if assert.Nil(err) {
assert.Equal(test.want, out)
}
}
}
}
func TestGetNOAAinterval(t *testing.T) {
assert := assert.New(t)
env := &Env{}
t.Run("default", func(t *testing.T) {
// Test default
err := env.GetNOAAinterval()
if assert.Nil(err) {
assert.Equal(12, env.NOAAinterval)
}
})
t.Run("pass", func(t *testing.T) {
// Set a positive number
tests := []struct {
input string
want int
errExpected bool
}{
{"15", 15, false},
{"-1", 0, true},
{"aaaaaaaaaaaaa", 0, true},
}
for _, test := range tests {
err := os.Setenv("NOAA_INTERVAL", test.input)
if err != nil {
t.Fatalf("unable to set interval: %v", err)
}
err = env.GetNOAAinterval()
if test.errExpected {
assert.NotNil(err)
} else {
if assert.Nil(err) {
assert.Equal(test.want, env.NOAAinterval)
}
}
}
})
}