Skip to content

Commit

Permalink
feat: update dblib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommi2Day committed Feb 12, 2023
1 parent 6112885 commit bfd7e11
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/sijms/go-ora/v2 v2.5.27
github.com/stretchr/testify v1.8.1
github.com/tommi2day/gomodules/common v0.0.0-20230209185003-c143378b8a50
github.com/tommi2day/gomodules/dblib v0.0.0-20230209185234-2d0f35878ef9
github.com/tommi2day/gomodules/dblib v0.0.0-20230211121340-6112885ed8de
github.com/tommi2day/gomodules/pwlib v0.0.0-20230209181233-cf71fab94d23
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad
golang.org/x/tools v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ github.com/tomarrell/wrapcheck/v2 v2.8.0 h1:qDzbir0xmoE+aNxGCPrn+rUSxAX+nG6vREgb
github.com/tomarrell/wrapcheck/v2 v2.8.0/go.mod h1:ao7l5p0aOlUNJKI0qVwB4Yjlqutd0IvAB9Rdwyilxvg=
github.com/tommi2day/gomodules/common v0.0.0-20230209185003-c143378b8a50 h1:wS1uczfT4kO1OqZ3d2z20TH3yt/jY52QmJQkL8DepZY=
github.com/tommi2day/gomodules/common v0.0.0-20230209185003-c143378b8a50/go.mod h1:tBmaQ8Phsl/Hug9TNulQwu7OFV5ZraFAycSEbhs84aE=
github.com/tommi2day/gomodules/dblib v0.0.0-20230209185234-2d0f35878ef9 h1:EJlM6f40xrfZdOAgQwZMR31jJa0yq3lCVSDIxVLNnZo=
github.com/tommi2day/gomodules/dblib v0.0.0-20230209185234-2d0f35878ef9/go.mod h1:V7ORp0FEAlHUPYl2c7LSLoUwXe/YJfQFjudL/5tyaoY=
github.com/tommi2day/gomodules/dblib v0.0.0-20230211121340-6112885ed8de h1:87xl/dbL39ro4Nux7mArWGthguIhE2uJS16o6BPcGZQ=
github.com/tommi2day/gomodules/dblib v0.0.0-20230211121340-6112885ed8de/go.mod h1:V7ORp0FEAlHUPYl2c7LSLoUwXe/YJfQFjudL/5tyaoY=
github.com/tommi2day/gomodules/pwlib v0.0.0-20230209181233-cf71fab94d23 h1:FhXa1FNJw78Q50oAPQnpHCpXFt5X9+25hxtDQxH6fmk=
github.com/tommi2day/gomodules/pwlib v0.0.0-20230209181233-cf71fab94d23/go.mod h1:8h1ozWoBZtqHD62en0dgLI2Uvjyx5mOsfixQx3NhtfA=
github.com/tommy-muehle/go-mnd/v2 v2.5.1 h1:NowYhSdyE/1zwK9QCLeRb6USWdoif80Ie+v+yU8u1Zw=
Expand Down
9 changes: 0 additions & 9 deletions test/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/tommi2day/gomodules/dblib"

_ "github.com/glebarez/go-sqlite"
"github.com/sijms/go-ora/v2/network"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -19,14 +18,6 @@ var urlOptions = map[string]string{
// "CONNECTION TIMEOUT": "3",
}

func makeOerr(code int, msg string) *network.OracleError {
e := &network.OracleError{
ErrCode: code,
ErrMsg: msg,
}
return e
}

