Testing Multiple Functions

I have created 10 test files and a main file to call the files But it was not calling the files in order Like…

  1. Drop table.
  2. Insert table
  3. Create table

It runs in the above seq but it should not, If it runs in that sequence it will produce the error that table is not created.

How to make it i run in the order 3,2,1.

Thanks.

You can not call “files” but only functions and methods. And usually they should run in exactly the order you specify.

Perhaps you are able to show us some code that reproduces the behaviour you are observing?

Main Code:- which calls test functions in different files.

package main

import (
    "fmt"
	a "github.com/ibmdb/go_ibm_db"
	"database/sql"
	)
	
var con ="HOSTNAME=host;PORT=number;DATABASE=name;UID=username;PWD=password"
	
func Createconnection()(db *sql.DB){
    db,_=sql.Open("go_ibm_db",con)
    return db
	}


func Createtable( ) error{
    db, err:=sql.Open("go_ibm_db", con)
    db.Exec("DROP table rocket")
    _,err=db.Exec("create table rocket(a int)")
	_,err=db.Exec("create table rocket1(a int)")
    if err != nil{
        return err
    }
    return nil
}
func Insert() error{
    db,err:=sql.Open("go_ibm_db",con)
    _,err =db.Exec("insert into rocket values(1)")
	if err != nil{
        return err
    }
    return nil
}

func Drop() error{
    db,err:=sql.Open("go_ibm_db",con)
    _,err =db.Exec("drop table rocket1")
	if err != nil{
        return err
    }
    return nil
}

func Prepare() error{
db,_:=sql.Open("go_ibm_db",con)
_,err:=db.Prepare("select * from rocket")
if err!=nil{
return err
}
return nil
}

func Query() error{
db,_:=sql.Open("go_ibm_db",con)
st,_:=db.Prepare("select * from rocket")
_,err:=st.Query()
if err != nil{
return err
} 
return nil
}

func Scan() error{
db,_:=sql.Open("go_ibm_db",con)
st,_:=db.Prepare("select * from rocket")
rows,err:=st.Query()
for rows.Next(){
var a string
err = rows.Scan(&a)
if err != nil{
return err
}
}
return nil
}

func Next() error{
db,_:=sql.Open("go_ibm_db",con)
st,_:=db.Prepare("select * from rocket")
rows,err:=st.Query()
for rows.Next(){
var a string
err = rows.Scan(&a)
if err != nil{
return err
}
}
return nil
}

func Columns() error{
db,_:=sql.Open("go_ibm_db",con)
st,_:=db.Prepare("select * from rocket")
rows,_:=st.Query()
_,err := rows.Columns()
if err != nil{
return err
}
for rows.Next(){
var a string
_ = rows.Scan(&a)
}
return nil
}



func Queryrow() error{
a:=1
var uname int
db,err:=sql.Open("go_ibm_db",con)
err=db.QueryRow("select a from rocket where a=?",a).Scan(&uname)
if err != nil{
return err
}
return nil
}

func Begin() error{
db,err:=sql.Open("go_ibm_db",con)
_,err=db.Begin()
if err != nil{
return err
}
return nil
}

func Commit() error{
    db,err:=sql.Open("go_ibm_db",con)
    bg,err:=db.Begin()
	db.Exec("DROP table u")
	_,err=bg.Exec("create table u(id int)")
	err=bg.Commit()
	if err!=nil{
	return err
	}
	return nil
}

func Close()(error){
    db,_:=sql.Open("go_ibm_db",con)
    err:=db.Close()
	if err!=nil{
	return err
	}
	return nil
	}

func PoolOpen() (int){
    pool:=a.Pconnect("PoolSize=50")
    db:=pool.Open(con)
    if(db == nil){
        return 0
    }else {
        return 1
    } 
}
	
func main(){
result:=Createconnection()
if(result != nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}

result1:=Createtable()
if(result1 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}

result2:=Insert()
if(result2 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result3:=Drop()
if(result3 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result4:=Prepare()
if(result4 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result5:=Query()
if(result5 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}

result6:=Scan()
if(result6 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result7:=Next()
if(result7 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}

result8:=Columns()
if(result8 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}

result9:=Queryrow()
if(result9 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result10:=Begin()
if(result10== nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result11:=Commit()
if(result11 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result12:=Close()
if(result12 == nil){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
result13:=PoolOpen()
if(result13 ==1){
fmt.Println("Pass")
} else {
fmt.Println("fail")
}
}

Order of files in the picture:-

Can you provide some code that reproduces the behaviour without requiring us to have a IBM-DB set up and running?

From a first glance it looks as if the main function does run stuff in proper order.

Also could you please elaborate how the screenshot is releated?

@NobbZ

CODE WITHOUT DB:-

package main

import (
    "fmt"
	)
	
func Createconnection()(int){
	fmt.Println("this is Createconnection")
    return 1
	}


func Createtable( ) error{
	fmt.Println("this is Createtable")
	return nil

}
func Insert() error{
	fmt.Println("this is Insert")
	return nil
}

func Drop() error{
	fmt.Println("this is Drop")
	return nil
}

func Prepare() error{
	fmt.Println("this is Prepare")
	return nil
}

func Query() error{
	fmt.Println("this is Query")
	return nil
}

func Scan() error{
	fmt.Println("this is Scan")
	return nil
}

func Next() error{
	fmt.Println("this is Next")
	return nil
}

func Columns() error{
	fmt.Println("this is Columns")
	return nil
}



func Queryrow() error{
	fmt.Println("this is Queryrow")
	return nil
}

func Begin() error{
	fmt.Println("this is Begin")
	return nil
}

func Commit() error{
	fmt.Println("this is Commit")
	return nil
}

func Close()(error){
	fmt.Println("this is Close")
	return nil
}

func PoolOpen() (int){
	fmt.Println("this is PoolOpen")
	return 1
}
	
func main(){

	result:=Createconnection()
	if(result != 1){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}

	result1:=Createtable()
	if(result1 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}

	result2:=Insert()
	if(result2 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result3:=Drop()
	if(result3 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result4:=Prepare()
	if(result4 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result5:=Query()
	if(result5 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}

	result6:=Scan()
	if(result6 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result7:=Next()
	if(result7 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}

	result8:=Columns()
	if(result8 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}

	result9:=Queryrow()
	if(result9 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result10:=Begin()
	if(result10== nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result11:=Commit()
	if(result11 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result12:=Close()
	if(result12 == nil){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
	result13:=PoolOpen()
	if(result13 ==1){
		fmt.Println("Pass")
	} else {
		fmt.Println("fail")
	}
}

OUTPUT:-

check the calling sequence in MAIN func and the output (Both are different).

HOW SCREENSHOT IS RELATED?(from the above comment)

Those the test files which the main file is using.

Okay, so you are not using your main.go at all, but running go test? No, you can not change the order the tests are run. You need to redesign your tests.

In a good testsuite, one test does not rely on the other.

Ok. That means in insert data func i have create a table and insert the data.

Thanks.

Your code have some issues:

  • never use database open more than once
  • db must be global variable
  • use function with parameters for create tables,insert,etc.
  • don’t use prepare until you know exactly how to use
  • con seems to be more a constant than a variable
  • handle the error after each exec call not after 3 calls :man_facepalming:
  • indent the lines to be more readable
3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.