Files
smolid/ids_test.go
2026-01-18 13:38:06 -05:00

85 lines
2.0 KiB
Go

package smolid
import (
"testing"
"time"
)
var testTimestamp = time.Date(2025, 2, 1, 0, 0, 0, 0, time.UTC)
func TestIDTimestamp(t *testing.T) {
now = func() time.Time { return testTimestamp }
id := New()
// make sure it has the right timestamp
if !id.Time().Equal(testTimestamp) {
t.Errorf("Invalid time. Expected %v, got %v", testTimestamp, id.Time())
}
// and the right version
if id.Version() != 1 {
t.Errorf("Expected version 1, got %v", id.Version())
}
}
func TestIDFromString(t *testing.T) {
id, err := FromString("ACPJE64AEYEZ6")
if err != nil {
t.Fatal(err)
}
if id.Version() != 1 {
t.Errorf("Expected version 1, got %v", id.Version())
}
id, err = FromString("ccpje64aeyez6")
if err != nil {
t.Fatal(err)
}
if id.Version() != 1 {
t.Errorf("Expected version 1, got %v", id.Version())
}
}
func TestIDFromStringInvalid(t *testing.T) {
_, err := FromString("ACPJE64AEYEZ")
if err == nil {
t.Fatal("Expected error")
}
_, err = FromString("invalid")
if err == nil {
t.Fatal("Expected error")
}
}
func TestNewForType(t *testing.T) {
now = func() time.Time { return testTimestamp }
const (
MyType = iota
MyOtherType
)
id := NewForType(MyType)
// make sure it has the right timestamp still
if !id.Time().Equal(testTimestamp) {
t.Errorf("Invalid time. Expected %v, got %v", testTimestamp, id.Time())
}
// and the right version still
if id.Version() != 1 {
t.Errorf("Expected version 1, got %v", id.Version())
}
// and finally make sure we're able to extract the embedded type id
if id.Type() != MyType {
t.Errorf("Expected type %v, got %v", MyType, id.Type())
}
// Then the same deal for the other type
id = NewForType(MyOtherType)
if !id.Time().Equal(testTimestamp) {
t.Errorf("Invalid time. Expected %v, got %v", testTimestamp, id.Time())
}
if id.Version() != 1 {
t.Errorf("Expected version 1, got %v", id.Version())
}
if id.Type() != MyOtherType {
t.Errorf("Expected type %v, got %v", MyOtherType, id.Type())
}
}