-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbasic_test.go
More file actions
180 lines (154 loc) · 6.47 KB
/
basic_test.go
File metadata and controls
180 lines (154 loc) · 6.47 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
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
175
176
177
178
179
180
package cinnabot
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/mock"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
var (
mockMsg message
)
type mockBot struct {
mock.Mock
}
func (mb *mockBot) GetUpdatesChan(config tgbotapi.UpdateConfig) (tgbotapi.UpdatesChannel, error) {
return nil, nil
}
func (mb *mockBot) Send(c tgbotapi.Chattable) (tgbotapi.Message, error) {
args := mb.Called(c)
return tgbotapi.Message{}, args.Error(0)
}
func setup() {
mockMsg = message{
Args: []string{"test_args1", "test_args2"},
Message: &tgbotapi.Message{
MessageID: 1,
From: &tgbotapi.User{
ID: 999,
FirstName: "test_first_name_user",
},
Location: &tgbotapi.Location{
Latitude: 1.31760778241046,
Longitude: 103.76768583722071,
},
},
}
}
func TestMain(m *testing.M) {
setup()
os.Exit(m.Run())
}
func TestSayHello(t *testing.T) {
mb := mockBot{}
cb := Cinnabot{
bot: &mb,
}
expectedMsgStr := "Hello there, " + mockMsg.From.FirstName + "!"
expectedMsg := tgbotapi.NewMessage(999, expectedMsgStr)
mb.On("Send", expectedMsg).Return(nil)
cb.SayHello(&mockMsg)
}
func TestEcho(t *testing.T) {
mb := mockBot{}
cb := Cinnabot{
bot: &mb,
}
expectedMsgStr := "🤖: " + strings.Join(mockMsg.Args, " ")
expectedMsg := tgbotapi.NewMessage(999, expectedMsgStr)
mb.On("Send", expectedMsg).Return(nil)
cb.Echo(&mockMsg)
}
func TestCapitalize(t *testing.T) {
mb := mockBot{}
cb := Cinnabot{
bot: &mb,
}
expectedMsgStr := "TEST_ARGS1 TEST_ARGS2"
expectedMsg := tgbotapi.NewMessage(999, expectedMsgStr)
mb.On("Send", expectedMsg).Return(nil)
cb.Capitalize(&mockMsg)
}
//Bus Timings should be tested in three phases
//1. Pop three closest busstops according to location
//2. The right format
//3. HTTP request works
//TestBus tests the format of the response string.
// func TestBus(t *testing.T) {
// mb := mockBot{}
// cb := Cinnabot{
// bot: &mb,
// }
// BSH := makeHeap(*mockMsg.Location)
// oldgetBusStops := makeHeap
// defer func() { makeHeap = oldgetBusStops }()
// makeHeap = func(loc tgbotapi.Location) BusStopHeap {
// return BSH
// }
// oldreadall := ioutil.ReadAll
// defer func() { ioutil.ReadAll = oldreadall }()
// //Simulates with a default response @ busStop code 19059
// ioutil.ReadAll = func(r io.Reader) ([]byte, error) {
// responseText := "{\"odata.metadata\":\"http://datamall2.mytransport.sg/ltaodataservice/$metadata#BusArrivalv2/" +
// "@Element\",\"BusStopCode\":\"42109\",\"Services\":[{\"ServiceNo\":\"170\",\"Operator\":\"SBST\",\"NextBus" +
// "\":{\"OriginCode\":\"46239\",\"DestinationCode\":\"01109\",\"EstimatedArrival\":\"2017-12-22T01:38:47" +
// "+08:00\",\"Latitude\":\"1.4398775\",\"Longitude\":\"103.768522\",\"VisitNumber\":\"1\",\"Load\":\"" +
// "SEA\",\"Feature\":\"WAB\",\"Type\":\"SD\"},\"NextBus2\":{\"OriginCode\":\"\",\"DestinationCode\"" +
// ":\"\",\"EstimatedArrival\":\"\",\"Latitude\":\"\",\"Longitude\":\"\",\"VisitNumber\":\"\",\"" +
// "Load\":\"\",\"Feature\":\"\",\"Type\":\"\"},\"NextBus3\":{\"OriginCode\":\"\",\"Destin" +
// "ationCode\":\"\",\"EstimatedArrival\":\"\",\"Latitude\":\"\",\"Longitude\":\"\",\"V" +
// "isitNumber\":\"\",\"Load\":\"\",\"Feature\":\"\",\"Type\":\"\"}}]}"
// return []byte(responseText), nil
// }
// expectedMsgStr := "Opp Beauty World Ctr\n================\nBus 170 : 25 minutes\n\nOpp Beauty World Ctr\n=======" +
// "=========\nBus 170 : 25 minutes\n\nOpp Beauty World Ctr\n================\nBus 170 : 25 minutes\n\n"
// expectedMsg := tgbotapi.NewMessage(999, expectedMsgStr)
// mb.On("Send", expectedMsg).Return(nil)
// cb.BusTimings(&mockMsg)
// }
//Test Weather tests format of output
// func TestWeather(t *testing.T) {
// mb := mockBot{}
// cb := Cinnabot{
// bot: &mb,
// }
// oldreadall := ioutil.ReadAll
// defer func() { ioutil.ReadAll = oldreadall }()
// //Simulates with a default response
// ioutil.ReadAll = func(r io.Reader) ([]byte, error) {
// responseText := "{\"area_metadata\":[{\"name\":\"Bukit Batok\",\"label_location\":{\"latitude\":1.353,\"" +
// "longitude\":103.754}},{\"name\":\"Bukit Merah\",\"label_location\":{\"latitude\":1.277,\"longitude\":103.8" +
// "19}},{\"name\":\"Bukit Panjang\",\"label_location\":{\"latitude\":1.362,\"longitude\":103.77195}},{\"n" +
// "ame\":\"Bukit Timah\",\"label_location\":{\"latitude\":1.325,\"longitude\":103.791}},{\"name\":\"C" +
// "entral Water Catchment\",\"label_location\":{\"latitude\":1.38,\"longitude\":103.805}},{\"nam" +
// "e\":\"Changi\",\"label_location\":{\"latitude\":1.357,\"longitude\":103.987}},{\"name\":\"" +
// "Choa Chu Kang\",\"label_location\":{\"latitude\":1.377,\"longitude\":103.745}},{\"name" +
// "\":\"Clementi\",\"label_location\":{\"latitude\":1.315,\"longitude\":103.76}},{" +
// "\"name\":\"City\",\"label_location\":{\"latitude\":1.292,\"longitude\":1" +
// "03.844}},{\"name\":\"Geylang\",\"label_location\":{\"latitude\":1.318" +
// ",\"longitude\":103.884}},{\"name\":\"Hougang\",\"label_location\":{\"" +
// "latitude\":1.361218,\"longitude\":103.886}},{\"name\":\"Jalan " +
// "Bahar\",\"label_location\":{\"latitude\":1.347,\"longitude\"" +
// ":103.67}}}],\"items\":[{\"update_timestamp\":\"2017-12-" +
// "22T15:41:18+08:00\",\"timestamp\":\"2017-12-22T15:00:" +
// "00+08:00\",\"valid_period\":{\"start\":\"2017-12-" +
// "22T15:00:00+08:00\",\"end\":\"2017-12-22T17:0" +
// "0:00+08:00\"},\"forecasts\":[{\"area\":\"" +
// "Bukit Batok\",\"forecast\":\"Partly " +
// "Cloudy (Day)\"},{\"area\":\"Bukit" +
// " Merah\",\"forecast\":\"Partl" +
// "y Cloudy (Day)\"},{\"area" +
// "\":\"Bukit Panjang\"," +
// "\"forecast\":\"Par" +
// "tly Cloudy (Da" +
// "y)\"},{\"a" +
// "rea\":\"" +
// "Buk" +
// "it Timah\",\"forecast\":\"Partly Cloudy (Day)\"},{\"area\":\"Central Water Catchment\",\"forecast\":\"Partly Cloudy (Day)\"},{\"area\":\"Changi\",\"forecast\":\"Light Showers\"},{\"area\":\"Choa Chu Kang\",\"forecast\":\"Partly Cloudy (Day)\"},{\"area\":\"Clementi\",\"forecast\":\"Partly Cloudy (Day)\"},{\"area\":\"City\",\"forecast\":\"Light Showers\"},{\"area\":\"Geylang\",\"forecast\":\"Light Showers\"},{\"area\":\"Hougang\",\"forecast\":\"Light Showers\"},{\"area\":\"Jalan Bahar\",\"forecast\":\"Partly Cloudy (Day)\"}}"
// return []byte(responseText), nil
// }
// expectedMsgStr := "🤖 The forecast is Partly Cloudy (Day) for Clementi"
// expectedMsg := tgbotapi.NewMessage(999, expectedMsgStr)
// mb.On("Send", expectedMsg).Return(nil)
// cb.BusTimings(&mockMsg)
// }