1/* 2Copyright 2014 SAP SE 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package driver 18 19import ( 20 "bytes" 21 "math" 22 "testing" 23 "time" 24 25 p "github.com/SAP/go-hdb/internal/protocol" 26) 27 28type testCustomInt int 29 30func assertEqualInt(t *testing.T, dt p.DataType, v interface{}, r int64) { 31 cv, err := convertNamedValue(0, nil, dt, v) 32 if err != nil { 33 t.Fatal(err) 34 } 35 if cv.(int64) != r { 36 t.Fatalf("assert equal int failed %v - %d expected", cv, r) 37 } 38} 39 40func assertEqualIntOutOfRangeError(t *testing.T, dt p.DataType, v interface{}) { 41 _, err := convertNamedValue(0, nil, dt, v) 42 if err != ErrIntegerOutOfRange { 43 t.Fatalf("assert equal out of range error failed %s %v", dt, v) 44 } 45} 46 47func TestConvertInteger(t *testing.T) { 48 49 // integer data types 50 assertEqualInt(t, p.DtTinyint, 42, 42) 51 assertEqualInt(t, p.DtSmallint, 42, 42) 52 assertEqualInt(t, p.DtInteger, 42, 42) 53 assertEqualInt(t, p.DtBigint, 42, 42) 54 55 // custom integer data type 56 assertEqualInt(t, p.DtInteger, testCustomInt(42), 42) 57 58 // integer reference 59 i := 42 60 assertEqualInt(t, p.DtBigint, &i, 42) 61 62 // min max values 63 assertEqualIntOutOfRangeError(t, p.DtTinyint, minTinyint-1) 64 assertEqualIntOutOfRangeError(t, p.DtTinyint, maxTinyint+1) 65 assertEqualIntOutOfRangeError(t, p.DtSmallint, minSmallint-1) 66 assertEqualIntOutOfRangeError(t, p.DtSmallint, maxSmallint+1) 67 assertEqualIntOutOfRangeError(t, p.DtInteger, minInteger-1) 68 assertEqualIntOutOfRangeError(t, p.DtInteger, maxInteger+1) 69 70} 71 72type testCustomFloat float32 73 74func assertEqualFloat(t *testing.T, dt p.DataType, v interface{}, r float64) { 75 cv, err := convertNamedValue(0, nil, dt, v) 76 if err != nil { 77 t.Fatal(err) 78 } 79 if cv.(float64) != r { 80 t.Fatalf("assert equal float failed %v - %f expected", cv, r) 81 } 82} 83 84func assertEqualFloatOutOfRangeError(t *testing.T, dt p.DataType, v interface{}) { 85 _, err := convertNamedValue(0, nil, dt, v) 86 if err != ErrFloatOutOfRange { 87 t.Fatalf("assert equal out of range error failed %s %v", dt, v) 88 } 89} 90 91func TestConvertFloat(t *testing.T) { 92 93 realValue := float32(42.42) 94 doubleValue := float64(42.42) 95 96 // float data types 97 assertEqualFloat(t, p.DtReal, realValue, float64(realValue)) 98 assertEqualFloat(t, p.DtDouble, doubleValue, doubleValue) 99 100 // custom float data type 101 assertEqualFloat(t, p.DtReal, testCustomFloat(realValue), float64(realValue)) 102 103 // float reference 104 assertEqualFloat(t, p.DtReal, &realValue, float64(realValue)) 105 106 // min max values 107 assertEqualFloatOutOfRangeError(t, p.DtReal, math.Nextafter(maxReal, maxDouble)) 108 assertEqualFloatOutOfRangeError(t, p.DtReal, math.Nextafter(maxReal, maxDouble)*-1) 109 110} 111 112type testCustomTime time.Time 113 114func assertEqualTime(t *testing.T, v interface{}, r time.Time) { 115 cv, err := convertNamedValue(0, nil, p.DtTime, v) 116 if err != nil { 117 t.Fatal(err) 118 } 119 if !cv.(time.Time).Equal(r) { 120 t.Fatalf("assert equal time failed %v - %v expected", cv, r) 121 } 122} 123 124func TestConvertTime(t *testing.T) { 125 126 timeValue := time.Now() 127 128 // time data type 129 assertEqualTime(t, timeValue, timeValue) 130 131 // custom time data type 132 assertEqualTime(t, testCustomTime(timeValue), timeValue) 133 134 // time reference 135 assertEqualTime(t, &timeValue, timeValue) 136 137} 138 139type testCustomString string 140 141func assertEqualString(t *testing.T, dt p.DataType, v interface{}, r string) { 142 cv, err := convertNamedValue(0, nil, dt, v) 143 if err != nil { 144 t.Fatal(err) 145 } 146 if cv.(string) != r { 147 t.Fatalf("assert equal string failed %v - %s expected", cv, r) 148 } 149} 150 151func TestConvertString(t *testing.T) { 152 153 stringValue := "Hello World" 154 155 // string data types 156 assertEqualString(t, p.DtString, stringValue, stringValue) 157 158 // custom string data type 159 assertEqualString(t, p.DtString, testCustomString(stringValue), stringValue) 160 161 // string reference 162 assertEqualString(t, p.DtString, &stringValue, stringValue) 163 164} 165 166type testCustomBytes []byte 167 168func assertEqualBytes(t *testing.T, dt p.DataType, v interface{}, r []byte) { 169 cv, err := convertNamedValue(0, nil, dt, v) 170 if err != nil { 171 t.Fatal(err) 172 } 173 if bytes.Compare(cv.([]byte), r) != 0 { 174 t.Fatalf("assert equal bytes failed %v - %v expected", cv, r) 175 } 176} 177 178func TestConvertBytes(t *testing.T) { 179 180 bytesValue := []byte("Hello World") 181 182 // bytes data types 183 assertEqualBytes(t, p.DtString, bytesValue, bytesValue) 184 assertEqualBytes(t, p.DtBytes, bytesValue, bytesValue) 185 186 // custom bytes data type 187 assertEqualBytes(t, p.DtString, testCustomBytes(bytesValue), bytesValue) 188 assertEqualBytes(t, p.DtBytes, testCustomBytes(bytesValue), bytesValue) 189 190 // bytes reference 191 assertEqualBytes(t, p.DtString, &bytesValue, bytesValue) 192 assertEqualBytes(t, p.DtBytes, &bytesValue, bytesValue) 193 194} 195