Skip to content

Commit

Permalink
update: [help] Random generation function & unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kainonly committed Sep 23, 2023
1 parent 6602036 commit b131f05
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 66 deletions.
61 changes: 0 additions & 61 deletions help/help.go

This file was deleted.

33 changes: 33 additions & 0 deletions help/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package help

import "math/rand"

func Random(n int, charset ...string) string {
letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
if len(charset) != 0 {
letters = charset[0]
}

b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}

return string(b)
}

func RandomNumber(n int) string {
return Random(n, "0123456789")
}

func RandomLowercase(n int) string {
return Random(n, "abcdefghijklmnopqrstuvwxyz")
}

func RandomUppercase(n int) string {
return Random(n, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
}

func RandomAlphabet(n int) string {
return Random(n, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
}
49 changes: 49 additions & 0 deletions help/random_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package help_test

import (
"github.com/stretchr/testify/assert"
"github.com/weplanx/go/help"
"testing"
)

func TestRandom(t *testing.T) {
v1 := help.Random(16)
assert.Len(t, v1, 16)
t.Log(v1)
v2 := help.Random(32)
assert.Len(t, v2, 32)
t.Log(v2)
v3 := help.Random(8)
assert.Len(t, v3, 8)
t.Log(v3)
v4 := help.Random(32, "0123456789abcdef")
assert.Len(t, v4, 32)
t.Log(v4)
v5 := help.Random(64, "0123456789abcdef")
assert.Len(t, v5, 64)
t.Log(v5)
}

func TestRandomNumber(t *testing.T) {
v := help.RandomNumber(6)
assert.Len(t, v, 6)
t.Log(v)
}

func TestRandomAlphabet(t *testing.T) {
v := help.RandomAlphabet(16)
assert.Len(t, v, 16)
t.Log(v)
}

func TestRandomUppercase(t *testing.T) {
v := help.RandomUppercase(8)
assert.Len(t, v, 8)
t.Log(v)
}

func TestRandomLowercase(t *testing.T) {
v := help.RandomLowercase(8)
assert.Len(t, v, 8)
t.Log(v)
}
19 changes: 19 additions & 0 deletions help/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package help

import (
"fmt"
"github.com/cloudwego/hertz/pkg/app/server/binding"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func RegValidate() {
binding.MustRegValidateFunc("mongoId", func(args ...interface{}) error {
if len(args) != 1 {
return fmt.Errorf("the args must be one")
}
if _, e := primitive.ObjectIDFromHex(args[0].(string)); e != nil {
return e
}
return nil
})
}
20 changes: 18 additions & 2 deletions rest/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,24 @@ func TestMain(m *testing.M) {
help.RegValidate()
engine = route.NewEngine(config.NewOptions([]config.Option{}))
engine.Use(ErrHandler())
help.RestRoutes(engine.Group(""), &rest.Controller{Service: service})

controller := &rest.Controller{Service: service}
r := engine.Group(":collection")
{
r.GET(":id", controller.FindById)
r.POST("create", controller.Create)
r.POST("bulk_create", controller.BulkCreate)
r.POST("size", controller.Size)
r.POST("find", controller.Find)
r.POST("find_one", controller.FindOne)
r.POST("update", controller.Update)
r.POST("bulk_delete", controller.BulkDelete)
r.POST("sort", controller.Sort)
r.PATCH(":id", controller.UpdateById)
r.PUT(":id", controller.Replace)
r.DELETE(":id", controller.Delete)
}
engine.POST("transaction", controller.Transaction)
engine.POST("commit", controller.Commit)
os.Exit(m.Run())
}

Expand Down
8 changes: 7 additions & 1 deletion sessions/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ func TestMain(m *testing.M) {
engine = route.NewEngine(config.NewOptions([]config.Option{}))
engine.Use(ErrHandler())
help.RegValidate()
help.SessionsRoutes(engine.Group(""), &sessions.Controller{Service: service})
controller := &sessions.Controller{Service: service}
r := engine.Group("sessions")
{
r.GET("", controller.Lists)
r.DELETE(":uid", controller.Remove)
r.POST("clear", controller.Clear)
}
os.Exit(m.Run())
}

Expand Down
9 changes: 7 additions & 2 deletions values/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/nats-io/nats.go"
"github.com/nats-io/nkeys"
"github.com/weplanx/go/cipher"
"github.com/weplanx/go/help"
"github.com/weplanx/go/values"
"log"
"net/http"
Expand Down Expand Up @@ -66,7 +65,13 @@ func TestMain(m *testing.M) {
)
engine = route.NewEngine(config.NewOptions([]config.Option{}))
engine.Use(ErrHandler())
help.ValuesRoutes(engine.Group(""), &values.Controller{Service: service})
controller := &values.Controller{Service: service}
r := engine.Group("values")
{
r.GET("", controller.Get)
r.PATCH("", controller.Set)
r.DELETE(":key", controller.Remove)
}
os.Exit(m.Run())
}

Expand Down

0 comments on commit b131f05

Please sign in to comment.