// package test

// import (
// 	"encoding/json"
// 	"net/http"
// 	"testing"

// 	"gotest.tools/assert"

// 	"github.com/pingcap/enterprise-toolkit/tispector/controller"
// )

// func TestListRule(t *testing.T) {
// 	req, _ := http.NewRequest("POST", "/db-injection/rule/list", nil)
// 	req.Header.Set("token", token)
// 	respWriter := serverRouter(req)
// 	assert.Equal(t, 200, respWriter.Code)

// 	resp := &controller.Resp{}
// 	json.Unmarshal(respWriter.Body.Bytes(), resp)

// 	assert.Equal(t, 0, resp.Code)
// }
package test

import (
	"database/sql"
	"fmt"
	"testing"

	_ "github.com/go-sql-driver/mysql"
	"github.com/pingcap/enterprise-toolkit/tispector/service/checker"
	"github.com/pingcap/enterprise-toolkit/tispector/service/db_info"
	task "github.com/pingcap/enterprise-toolkit/tispector/service/task"
)

func TestListRule(t *testing.T) {
	SQLContent := "select * from table_a"

	cluster := db_info.DbInjectionCluster{
		ID:          0,
		Name:        "dbinjection",
		Description: "1",
		Addr:        "192.168.171.6",
		User:        "root",
		Pwd:         "mysql.1234",
		Ct:          0,
		Ut:          0,
		Operator:    "1",
	}

	var dbName string = "dbinjection"
	defaultDBName := dbName

	db, err := sql.Open("mysql",
		fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", cluster.User, cluster.Pwd, cluster.Addr, dbName))
	if err != nil {
		fmt.Printf("open db conn err: %s", err.Error())
	}

	defaultDB, err := sql.Open("mysql",
		fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", cluster.User, cluster.Pwd, cluster.Addr, defaultDBName))
	if err != nil {
		fmt.Printf("open db conn err: %s", err.Error())
	}

	dbInfo := task.DBInfo{DB: db, DefaultDB: defaultDB, DBName: dbName}
	pass, suggestion, affectRow := checker.Checker.SqlCheck(SQLContent, "", "", &dbInfo)
	if affectRow > 0 {
		fmt.Printf("PASS = %t %s %d", pass, suggestion, affectRow)
	}
	if !pass {
		fmt.Printf("PASS = %t %s %d", pass, suggestion, affectRow)
	}

	dbInfo.CloseConn()

}
