-
Notifications
You must be signed in to change notification settings - Fork 4
/
poll_test.go
215 lines (171 loc) · 5.31 KB
/
poll_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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"math/rand"
"net/http"
"os"
"strconv"
"testing"
"time"
)
type TeleMockError struct {
}
func (TeleMockError) Error() string {
return "test"
}
type TeleBotMock struct {
}
func (TeleBotMock) Send(to tb.Recipient, what interface{}, options ...interface{}) (*tb.Message, error) {
return &tb.Message{ID:1}, nil
}
func (TeleBotMock) Handle(endpoint interface{}, handler interface{}) {
}
func (TeleBotMock) Edit(message tb.Editable, what interface{}, options ...interface{}) (*tb.Message, error) {
return &tb.Message{ID:1}, nil
}
func (TeleBotMock) Respond(callback *tb.Callback, responseOptional ...*tb.CallbackResponse) error {
return nil
}
func (TeleBotMock) Pin(message tb.Editable, options ...interface{}) error {
return nil
}
func (TeleBotMock) ChatMemberOf(chat *tb.Chat, user *tb.User) (*tb.ChatMember, error) {
return &tb.ChatMember{}, TeleMockError{}
}
func (TeleBotMock) Start() {
}
func (TeleBotMock) Stop() {
}
func (TeleBotMock) ChatByID(id string) (*tb.Chat, error) {
return &tb.Chat{ID:1,FirstName:"x", LastName:"y", Username:"z"}, nil
}
func CreatePoll(users []string) *Poll {
config := loadEnvConfiguration(true)
bot, _ := NewBot(config.Telegram.Token);
bot.Tbot = TeleBotMock{}
return NewPoll("pic.jpg", randSeq(5), "Test poll", users, 10, time.Now().Add(time.Duration(1000000000000)), bot,"58547223");
}
func CreateVoter() *tb.User {
return &tb.User{rand.Int(),
randSeq(5),
randSeq(5),
randSeq(5),
"ru"}
}
func TestPollCreate(t *testing.T) {
fmt.Println("Testing creating poll")
p := CreatePoll([]string{"kroshilin", "ivanov", "petrov"});
if len(p.membersList) != 3 {
t.Errorf("Count of members does not match provided!")
}
}
func TestCheckIfUserMember(t *testing.T) {
fmt.Println("Testing membership verifier")
p := CreatePoll([]string{"1", "2", "3"});
voter := CreateVoter();
voter.ID= 1;
if !p.checkIfUserIsMember(voter) {
t.Errorf("Membership verifying error!")
}
voter1 := CreateVoter();
voter.ID= 6;
if p.checkIfUserIsMember(voter1) {
t.Errorf("Membership verifying error!")
}
}
func TestVoteFilter(t *testing.T) {
fmt.Println("Testing vote filter")
p := CreatePoll([]string{"1", "2"});
voter := CreateVoter();
voter.ID = 1;
buttonId, response := p.filterVote(voter, btnYesId);
if buttonId != btnYesId {
t.Errorf("Failed to check that club member voted YES and correctly passed filter!")
fmt.Println(buttonId, response)
}
buttonId3, _ := p.filterVote(voter, btnNoId);
if buttonId3 != btnNoId {
t.Errorf("Failed to check that club member voted NO and correctly passed filter!")
}
voter2 := CreateVoter();
voter2.ID = 3;
buttonId2, response2 := p.filterVote(voter2, btnYesId);
if buttonId2 != pseudoBtnQueue {
t.Errorf("Failed to check that NON member voted YES and queued before opening!")
fmt.Println(buttonId2, response2)
}
buttonId3, response3 := p.filterVote(voter2, btnNoId);
if buttonId3 != btnNoId {
t.Errorf("Failed to check that NON member voted NO and was added to NO map!")
fmt.Println(buttonId3, response3)
}
p.pollOpensForEveryoneAt = time.Now();
buttonId4, response4 := p.filterVote(voter2, btnYesId);
if buttonId4 != btnYesId {
t.Errorf("Failed to check that NON member voted YES and was added to yes map after opening!")
fmt.Println(buttonId4, response4)
}
}
func TestPlayersRedistributionAfterOpen(t *testing.T) {
fmt.Println("Testing players redistribution")
p := CreatePoll([]string{"1", "2"});
p.playersLimit = 4
voter1 := CreateVoter();
voter1.ID = 1;
p.onVote(voter1, p.pollId + btnYesId);
voter2 := CreateVoter();
voter2.ID = 2;
p.onVote(voter2, p.pollId + btnYesId);
if len(p.results[btnYesId]) != 2 {
t.Errorf("Fail to assert that there are two YES voters!")
}
voter3 := CreateVoter();
voter3.ID = 3;
p.onVote(voter3, p.pollId + btnYesId);
voter4 := CreateVoter();
voter4.ID = 4;
p.onVote(voter4, p.pollId + btnYesId);
if len(p.results[pseudoBtnQueue]) != 2 {
t.Errorf("Fail to assert that there are two queued voters!")
}
if len(p.results[btnYesId]) != 2 {
t.Errorf("Fail to assert that there are two YES voters!")
}
p.redistributeVotesOnOpenForEveryone()
if len(p.results[btnYesId]) != 4 {
t.Errorf("Fail to assert that voters were redistributed!")
}
}
func setupFullPoll(members []string, fake bool) (*Poll, *Bot) {
os.Setenv("TZ", "Europe/Nicosia")
rand.Seed(time.Now().UnixNano())
config := loadEnvConfiguration(true)
bot, _ := NewBot(config.Telegram.Token);
if fake {
bot.Tbot = TeleBotMock{}
}
picturer := picturer{&http.Client{}, config.PicturerApi}
picture := picturer.GiveMePictureOf(config.PictureTags)
t,_ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
opensIn := config.pollOpensForEveryoneBeforeEnd
if !fake {
opensIn += 10;
}
opensAt := t.Add(time.Second * time.Duration(opensIn))
p := postPoll(t.Format("01/02 15:04") + "\nText description", picture, bot, config.PollRecipientId, members, config.playersLimit, opensAt)
bot.addButtonsHandlers(p.buttons, p.onVote)
if fake {
time.AfterFunc(time.Duration(time.Second*time.Duration(config.pollOpensForEveryoneBeforeEnd+1)), bot.Tbot.Stop)
}
bot.Tbot.Start()
return p, bot
}
func TestRealPoll(t *testing.T) {
real, _ := strconv.Atoi(os.Getenv("REAL_POLL"))
if real == 0 {
return
}
config := loadEnvConfiguration(true)
setupFullPoll(config.clubMembers, false)
}