好友db测试

This commit is contained in:
zhaocy 2022-06-08 17:59:53 +08:00
parent 3dbdfdf8a7
commit f036c1a5e2
10 changed files with 77 additions and 57 deletions

View File

@ -25,7 +25,7 @@ type Cache_FriendData struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
UserId string `protobuf:"bytes,1,opt,name=userId,proto3" json:"userId,omitempty"` UserId string `protobuf:"bytes,1,opt,name=userId,proto3" json:"userId,omitempty" bson:"_id"` //tags:{bson:"_id"}用户Id
FriendId []string `protobuf:"bytes,2,rep,name=friendId,proto3" json:"friendId,omitempty"` FriendId []string `protobuf:"bytes,2,rep,name=friendId,proto3" json:"friendId,omitempty"`
} }

View File

@ -3,6 +3,6 @@ option go_package = ".;pb";
message Cache_FriendData { message Cache_FriendData {
string userId = 1; string userId = 1; //tags:{bson:"_id"}Id
repeated string friendId = 2; repeated string friendId = 2;
} }

10
sys/cache/friend.go vendored
View File

@ -15,15 +15,15 @@ func getRdsUserKey(userId string) string {
} }
type IFriend interface { type IFriend interface {
FriendAdd(data *pb.Cache_FriendData) error FriendAdd(data *pb.Cache_FriendData) (err error)
FriendGetTotal(userId string) int32 FriendGetTotal(userId string) int32
} }
func (this *Cache) FriendAdd(data *pb.Cache_FriendData) error { func (this *Cache) FriendAdd(data *pb.Cache_FriendData) (err error) {
if err := db.Defsys.FriendApply(data); err == nil { if err = db.Defsys.FriendApply(data); err == nil {
return this.redis.ZAdd(fmt.Sprintf(Redis_FriendCache, data.UserId)) err = this.redis.Set(fmt.Sprintf(Redis_FriendCache, data.UserId), data, 0)
} }
return nil return
} }
func (this *Cache) FriendGetTotal(userId string) int32 { func (this *Cache) FriendGetTotal(userId string) int32 {

View File

@ -1,12 +1,24 @@
package cache package cache_test
import ( import (
"go_dreamfactory/pb"
"go_dreamfactory/sys/cache"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestFriendAdd(t *testing.T) {
err := cache.Defsys.FriendAdd(&pb.Cache_FriendData{
UserId: "629f159310d6970846f7ef26",
FriendId: []string{"629f147e3d276120561bfa18", "629eb3f4132dc4bb26139659"},
})
require.Nil(t, err, nil)
}
func TestFriendGetTotal(t *testing.T) { func TestFriendGetTotal(t *testing.T) {
total := cache.FriendGetTotal("629f159310d6970846f7ef26") total := cache.Defsys.FriendGetTotal("629f159310d6970846f7ef26")
assert.Equal(t, total, int32(1)) assert.Equal(t, total, int32(1))
} }

23
sys/cache/init_test.go vendored Normal file
View File

@ -0,0 +1,23 @@
package cache_test
import (
"fmt"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"os"
"testing"
)
func TestMain(m *testing.M) {
if err := db.OnInit(nil, db.Set_MongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), db.Set_MongodbDatabase("dreamfactory")); err != nil {
fmt.Printf("err:%v\n", err)
return
}
if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil {
fmt.Printf("err:%v\n", err)
return
}
defer os.Exit(m.Run())
}

View File

@ -3,19 +3,10 @@ package cache_test
import ( import (
"fmt" "fmt"
"go_dreamfactory/sys/cache" "go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"testing" "testing"
) )
func Test_Pack_AddItemToUserPack(t *testing.T) { func Test_Pack_AddItemToUserPack(t *testing.T) {
if err := db.OnInit(nil, db.Set_MongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), db.Set_MongodbDatabase("dreamfactory")); err != nil {
fmt.Printf("err:%v\n", err)
return
}
if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil {
fmt.Printf("err:%v\n", err)
return
}
err := cache.Defsys.Pack_AddItemToUserPack("liwei1dao", 1001, 100) err := cache.Defsys.Pack_AddItemToUserPack("liwei1dao", 1001, 100)
fmt.Printf("Pack_AddItemToUserPack err:%v\n", err) fmt.Printf("Pack_AddItemToUserPack err:%v\n", err)
} }

View File

@ -1,31 +1,15 @@
package cache package cache_test
import ( import (
"fmt" "fmt"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"log" "go_dreamfactory/sys/cache"
"testing" "testing"
"go_dreamfactory/lego/sys/redis"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
var cache *Cache
func TestMain(m *testing.M) {
iredis, err := redis.NewSys(redis.Redis_Cluster_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}),
redis.SetRedis_Cluster_Password(""), redis.SetRedisType(redis.Redis_Cluster))
if err != nil {
log.Fatal(err)
}
cache = &Cache{
redis: iredis,
}
defer m.Run()
}
func TestUpdateUser(t *testing.T) { func TestUpdateUser(t *testing.T) {
user := &pb.Cache_UserData{ user := &pb.Cache_UserData{
SessionId: "1", SessionId: "1",
@ -35,11 +19,11 @@ func TestUpdateUser(t *testing.T) {
Account: "aaa", Account: "aaa",
}, },
} }
err := cache.Update(user) err := cache.Defsys.Update(user)
require.Nil(t, err) require.Nil(t, err)
} }
func TestGetUser(t *testing.T) { func TestGetUser(t *testing.T) {
c := cache.Get("62157") c := cache.Defsys.Get("62157")
fmt.Println(c) fmt.Println(c)
} }

View File

@ -3,6 +3,9 @@ package db
import ( import (
"go_dreamfactory/lego/core" "go_dreamfactory/lego/core"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
) )
const ( const (
@ -14,6 +17,9 @@ type IFriend interface {
} }
func (this *DB) FriendApply(data *pb.Cache_FriendData) error { func (this *DB) FriendApply(data *pb.Cache_FriendData) error {
_, err := this.mgo.InsertOne(DB_FriendTable, data) err := this.mgo.FindOneAndUpdate(DB_FriendTable,
bson.M{"_id": data.UserId},
bson.M{"$set": bson.M{"friendid": data.FriendId}},
options.FindOneAndUpdate().SetUpsert(true)).Err()
return err return err
} }

22
sys/db/init_test.go Normal file
View File

@ -0,0 +1,22 @@
package db
import (
"go_dreamfactory/lego/sys/mgo"
"log"
"os"
"testing"
)
var db *DB
func TestMain(m *testing.M) {
imgo, err := mgo.NewSys(mgo.SetMongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), mgo.SetMongodbDatabase("dreamfactory"))
if err != nil {
log.Fatal(err)
}
db = &DB{
mgo: imgo,
}
defer os.Exit(m.Run())
}

View File

@ -2,31 +2,13 @@ package db
import ( import (
"go_dreamfactory/pb" "go_dreamfactory/pb"
"log"
"os"
"testing" "testing"
"go_dreamfactory/lego/sys/mgo"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
var db *DB
func TestMain(m *testing.M) {
imgo, err := mgo.NewSys(mgo.SetMongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), mgo.SetMongodbDatabase("dreamfactory"))
if err != nil {
log.Fatal(err)
}
db = &DB{
mgo: imgo,
}
defer os.Exit(m.Run())
}
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
user := &pb.DB_UserData{ user := &pb.DB_UserData{
Account: "legu3", Account: "legu3",