package dbutil

import (
	"fmt"
	"testing"
	fileutil "tiloader/pkg/fileutil"
	log "tiloader/pkg/logutil"
)

func TestGenSql(t *testing.T) {
	log.InitLog("tiloader.log", "tiloader_err.log") //待config完成后在完善
	rowData := "20210901ZYZYG00000000001ZYKFG1020210901131532JYZHZYG00000001  ZYG      00000001000000000000000100000000022            10000QZYG00000001           156ZYG                              202109011000000020210901                    20210901                    00000                 0000000000                              0000000000000000                                                                  00000000000000000                   20210901                                                                                   00                                                          00000 0000000000000000    00000                       00000000000000000000000000"
	//sqlCh := make(chan string, 10)

	col1Info := fileutil.ColInfo{"col_1", 1, 24}
	col2Info := fileutil.ColInfo{"col_2", 25, 30}
	col3Info := fileutil.ColInfo{"col_3", 31, 31}

	mycols := [...]fileutil.ColInfo{col1Info, col2Info, col3Info}

	descFile := fileutil.DescFile{
		DataFileName: "",
		TableName:    "Test",
		Method:       0,
		Cols:         mycols[:],
	}
	sqlInfo := GenSql(rowData, descFile)
	//close(sqlCh)
	//for sql := range sqlCh {
	fmt.Println(sqlInfo.SqlHalf1)
	fmt.Println(sqlInfo.SqlHalf2)
	//}
	t.Logf("TestGenSql PASS.")
}

func TestGenData(t *testing.T) {
	log.InitLog("tiloader.log", "tiloader_err.log") //待config完成后在完善
	rowData := "20210901ZYZYG00000000001ZYKFG1020210901131532JYZHZYG00000001  ZYG      00000001000000000000000100000000022            10000QZYG00000001           156ZYG                              202109011000000020210901                    20210901                    00000                 0000000000                              0000000000000000                                                                  00000000000000000                   20210901                                                                                   00                                                          00000 0000000000000000    00000                       00000000000000000000000000"

	col1Info := fileutil.ColInfo{"col_1", 1, 24}
	col2Info := fileutil.ColInfo{"col_2", 25, 30}
	col3Info := fileutil.ColInfo{"col_3", 31, 31}

	mycols := [...]fileutil.ColInfo{col1Info, col2Info, col3Info}

	descFile := fileutil.DescFile{
		DataFileName: "",
		TableName:    "Test",
		Method:       0,
		Cols:         mycols[:],
	}

	param := make([]string, len(descFile.Cols))

	GenData(rowData, descFile, param)

	if len(param) != 3 {
		t.Fatalf("test failed, len is error")
	}
	if param[0] != "20210901ZYZYG00000000001" {
		t.Fatalf("test failed, param[0] content error")
	}
	if param[1] != "ZYKFG1" {
		t.Fatalf("test failed, param[1] content error")
	}
	if param[2] != "0" {
		t.Fatalf("test failed, param[2] content error")
	}
	t.Logf("len=%d cap=%d slice=%v\n", len(param), cap(param), param)
}

func TestExecSql(t *testing.T) {
	log.InitLog("tiloader.log", "tiloader.err")
	sql := "insert into test(col_1,col_2,col_3) values (?,?,?), (?,?,?)"

	//读取配置文件
	cfgParser := new(fileutil.ConfigFileParser)
	cfg, err := cfgParser.Parse("../../config/config.toml")
	if err != nil {
		t.Error(err.Error())
		return
	}

	params := [...]string{"aaa", "aa", "a", "bbb", "bb", "b"}

	result, err := Insert(sql, params[:], cfg)
	if err != nil {
		t.Error(err.Error())
	}
	if result != nil {
		t.Log("test ok")
	}
}
