Skip to content

Commit

Permalink
Allow putting test connection string into .connstr file
Browse files Browse the repository at this point in the history
  • Loading branch information
denisenkom committed Nov 12, 2020
1 parent 162e654 commit 095ece7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea
/.connstr
35 changes: 28 additions & 7 deletions conn_str_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mssql

import (
"bufio"
"io"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -182,14 +184,33 @@ func testConnParams(t testing.TB) connectParams {
params.logFlags = logFlags
return params
}
return connectParams{
host: os.Getenv("HOST"),
instance: os.Getenv("INSTANCE"),
database: os.Getenv("DATABASE"),
user: os.Getenv("SQLUSER"),
password: os.Getenv("SQLPASSWORD"),
logFlags: logFlags,
if len(os.Getenv("HOST")) > 0 && len(os.Getenv("DATABASE")) > 0 {
return connectParams{
host: os.Getenv("HOST"),
instance: os.Getenv("INSTANCE"),
database: os.Getenv("DATABASE"),
user: os.Getenv("SQLUSER"),
password: os.Getenv("SQLPASSWORD"),
logFlags: logFlags,
}
}
// try loading connection string from file
f, err := os.Open(".connstr")
if err == nil {
rdr := bufio.NewReader(f)
dsn, err := rdr.ReadString('\n')
if err != io.EOF {
t.Fatal(err)
}
params, err := parseConnectParams(dsn)
if err != nil {
t.Fatal("unable to parse connection string loaded from file", err)
}
params.logFlags = logFlags
return params
}
t.Skip("no database connection string")
return connectParams{}
}

func TestConnParseRoundTripFixed(t *testing.T) {
Expand Down
9 changes: 1 addition & 8 deletions tds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/hex"
"net"
"net/url"
"os"
"runtime"
"testing"
)
Expand Down Expand Up @@ -165,13 +164,7 @@ func TestSendSqlBatch(t *testing.T) {
}

func checkConnStr(t testing.TB) {
if len(os.Getenv("SQLSERVER_DSN")) > 0 {
return
}
if len(os.Getenv("HOST")) > 0 && len(os.Getenv("DATABASE")) > 0 {
return
}
t.Skip("no database connection string")
testConnParams(t)
}

// makeConnStr returns a URL struct so it may be modified by various
Expand Down

0 comments on commit 095ece7

Please sign in to comment.