github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/type_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package dosa 22 23 import ( 24 "testing" 25 26 "github.com/satori/go.uuid" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestNewUUID(t *testing.T) { 31 id := NewUUID() 32 assert.NotNil(t, id) 33 assert.NotEqual(t, string(id), "") 34 } 35 36 func TestUUIDToBytes(t *testing.T) { 37 id := UUID(uuid.NewV4().String()) 38 bs, err := id.Bytes() 39 assert.NoError(t, err) 40 assert.Len(t, bs, 16) 41 42 invalidUUID := UUID("xyz-1827380-kdwi-4829") 43 _, err = invalidUUID.Bytes() 44 assert.Error(t, err) 45 } 46 47 func TestBytesToUUID(t *testing.T) { 48 id := uuid.NewV4() 49 bs := id.Bytes() 50 uid, err := BytesToUUID(bs) 51 assert.NoError(t, err) 52 assert.EqualValues(t, id.String(), uid) 53 54 invalidBs := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 55 _, err = BytesToUUID(invalidBs) 56 assert.Error(t, err) 57 } 58 59 func TestFromString(t *testing.T) { 60 tt := []struct { 61 input string 62 expected Type 63 }{ 64 { 65 input: TUUID.String(), 66 expected: TUUID, 67 }, 68 { 69 input: String.String(), 70 expected: String, 71 }, 72 { 73 input: Int32.String(), 74 expected: Int32, 75 }, 76 { 77 input: Int64.String(), 78 expected: Int64, 79 }, 80 { 81 input: Double.String(), 82 expected: Double, 83 }, 84 { 85 input: Blob.String(), 86 expected: Blob, 87 }, 88 { 89 input: Timestamp.String(), 90 expected: Timestamp, 91 }, 92 { 93 input: Bool.String(), 94 expected: Bool, 95 }, 96 { 97 input: "invalid", 98 expected: Invalid, 99 }, 100 } 101 102 for _, tc := range tt { 103 assert.Equal(t, FromString(tc.input), tc.expected) 104 } 105 }