github.com/snowflakedb/gosnowflake@v1.9.0/old_driver_test.go (about) 1 // Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved. 2 3 package gosnowflake 4 5 import ( 6 "bytes" 7 "reflect" 8 "testing" 9 ) 10 11 const ( 12 forceARROW = "ALTER SESSION SET GO_QUERY_RESULT_FORMAT = ARROW" 13 forceJSON = "ALTER SESSION SET GO_QUERY_RESULT_FORMAT = JSON" 14 ) 15 16 func TestJSONInt(t *testing.T) { 17 testInt(t, true) 18 } 19 20 func TestJSONFloat32(t *testing.T) { 21 testFloat32(t, true) 22 } 23 24 func TestJSONFloat64(t *testing.T) { 25 testFloat64(t, true) 26 } 27 28 func TestJSONVariousTypes(t *testing.T) { 29 runDBTest(t, func(dbt *DBTest) { 30 dbt.mustExec(forceJSON) 31 rows := dbt.mustQuery(selectVariousTypes) 32 defer rows.Close() 33 if !rows.Next() { 34 dbt.Error("failed to query") 35 } 36 cc, err := rows.Columns() 37 if err != nil { 38 dbt.Errorf("columns: %v", cc) 39 } 40 ct, err := rows.ColumnTypes() 41 if err != nil { 42 dbt.Errorf("column types: %v", ct) 43 } 44 var v1 float32 45 var v2 int 46 var v3 string 47 var v4 float64 48 var v5 []byte 49 var v6 bool 50 err = rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6) 51 if err != nil { 52 dbt.Errorf("failed to scan: %#v", err) 53 } 54 if v1 != 1.0 { 55 dbt.Errorf("failed to scan. %#v", v1) 56 } 57 if ct[0].Name() != "C1" || ct[1].Name() != "C2" || ct[2].Name() != "C3" || ct[3].Name() != "C4" || ct[4].Name() != "C5" || ct[5].Name() != "C6" { 58 dbt.Errorf("failed to get column names: %#v", ct) 59 } 60 if ct[0].ScanType() != reflect.TypeOf(float64(0)) { 61 dbt.Errorf("failed to get scan type. expected: %v, got: %v", reflect.TypeOf(float64(0)), ct[0].ScanType()) 62 } 63 if ct[1].ScanType() != reflect.TypeOf(int64(0)) { 64 dbt.Errorf("failed to get scan type. expected: %v, got: %v", reflect.TypeOf(int64(0)), ct[1].ScanType()) 65 } 66 var pr, sc int64 67 var cLen int64 68 var canNull bool 69 pr, sc = dbt.mustDecimalSize(ct[0]) 70 if pr != 30 || sc != 2 { 71 dbt.Errorf("failed to get precision and scale. %#v", ct[0]) 72 } 73 dbt.mustFailLength(ct[0]) 74 canNull = dbt.mustNullable(ct[0]) 75 if canNull { 76 dbt.Errorf("failed to get nullable. %#v", ct[0]) 77 } 78 if cLen != 0 { 79 dbt.Errorf("failed to get length. %#v", ct[0]) 80 } 81 if v2 != 2 { 82 dbt.Errorf("failed to scan. %#v", v2) 83 } 84 pr, sc = dbt.mustDecimalSize(ct[1]) 85 if pr != 38 || sc != 0 { 86 dbt.Errorf("failed to get precision and scale. %#v", ct[1]) 87 } 88 dbt.mustFailLength(ct[1]) 89 canNull = dbt.mustNullable(ct[1]) 90 if canNull { 91 dbt.Errorf("failed to get nullable. %#v", ct[1]) 92 } 93 if v3 != "t3" { 94 dbt.Errorf("failed to scan. %#v", v3) 95 } 96 dbt.mustFailDecimalSize(ct[2]) 97 cLen = dbt.mustLength(ct[2]) 98 if cLen != 2 { 99 dbt.Errorf("failed to get length. %#v", ct[2]) 100 } 101 canNull = dbt.mustNullable(ct[2]) 102 if canNull { 103 dbt.Errorf("failed to get nullable. %#v", ct[2]) 104 } 105 if v4 != 4.2 { 106 dbt.Errorf("failed to scan. %#v", v4) 107 } 108 dbt.mustFailDecimalSize(ct[3]) 109 dbt.mustFailLength(ct[3]) 110 canNull = dbt.mustNullable(ct[3]) 111 if canNull { 112 dbt.Errorf("failed to get nullable. %#v", ct[3]) 113 } 114 if !bytes.Equal(v5, []byte{0xab, 0xcd}) { 115 dbt.Errorf("failed to scan. %#v", v5) 116 } 117 dbt.mustFailDecimalSize(ct[4]) 118 cLen = dbt.mustLength(ct[4]) // BINARY 119 if cLen != 8388608 { 120 dbt.Errorf("failed to get length. %#v", ct[4]) 121 } 122 canNull = dbt.mustNullable(ct[4]) 123 if canNull { 124 dbt.Errorf("failed to get nullable. %#v", ct[4]) 125 } 126 if !v6 { 127 dbt.Errorf("failed to scan. %#v", v6) 128 } 129 dbt.mustFailDecimalSize(ct[5]) 130 dbt.mustFailLength(ct[5]) 131 /*canNull = dbt.mustNullable(ct[5]) 132 if canNull { 133 dbt.Errorf("failed to get nullable. %#v", ct[5]) 134 }*/ 135 136 }) 137 } 138 139 func TestJSONString(t *testing.T) { 140 testString(t, true) 141 } 142 143 func TestJSONSimpleDateTimeTimestampFetch(t *testing.T) { 144 testSimpleDateTimeTimestampFetch(t, true) 145 } 146 147 func TestJSONDateTime(t *testing.T) { 148 testDateTime(t, true) 149 } 150 151 func TestJSONTimestampLTZ(t *testing.T) { 152 testTimestampLTZ(t, true) 153 } 154 155 func TestJSONTimestampTZ(t *testing.T) { 156 testTimestampTZ(t, true) 157 } 158 159 func TestJSONNULL(t *testing.T) { 160 testNULL(t, true) 161 } 162 163 func TestJSONVariant(t *testing.T) { 164 testVariant(t, true) 165 } 166 167 func TestJSONArray(t *testing.T) { 168 testArray(t, true) 169 } 170 171 func TestLargeSetJSONResultWithDecoder(t *testing.T) { 172 testLargeSetResult(t, 10000, true) 173 } 174 175 func TestLargeSetResultWithCustomJSONDecoder(t *testing.T) { 176 CustomJSONDecoderEnabled = true 177 // less number of rows to avoid Travis timeout 178 testLargeSetResult(t, 20000, true) 179 } 180 181 func TestBindingJSONInterface(t *testing.T) { 182 runDBTest(t, func(dbt *DBTest) { 183 dbt.mustExec(forceJSON) 184 rows := dbt.mustQuery(selectVariousTypes) 185 defer rows.Close() 186 if !rows.Next() { 187 dbt.Error("failed to query") 188 } 189 var v1, v2, v3, v4, v5, v6 interface{} 190 if err := rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6); err != nil { 191 dbt.Errorf("failed to scan: %#v", err) 192 } 193 if s, ok := v1.(string); !ok || s != "1.00" { 194 dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v1) 195 } 196 if s, ok := v2.(string); !ok || s != "2" { 197 dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v2) 198 } 199 if s, ok := v3.(string); !ok || s != "t3" { 200 dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v3) 201 } 202 if s, ok := v4.(string); !ok || s != "4.2" { 203 dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v4) 204 } 205 }) 206 }