chat_back_go/internal/domain/uuid_test.go

149 lines
3.2 KiB
Go

package domain
import (
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/v2/bson"
)
func TestUUID_IsZero(t *testing.T) {
testTable := []struct {
name string
uuid UUID
expected bool
}{
{
name: "zero",
expected: true,
},
{
name: "non_zero",
uuid: UUID(uuid.NullUUID{Valid: true}),
expected: false,
},
}
for _, tc := range testTable {
t.Run(tc.name, func(t *testing.T) {
res := tc.uuid.IsZero()
assert.Equal(t, tc.expected, res)
})
}
}
func TestUUID_MarshalBSONValue(t *testing.T) {
testTable := []struct {
name string
uuid UUID
expectedUuid []byte
}{
{
name: "zero",
},
{
name: "non_zero",
uuid: UUID(
uuid.NullUUID{
UUID: [16]byte{183, 27, 35, 52, 27, 195, 79, 180, 160, 72, 9, 103, 64, 182, 247, 131},
Valid: true,
},
),
expectedUuid: []byte{37, 0, 0, 0, 98, 55, 49, 98, 50, 51, 51, 52, 45, 49, 98, 99, 51, 45, 52, 102, 98, 52, 45, 97, 48, 52, 56, 45, 48, 57, 54, 55, 52, 48, 98, 54, 102, 55, 56, 51, 0},
},
}
for _, tc := range testTable {
t.Run(tc.name, func(t *testing.T) {
typ, uid, err := tc.uuid.MarshalBSONValue()
assert.Equal(t, byte(bson.TypeString), typ)
assert.Equal(t, tc.expectedUuid, uid)
assert.Equal(t, nil, err)
})
}
}
func TestUUID_UnmarshalBSONValue(t *testing.T) {
testTable := []struct {
name string
data []byte
expectedUuid UUID
expectingErr bool
}{
{
name: "zero",
},
{
name: "valid_not_zero",
data: []byte{37, 0, 0, 0, 98, 55, 49, 98, 50, 51, 51, 52, 45, 49, 98, 99, 51, 45, 52, 102, 98, 52, 45, 97, 48, 52, 56, 45, 48, 57, 54, 55, 52, 48, 98, 54, 102, 55, 56, 51, 0},
expectedUuid: UUID(
uuid.NullUUID{
UUID: [16]byte{183, 27, 35, 52, 27, 195, 79, 180, 160, 72, 9, 103, 64, 182, 247, 131},
Valid: true,
},
),
},
{
name: "parsing_error",
data: []byte{37, 0, 0, 0, 98, 3, 55, 49, 98, 50, 51, 51, 52, 45, 49, 98, 99, 51, 45, 52, 102, 98, 52, 45, 97, 48, 52, 56, 45, 48, 57, 54, 55, 52, 48, 98, 54, 102, 55, 56, 51, 0},
expectingErr: true,
},
}
for _, tc := range testTable {
t.Run(tc.name, func(t *testing.T) {
var uid UUID
err := uid.UnmarshalBSONValue(0, tc.data)
assert.Equal(t, tc.expectedUuid, uid)
assert.Equal(t, err != nil, tc.expectingErr)
})
}
}
func TestUUID_String(t *testing.T) {
testTable := []struct {
name string
uuid UUID
expectedUuid string
}{
{
name: "zero",
expectedUuid: "<nil>",
},
{
name: "non_zero",
uuid: UUID(
uuid.NullUUID{
UUID: [16]byte{183, 27, 35, 52, 27, 195, 79, 180, 160, 72, 9, 103, 64, 182, 247, 131},
Valid: true,
},
),
expectedUuid: "b71b2334-1bc3-4fb4-a048-096740b6f783",
},
}
for _, tc := range testTable {
t.Run(tc.name, func(t *testing.T) {
uid := tc.uuid.String()
assert.Equal(t, tc.expectedUuid, uid)
})
}
}
func Test_GenerateUUID(t *testing.T) {
_ = GenerateUUID()
}
func Test_GenerateTestUUID(t *testing.T) {
uid1 := GenerateTestUUID()
uid2 := GenerateTestUUID()
uid3 := GenerateTestUUID()
assert.Equal(t, uid1, uid2)
assert.Equal(t, uid1, uid3)
}