Fix broken GenerateAdvisoryLockId() test

- Add it to the test suite to ensure that it doesn't break again
This commit is contained in:
Dale Hui 2018-07-24 17:22:26 -07:00
parent 857d7a620d
commit 97eb0d191b
3 changed files with 24 additions and 7 deletions

View File

@ -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/{}

View File

@ -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)

View File

@ -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)
}