diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index b0843023..393a91c7 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -1 +1,185 @@ package mysql + +import ( + "os" + "testing" + "time" + + "github.com/gofiber/utils" +) + +var storeConfig = Config{ + DropTable: true, +} + +func init() { + if v := os.Getenv("MYSQL_ADDRESS"); v != "" { + storeConfig.Address = v + } + if v := os.Getenv("MYSQL_USERNAME"); v != "" { + storeConfig.Username = v + } + if v := os.Getenv("MYSQL_PASSWORD"); v != "" { + storeConfig.Password = v + } + if v := os.Getenv("MYSQL_DATABASE"); v != "" { + storeConfig.DatabaseName = v + } +} + +func Test_Set(t *testing.T) { + + store := New(storeConfig) + + id := "hello" + value := []byte("Hi there!") + + store.Set(id, value, 0) + + var ( + returnedValue []byte + exp int64 + ) + + store.db.QueryRow(store.sqlSelect, id).Scan(&returnedValue, &exp) + + utils.AssertEqual(t, returnedValue, value) + utils.AssertEqual(t, exp, int64(0)) + +} + +func Test_SetExpiry(t *testing.T) { + + store := New(storeConfig) + + id := "hello" + value := []byte("Hi there!") + expiry := time.Second * 10 + + store.Set(id, value, expiry) + + now := time.Now().Unix() + var ( + returnedValue []byte + exp int64 + ) + store.db.QueryRow(store.sqlSelect, id).Scan(&returnedValue, &exp) + + delta := exp - now + upperBound := int64(expiry.Seconds()) + lowerBound := upperBound - 2 + + if !(delta <= upperBound && delta > lowerBound) { + t.Fatalf("Test_SetExpiry: expiry delta out of bounds (is %d, must be %d