From 7ea45835b9386886e39231d68051441162e5db0a Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Sun, 8 Oct 2017 01:26:53 +0300 Subject: [PATCH] Fix some issues in tests These issues were found by megacheck. Mostly unused values and unchecked/underchecked errors. --- conn_test.go | 59 +++++++++++++++++++++++--------------------------- notify_test.go | 6 +++++ 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/conn_test.go b/conn_test.go index d3c01f34..092e7bd2 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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) } @@ -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 { @@ -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\"", diff --git a/notify_test.go b/notify_test.go index 82a77e1e..075666dd 100644 --- a/notify_test.go +++ b/notify_test.go @@ -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 { @@ -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 {