Skip to content

Commit

Permalink
Fix some issues in tests
Browse files Browse the repository at this point in the history
These issues were found by megacheck. Mostly unused values and
unchecked/underchecked errors.
  • Loading branch information
ainar-g authored and rafael84 committed Oct 12, 2017
1 parent 8a5b90e commit 7ea4583
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
59 changes: 27 additions & 32 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,12 +935,14 @@ func TestParseErrorInExtendedQuery(t *testing.T) {
db := openTestConn(t)
defer db.Close()

rows, err := db.Query("PARSE_ERROR $1", 1)
if err == nil {
t.Fatal("expected error")
_, err := db.Query("PARSE_ERROR $1", 1)
pqErr, _ := err.(*Error)
// Expecting a syntax error.
if err == nil || pqErr == nil || pqErr.Code != "42601" {
t.Fatalf("expected syntax error, got %s", err)
}

rows, err = db.Query("SELECT 1")
rows, err := db.Query("SELECT 1")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1392,36 +1394,29 @@ func TestParseOpts(t *testing.T) {
}

func TestRuntimeParameters(t *testing.T) {
type RuntimeTestResult int
const (
ResultUnknown RuntimeTestResult = iota
ResultSuccess
ResultError // other error
)

tests := []struct {
conninfo string
param string
expected string
expectedOutcome RuntimeTestResult
conninfo string
param string
expected string
success bool
}{
// invalid parameter
{"DOESNOTEXIST=foo", "", "", ResultError},
{"DOESNOTEXIST=foo", "", "", false},
// we can only work with a specific value for these two
{"client_encoding=SQL_ASCII", "", "", ResultError},
{"datestyle='ISO, YDM'", "", "", ResultError},
{"client_encoding=SQL_ASCII", "", "", false},
{"datestyle='ISO, YDM'", "", "", false},
// "options" should work exactly as it does in libpq
{"options='-c search_path=pqgotest'", "search_path", "pqgotest", ResultSuccess},
{"options='-c search_path=pqgotest'", "search_path", "pqgotest", true},
// pq should override client_encoding in this case
{"options='-c client_encoding=SQL_ASCII'", "client_encoding", "UTF8", ResultSuccess},
{"options='-c client_encoding=SQL_ASCII'", "client_encoding", "UTF8", true},
// allow client_encoding to be set explicitly
{"client_encoding=UTF8", "client_encoding", "UTF8", ResultSuccess},
{"client_encoding=UTF8", "client_encoding", "UTF8", true},
// test a runtime parameter not supported by libpq
{"work_mem='139kB'", "work_mem", "139kB", ResultSuccess},
{"work_mem='139kB'", "work_mem", "139kB", true},
// test fallback_application_name
{"application_name=foo fallback_application_name=bar", "application_name", "foo", ResultSuccess},
{"application_name='' fallback_application_name=bar", "application_name", "", ResultSuccess},
{"fallback_application_name=bar", "application_name", "bar", ResultSuccess},
{"application_name=foo fallback_application_name=bar", "application_name", "foo", true},
{"application_name='' fallback_application_name=bar", "application_name", "", true},
{"fallback_application_name=bar", "application_name", "bar", true},
}

for _, test := range tests {
Expand All @@ -1436,23 +1431,23 @@ func TestRuntimeParameters(t *testing.T) {
continue
}

tryGetParameterValue := func() (value string, outcome RuntimeTestResult) {
tryGetParameterValue := func() (value string, success bool) {
defer db.Close()
row := db.QueryRow("SELECT current_setting($1)", test.param)
err = row.Scan(&value)
if err != nil {
return "", ResultError
return "", false
}
return value, ResultSuccess
return value, true
}

value, outcome := tryGetParameterValue()
if outcome != test.expectedOutcome && outcome == ResultError {
value, success := tryGetParameterValue()
if success != test.success && !test.success {
t.Fatalf("%v: unexpected error: %v", test.conninfo, err)
}
if outcome != test.expectedOutcome {
if success != test.success {
t.Fatalf("unexpected outcome %v (was expecting %v) for conninfo \"%s\"",
outcome, test.expectedOutcome, test.conninfo)
success, test.success, test.conninfo)
}
if value != test.expected {
t.Fatalf("bad value for %s: got %s, want %s with conninfo \"%s\"",
Expand Down
6 changes: 6 additions & 0 deletions notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func TestConnUnlisten(t *testing.T) {
}

_, err = db.Exec("NOTIFY notify_test")
if err != nil {
t.Fatal(err)
}

err = expectNotification(t, channel, "notify_test", "")
if err != nil {
Expand Down Expand Up @@ -159,6 +162,9 @@ func TestConnUnlistenAll(t *testing.T) {
}

_, err = db.Exec("NOTIFY notify_test")
if err != nil {
t.Fatal(err)
}

err = expectNotification(t, channel, "notify_test", "")
if err != nil {
Expand Down

0 comments on commit 7ea4583

Please sign in to comment.