Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Tests for uuid.go in utils package #597

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions utils/uuid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utils

import (
"testing"
)

// TestNewUUID tests the NewUUID function to ensure that it returns
// a valid UUID string of the expected length and format
func TestNewUUID(t *testing.T) {
uuidStr, err := NewUUID()
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

if len(uuidStr) != 36 {
t.Fatalf("expected UUID length of 36, got %d", len(uuidStr))
}

expectedFormat := "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
if !matchesFormat(uuidStr, expectedFormat) {
t.Fatalf("UUID does not match expected format, got %v", uuidStr)
}
}

// matchesFormat checks whether the given UUID string
// matches the expected format
func matchesFormat(uuidStr, format string) bool {
if len(uuidStr) != len(format) {
return false
}
for i := range uuidStr {
if format[i] == 'x' && !isHex(uuidStr[i]) {
return false
} else if format[i] == '-' && uuidStr[i] != '-' {
return false
}
}
return true
}

// isHex checks whether the given character is hexadecimal or not
func isHex(char byte) bool {
return (char >= '0' && char <= '9') || (char >= 'a' && char <= 'f')
}
Loading