github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/pkg/uuid/uuid_test.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package uuid
    18  
    19  import (
    20  	"github.com/onokonem/sillyQueueServer/timeuuid"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  // this tests that we can get the time out of a time-uuid.
    26  func TestUuidTimeRoundTrip(t *testing.T) {
    27  	tcs := []struct {
    28  		Name      string
    29  		InputTime time.Time
    30  	}{
    31  		{
    32  			Name: "current time",
    33  			// note the 0 at the end: we do not support nanosecond precision (milliseconds are good enough)
    34  			InputTime: time.Date(2024, 1, 1, 1, 1, 1, 0, time.UTC),
    35  		},
    36  		{
    37  			Name:      "future time",
    38  			InputTime: time.Date(2042, 7, 1, 1, 1, 1, 0, time.UTC),
    39  		},
    40  		{
    41  			Name:      "past time",
    42  			InputTime: time.Date(1999, 12, 7, 1, 1, 1, 0, time.UTC),
    43  		},
    44  	}
    45  	for _, tc := range tcs {
    46  		tc := tc
    47  		t.Run(tc.Name, func(t *testing.T) {
    48  			t.Parallel()
    49  
    50  			uuidFromTime := timeuuid.UUIDFromTime(tc.InputTime)
    51  			timestamp := GetTime(&uuidFromTime)
    52  			actualTime := timestamp.AsTime()
    53  
    54  			if actualTime != tc.InputTime {
    55  				t.Fatalf("expected a different time.\nExpected: %v\nGot %v", tc.InputTime, actualTime)
    56  			}
    57  		})
    58  	}
    59  }