github.com/tobgu/qframe@v0.4.0/internal/io/sql/column_test.go (about) 1 package sql 2 3 import ( 4 "math" 5 "testing" 6 ) 7 8 func assertEqual(t *testing.T, expected, actual interface{}) { 9 t.Helper() 10 if expected != actual { 11 t.Errorf("%v != %v", expected, actual) 12 } 13 } 14 15 func panicOnErr(err error) { 16 if err != nil { 17 panic(err) 18 } 19 } 20 21 func TestColumn(t *testing.T) { 22 // Column with two NULL values 23 col := &Column{} 24 panicOnErr(col.Scan(0.0)) 25 panicOnErr(col.Scan(nil)) 26 panicOnErr(col.Scan(2.0)) 27 panicOnErr(col.Scan(nil)) 28 data := col.Data().([]float64) 29 assertEqual(t, 4, len(data)) 30 assertEqual(t, data[0], 0.0) 31 assertEqual(t, true, math.IsNaN(data[1])) 32 assertEqual(t, data[2], 2.0) 33 assertEqual(t, true, math.IsNaN(data[3])) 34 35 // Column with NULL values at the head 36 col = &Column{} 37 panicOnErr(col.Scan(nil)) 38 panicOnErr(col.Scan(nil)) 39 panicOnErr(col.Scan(0.0)) 40 panicOnErr(col.Scan(1.0)) 41 data = col.Data().([]float64) 42 assertEqual(t, 4, len(data)) 43 44 // Column with all NULL values 45 col = &Column{} 46 panicOnErr(col.Scan(nil)) 47 panicOnErr(col.Scan(nil)) 48 panicOnErr(col.Scan(nil)) 49 panicOnErr(col.Scan(nil)) 50 assertEqual(t, nil, col.Data()) 51 52 } 53 54 func TestColumnCoercion(t *testing.T) { 55 col := &Column{} 56 col.coerce = Int64ToBool(col) 57 panicOnErr(col.Scan(int64(1))) 58 panicOnErr(col.Scan(int64(0))) 59 panicOnErr(col.Scan(int64(1))) 60 panicOnErr(col.Scan(int64(0))) 61 data := col.Data().([]bool) 62 assertEqual(t, 4, len(data)) 63 assertEqual(t, true, data[0]) 64 assertEqual(t, false, data[1]) 65 assertEqual(t, true, data[2]) 66 assertEqual(t, false, data[3]) 67 } 68 69 func BenchmarkColumn(b *testing.B) { 70 col := &Column{} 71 for n := 0; n < b.N; n++ { 72 _ = col.Scan(1.0) 73 } 74 }