diff options
| -rw-r--r-- | lib/uuidv7/uuid.go | 4 | ||||
| -rw-r--r-- | lib/uuidv7/uuid_test.go | 55 |
2 files changed, 57 insertions, 2 deletions
diff --git a/lib/uuidv7/uuid.go b/lib/uuidv7/uuid.go index 43e293ca..b1a1165a 100644 --- a/lib/uuidv7/uuid.go +++ b/lib/uuidv7/uuid.go @@ -88,7 +88,7 @@ func (id *UUID) Equal(v any) (err error) { if !ok { other, ok := v.(UUID) if !ok { - return fmt.Errorf(`uuidv7: Equal: want type %T, got %T`, &id, v) + return fmt.Errorf(`uuidv7: expecting type *uuidv7.UUID, got %T`, v) } ptr = &other } @@ -173,7 +173,7 @@ func (id *UUID) UnmarshalText(data []byte) (err error) { // Scan scans the raw database value into id. // This method implement [database/sql.Scanner] interface. -// Column with NULL value will returns no error set it to zero UUID. +// Column with NULL value will returns no error and set id to zero. func (id *UUID) Scan(src any) (err error) { switch v := src.(type) { case []byte: diff --git a/lib/uuidv7/uuid_test.go b/lib/uuidv7/uuid_test.go index 126945d3..4972a70c 100644 --- a/lib/uuidv7/uuid_test.go +++ b/lib/uuidv7/uuid_test.go @@ -9,6 +9,42 @@ import ( "git.sr.ht/~shulhan/pakakeh.go/lib/test" ) +func TestUUID_Equal(t *testing.T) { + id := Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`) + + listCase := []struct { + desc string + v any + expError string + }{{ + desc: `EmptyInterface`, + expError: `uuidv7: expecting type *uuidv7.UUID, got <nil>`, + }, { + desc: `NotEqualTime`, + v: Parse(`019CD2F8-1AE4-774E-BFFF-FFFFFFFFFFFF`), + expError: `uuidv7: not equal, want 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF, got 019CD2F8-1AE4-774E-BFFF-FFFFFFFFFFFF`, + }, { + desc: `NotEqualRand`, + v: Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFF0`), + expError: `uuidv7: not equal, want 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF, got 019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFF0`, + }, { + desc: `AsPointerUUID`, + v: new(Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`)), + }, { + desc: `AsUUID`, + v: Parse(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`), + }} + var gotError string + for _, tc := range listCase { + gotError = `` + err := id.Equal(tc.v) + if err != nil { + gotError = err.Error() + } + test.Assert(t, tc.desc, tc.expError, gotError) + } +} + func TestUUID_Scan(t *testing.T) { id := Parse(`019B76DA-A800-7000-8000-00000000001A`) @@ -113,3 +149,22 @@ func TestUUID_UnmarshalText(t *testing.T) { test.Assert(t, tc.desc, tc.exp, id.String()) } } + +func TestUUID_Value(t *testing.T) { + var id UUID + v, err := id.Value() + if err != nil { + t.Fatal(err) + } + test.Assert(t, `ValueZero`, v.(string), `00000000-0000-0000-0000-000000000000`) + + err = id.Scan([]byte(`019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`)) + if err != nil { + t.Fatal(err) + } + v, err = id.Value() + if err != nil { + t.Fatal(err) + } + test.Assert(t, `ValueZero`, v.(string), `019CD2F8-1AE3-774E-BFFF-FFFFFFFFFFFF`) +} |
