From 97eb0d191b382c0e731de1efb790dfb3ae2ebc4a Mon Sep 17 00:00:00 2001 From: Dale Hui Date: Tue, 24 Jul 2018 17:22:26 -0700 Subject: [PATCH] Fix broken GenerateAdvisoryLockId() test - Add it to the test suite to ensure that it doesn't break again --- Makefile | 1 + database/util.go | 2 +- database/util_test.go | 28 ++++++++++++++++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 41bef89..e54d6b2 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ test-with-flags: @go test $(TEST_FLAGS) . @go test $(TEST_FLAGS) ./cli/... + @go test $(TEST_FLAGS) ./database @go test $(TEST_FLAGS) ./testing/... @echo -n '$(SOURCE)' | tr -s ' ' '\n' | xargs -I{} go test $(TEST_FLAGS) ./source/{} diff --git a/database/util.go b/database/util.go index c636a7a..7de1d1b 100644 --- a/database/util.go +++ b/database/util.go @@ -7,7 +7,7 @@ import ( const advisoryLockIdSalt uint = 1486364155 -// inspired by rails migrations, see https://goo.gl/8o9bCT +// GenerateAdvisoryLockId inspired by rails migrations, see https://goo.gl/8o9bCT func GenerateAdvisoryLockId(databaseName string) (string, error) { sum := crc32.ChecksumIEEE([]byte(databaseName)) sum = sum * uint32(advisoryLockIdSalt) diff --git a/database/util_test.go b/database/util_test.go index 905c840..0b66d2d 100644 --- a/database/util_test.go +++ b/database/util_test.go @@ -1,12 +1,28 @@ package database +import ( + "testing" +) + func TestGenerateAdvisoryLockId(t *testing.T) { - id, err := p.generateAdvisoryLockId("database_name") - if err != nil { - t.Errorf("expected err to be nil, got %v", err) + testcases := []struct { + dbname string + expectedID string // empty string signifies that an error is expected + }{ + {dbname: "database_name", expectedID: "1764327054"}, } - if len(id) == 0 { - t.Errorf("expected generated id not to be empty") + + for _, tc := range testcases { + t.Run(tc.dbname, func(t *testing.T) { + if id, err := GenerateAdvisoryLockId("database_name"); err == nil { + if id != tc.expectedID { + t.Error("Generated incorrect ID:", id, "!=", tc.expectedID) + } + } else { + if tc.expectedID != "" { + t.Error("Got unexpected error:", err) + } + } + }) } - t.Logf("generated id: %v", id) }