示例#1
0
func TestList(t *testing.T) {
	//setting database name
	DEM.G_Dn = "mysql"
	//setting database connection
	DEM.G_Dsn = TDbCon
	//default open database function.
	DbCon = DEM.OpenDem
	///
	dbutil.DbExecF(OpenDb(), "../ttable.sql")
	//
	err := ListData()
	if err != nil {
		t.Error(err.Error())
		return
	}
	//
	//mock open error
	DEM.Evb.SetErrs(DEM.OPEN_ERR | DEM.BEGIN_ERR)
	err = ListData()
	if err == nil {
		t.Error("not error")
	}
	//
	//mock  empty data error
	DEM.Evb.SetErrs(DEM.EMPTY_DATA_ERR)
	err = ListData()
	if err == nil {
		t.Error("not error")
	}
	//
	//clear error
	DEM.Evb.SetErrs(0)
	//add matched query error by regex
	DEM.Evb.AddQErr3(".*insert.*ttable.*")
	err = ListData()
	if err == nil {
		t.Error("not error")
	}
	//clear query error
	DEM.Evb.ClsQErr()
	//add matched query error by regex
	DEM.Evb.AddQErr3(".*delete.*ttable.*")
	err = ListData()
	if err == nil {
		t.Error("not error")
	}

	//error count match
	DEM.Evb.AddEC(DEM.OPEN_ERR, 2)
}
示例#2
0
文件: dem_test.go 项目: Centny/DEM
func TestDem(t *testing.T) {
	G_Dn = ""
	G_Dsn = ""
	ShowLog(true)
	db, _ := sql.Open("DEM", test.TDbCon)
	db.Begin()
	NewEvBase("")
	//
	G_Dn = "mysql"
	G_Dsn = TDbCon
	ev := Evb
	db = OpenDem()
	T(db, t)
	db.Close()
	//
	db = OpenDem()
	dbutil.DbExecF(db, "ttable.sql")
	//
	ev.AddQErr3(".*ttable.*")
	T2(db, t)
	ev.ClsQErr()
	//
	ev.AddQErr3(".*t__able.*")
	//
	ev.SetErrs(BEGIN_ERR)
	T2(db, t)
	ev.SetErrs(PREPARE_ERR)
	T2(db, t)
	ev.SetErrs(TX_ROLLBACK_ERR)
	T2(db, t)
	ev.SetErrs(TX_COMMIT_ERR)
	T2(db, t)
	//
	ev.SetErrs(STMT_QUERY_ERR)
	T2(db, t)
	ev.SetErrs(STMT_EXEC_ERR)
	T2(db, t)
	ev.SetErrs(STMT_CLOSE_ERR)
	T2(db, t)
	//
	ev.SetErrs(ROWS_NEXT_ERR)
	T2(db, t)
	ev.SetErrs(ROWS_AFFECTED_ERR)
	T2(db, t)
	ev.SetErrs(ROWS_CLOSE_ERR)
	T2(db, t)
	ev.SetErrs(LAST_INSERT_ID_ERR)
	T2(db, t)
	//
	ev.SetErrs(EMPTY_DATA_ERR)
	T2(db, t)
	//
	ev.SetErrs(CLOSE_ERR)
	db.Close()
	//
	ev.SetErrs(0)
	db.Close()
	//
	ev.SetErrs(OPEN_ERR)
	db = OpenDem()
	db.Begin()
	//
	ev.AddErrs(OPEN_ERR)
	//
	var ee STErr = 100
	fmt.Println(ee.String())
	fmt.Println(OPEN_ERR.String())
	fmt.Println(EMPTY_DATA_ERR.String())
	ev.ResetErr()
}
示例#3
0
文件: dem_test.go 项目: Centny/DEM
func T(db *sql.DB, t *testing.T) {
	fmt.Println("----->")
	err := dbutil.DbExecF(db, "ttable.sql")
	if err != nil {
		t.Error(err.Error())
	}
	fmt.Println("abbbb--->")
	res, err := dbutil.DbQuery(db, "select * from ttable where tid>?", 1)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(res) < 1 {
		t.Error("not data")
		return
	}
	if len(res[0]) < 1 {
		t.Error("data is empty")
		return
	}
	bys, err := json.Marshal(res)
	fmt.Println(string(bys))
	//
	var mres []TSt
	err = dbutil.DbQueryS(db, &mres, "select * from ttable where tid>?", 1)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(mres) < 1 {
		t.Error("not data")
		return
	}
	fmt.Println("...", mres[0].T, util.Timestamp(mres[0].Time), util.Timestamp(time.Now()))
	fmt.Println(mres, mres[0].Add1)
	//
	ivs, err := dbutil.DbQueryInt(db, "select * from ttable where tid")
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(ivs) < 1 {
		t.Error("not data")
		return
	}
	//
	svs, err := dbutil.DbQueryString(db, "select tname from ttable")
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(svs) < 1 {
		t.Error("not data")
		return
	}
	//
	iid, err := dbutil.DbInsert(db, "insert into ttable(tname,titem,tval,status,time) values('name','item','val','N',now())")
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(iid)
	//
	tx, _ := db.Begin()
	iid2, err := dbutil.DbInsert2(tx, "insert into ttable(tname,titem,tval,status,time) values('name','item','val','N',now())")
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(iid2)
	tx.Commit()
	//
	erow, err := dbutil.DbUpdate(db, "delete from ttable where tid=?", iid)
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(erow, "-----")
	//
	tx, _ = db.Begin()
	erow, err = dbutil.DbUpdate2(tx, "delete from ttable where tid=?", iid2)
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(erow, "-----")
	tx.Commit()
	//
	_, err = dbutil.DbQuery(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryInt(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryString(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbInsert(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx, _ = db.Begin()
	_, err = dbutil.DbInsert2(tx, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = dbutil.DbUpdate(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = dbutil.DbUpdate2(tx, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = dbutil.DbQuery(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryInt(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryString(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbInsert(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = dbutil.DbInsert2(tx, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = dbutil.DbUpdate(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = dbutil.DbUpdate2(tx, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	err = dbutil.DbQueryS(nil, nil, "select * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	dbutil.DbQueryInt(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbQueryString(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbInsert(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbUpdate(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbInsert2(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbUpdate2(nil, "select * from ttable where tid>?", 1, 2)
}