github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/types/scalar_types_test.go (about) 1 /* 2 * Copyright (C) 2019 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package types 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestTypeForName(t *testing.T) { 27 for name, tid := range typeNameMap { 28 typ, ok := TypeForName(name) 29 require.EqualValues(t, tid, typ, "%s != %s", name, typ.Name()) 30 require.True(t, ok) 31 } 32 typ, ok := TypeForName("--invalid--") 33 require.EqualValues(t, 0, typ) 34 require.False(t, ok) 35 } 36 37 func TestValueForType(t *testing.T) { 38 for name, tid := range typeNameMap { 39 val := ValueForType(tid) 40 require.EqualValues(t, tid, val.Tid, "%s != %s", name, val.Tid.Name()) 41 require.NotNil(t, val.Value) 42 } 43 val := ValueForType(UndefinedID) 44 require.EqualValues(t, 0, val.Tid) 45 require.Nil(t, val.Value) 46 } 47 48 func TestParseTimeWithoutTZ(t *testing.T) { 49 tests := []struct { 50 in string 51 out time.Time 52 }{ 53 {in: "2018-10-28T04:00:10", 54 out: time.Date(2018, 10, 28, 4, 00, 10, 0, time.UTC)}, 55 {in: "2018-05-30T09:30:10.5", 56 out: time.Date(2018, 5, 30, 9, 30, 10, 500000000, time.UTC)}, 57 {in: "2018", 58 out: time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)}, 59 {in: "2018-01", 60 out: time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)}, 61 {in: "2018-01-01", 62 out: time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)}, 63 } 64 for _, tc := range tests { 65 out, err := ParseTime(tc.in) 66 require.NoError(t, err) 67 require.EqualValues(t, tc.out, out) 68 } 69 } 70 71 func TestParseTimeWithTZ(t *testing.T) { 72 var err error 73 74 // Set local time to UTC. 75 time.Local, err = time.LoadLocation("UTC") 76 require.NoError(t, err) 77 78 tests := []struct { 79 in string 80 out time.Time 81 }{ 82 {in: "2018-10-28T04:00:10Z", 83 out: time.Date(2018, 10, 28, 4, 00, 10, 0, time.UTC)}, 84 {in: "2018-10-28T04:00:10-00:00", 85 out: time.Date(2018, 10, 28, 4, 00, 10, 0, time.UTC)}, 86 {in: "2018-05-30T09:30:10.5Z", 87 out: time.Date(2018, 5, 30, 9, 30, 10, 500000000, time.UTC)}, 88 {in: "2018-05-30T09:30:10.5-00:00", 89 out: time.Date(2018, 5, 30, 9, 30, 10, 500000000, time.UTC)}, 90 {in: "2018-05-30T09:30:10-06:00", 91 out: time.Date(2018, 5, 30, 9, 30, 10, 0, time.FixedZone("", -6*60*60))}, 92 {in: "2018-05-28T14:41:57+30:00", 93 out: time.Date(2018, 5, 28, 14, 41, 57, 0, time.FixedZone("", 30*60*60))}, 94 } 95 for _, tc := range tests { 96 out, err := ParseTime(tc.in) 97 require.NoError(t, err) 98 require.EqualValues(t, tc.out, out) 99 } 100 }