-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis_repo_test.go
85 lines (61 loc) · 1.47 KB
/
redis_repo_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
package leaderboard
import (
"testing"
"github.com/go-redis/redis"
uuid "github.com/satori/go.uuid"
)
func TestAdd(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: "db:6379",
})
repo := newRedisRepo(uuid.NewV4().String(), client)
_, posTehsis, err := repo.add("tehsis", 10)
if posTehsis != 1 {
t.Error("Expected position 1 and is ", posTehsis)
}
_, posTehsis, err = repo.get("tehsis")
if err != nil {
t.Error("Error should be nil", err)
}
if posTehsis != 1 {
t.Error("Expected position 1 and is ", posTehsis)
}
}
func TestGet(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: "db:6379",
})
repo := newRedisRepo(uuid.NewV4().String(), client)
score, _, err := repo.add("tehsis", 10)
if err != nil {
t.Error("Error should be nil", err)
}
score, _, err = repo.get("tehsis")
if err != nil {
t.Error("Error should be nil", err)
}
if score != 10 {
t.Error("Expected score 10 and got", score)
}
}
func TestRange(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: "db:6379",
})
repo := newRedisRepo(uuid.NewV4().String(), client)
for i := 0; i < 10; i++ {
repo.add(uuid.NewV4().String(), uint(i))
}
top10, err := repo.repoRange(1, 10)
if err != nil {
t.Error("Error should be nil", err)
}
for index, score := range top10 {
if score.Points != uint(9-index) {
t.Error("Expected score ", string(uint(index)))
}
if score.Username == "unknown" {
t.Error("Unknown user found")
}
}
}