Add several more rust tests for parameter binding

This commit is contained in:
PThorpe92 2025-05-07 22:49:07 -04:00
parent 56f5f47e86
commit 50f2621c12
No known key found for this signature in database
GPG key ID: 66DB3FBACBDD05CC
7 changed files with 288 additions and 111 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"math"
"os"
"testing"
_ "github.com/tursodatabase/limbo"
@ -16,6 +17,7 @@ var (
)
func TestMain(m *testing.M) {
log.SetOutput(os.Stdout)
conn, connErr = sql.Open("sqlite3", ":memory:")
if connErr != nil {
panic(connErr)
@ -609,80 +611,80 @@ func TestJSONFunctions(t *testing.T) {
}
// TODO: make these pass, this is a separate issue
// func TestParameterOrdering(t *testing.T) {
// newConn, err := sql.Open("sqlite3", ":memory:")
// if err != nil {
// t.Fatalf("Error opening new connection: %v", err)
// }
// sql := "CREATE TABLE test (a,b,c);"
// newConn.Exec(sql)
//
// // Test inserting with parameters in a different order than
// // the table definition.
// sql = "INSERT INTO test (b, c ,a) VALUES (?, ?, ?);"
// expectedValues := []int{1, 2, 3}
// stmt, err := newConn.Prepare(sql)
// _, err = stmt.Exec(expectedValues[1], expectedValues[2], expectedValues[0])
// if err != nil {
// t.Fatalf("Error preparing statement: %v", err)
// }
// // check that the values are in the correct order
// query := "SELECT a,b,c FROM test;"
// rows, err := newConn.Query(query)
// if err != nil {
// t.Fatalf("Error executing query: %v", err)
// }
// for rows.Next() {
// var a, b, c int
// err := rows.Scan(&a, &b, &c)
// if err != nil {
// t.Fatal("Error scanning row: ", err)
// }
// result := []int{a, b, c}
// for i := range 3 {
// if result[i] != expectedValues[i] {
// fmt.Printf("RESULTS: %d, %d, %d\n", a, b, c)
// fmt.Printf("EXPECTED: %d, %d, %d\n", expectedValues[0], expectedValues[1], expectedValues[2])
// }
// }
// }
//
// -- part 2 --
// mixed parameters and regular values
// sql2 := "CREATE TABLE test2 (a,b,c);"
// newConn.Exec(sql2)
// expectedValues2 := []int{1, 2, 3}
//
// // Test inserting with parameters in a different order than
// // the table definition, with a mixed regular parameter included
// sql2 = "INSERT INTO test2 (a, b ,c) VALUES (1, ?, ?);"
// _, err = newConn.Exec(sql2, expectedValues2[1], expectedValues2[2])
// if err != nil {
// t.Fatalf("Error preparing statement: %v", err)
// }
// // check that the values are in the correct order
// query2 := "SELECT a,b,c FROM test2;"
// rows2, err := newConn.Query(query2)
// if err != nil {
// t.Fatalf("Error executing query: %v", err)
// }
// for rows2.Next() {
// var a, b, c int
// err := rows2.Scan(&a, &b, &c)
// if err != nil {
// t.Fatal("Error scanning row: ", err)
// }
// // result := []int{a, b, c}
//
// fmt.Printf("RESULTS: %d, %d, %d\n", a, b, c)
// fmt.Printf("EXPECTED: %d, %d, %d\n", expectedValues[0], expectedValues[1], expectedValues[2])
// for i := range 3 {
// if result[i] != expectedValues[i] {
// t.Fatalf("Expected %d, got %d", expectedValues[i], result[i])
// }
//}
//}
//}
func TestParameterOrdering(t *testing.T) {
newConn, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Error opening new connection: %v", err)
}
sql := "CREATE TABLE test (a,b,c);"
newConn.Exec(sql)
// Test inserting with parameters in a different order than
// the table definition.
sql = "INSERT INTO test (b, c ,a) VALUES (?, ?, ?);"
expectedValues := []int{1, 2, 3}
stmt, err := newConn.Prepare(sql)
_, err = stmt.Exec(expectedValues[1], expectedValues[2], expectedValues[0])
if err != nil {
t.Fatalf("Error preparing statement: %v", err)
}
// check that the values are in the correct order
query := "SELECT a,b,c FROM test;"
rows, err := newConn.Query(query)
if err != nil {
t.Fatalf("Error executing query: %v", err)
}
for rows.Next() {
var a, b, c int
err := rows.Scan(&a, &b, &c)
if err != nil {
t.Fatal("Error scanning row: ", err)
}
result := []int{a, b, c}
for i := range 3 {
if result[i] != expectedValues[i] {
fmt.Printf("RESULTS: %d, %d, %d\n", a, b, c)
fmt.Printf("EXPECTED: %d, %d, %d\n", expectedValues[0], expectedValues[1], expectedValues[2])
}
}
}
// -- part 2 --
// mixed parameters and regular values
sql2 := "CREATE TABLE test2 (a,b,c);"
newConn.Exec(sql2)
expectedValues2 := []int{1, 2, 3}
// Test inserting with parameters in a different order than
// the table definition, with a mixed regular parameter included
sql2 = "INSERT INTO test2 (a, b ,c) VALUES (1, ?, ?);"
_, err = newConn.Exec(sql2, expectedValues2[1], expectedValues2[2])
if err != nil {
t.Fatalf("Error preparing statement: %v", err)
}
// check that the values are in the correct order
query2 := "SELECT a,b,c FROM test2;"
rows2, err := newConn.Query(query2)
if err != nil {
t.Fatalf("Error executing query: %v", err)
}
for rows2.Next() {
var a, b, c int
err := rows2.Scan(&a, &b, &c)
if err != nil {
t.Fatal("Error scanning row: ", err)
}
result := []int{a, b, c}
fmt.Printf("RESULTS: %d, %d, %d\n", a, b, c)
fmt.Printf("EXPECTED: %d, %d, %d\n", expectedValues[0], expectedValues[1], expectedValues[2])
for i := range 3 {
if result[i] != expectedValues[i] {
t.Fatalf("Expected %d, got %d", expectedValues[i], result[i])
}
}
}
}
// TODO: make this pass
// func TestUpdateParameters(t *testing.T) {