-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
93 lines (77 loc) · 2.54 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
package main
import (
"path/filepath"
"testing"
)
func TestFetchUsers(t *testing.T) {
fileName := "test_users.csv"
path := filepath.Join("testdata", fileName)
results, err := fetchUsers(path)
if err != nil {
t.Fatalf("Fatal error in parsing csv: %s", err)
}
for i, expected := range expectedUsers() {
if expected.Name != results[i].Name {
t.Errorf("Name: Expected: %s, Got %s", expected.Name, results[i].Name)
}
if expected.Email != results[i].Email {
t.Errorf("Email: Expected: %s, Got %s", expected.Email, results[i].Email)
}
}
}
func TestBuildMessagesTwoUsers(t *testing.T) {
users := []User{
User{"Star Fox", "sf@sfox.com"},
User{"Falco Lombardi", "fl@sfox.com"},
}
expectedMessages := []Message{
Message{SecretSanta: users[0], Recipient: users[1]},
Message{SecretSanta: users[1], Recipient: users[0]},
}
results := buildMessages(users)
for i, expected := range expectedMessages {
if expected.SecretSanta != results[i].SecretSanta {
t.Errorf("SecretSanta: Expected: %s, Got %s", expected.SecretSanta, results[i].SecretSanta)
}
if expected.Recipient != results[i].Recipient {
t.Errorf("Recipient: Expected: %s, Got %s", expected.Recipient, results[i].Recipient)
}
}
}
func TestBuildMessagesAllUsersAccountedFor(t *testing.T) {
users := expectedUsers()
results := buildMessages(users)
secretSantaUserCount := make(map[string]int)
recipientUserCount := make(map[string]int)
// count the users as secret santa and recipient
for _, message := range results {
secretSantaUserCount[message.SecretSanta.Email]++
recipientUserCount[message.Recipient.Email]++
}
// check that a user is a secret santa and recipient only once
for _, user := range users {
if secretSantaUserCount[user.Email] != 1 {
t.Errorf("%s was not made a secret santa", user.Email)
}
if recipientUserCount[user.Email] != 1 {
t.Errorf("%s was not made a recipient", user.Email)
}
delete(secretSantaUserCount, user.Email)
delete(recipientUserCount, user.Email)
}
if len(secretSantaUserCount) != 0 {
t.Errorf("Additional secret santas were generated: %v", secretSantaUserCount)
}
if len(recipientUserCount) != 0 {
t.Errorf("Additional recipients were generated: %v", recipientUserCount)
}
}
func expectedUsers() []User {
return []User{
User{"Philip J Fry", "pfry@planetexpress.com"},
User{"Turanga Leela", "tleela@planetexpress.com"},
User{"Dr. Amy Wong", "awong@planetexpress.com"},
User{"Professor Hubert J. Farnsworth", "hfarnsworth@planetexpress.com"},
User{"Bender Bending Rodriguez", "bender@planetexpress.com"},
}
}