package core

import (
	"errors"
	"testing"

	"github.com/linlexing/dbx/ddb"

	"os"

	"dbweb/lib/bill"

	_ "github.com/linlexing/dbx/oracle"
	_ "github.com/linlexing/dbx/sqlite"
	_ "github.com/mattn/go-oci8"
	_ "github.com/mattn/go-sqlite3"
)

func TestSqlite(t *testing.T) {
	fileName := "e:\\temp\\test.sq3"
	dumpDB, err := ddb.Openx("sqlite3", fileName)
	if err != nil {
		t.Error(err)
	}
	defer dumpDB.Close()
	defer os.Remove(fileName)
	if _, err = dumpDB.Exec("create table a(a text)"); err != nil {
		t.Error(err)
	}
}
func TestOracleOpen(t *testing.T) {
	billDB, err := ddb.Openx("oci8", "tjbm/llx123@localhost:1521/orcl")
	if err != nil {
		t.Error(err)
	}
	if billDB == nil {
		t.Error("billDB nil")
	}
	var iCount int64
	if err = billDB.QueryRow("select 1 from dual").Scan(&iCount); err != nil {
		t.Error(err)
	}
	if iCount != 1 {
		t.Error("not equ 1")
	}
	defer billDB.Close()
}
func TestOpenBill(t *testing.T) {
	billDB, err := ddb.Openx("oci8", "tjbm/llx123@localhost:1521/orcl")
	if err != nil {
		t.Error(err)
	}
	if billDB == nil {
		t.Error("billDB nil")
	}
	var iCount int64
	if err = billDB.QueryRow("select 1 from dual").Scan(&iCount); err != nil {
		t.Error(err)
	}
	if iCount != 1 {
		t.Error("not equ 1")
	}
	defer billDB.Close()
	_, err = bill.OpenBill(billDB, "dept")
	if err != nil {
		t.Error(err)
	}
}
func TestDumpBill(t *testing.T) {
	billDB := InitMetaDB("oci8", "tjbm/llx123@localhost:1521/orcl")
	fileName := "e:\\temp\\test.sq3"
	if billDB == nil {
		t.Error(errors.New("billDB nil"))
	}
	var iCount int64
	if err := billDB.QueryRow("select 1 from dual").Scan(&iCount); err != nil {
		t.Error(err)
	}
	if iCount != 1 {
		t.Error(errors.New("not equ 1"))
	}
	defer billDB.Close()
	dumpDB, err := ddb.Openx("sqlite3", fileName)
	if err != nil {
		t.Error(err)
	}
	if dumpDB == nil {
		t.Error(errors.New("dumpDB nil"))
	}

	defer func() {
		if err = dumpDB.Close(); err != nil {
			LOG.Panic(err)
		}
		if err = os.Remove(fileName); err != nil {
			LOG.Panic(err)
		}
	}()
	user := NewUser(billDB, "sys")

	if err := DumpBillTest(billDB, dumpDB, user); err != nil {
		t.Error(err)
	}
}
