Skip to content

Commit

Permalink
refactor(ident): revise test functions and add mock identifier
Browse files Browse the repository at this point in the history
Refactor the `ident` package test code to utilize a mock identifier implementation,
which simplifies the testing of generation, validation, and size determination
of identifiers. Removed redundant logging statements from the test functions
and updated the tests to reflect the new mock-based approach.

BREAKING CHANGE: Test functions now rely on a mock identifier that expects
specific behavior. This may affect the way tests are set up and executed,requiring updates to the test suite to accommodate the changes.
  • Loading branch information
godcong committed Jul 10, 2024
1 parent 139139c commit f6f9daf
Showing 1 changed file with 49 additions and 15 deletions.
64 changes: 49 additions & 15 deletions ident/ident_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
// Copyright (c) 2024 OrigAdmin. All rights reserved.

// Package ident provides the helpers functions.
package ident
package ident_test

import (
"strings"
"testing"

"github.com/origadmin/toolkits/ident"
)

func TestNewXID(t *testing.T) {
t.Logf("xid: %s", strings.ToUpper(NewXID()))
t.Logf("xid string size: %d", len(NewXID()))
type mockIdentifier struct{}

func (m *mockIdentifier) Name() string {
return "MockIdentifier"
}

func (m *mockIdentifier) Gen() string {
return "mock-generated-id"
}

func (m *mockIdentifier) Validate(id string) bool {
return id == "mock-generated-id"
}

func TestMustNewUUID(t *testing.T) {
t.Logf("uuid: %s", strings.ToUpper(MustNewUUID()))
t.Logf("uuid string size: %d", len(MustNewUUID()))
func (m *mockIdentifier) Size() int {
return 12 // Replace with the actual size of the generated identifier
}

func TestMustNewULID(t *testing.T) {
t.Logf("ulid: %s", strings.ToUpper(MustNewULID()))
t.Logf("ulid string size: %d", len(MustNewULID()))
MustNewULID()
func TestGenID(t *testing.T) {
ident.Use(&mockIdentifier{})
generatedID := ident.GenID()
expectedID := "mock-generated-id"

if generatedID != expectedID {
t.Errorf("GenID() = %v, want %v", generatedID, expectedID)
}
}

func TestGenSize(t *testing.T) {
ident.Use(&mockIdentifier{})
size := ident.GenSize()
expectedSize := 12 // Replace with the actual size of the generated identifier

if size != expectedSize {
t.Errorf("GenSize() = %v, want %v", size, expectedSize)
}
}

func TestValidate(t *testing.T) {
ident.Use(&mockIdentifier{})
validID := "mock-generated-id"
invalidID := "invalid-id"

if !ident.Validate(validID) {
t.Errorf("Validate(%v) = %v, want %v", validID, false, true)
}

if ident.Validate(invalidID) {
t.Errorf("Validate(%v) = %v, want %v", invalidID, true, false)
}
}

0 comments on commit f6f9daf

Please sign in to comment.