| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package testutil
- import (
- "context"
- "fmt"
- "math/rand"
- "time"
- "perms-system-server/internal/config"
- "github.com/zeromicro/go-zero/core/stores/cache"
- "github.com/zeromicro/go-zero/core/stores/redis"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- "golang.org/x/crypto/bcrypt"
- )
- var testConfig = config.Config{
- MySQL: struct{ DataSource string }{
- DataSource: "root:NsDmWyM@312@tcp(127.0.0.1:3306)/perms_system?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai",
- },
- CacheRedis: config.CacheRedisConf{
- Nodes: cache.CacheConf{
- {
- RedisConf: redis.RedisConf{
- Host: "127.0.0.1:6379",
- Pass: "NsDmWyM@312",
- Type: "node",
- },
- Weight: 100,
- },
- },
- KeyPrefix: "test_perms",
- },
- Auth: struct {
- AccessSecret string
- AccessExpire int64
- RefreshSecret string
- RefreshExpire int64
- }{
- AccessSecret: "test-access-secret",
- AccessExpire: 7200,
- RefreshSecret: "test-refresh-secret",
- RefreshExpire: 604800,
- },
- }
- func GetTestConfig() config.Config {
- return testConfig
- }
- func GetTestSqlConn() sqlx.SqlConn {
- return sqlx.NewMysql(testConfig.MySQL.DataSource)
- }
- func GetTestCacheConf() cache.CacheConf {
- return testConfig.CacheRedis.Nodes
- }
- func GetTestCachePrefix() string {
- return testConfig.CacheRedis.KeyPrefix
- }
- func UniqueId() string {
- return fmt.Sprintf("t_%d_%d", time.Now().UnixNano(), rand.Intn(100000))
- }
- func HashPassword(pwd string) string {
- h, _ := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.MinCost)
- return string(h)
- }
- func CleanTable(ctx context.Context, conn sqlx.SqlConn, table string, ids ...int64) {
- for _, id := range ids {
- query := fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", table)
- conn.ExecCtx(ctx, query, id)
- }
- }
- func CleanTableByField(ctx context.Context, conn sqlx.SqlConn, table, field string, value interface{}) {
- query := fmt.Sprintf("DELETE FROM %s WHERE `%s` = ?", table, field)
- conn.ExecCtx(ctx, query, value)
- }
|