Commit b868e423 authored by vicotor's avatar vicotor

add test code

parent 8c62bdfa
package cachedata
import (
"context"
"github.com/odysseus/payment/model"
"testing"
)
var (
_cache *CacheData
)
func newCache() *CacheData {
if _cache != nil {
return _cache
}
_cache = NewCacheData(context.Background(), RedisConnParam{
Addr: "127.0.0.1:6739",
Password: "123456",
DbIndex: 0,
}, model.DbConfig{
Host: "127.0.0.1",
Port: 3306,
DbName: "backend",
Passwd: "123456",
})
return _cache
}
func TestCacheData_Query(t *testing.T) {
cache := newCache()
testPath := ""
testUid := int64(1)
task, err := cache.Query(testPath, testUid)
if err != nil {
t.Error(err)
}
t.Log(task)
}
func TestCacheData_MQuery(t *testing.T) {
cache := newCache()
testPaths := []string{
"path1",
"path2",
"path3",
}
testUids := []int64{
1,
2,
3,
}
taskChan, err := cache.MQuery(testPaths, testUids)
if err != nil {
t.Error(err)
}
count := 0
for task := range taskChan {
count++
t.Log(task)
if count == len(testPaths) {
break
}
}
}
func TestCacheData_SetUserInfo(t *testing.T) {
cache := newCache()
testUid := int64(1)
user, err := cache.GetUserInfo(testUid)
if err != nil {
t.Error(err)
}
t.Log(user)
}
func TestCacheData_UpdateUserBalance(t *testing.T) {
cache := newCache()
testUid := int64(1)
testBalance := 100
err := cache.UpdateUserBalance(testUid, testBalance)
if err != nil {
t.Error(err)
}
}
func TestCacheData_UpdateUserLevel(t *testing.T) {
cache := newCache()
testUid := int64(1)
testLevel := 1
err := cache.UpdateUserLevel(testUid, testLevel)
if err != nil {
t.Error(err)
}
}
func TestCacheData_UpdateUserDeleted(t *testing.T) {
cache := newCache()
testUid := int64(1)
testDeleted := 1
err := cache.UpdateUserDeleted(testUid, testDeleted)
if err != nil {
t.Error(err)
}
}
func TestCacheData_UpdateUserBalanceAndLevel(t *testing.T) {
cache := newCache()
testUid := int64(1)
testBalance := 100
testLevel := 1
// update balance first
err := cache.UpdateUserBalance(testUid, testBalance)
if err != nil {
t.Error(err)
}
// update level second.
err = cache.UpdateUserLevel(testUid, testLevel)
if err != nil {
t.Error(err)
}
}
...@@ -10,13 +10,13 @@ type DbConfig struct { ...@@ -10,13 +10,13 @@ type DbConfig struct {
User string User string
Passwd string Passwd string
Host string Host string
Port string Port int
DbName string DbName string
} }
func DbInit(dbconf DbConfig) { func DbInit(dbconf DbConfig) {
// Set up database // Set up database
datasource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbconf.User, dbconf.Passwd, dbconf.Host, dbconf.Port, dbconf.DbName) datasource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", dbconf.User, dbconf.Passwd, dbconf.Host, dbconf.Port, dbconf.DbName)
orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDriver("mysql", orm.DRMySQL)
err := orm.RegisterDataBase("default", "mysql", datasource) err := orm.RegisterDataBase("default", "mysql", datasource)
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment