diff options
author | EuAndreh <eu@euandre.org> | 2024-10-01 09:28:52 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-10-01 09:28:52 -0300 |
commit | ee04b91cd881449fdc215545989e2ddd77282819 (patch) | |
tree | 014469e2b77cd968dde21052574138a6b7ba9236 /tests | |
parent | Remove code for modules, vtables, tracing and ad-hoc C functions (diff) | |
download | golite-ee04b91cd881449fdc215545989e2ddd77282819.tar.gz golite-ee04b91cd881449fdc215545989e2ddd77282819.tar.xz |
Remove unwanted fieatures on code and tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/golite.go | 434 |
1 files changed, 28 insertions, 406 deletions
diff --git a/tests/golite.go b/tests/golite.go index 682cead..ee0f658 100644 --- a/tests/golite.go +++ b/tests/golite.go @@ -14,8 +14,6 @@ import ( "os" "path" "reflect" - "regexp" - "strconv" "strings" "sync" "testing" @@ -3057,121 +3055,6 @@ func TestDateTimeNow(t *testing.T) { } } -func TestFunctionRegistration(t *testing.T) { - addi8_16_32 := func(a int8, b int16) int32 { return int32(a) + int32(b) } - addi64 := func(a, b int64) int64 { return a + b } - addu8_16_32 := func(a uint8, b uint16) uint32 { return uint32(a) + uint32(b) } - addu64 := func(a, b uint64) uint64 { return a + b } - addiu := func(a int, b uint) int64 { return int64(a) + int64(b) } - addf32_64 := func(a float32, b float64) float64 { return float64(a) + b } - not := func(a bool) bool { return !a } - regex := func(re, s string) (bool, error) { - return regexp.MatchString(re, s) - } - generic := func(a any) int64 { - switch a.(type) { - case int64: - return 1 - case float64: - return 2 - case []byte: - return 3 - case string: - return 4 - default: - panic("unreachable") - } - } - variadic := func(a, b int64, c ...int64) int64 { - ret := a + b - for _, d := range c { - ret += d - } - return ret - } - variadicGeneric := func(a ...any) int64 { - return int64(len(a)) - } - - sql.Register("sqlite3_FunctionRegistration", &SQLiteDriver{ - ConnectHook: func(conn *SQLiteConn) error { - if err := conn.RegisterFunc("addi8_16_32", addi8_16_32, true); err != nil { - return err - } - if err := conn.RegisterFunc("addi64", addi64, true); err != nil { - return err - } - if err := conn.RegisterFunc("addu8_16_32", addu8_16_32, true); err != nil { - return err - } - if err := conn.RegisterFunc("addu64", addu64, true); err != nil { - return err - } - if err := conn.RegisterFunc("addiu", addiu, true); err != nil { - return err - } - if err := conn.RegisterFunc("addf32_64", addf32_64, true); err != nil { - return err - } - if err := conn.RegisterFunc("not", not, true); err != nil { - return err - } - if err := conn.RegisterFunc("regex", regex, true); err != nil { - return err - } - if err := conn.RegisterFunc("generic", generic, true); err != nil { - return err - } - if err := conn.RegisterFunc("variadic", variadic, true); err != nil { - return err - } - if err := conn.RegisterFunc("variadicGeneric", variadicGeneric, true); err != nil { - return err - } - return nil - }, - }) - db, err := sql.Open("sqlite3_FunctionRegistration", ":memory:") - if err != nil { - t.Fatal("Failed to open database:", err) - } - defer db.Close() - - ops := []struct { - query string - expected any - }{ - {"SELECT addi8_16_32(1,2)", int32(3)}, - {"SELECT addi64(1,2)", int64(3)}, - {"SELECT addu8_16_32(1,2)", uint32(3)}, - {"SELECT addu64(1,2)", uint64(3)}, - {"SELECT addiu(1,2)", int64(3)}, - {"SELECT addf32_64(1.5,1.5)", float64(3)}, - {"SELECT not(1)", false}, - {"SELECT not(0)", true}, - {`SELECT regex('^foo.*', 'foobar')`, true}, - {`SELECT regex('^foo.*', 'barfoobar')`, false}, - {"SELECT generic(1)", int64(1)}, - {"SELECT generic(1.1)", int64(2)}, - {`SELECT generic(NULL)`, int64(3)}, - {`SELECT generic('foo')`, int64(4)}, - {"SELECT variadic(1,2)", int64(3)}, - {"SELECT variadic(1,2,3,4)", int64(10)}, - {"SELECT variadic(1,1,1,1,1,1,1,1,1,1)", int64(10)}, - {`SELECT variadicGeneric(1,'foo',2.3, NULL)`, int64(4)}, - } - - for _, op := range ops { - ret := reflect.New(reflect.TypeOf(op.expected)) - err = db.QueryRow(op.query).Scan(ret.Interface()) - if err != nil { - t.Errorf("Query %q failed: %s", op.query, err) - } else if !reflect.DeepEqual(ret.Elem().Interface(), op.expected) { - t.Errorf("Query %q returned wrong value: got %v (%T), want %v (%T)", op.query, ret.Elem().Interface(), ret.Elem().Interface(), op.expected, op.expected) - } - } -} - type sumAggregator int64 func (s *sumAggregator) Step(x int64) { @@ -3691,36 +3574,6 @@ func TestNamedParam(t *testing.T) { var customFunctionOnce sync.Once -func BenchmarkCustomFunctions(b *testing.B) { - customFunctionOnce.Do(func() { - customAdd := func(a, b int64) int64 { - return a + b - } - - sql.Register("sqlite3_BenchmarkCustomFunctions", &SQLiteDriver{ - ConnectHook: func(conn *SQLiteConn) error { - // Impure function to force sqlite to reexecute it each time. - return conn.RegisterFunc("custom_add", customAdd, false) - }, - }) - }) - - db, err := sql.Open("sqlite3_BenchmarkCustomFunctions", ":memory:") - if err != nil { - b.Fatal("Failed to open database:", err) - } - defer db.Close() - - b.ResetTimer() - for i := 0; i < b.N; i++ { - var i int64 - err = db.QueryRow("SELECT custom_add(1,2)").Scan(&i) - if err != nil { - b.Fatal("Failed to run custom add:", err) - } - } -} - func TestSuite(t *testing.T) { initializeTestDB(t) defer freeTestDB() @@ -3730,30 +3583,10 @@ func TestSuite(t *testing.T) { } } -func BenchmarkSuite(b *testing.B) { - initializeTestDB(b) - defer freeTestDB() - - for _, benchmark := range benchmarks { - b.Run(benchmark.Name, benchmark.F) - } -} - -// Dialect is a type of dialect of databases. -type Dialect int - -// Dialects for databases. -const ( - SQLITE Dialect = iota // SQLITE mean SQLite3 dialect - POSTGRESQL // POSTGRESQL mean PostgreSQL dialect - MYSQL // MYSQL mean MySQL dialect -) - // DB provide context for the tests type TestDB struct { testing.TB *sql.DB - dialect Dialect once sync.Once tempFilename string } @@ -3768,7 +3601,7 @@ func initializeTestDB(t testing.TB) { t.Fatal(err) } - tdb = &TestDB{t, d, SQLITE, sync.Once{}, tempFilename} + tdb = &TestDB{t, d, sync.Once{}, tempFilename} } func freeTestDB() { @@ -3783,7 +3616,7 @@ func freeTestDB() { } // the following tables will be created and dropped during the test -var testTables = []string{"foo", "bar", "t", "bench"} +var testTables = []string{"foo"} var tests = []testing.InternalTest{ {Name: "TestResult", F: testResult}, @@ -3796,15 +3629,6 @@ var tests = []testing.InternalTest{ {Name: "TestExecEmptyQuery", F: testExecEmptyQuery}, } -var benchmarks = []testing.InternalBenchmark{ - {Name: "BenchmarkExec", F: benchmarkExec}, - {Name: "BenchmarkQuery", F: benchmarkQuery}, - {Name: "BenchmarkParams", F: benchmarkParams}, - {Name: "BenchmarkStmt", F: benchmarkStmt}, - {Name: "BenchmarkRows", F: benchmarkRows}, - {Name: "BenchmarkStmtRows", F: benchmarkStmtRows}, -} - func (db *TestDB) mustExec(sql string, args ...any) sql.Result { res, err := db.Exec(sql, args...) if err != nil { @@ -3815,90 +3639,17 @@ func (db *TestDB) mustExec(sql string, args ...any) sql.Result { func (db *TestDB) tearDown() { for _, tbl := range testTables { - switch db.dialect { - case SQLITE: - db.mustExec("drop table if exists " + tbl) - case MYSQL, POSTGRESQL: - db.mustExec("drop table if exists " + tbl) - default: - db.Fatal("unknown dialect") - } - } -} - -// q replaces ? parameters if needed -func (db *TestDB) q(sql string) string { - switch db.dialect { - case POSTGRESQL: // replace with $1, $2, .. - qrx := regexp.MustCompile(`\?`) - n := 0 - return qrx.ReplaceAllStringFunc(sql, func(string) string { - n++ - return "$" + strconv.Itoa(n) - }) - } - return sql -} - -func (db *TestDB) blobType(size int) string { - switch db.dialect { - case SQLITE: - return fmt.Sprintf("blob[%d]", size) - case POSTGRESQL: - return "bytea" - case MYSQL: - return fmt.Sprintf("VARBINARY(%d)", size) - } - panic("unknown dialect") -} - -func (db *TestDB) serialPK() string { - switch db.dialect { - case SQLITE: - return "integer primary key autoincrement" - case POSTGRESQL: - return "serial primary key" - case MYSQL: - return "integer primary key auto_increment" - } - panic("unknown dialect") -} - -func (db *TestDB) now() string { - switch db.dialect { - case SQLITE: - return "datetime('now')" - case POSTGRESQL: - return "now()" - case MYSQL: - return "now()" - } - panic("unknown dialect") -} - -func makeBench() { - if _, err := tdb.Exec("create table bench (n varchar(32), i integer, d double, s varchar(32), t datetime)"); err != nil { - panic(err) - } - st, err := tdb.Prepare("insert into bench values (?, ?, ?, ?, ?)") - if err != nil { - panic(err) - } - defer st.Close() - for i := 0; i < 100; i++ { - if _, err = st.Exec(nil, i, float64(i), fmt.Sprintf("%d", i), time.Now()); err != nil { - panic(err) - } + db.mustExec("drop table if exists " + tbl) } } // testResult is test for result func testResult(t *testing.T) { tdb.tearDown() - tdb.mustExec("create temporary table test (id " + tdb.serialPK() + ", name varchar(10))") + tdb.mustExec("create temporary table test (id integer primary key autoincrement, name varchar(10))") for i := 1; i < 3; i++ { - r := tdb.mustExec(tdb.q("insert into test (name) values (?)"), fmt.Sprintf("row %d", i)) + r := tdb.mustExec("insert into test (name) values (?)", fmt.Sprintf("row %d", i)) n, err := r.RowsAffected() if err != nil { t.Fatal(err) @@ -3923,13 +3674,13 @@ func testResult(t *testing.T) { func testBlobs(t *testing.T) { tdb.tearDown() var blob = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} - tdb.mustExec("create table foo (id integer primary key, bar " + tdb.blobType(16) + ")") - tdb.mustExec(tdb.q("insert into foo (id, bar) values(?,?)"), 0, blob) + tdb.mustExec("create table foo (id integer primary key, bar blob[16])") + tdb.mustExec("insert into foo (id, bar) values(?,?)", 0, blob) want := fmt.Sprintf("%x", blob) b := make([]byte, 16) - err := tdb.QueryRow(tdb.q("select bar from foo where id = ?"), 0).Scan(&b) + err := tdb.QueryRow("select bar from foo where id = ?", 0).Scan(&b) got := fmt.Sprintf("%x", b) if err != nil { t.Errorf("[]byte scan: %v", err) @@ -3937,7 +3688,7 @@ func testBlobs(t *testing.T) { t.Errorf("for []byte, got %q; want %q", got, want) } - err = tdb.QueryRow(tdb.q("select bar from foo where id = ?"), 0).Scan(&got) + err = tdb.QueryRow("select bar from foo where id = ?", 0).Scan(&got) want = string(blob) if err != nil { t.Errorf("string scan: %v", err) @@ -3948,13 +3699,13 @@ func testBlobs(t *testing.T) { func testMultiBlobs(t *testing.T) { tdb.tearDown() - tdb.mustExec("create table foo (id integer primary key, bar " + tdb.blobType(16) + ")") + tdb.mustExec("create table foo (id integer primary key, bar blob[16])") var blob0 = []byte{0, 1, 2, 3, 4, 5, 6, 7} - tdb.mustExec(tdb.q("insert into foo (id, bar) values(?,?)"), 0, blob0) + tdb.mustExec("insert into foo (id, bar) values(?,?)", 0, blob0) var blob1 = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} - tdb.mustExec(tdb.q("insert into foo (id, bar) values(?,?)"), 1, blob1) + tdb.mustExec("insert into foo (id, bar) values(?,?)", 1, blob1) - r, err := tdb.Query(tdb.q("select bar from foo order by id")) + r, err := tdb.Query("select bar from foo order by id") if err != nil { t.Fatal(err) } @@ -3999,11 +3750,11 @@ func testMultiBlobs(t *testing.T) { // testBlobs tests that we distinguish between null and zero-length blobs func testNullZeroLengthBlobs(t *testing.T) { tdb.tearDown() - tdb.mustExec("create table foo (id integer primary key, bar " + tdb.blobType(16) + ")") - tdb.mustExec(tdb.q("insert into foo (id, bar) values(?,?)"), 0, nil) - tdb.mustExec(tdb.q("insert into foo (id, bar) values(?,?)"), 1, []byte{}) + tdb.mustExec("create table foo (id integer primary key, bar blob[16])") + tdb.mustExec("insert into foo (id, bar) values(?,?)", 0, nil) + tdb.mustExec("insert into foo (id, bar) values(?,?)", 1, []byte{}) - r0 := tdb.QueryRow(tdb.q("select bar from foo where id=0")) + r0 := tdb.QueryRow("select bar from foo where id=0") var b0 []byte err := r0.Scan(&b0) if err != nil { @@ -4013,7 +3764,7 @@ func testNullZeroLengthBlobs(t *testing.T) { t.Errorf("for id=0, got %x; want nil", b0) } - r1 := tdb.QueryRow(tdb.q("select bar from foo where id=1")) + r1 := tdb.QueryRow("select bar from foo where id=1") var b1 []byte err = r1.Scan(&b1) if err != nil { @@ -4034,10 +3785,10 @@ func testManyQueryRow(t *testing.T) { } tdb.tearDown() tdb.mustExec("create table foo (id integer primary key, name varchar(50))") - tdb.mustExec(tdb.q("insert into foo (id, name) values(?,?)"), 1, "bob") + tdb.mustExec("insert into foo (id, name) values(?,?)", 1, "bob") var name string for i := 0; i < 10000; i++ { - err := tdb.QueryRow(tdb.q("select name from foo where id = ?"), 1).Scan(&name) + err := tdb.QueryRow("select name from foo where id = ?", 1).Scan(&name) if err != nil || name != "bob" { t.Fatalf("on query %d: err=%v, name=%q", i, err, name) } @@ -4058,12 +3809,12 @@ func testTxQuery(t *testing.T) { t.Fatal(err) } - _, err = tx.Exec(tdb.q("insert into foo (id, name) values(?,?)"), 1, "bob") + _, err = tx.Exec("insert into foo (id, name) values(?,?)", 1, "bob") if err != nil { t.Fatal(err) } - r, err := tx.Query(tdb.q("select name from foo where id = ?"), 1) + r, err := tx.Query("select name from foo where id = ?", 1) if err != nil { t.Fatal(err) } @@ -4083,7 +3834,6 @@ func testTxQuery(t *testing.T) { } } -// testPreparedStmt is test for prepared statement func testPreparedStmt(t *testing.T) { tdb.tearDown() tdb.mustExec("CREATE TABLE t (count INT)") @@ -4091,7 +3841,7 @@ func testPreparedStmt(t *testing.T) { if err != nil { t.Fatalf("prepare 1: %v", err) } - ins, err := tdb.Prepare(tdb.q("INSERT INTO t (count) VALUES (?)")) + ins, err := tdb.Prepare("INSERT INTO t (count) VALUES (?)") if err != nil { t.Fatalf("prepare 2: %v", err) } @@ -4143,123 +3893,6 @@ func testExecEmptyQuery(t *testing.T) { } } -// Benchmarks need to use panic() since b.Error errors are lost when -// running via testing.Benchmark() I would like to run these via go -// test -bench but calling Benchmark() from a benchmark test -// currently hangs go. - -// benchmarkExec is benchmark for exec -func benchmarkExec(b *testing.B) { - for i := 0; i < b.N; i++ { - if _, err := tdb.Exec("select 1"); err != nil { - panic(err) - } - } -} - -// benchmarkQuery is benchmark for query -func benchmarkQuery(b *testing.B) { - for i := 0; i < b.N; i++ { - var n sql.NullString - var i int - var f float64 - var s string - // var t time.Time - if err := tdb.QueryRow("select null, 1, 1.1, 'foo'").Scan(&n, &i, &f, &s); err != nil { - panic(err) - } - } -} - -// benchmarkParams is benchmark for params -func benchmarkParams(b *testing.B) { - for i := 0; i < b.N; i++ { - var n sql.NullString - var i int - var f float64 - var s string - // var t time.Time - if err := tdb.QueryRow("select ?, ?, ?, ?", nil, 1, 1.1, "foo").Scan(&n, &i, &f, &s); err != nil { - panic(err) - } - } -} - -// benchmarkStmt is benchmark for statement -func benchmarkStmt(b *testing.B) { - st, err := tdb.Prepare("select ?, ?, ?, ?") - if err != nil { - panic(err) - } - defer st.Close() - - for n := 0; n < b.N; n++ { - var n sql.NullString - var i int - var f float64 - var s string - // var t time.Time - if err := st.QueryRow(nil, 1, 1.1, "foo").Scan(&n, &i, &f, &s); err != nil { - panic(err) - } - } -} - -// benchmarkRows is benchmark for rows -func benchmarkRows(b *testing.B) { - tdb.once.Do(makeBench) - - for n := 0; n < b.N; n++ { - var n sql.NullString - var i int - var f float64 - var s string - var t time.Time - r, err := tdb.Query("select * from bench") - if err != nil { - panic(err) - } - for r.Next() { - if err = r.Scan(&n, &i, &f, &s, &t); err != nil { - panic(err) - } - } - if err = r.Err(); err != nil { - panic(err) - } - } -} - -// benchmarkStmtRows is benchmark for statement rows -func benchmarkStmtRows(b *testing.B) { - tdb.once.Do(makeBench) - - st, err := tdb.Prepare("select * from bench") - if err != nil { - panic(err) - } - defer st.Close() - - for n := 0; n < b.N; n++ { - var n sql.NullString - var i int - var f float64 - var s string - var t time.Time - r, err := st.Query() - if err != nil { - panic(err) - } - for r.Next() { - if err = r.Scan(&n, &i, &f, &s, &t); err != nil { - panic(err) - } - } - if err = r.Err(); err != nil { - panic(err) - } - } -} func MainTest() { @@ -4293,11 +3926,7 @@ func MainTest() { { "TestFTS3", TestFTS3 }, { "TestFTS4", TestFTS4 }, { "TestMathFunctions", TestMathFunctions }, - // { "TestPreUpdateHook", TestPreUpdateHook }, { "TestSerializeDeserialize", TestSerializeDeserialize }, - // { "TestUnlockNotify", TestUnlockNotify }, - // { "TestUnlockNotifyMany", TestUnlockNotifyMany }, - // { "TestUnlockNotifyDeadlock", TestUnlockNotifyDeadlock }, { "TestOpen", TestOpen }, { "TestOpenWithVFS", TestOpenWithVFS }, { "TestOpenNoCreate", TestOpenNoCreate }, @@ -4324,7 +3953,6 @@ func MainTest() { { "TestVersion", TestVersion }, { "TestStringContainingZero", TestStringContainingZero }, { "TestDateTimeNow", TestDateTimeNow }, - // { "TestFunctionRegistration", TestFunctionRegistration }, // { "TestAggregatorRegistration", TestAggregatorRegistration }, // { "TestAggregatorRegistration_GenericReturn", TestAggregatorRegistration_GenericReturn }, // { "TestCollationRegistration", TestCollationRegistration }, @@ -4336,20 +3964,14 @@ func MainTest() { { "TestNilAndEmptyBytes", TestNilAndEmptyBytes }, { "TestInsertNilByteSlice", TestInsertNilByteSlice }, { "TestNamedParam", TestNamedParam }, - // { "TestSuite", TestSuite }, // FIXME: too slow - } - - benchmarks := []testing.InternalBenchmark { - /* - { "BenchmarkCustomFunctions", BenchmarkCustomFunctions }, - { "BenchmarkSuite", BenchmarkSuite }, - */ + { "TestSuite", TestSuite }, // FIXME: too slow } - fuzzTargets := []testing.InternalFuzzTarget {} - examples := []testing.InternalExample {} + benchmarks := []testing.InternalBenchmark {} + fuzzTargets := []testing.InternalFuzzTarget{} + examples := []testing.InternalExample {} m := testing.MainStart( - testdeps.TestDeps {}, + testdeps.TestDeps{}, tests, benchmarks, fuzzTargets, |