47 lines
965 B
Go
47 lines
965 B
Go
package storage_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.akyoto.dev/go/assert"
|
|
"git.akyoto.dev/go/ocean"
|
|
"git.akyoto.dev/go/ocean/storage"
|
|
)
|
|
|
|
var _ ocean.Storage[string] = (*storage.File[string])(nil)
|
|
|
|
type User struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func TestFilePersistence(t *testing.T) {
|
|
users, err := ocean.New[User]("test", &storage.File[User]{})
|
|
assert.Nil(t, err)
|
|
|
|
defer users.Sync()
|
|
defer users.Clear()
|
|
|
|
users.Set("1", &User{Name: "User 1"})
|
|
users.Set("2", &User{Name: "User 2"})
|
|
users.Set("3", &User{Name: "User 3"})
|
|
users.Sync()
|
|
|
|
reload, err := ocean.New[User]("test", &storage.File[User]{})
|
|
assert.Nil(t, err)
|
|
|
|
user, err := reload.Get("1")
|
|
assert.Nil(t, err)
|
|
assert.NotNil(t, user)
|
|
assert.Equal(t, user.Name, "User 1")
|
|
|
|
user, err = reload.Get("2")
|
|
assert.Nil(t, err)
|
|
assert.NotNil(t, user)
|
|
assert.Equal(t, user.Name, "User 2")
|
|
|
|
user, err = reload.Get("3")
|
|
assert.Nil(t, err)
|
|
assert.NotNil(t, user)
|
|
assert.Equal(t, user.Name, "User 3")
|
|
}
|