<commit_before>package hive

import (
	//"fmt"
	"github.com/eaciit/toolkit"
	. "github.com/frezadev/hdc/hive"
	//. "github.com/eaciit/hdc/hive"
	// "reflect"
	"log"
	"os"
	"testing"
)

var h *Hive
var e error

type Sample7 struct {
	Code        string `tag_name:"code"`
	Description string `tag_name:"description"`
	Total_emp   string `tag_name:"total_emp"`
	Salary      string `tag_name:"salary"`
}

func killApp(code int) {
	if h != nil {
		h.Conn.Close()
	}
	os.Exit(code)
}

func fatalCheck(t *testing.T, what string, e error) {
	if e != nil {
		t.Fatalf("%s: %s", what, e.Error())
	}
}

/*func TestHiveConnect(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
}*/

func TestHiveExec(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
	q := "select * from sample_07 limit 5;"

	h.Conn.Open()

	result, e := h.Exec(q)

	if e != nil {
		log.Printf("error: \n%v\n", e)

	} else {
		log.Printf("result: \n%v\n", result)

		for _, res := range result {
			var tmp toolkit.M
			h.ParseOutput(res, &tmp)
			log.Println(tmp)
		}
	}

	h.Conn.Close()
}

/* Populate will exec query and immidiately return the value into object
Populate is suitable for short type query that return limited data,
Exec is suitable for long type query that return massive amount of data and require time to produce it

Ideally Populate should call Exec as well but already have predefined function on it receiving process
*/
/*func TestHivePopulate(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
	q := "select * from sample_07 limit 5;"

	var result []toolkit.M

	h.Conn.Open(nil)

	e := h.Populate(q, &result)
	fatalCheck(t, "Populate", e)

	if len(result) != 5 {
		log.Printf("Error want %d got %d", 5, len(result))
	}

	log.Printf("Result: \n%s", toolkit.JsonString(result))

	h.Conn.Close()
}*/

func TestExecLine(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
	q := "select * from sample_07 limit 5;"
	x := "select * from sample_07 limit 10;"
	var DoSomething = func(res string) (interface{}, error) {
		tmp := Sample7{}
		//h.ParseOutput(res, &tmp)
		log.Println(res)
		return tmp, nil
	}

	var DoElse = func(res string) (interface{}, error) {
		tmp := Sample7{}
		//h.ParseOutput(res, &tmp)
		log.Printf("Else: %v\n", res)
		return tmp, nil
	}

	h.Conn.Open()
	h.Conn.FnReceive = DoSomething
	h.ExecLineX(q)
	h.Conn.FnReceive = DoElse
	h.ExecLineX(x)
	// log.Printf("error: \n%v\n", e)

	h.Conn.Close()
}

//func main() {

/*fmt.Println("---------------------- EXEC LINE ----------------")<commit_msg>testing<commit_after>package hive

import (
	//"fmt"
	"github.com/eaciit/toolkit"
	. "github.com/frezadev/hdc/hive"
	//. "github.com/eaciit/hdc/hive"
	// "reflect"
	"log"
	"os"
	"testing"
)

var h *Hive
var e error

type Sample7 struct {
	Code        string `tag_name:"code"`
	Description string `tag_name:"description"`
	Total_emp   string `tag_name:"total_emp"`
	Salary      string `tag_name:"salary"`
}

func killApp(code int) {
	if h != nil {
		h.Conn.Close()
	}
	os.Exit(code)
}

func fatalCheck(t *testing.T, what string, e error) {
	if e != nil {
		t.Fatalf("%s: %s", what, e.Error())
	}
}

/*func TestHiveConnect(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
}*/

func TestHiveExec(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
	q := "select * from sample_07 limit 5;"

	h.Conn.Open()

	result, e := h.Exec(q)

	if e != nil {
		log.Printf("error: \n%v\n", e)

	} else {
		log.Printf("result: \n%v\n", result)

		for _, res := range result {
			var tmp toolkit.M
			h.ParseOutput(res, &tmp)
			log.Println(tmp)
		}
	}

	h.Conn.Close()
}

/* Populate will exec query and immidiately return the value into object
Populate is suitable for short type query that return limited data,
Exec is suitable for long type query that return massive amount of data and require time to produce it

Ideally Populate should call Exec as well but already have predefined function on it receiving process
*/
/*func TestHivePopulate(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
	q := "select * from sample_07 limit 5;"

	var result []toolkit.M

	h.Conn.Open(nil)

	e := h.Populate(q, &result)
	fatalCheck(t, "Populate", e)

	if len(result) != 5 {
		log.Printf("Error want %d got %d", 5, len(result))
	}

	log.Printf("Result: \n%s", toolkit.JsonString(result))

	h.Conn.Close()
}*/

func TestExecLine(t *testing.T) {
	h = HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
	q := "select * from sample_07 limit 5;"
	x := "select * from sample_07 limit 10;"
	var DoSomething = func(res string) (interface{}, error) {
		tmp := Sample7{}
		//h.ParseOutput(res, &tmp)
		log.Println(res)
		return tmp, nil
	}

	var DoElse = func(res string) (interface{}, error) {
		tmp := Sample7{}
		//h.ParseOutput(res, &tmp)
		log.Printf("Else: %v\n", res)
		return tmp, nil
	}

	h.Conn.FnReceive = DoSomething
	h.Conn.Open()

	h.ExecLineX(q)
	h.ExecLineX(x)
	// log.Printf("error: \n%v\n", e)

	h.Conn.Close()
}

//func main() {

/*fmt.Println("---------------------- EXEC LINE ----------------")
{'fraction_non_alphanumeric': 0.18911119661408235, 'fraction_numerical': 0.030011542901115813, 'mean_word_length': 3.3615771812080535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '463057', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2007, 'n_words': 514}