util/id: move tests to micro-tests repo
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
45252fe4a6
commit
e10f8c0fa0
1
go.mod
1
go.mod
@ -8,7 +8,6 @@ require (
|
||||
github.com/imdario/mergo v0.3.12
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34
|
||||
github.com/stretchr/testify v1.7.0
|
||||
github.com/unistack-org/micro-proto v0.0.9
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed
|
||||
)
|
||||
|
10
go.sum
10
go.sum
@ -1,5 +1,3 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/ef-ds/deque v1.0.4 h1:iFAZNmveMT9WERAkqLJ+oaABF9AcVQ5AjXem/hroniI=
|
||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
||||
github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5/o=
|
||||
@ -11,13 +9,8 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34 h1:vBfVmA5mZhsQa2jr1FOL9nfA37N/jnbBmi5XUfviVTI=
|
||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
|
||||
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
|
||||
@ -32,9 +25,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -1,41 +0,0 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHasNoCollisions(t *testing.T) {
|
||||
tries := 100_000
|
||||
used := make(map[string]bool, tries)
|
||||
for i := 0; i < tries; i++ {
|
||||
id := Must()
|
||||
require.False(t, used[id], "shouldn't return colliding IDs")
|
||||
used[id] = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlatDistribution(t *testing.T) {
|
||||
tries := 100_000
|
||||
alphabet := "abcdefghij"
|
||||
size := 10
|
||||
chars := make(map[rune]int)
|
||||
for i := 0; i < tries; i++ {
|
||||
id := Must(Alphabet(alphabet), Size(size))
|
||||
for _, r := range id {
|
||||
chars[r]++
|
||||
}
|
||||
}
|
||||
|
||||
for _, count := range chars {
|
||||
require.InEpsilon(t, size*tries/len(alphabet), count, .01, "should have flat distribution")
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark id generator
|
||||
func BenchmarkNanoid(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, _ = New()
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
package id_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
id "github.com/unistack-org/micro/v3/util/id"
|
||||
)
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
t.Run("short alphabet", func(t *testing.T) {
|
||||
alphabet := ""
|
||||
_, err := id.New(id.Alphabet(alphabet), id.Size(32))
|
||||
assert.Error(t, err, "should return error if the alphabet is too small")
|
||||
})
|
||||
|
||||
t.Run("long alphabet", func(t *testing.T) {
|
||||
alphabet := strings.Repeat("a", 256)
|
||||
_, err := id.New(id.Alphabet(alphabet), id.Size(32))
|
||||
assert.Error(t, err, "should return error if the alphabet is too long")
|
||||
})
|
||||
|
||||
t.Run("negative ID length", func(t *testing.T) {
|
||||
_, err := id.New(id.Alphabet("abcdef"), id.Size(-1))
|
||||
assert.Error(t, err, "should return error if the requested ID length is invalid")
|
||||
})
|
||||
|
||||
t.Run("happy path", func(t *testing.T) {
|
||||
alphabet := "abcdef"
|
||||
id, err := id.New(id.Alphabet(alphabet), id.Size(6))
|
||||
assert.NoError(t, err, "shouldn't return error")
|
||||
assert.Len(t, id, 6, "should return ID of requested length")
|
||||
for _, r := range id {
|
||||
assert.True(t, strings.ContainsRune(alphabet, r), "should use given alphabet")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("works with unicode", func(t *testing.T) {
|
||||
alphabet := "🚀💩🦄🤖"
|
||||
id, err := id.New(id.Alphabet(alphabet), id.Size(6))
|
||||
assert.NoError(t, err, "shouldn't return error")
|
||||
assert.Equal(t, utf8.RuneCountInString(id), 6, "should return ID of requested length")
|
||||
for _, r := range id {
|
||||
assert.True(t, strings.ContainsRune(alphabet, r), "should use given alphabet")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Run("negative ID length", func(t *testing.T) {
|
||||
_, err := id.New(id.Size(-1))
|
||||
assert.Error(t, err, "should return error if the requested ID length is invalid")
|
||||
})
|
||||
|
||||
t.Run("happy path", func(t *testing.T) {
|
||||
nid, err := id.New()
|
||||
assert.NoError(t, err, "shouldn't return error")
|
||||
assert.Len(t, nid, id.DefaultSize, "should return ID of default length")
|
||||
})
|
||||
|
||||
t.Run("custom length", func(t *testing.T) {
|
||||
id, err := id.New(id.Size(6))
|
||||
assert.NoError(t, err, "shouldn't return error")
|
||||
assert.Len(t, id, 6, "should return ID of requested length")
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user