You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
1.8 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package uid
  15. import (
  16. "testing"
  17. "time"
  18. )
  19. func TestNew(t *testing.T) {
  20. tm := time.Date(2017, 1, 6, 0, 0, 0, 21, time.UTC)
  21. s := NewSpace("prefix", &Options{Time: tm})
  22. got := s.New()
  23. want := "prefix-20170106-21-0000"
  24. if got != want {
  25. t.Errorf("got %q, want %q", got, want)
  26. }
  27. s2 := NewSpace("prefix2", &Options{Sep: '_', Time: tm})
  28. got = s2.New()
  29. want = "prefix2_20170106_21_0000"
  30. if got != want {
  31. t.Errorf("got %q, want %q", got, want)
  32. }
  33. }
  34. func TestTimestamp(t *testing.T) {
  35. s := NewSpace("unique-ID", nil)
  36. startTime := s.Time
  37. uid := s.New()
  38. got, ok := s.Timestamp(uid)
  39. if !ok {
  40. t.Fatal("got ok = false, want true")
  41. }
  42. if !startTime.Equal(got) {
  43. t.Errorf("got %s, want %s", got, startTime)
  44. }
  45. got, ok = s.Timestamp("unique-ID-20160308-123-8")
  46. if !ok {
  47. t.Fatal("got false, want true")
  48. }
  49. if want := time.Date(2016, 3, 8, 0, 0, 0, 123, time.UTC); !want.Equal(got) {
  50. t.Errorf("got %s, want %s", got, want)
  51. }
  52. if _, ok = s.Timestamp("invalid-time-1234"); ok {
  53. t.Error("got true, want false")
  54. }
  55. }
  56. func TestOlder(t *testing.T) {
  57. s := NewSpace("uid", nil)
  58. // A non-matching ID returns false.
  59. id2 := NewSpace("different-prefix", nil).New()
  60. if got, want := s.Older(id2, time.Second), false; got != want {
  61. t.Errorf("got %t, want %t", got, want)
  62. }
  63. }