func newTestfile(filename string) error {
var err error
var f *os.File
Expand Down
42 changes: 27 additions & 15 deletions test/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ var dbhost = common.GetEnv("DB_HOST", "127.0.0.1")
var connectora = fmt.Sprintf("XE.local=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XEPDB1)))", dbhost, port)
var target string

// func TestMain(m *testing.M) {
// makeOerr create a pseudo ORA Errorcode
func makeOerr(code int, msg string) *network.OracleError {
e := &network.OracleError{
ErrCode: code,
ErrMsg: msg,
}
return e
}

// prepareContainer create an Oracle Docker Container
func prepareContainer() (pool *dockertest.Pool, dbContainer *dockertest.Resource, err error) {
dbContainer = nil
pool = nil
Expand Down Expand Up @@ -116,7 +125,7 @@ func TestWithOracle(t *testing.T) {
domain := dblib.GetDefaultDomain(tnsAdmin)
t.Logf("Default Domain: '%s'", domain)

tnsEntries, err := dblib.GetTnsnames(filename, true)
tnsEntries, d2, err := dblib.GetTnsnames(filename, true)
t.Run("Parse TNSNames.ora", func(t *testing.T) {
require.NoErrorf(t, err, "Parsing %s failed: %s", filename, err)
})
Expand All @@ -125,16 +134,18 @@ func TestWithOracle(t *testing.T) {
return
}

assert.Equalf(t, domain, d2, "Domain name mismatch '%s' -> '%s'", domain, d2)
e, found := dblib.GetEntry(alias, tnsEntries, domain)
require.True(t, found, "Alias not found")
desc := common.RemoveSpace(e.Desc)

pool, dbContainer, err := prepareContainer()
defer destroyContainer(pool, dbContainer)
require.NotNil(t, dbContainer, "Prepare failed")
defer destroyContainer(pool, dbContainer)

t.Run("Direct connect", func(t *testing.T) {
var db *sql.DB
var err error

t.Logf("connect to %s\n", target)
db, err = sql.Open("oracle", target)
assert.NoErrorf(t, err, "Open failed: %s", err)
Expand All @@ -144,39 +155,40 @@ func TestWithOracle(t *testing.T) {
})
t.Run("connect with function", func(t *testing.T) {
var db *sql.DB
var err error

connect := target
t.Logf("connect with %s\n", connect)
db, err = dblib.DBConnect("oracle", connect, TIMEOUT)
assert.NoErrorf(t, err, "Connect failed: %s", err)
assert.NoErrorf(t, err, "Connect failed: %v", err)
assert.IsType(t, &sql.DB{}, db, "Returned wrong type")
result, err := dblib.SelectOneStringValue(db, "select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual")
assert.NoErrorf(t, err, "Select returned error::%s", err)
assert.NoErrorf(t, err, "Select returned error::%v", err)
assert.NotEmpty(t, result)
t.Logf("Sysdate: %s", result)
})
t.Run("Check tns connect", func(t *testing.T) {
var db *sql.DB
var err error

connect := ora.BuildJDBC(DBUSER, DBPASSWORD, desc, urlOptions)
t.Logf("connect with %s\n", connect)
db, err = dblib.DBConnect("oracle", connect, TIMEOUT)
assert.NoErrorf(t, err, "Connect failed: %s", err)
assert.IsType(t, &sql.DB{}, db, "Returned wrong type")
})
t.Run("Check dummy connect", func(t *testing.T) {
t.Logf("connect with %s\n", desc)
ok, elapsed, err := dblib.CheckWithOracle("", "", desc, TIMEOUT)
assert.ErrorContainsf(t, err, "ORA-01017", "returned unexpected error: %s", err)
assert.True(t, ok, "Test not true as expected")
assert.Greater(t, elapsed, 0*time.Second, "Should be greater zero")
var db *sql.DB
connect := ora.BuildJDBC("dummy", "dummy", desc, urlOptions)
t.Logf("connect with dummy user to %s\n", desc)
db, err = dblib.DBConnect("oracle", connect, TIMEOUT)
assert.ErrorContainsf(t, err, "ORA-01017", "returned unexpected error: %v", err)
assert.IsType(t, &sql.DB{}, db, "Returned wrong type")
})
}

// TestGetVersion Test Version output should return a nonempty value
func TestHaveOerr(t *testing.T) {
var oerr *network.OracleError
var err = errors.New("test error")
var testErr = errors.New("test error")
t.Run("Oracle Error", func(t *testing.T) {
expectedCode := 1017
expectedMsg := "ORA-01017: Invalid User or Password"
Expand All @@ -189,7 +201,7 @@ func TestHaveOerr(t *testing.T) {
t.Run("Non Oracle Error", func(t *testing.T) {
expectedCode := 0
expectedMsg := ""
isOerr, actualCode, actualMsg := dblib.HaveOerr(err)
isOerr, actualCode, actualMsg := dblib.HaveOerr(testErr)
assert.False(t, isOerr, "Oerr false detected")
assert.Equal(t, expectedCode, actualCode, "Code doesnt match")
assert.Equal(t, expectedMsg, actualMsg, "Empty Msg expected")
Expand Down
2 changes: 1 addition & 1 deletion test/tns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestParseTns(t *testing.T) {
t.Logf("Default Domain: '%s'", domain)
filename := tnsAdmin + "/tnsnames.ora"
t.Logf("load from %s", filename)
tnsEntries, err := dblib.GetTnsnames(filename, true)
tnsEntries, domain, err := dblib.GetTnsnames(filename, true)
t.Run("Parse TNSNames.ora", func(t *testing.T) {
require.NoErrorf(t, err, "Parsing %s failed: %s", filename, err)
})
Expand Down

0 comments on commit bfd7e11

Please sign in to comment.