This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
users_test.go
124 lines (119 loc) · 2.86 KB
/
users_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
package busetabot
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
"google.golang.org/appengine/datastore"
)
func TestDatastoreUserRepository_UpdateUserLastSeenTime(t *testing.T) {
ctx, done, err := aetest.NewContext()
if err != nil {
t.Fatal(err)
}
defer done()
users := new(DatastoreUserRepository)
err = users.UpdateUserLastSeenTime(ctx, 1000, time.Time{})
if err != nil {
t.Fatal(err)
}
var u User
ctx, err = appengine.Namespace(ctx, namespace)
if err != nil {
t.Fatal(err)
}
k := datastore.NewKey(ctx, KindUser, "", 1000, nil)
err = datastore.Get(ctx, k, &u)
if err != nil {
t.Fatal(err)
}
expected := User{
LastSeenTime: time.Time{},
}
assert.Equal(t, expected, u)
}
func TestDatastoreUserRepository_GetUserFavourites(t *testing.T) {
ctx, done, err := aetest.NewContext()
if err != nil {
t.Fatal(err)
}
defer done()
const userID = 1
userRepository := new(DatastoreUserRepository)
t.Run("when user does not have any favourites", func(t *testing.T) {
actual, err := userRepository.GetUserFavourites(ctx, userID)
if err != nil {
t.Fatal(err)
}
assert.Len(t, actual, 0)
})
t.Run("when user has empty favourites", func(t *testing.T) {
{
ctx, err := appengine.Namespace(ctx, namespace)
if err != nil {
t.Fatal(err)
}
k := datastore.NewKey(ctx, KindFavourites, "", userID, nil)
f := Favourites{
Favourites: []string{},
}
_, err = datastore.Put(ctx, k, &f)
if err != nil {
t.Fatal(err)
}
}
actual, err := userRepository.GetUserFavourites(ctx, userID)
if err != nil {
t.Fatal(err)
}
assert.Len(t, actual, 0)
})
t.Run("when user has favourites", func(t *testing.T) {
f := Favourites{
Favourites: []string{"96049", "81111"},
}
{
ctx, err := appengine.Namespace(ctx, namespace)
if err != nil {
t.Fatal(err)
}
k := datastore.NewKey(ctx, KindFavourites, "", userID, nil)
_, err = datastore.Put(ctx, k, &f)
if err != nil {
t.Fatal(err)
}
}
actual, err := userRepository.GetUserFavourites(ctx, userID)
if err != nil {
t.Fatal(err)
}
expected := f.Favourites
assert.Equal(t, expected, actual)
})
}
func TestDatastoreUserRepository_SetUserFavourites(t *testing.T) {
ctx, done, err := aetest.NewContext()
if err != nil {
t.Fatal(err)
}
defer done()
const userID = 1
favourites := []string{"96049", "81111"}
userRepository := new(DatastoreUserRepository)
err = userRepository.SetUserFavourites(ctx, userID, favourites)
if err != nil {
t.Fatalf("%+v", err)
}
ctx, err = appengine.Namespace(ctx, namespace)
if err != nil {
t.Fatal(err)
}
k := datastore.NewKey(ctx, KindFavourites, "", int64(userID), nil)
var f Favourites
err = datastore.Get(ctx, k, &f)
if err != nil && err != datastore.ErrNoSuchEntity {
t.Fatal(err)
}
assert.Equal(t, favourites, f.Favourites)
}