Files
upstream-corteza/server/pkg/id/id_test.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

167 lines
3.4 KiB
Go

package id
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type (
TestRes struct {
Id ID
}
)
func TestNumericIdentifiers(t *testing.T) {
ints := []uint64{
1,
100,
231,
1000,
1234567890,
18446744073709551615, // max uint64
}
for _, i := range ints {
id, err := NumID(i)
require.NoError(t, err)
require.Equal(t, i, id.Number())
require.True(t, id.isNum())
require.False(t, id.isStr())
tmpRes := fmt.Sprintf(`{"Id":"%d"}`, i)
var tr TestRes
err = json.Unmarshal([]byte(tmpRes), &tr)
require.NoError(t, err)
require.Equal(t, i, tr.Id.Number())
require.True(t, tr.Id.isNum())
require.False(t, tr.Id.isStr())
bb, err := json.Marshal(&tr)
require.NoError(t, err)
require.Equal(t, tmpRes, string(bb))
}
}
func TestStringIdentifiers(t *testing.T) {
strs := []string{
"-1",
"asdf",
"019864d2-3d93-7a86-9012-ec15f6358ab0", // uuid v7
"0e04ae61-6887-4dda-bcef-c514240d1cd3", // uuid v4
"b0a372c4-6eb4-11f0-8de9-0242ac120002", // uuid v1
"00000000-0000-0000-0000-000000000000", // nil uuid
}
for _, s := range strs {
id, err := StrID(s)
require.NoError(t, err)
require.Equal(t, s, id.String())
require.False(t, id.isNum())
require.True(t, id.isStr())
tmpRes := fmt.Sprintf(`{"Id":"%s"}`, s)
var tr TestRes
err = json.Unmarshal([]byte(tmpRes), &tr)
require.NoError(t, err)
require.Equal(t, s, tr.Id.String())
require.False(t, tr.Id.isNum())
require.True(t, tr.Id.isStr())
bb, err := json.Marshal(&tr)
require.NoError(t, err)
require.Equal(t, tmpRes, string(bb))
}
}
func TestByteIdentifiers(t *testing.T) {
tcc := []struct {
in []byte
outNum uint64
outStr string
}{
{
in: []byte("123"),
outNum: 123,
outStr: "",
}, {
in: []byte("asdf"),
outNum: 0,
outStr: "asdf",
}, {
in: []byte("019864d2-3d93-7a86-9012-ec15f6358ab0"),
outNum: 0,
outStr: "019864d2-3d93-7a86-9012-ec15f6358ab0",
},
}
for _, tc := range tcc {
t.Run(string(tc.in), func(t *testing.T) {
id, err := ByteID(tc.in)
require.NoError(t, err)
require.Equal(t, tc.outNum, id.Number())
require.Equal(t, tc.outStr, id.String())
})
}
}
func TestInSlice(t *testing.T) {
t.Run("empty", func(t *testing.T) {
require.False(t, InSlice(MustNumID(1)))
})
t.Run("yes", func(t *testing.T) {
require.True(t, InSlice(MustNumID(2), MustNumID(1), MustNumID(2), MustNumID(3)))
})
t.Run("no", func(t *testing.T) {
require.False(t, InSlice(MustNumID(99), MustNumID(1), MustNumID(2), MustNumID(3)))
})
}
func TestRemoveFromSlice(t *testing.T) {
t.Run("empty", func(t *testing.T) {
require.Len(t, RemoveFromSlice(MustNumID(1)), 0)
})
t.Run("found", func(t *testing.T) {
slc := []ID{MustNumID(1), MustNumID(2)}
slc = RemoveFromSlice(MustNumID(1), slc...)
require.Len(t, slc, 1)
require.Equal(t, MustNumID(2), slc[0])
})
t.Run("not found", func(t *testing.T) {
slc := []ID{MustNumID(1), MustNumID(2)}
slc = RemoveFromSlice(MustNumID(999), slc...)
require.Len(t, slc, 2)
})
}
// func TestIdentifierAssignment(t *testing.T) {
// ints := []uint64{
// 1,
// 100,
// 231,
// 1000,
// 1234567890,
// 18446744073709551615, // max uint64
// }
// strs := []string{
// "1",
// "asdf",
// "019864d2-3d93-7a86-9012-ec15f6358ab0", // uuid v7
// "0e04ae61-6887-4dda-bcef-c514240d1cd3", // uuid v4
// "b0a372c4-6eb4-11f0-8de9-0242ac120002", // uuid v1
// "00000000-0000-0000-0000-000000000000", // nil uuid
// }
// }