Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
Added test to make sure non-select statements such as create, drop, etc.
can use sql.Query(...) and check for sql.ErrNoRows when there is no
resultset.
  • Loading branch information
asifjalil committed Mar 2, 2019
1 parent 2da2f8b commit 2f71683
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,48 @@ func TestSPClobOut(t *testing.T) {
t.Logf("%s returned %s\n", procStmt, resume)
}

// To check if we can use sql.Query to run a non-select statement
func TestDDLQuery(t *testing.T) {
tabname := "test"
createStmt := fmt.Sprintf("create table %s(col1 smallint)", tabname)
insertStmt := fmt.Sprintf("insert into %s values(1)", tabname)
selectStmt := fmt.Sprintf("select col1 from %s", tabname)
dropStmt := fmt.Sprintf("drop table %s", tabname)

db, err := newTestDB()
if err != nil {
t.Fatal(err)
}
defer db.close()

_, err = db.Query(createStmt)
switch {
case err == sql.ErrNoRows:
info(t, "expected\n")
case err != nil:
die(t, "%q failed: %v\n", createStmt, err)
}

_, err = db.Exec(insertStmt)
if err != nil {
die(t, "%q failed: %v\n", insertStmt, err)
}

var val int
err = db.QueryRow(selectStmt).Scan(&val)
if err != nil {
die(t, "%q failed: %v\n", selectStmt, err)
}
if val != 1 {
die(t, "Expected 1, got %d\n", val)
}

_, err = db.Exec(dropStmt)
if err != nil {
die(t, "%q failed: %v\n", dropStmt, err)
}
}

func logf(t *testing.T, format string, a ...interface{}) {
t.Logf(format, a...)
}
Expand Down

0 comments on commit 2f71683

Please sign in to comment.