surrealpatch/util/uuid/uuid_test.go

214 lines
4.8 KiB
Go
Raw Normal View History

2016-03-15 12:17:30 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2016-05-17 21:35:36 +00:00
package uuid
2016-03-15 12:17:30 +00:00
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestNewV1(t *testing.T) {
2016-05-17 21:35:36 +00:00
var str *UUID
2016-03-15 12:17:30 +00:00
2016-05-17 21:35:36 +00:00
str = NewV1()
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 1", func() {
So(str.Version(), ShouldEqual, 1)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
})
}
func TestNewV2(t *testing.T) {
2016-05-17 21:35:36 +00:00
var str *UUID
2016-03-15 12:17:30 +00:00
2016-05-17 21:35:36 +00:00
str = NewV2(0)
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 2", func() {
So(str.Version(), ShouldEqual, 2)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
})
}
func TestNewV3(t *testing.T) {
2016-05-17 21:35:36 +00:00
var str *UUID
2016-03-15 12:17:30 +00:00
2016-05-17 21:35:36 +00:00
str = NewV3(NamespaceDNS, "abcum.com")
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 3", func() {
So(str.Version(), ShouldEqual, 3)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
})
2016-05-17 21:35:36 +00:00
str = NewV3(NamespaceURL, "https://abcum.com")
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 3", func() {
So(str.Version(), ShouldEqual, 3)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
})
}
func TestNewV4(t *testing.T) {
2016-05-17 21:35:36 +00:00
var str *UUID
2016-03-15 12:17:30 +00:00
2016-05-17 21:35:36 +00:00
str = NewV4()
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 4", func() {
So(str.Version(), ShouldEqual, 4)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
})
}
func TestNewV5(t *testing.T) {
2016-05-17 21:35:36 +00:00
var str *UUID
2016-03-15 12:17:30 +00:00
2016-05-17 21:35:36 +00:00
str = NewV5(NamespaceDNS, "abcum.com")
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 5", func() {
So(str.Version(), ShouldEqual, 5)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
})
2016-05-17 21:35:36 +00:00
str = NewV5(NamespaceURL, "https://abcum.com")
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 5", func() {
So(str.Version(), ShouldEqual, 5)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
})
}
func TestParsing(t *testing.T) {
2016-05-17 21:35:36 +00:00
var str *UUID
2016-03-15 12:17:30 +00:00
2016-05-17 21:35:36 +00:00
str = GetUUID("thiswill-notbe-parsed-as-successful")
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
Convey("Should be exactly `00000000-0000-0000-0000-000000000000`", func() {
So(str.String(), ShouldEqual, "00000000-0000-0000-0000-000000000000")
})
})
2016-05-17 21:35:36 +00:00
str = GetUUID("1400A118-2749-4605-833C-E7437488BCBF")
2016-03-15 12:17:30 +00:00
Convey(str.String(), t, func() {
Convey("Should be a UUID", func() {
2016-05-17 21:35:36 +00:00
So(str, ShouldHaveSameTypeAs, &UUID{})
2016-03-15 12:17:30 +00:00
})
Convey("Should not be nil", func() {
So(str, ShouldNotBeNil)
})
Convey("Should be version 4", func() {
So(str.Version(), ShouldEqual, 4)
})
Convey("Should be of length 36", func() {
So(str.String(), ShouldHaveLength, 36)
})
Convey("Should be exactly `1400a118-2749-4605-833c-e7437488bcbf`", func() {
So(str.String(), ShouldEqual, "1400a118-2749-4605-833c-e7437488bcbf")
})
})
}