-
Notifications
You must be signed in to change notification settings - Fork 18
/
userTags_test.go
89 lines (77 loc) · 2.29 KB
/
userTags_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
package main
import (
"context"
"github.com/stretchr/testify/assert"
"sort"
"testing"
)
func TestAddOrUpdateUserTags(t *testing.T) {
migrateDatabase()
initializeDatabase()
defer tearDatabase()
defer dropDatabase()
_, err := db.Exec("INSERT INTO user_tags (user_id, tag, last_read) VALUES ('1', 'webdev', '2021-09-12 08:54:07')")
assert.Nil(t, err)
rows, err := db.Query("SELECT last_read FROM user_tags WHERE tag = 'webdev' LIMIT 1")
assert.Nil(t, err)
defer rows.Close()
rows.Next()
var webdevLastRead string
err = rows.Scan(&webdevLastRead)
assert.Nil(t, err)
err = addOrUpdateUserTags(context.Background(), "1", []string{"webdev", "javascript"})
assert.Nil(t, err)
rows, err = db.Query("SELECT user_id, tag, last_read FROM user_tags ORDER BY tag")
assert.Nil(t, err)
defer rows.Close()
var userId string
var tag string
var lastRead string
var i = 0
for rows.Next() {
err := rows.Scan(&userId, &tag, &lastRead)
assert.Nil(t, err)
assert.Equal(t, "1", userId)
if i == 0 {
assert.Equal(t, "javascript", tag)
} else if i == 1 {
assert.Equal(t, "webdev", tag)
assert.NotEqual(t, lastRead, webdevLastRead)
}
i++
}
assert.Equal(t, 2, i)
err = rows.Err()
assert.Nil(t, err)
}
func TestDeleteOldUserTags(t *testing.T) {
migrateDatabase()
initializeDatabase()
defer tearDatabase()
defer dropDatabase()
_, err := db.Exec("INSERT INTO user_tags (user_id, tag, last_read) VALUES ('1', 'webdev', '2021-01-12 08:54:07')")
assert.Nil(t, err)
err = addOrUpdateUserTags(context.Background(), "1", []string{"php", "javascript"})
assert.Nil(t, err)
err = deleteOldTags(context.Background())
assert.Nil(t, err)
rows, err := db.Query("SELECT count(*) FROM user_tags")
assert.Nil(t, err)
defer rows.Close()
rows.Next()
var count int
assert.NoError(t, rows.Scan(&count))
assert.Equal(t, 2, count)
}
func TestGetUserTags(t *testing.T) {
migrateDatabase()
initializeDatabase()
defer tearDatabase()
defer dropDatabase()
_, err := db.Exec("INSERT INTO user_tags (user_id, tag, last_read) VALUES ('1', 'webdev', '2021-01-12 08:54:07'), ('1', 'php', '2021-01-12 08:54:07'), ('2', 'webdev', '2021-01-12 08:54:07')")
assert.Nil(t, err)
tags, err := getUserTags(context.Background(), "1")
assert.Nil(t, err)
sort.Strings(tags)
assert.Equal(t, []string{"php", "webdev"}, tags)
}