package dbsync

import (
	"context"
	"path/filepath"
	"testing"
)

func TestInspectAndPreviewSQLite(t *testing.T) {
	dir := t.TempDir()
	dsn := "file:" + filepath.ToSlash(filepath.Join(dir, "inspect.db"))
	db, err := Open(DriverSQLite, dsn)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := db.Exec(`CREATE TABLE orders (id TEXT PRIMARY KEY, title TEXT); INSERT INTO orders VALUES ('a','one'),('b','two')`); err != nil {
		_ = db.Close()
		t.Fatal(err)
	}
	_ = db.Close()

	ep := Endpoint{Driver: DriverSQLite, DSN: dsn}
	ins, err := InspectEndpoint(context.Background(), ep, "remote", false)
	if err != nil || !ins.OK {
		t.Fatalf("inspect: %+v %v", ins, err)
	}
	if len(ins.Tables) != 1 || ins.Tables[0].Name != "orders" || ins.Tables[0].RowCount != 2 || ins.Tables[0].ColumnCount != 2 {
		t.Fatalf("tables=%+v", ins.Tables)
	}
	prev, err := PreviewTable(context.Background(), ep, "orders", 10)
	if err != nil || !prev.OK || prev.Total != 2 || len(prev.Rows) != 2 {
		t.Fatalf("preview: %+v %v", prev, err)
	}
}
