-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_test.go
130 lines (98 loc) · 3.39 KB
/
user_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
package database_test
import (
"log"
"testing"
"github.com/willeponken/go-cjdns/key"
)
type mockUser struct {
pubkey *key.Public
id uint64
invalid bool
}
func generateDuplicateUsers() (mockUsers []mockUser) {
mockKey, err := key.DecodePublic("lpu15wrt3tb6d8vngq9yh3lr4gmnkuv0rgcd2jwl5rp5v0mhlg30.k")
if err != nil {
log.Fatalf("populateMockUsers() returned unexpected error: %v", err)
}
mockUsers = append(mockUsers, mockUser{mockKey, 1, false}, mockUser{mockKey, 1, true}) // Second is invalid because it's duplicate
return
}
func generateMockUsers(numUsers int) []mockUser {
mockUsers := make([]mockUser, numUsers)
for i := 0; i < numUsers; i++ {
mockPubKey := key.Generate().Pubkey()
mockUsers[i] = mockUser{mockPubKey, uint64(i + 1), false}
}
return mockUsers
}
// TestAddUser_duplicate checks if adding duplicate users returns an error
func TestAddUser_duplicate(t *testing.T) {
db := MustOpen()
defer db.MustClose()
mockUsers := generateDuplicateUsers()
for row, test := range mockUsers {
id, err := db.AddUser(test.pubkey)
// Got error, but it should be a valid call
if err != nil && !test.invalid {
t.Errorf("Row: %d returned unexpected error: %v", row, err)
}
// Got no error, but it shouldn't be a valid call
if err == nil && test.invalid {
t.Errorf("Row: %d expected error but got id: %v", row, id)
}
// Got wrong id, and the call should be valid (i.e. we expect the correct id)
if id != test.id && !test.invalid {
t.Errorf("Row: %d unexpected id, got id: %d, wanted id: %d", row, id, test.id)
}
}
}
// TestAddUser_many checks if we're able to add 1000 users without hickups to the database
func TestAddUser_many(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode")
}
db := MustOpen()
defer db.MustClose()
mockUsers := generateMockUsers(1000)
for row, test := range mockUsers {
id, err := db.AddUser(test.pubkey)
// Got error, but it should be a valid call
if err != nil && !test.invalid {
t.Errorf("Row: %d returned unexpected error: %v", row, err)
}
// Got no error, but it shouldn't be a valid call
if err == nil && test.invalid {
t.Errorf("Row: %d expected error but got id: %v", row, id)
}
// Got wrong id, and the call should be valid (i.e. we expect the correct id)
if id != test.id && !test.invalid {
t.Errorf("Row: %d unexpected id, got id: %d, wanted id: %d", row, id, test.id)
}
}
}
// TestAddUser_fillGap checks if the gap created by adding 3 users then removing the 2nd will be filled when adding an user again
func TestAddDelUser_fillGap(t *testing.T) {
db := MustOpen()
defer db.MustClose()
mockUsers := generateMockUsers(3)
// Populate the database with the 3 users
for _, test := range mockUsers {
db.AddUser(test.pubkey)
}
secondUser := mockUsers[1]
db.DelUser(secondUser.pubkey) // Remove the second user
id, _ := db.AddUser(secondUser.pubkey) // Add the user again, should get the same as last time (2)
if id != secondUser.id {
t.Errorf("User was not added inside the gap, got id: %d, wanted: %d", id, secondUser.id)
}
}
// TestGetID_empty tests if GetID returns an error on empty database (ID: 0 and an error)
func TestGetID_empty(t *testing.T) {
db := MustOpen()
defer db.MustClose()
mockUser := generateMockUsers(1)[0]
id, err := db.GetID(mockUser.pubkey)
if id != 0 && err == nil {
t.Errorf("GetID returned unexpected id: %d, or a nil error", id)
}
}