|
| 1 | +package sqlite3_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "math" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mutablelogic/go-sqlite/sys/sqlite3" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_Results_001(t *testing.T) { |
| 14 | + db, err := sqlite3.OpenPathEx(":memory:", sqlite3.SQLITE_OPEN_CREATE, "") |
| 15 | + if err != nil { |
| 16 | + t.Fatal(err) |
| 17 | + } |
| 18 | + defer db.Close() |
| 19 | + st, err := db.Prepare("SELECT ?") |
| 20 | + if err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + defer st.Close() |
| 24 | + |
| 25 | + now := time.Now() |
| 26 | + |
| 27 | + var tests = []struct { |
| 28 | + in, out interface{} |
| 29 | + }{ |
| 30 | + {int(1), int64(1)}, |
| 31 | + {int8(2), int64(2)}, |
| 32 | + {int16(3), int64(3)}, |
| 33 | + {int32(4), int64(4)}, |
| 34 | + {int64(5), int64(5)}, |
| 35 | + {uint(6), int64(6)}, |
| 36 | + {uint8(7), int64(7)}, |
| 37 | + {uint16(8), int64(8)}, |
| 38 | + {uint32(9), int64(9)}, |
| 39 | + {uint64(10), int64(10)}, |
| 40 | + {"test", "test"}, |
| 41 | + {false, int64(0)}, |
| 42 | + {true, int64(1)}, |
| 43 | + {float64(math.Pi), float64(math.Pi)}, |
| 44 | + {float32(math.Pi), float64(float32(math.Pi))}, |
| 45 | + {now, now.Format(time.RFC3339)}, |
| 46 | + {time.Time{}, nil}, |
| 47 | + {nil, nil}, |
| 48 | + } |
| 49 | + |
| 50 | + for _, test := range tests { |
| 51 | + r, err := st.Exec(0, test.in) |
| 52 | + if err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + for { |
| 56 | + values, err := r.Next() |
| 57 | + if err == io.EOF { |
| 58 | + break |
| 59 | + } |
| 60 | + if len(values) != 1 { |
| 61 | + t.Error("Data count should be one") |
| 62 | + } |
| 63 | + out := values[0] |
| 64 | + if out != test.out { |
| 65 | + t.Errorf("Expected %v (%T) but got %v (%T) for bind type %T", test.out, test.out, out, out, test.in) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func Test_Results_002(t *testing.T) { |
| 72 | + db, err := sqlite3.OpenPathEx(":memory:", sqlite3.SQLITE_OPEN_CREATE, "") |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + defer db.Close() |
| 77 | + st, err := db.Prepare("SELECT ?") |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + defer st.Close() |
| 82 | + |
| 83 | + now := time.Now() |
| 84 | + |
| 85 | + var tests = []struct { |
| 86 | + in, out interface{} |
| 87 | + }{ |
| 88 | + {int(1), int(1)}, |
| 89 | + {int8(2), int8(2)}, |
| 90 | + {int16(3), int16(3)}, |
| 91 | + {int32(4), int32(4)}, |
| 92 | + {int64(5), int64(5)}, |
| 93 | + {uint(6), uint(6)}, |
| 94 | + {uint8(7), uint8(7)}, |
| 95 | + {uint16(8), uint16(8)}, |
| 96 | + {uint32(9), uint32(9)}, |
| 97 | + {uint64(10), uint64(10)}, |
| 98 | + {"test", "test"}, |
| 99 | + {false, false}, |
| 100 | + {true, true}, |
| 101 | + {float64(math.Pi), float64(math.Pi)}, |
| 102 | + {float32(math.Pi), float32(math.Pi)}, |
| 103 | + {now, now.Truncate(time.Second)}, |
| 104 | + {time.Time{}, time.Time{}}, |
| 105 | + } |
| 106 | + |
| 107 | + for _, test := range tests { |
| 108 | + r, err := st.Exec(0, test.in) |
| 109 | + if err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + for { |
| 113 | + values, err := r.Next(reflect.TypeOf(test.out)) |
| 114 | + if err == io.EOF { |
| 115 | + break |
| 116 | + } |
| 117 | + if len(values) != 1 { |
| 118 | + t.Error("Data count should be one") |
| 119 | + } |
| 120 | + out := values[0] |
| 121 | + if out != test.out { |
| 122 | + t.Errorf("Expected %v (%T) but got %v (%T) for bind type %T", test.out, test.out, out, out, test.in) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments