43 lines
862 B
Go
Raw Permalink Normal View History

2023-07-19 09:52:13 +00:00
package data_test
2023-07-10 14:27:41 +00:00
import (
"testing"
"git.akyoto.dev/go/assert"
2023-07-19 09:52:13 +00:00
"git.akyoto.dev/go/data"
2023-07-10 14:27:41 +00:00
)
2023-07-19 09:52:13 +00:00
var _ data.Storage[string] = (*data.File[string])(nil)
2023-07-10 14:27:41 +00:00
2023-07-12 14:05:09 +00:00
func TestFilePersistence(t *testing.T) {
2023-07-19 09:52:13 +00:00
users, err := data.NewFile[User]("test")
2023-07-10 14:27:41 +00:00
assert.Nil(t, err)
defer users.Clear()
2023-07-18 07:40:58 +00:00
// Write data to disk
2023-07-10 14:27:41 +00:00
users.Set("1", &User{Name: "User 1"})
users.Set("2", &User{Name: "User 2"})
users.Set("3", &User{Name: "User 3"})
users.Sync()
2023-07-18 07:40:58 +00:00
// Reload data from disk
2023-07-19 09:52:13 +00:00
reload, err := data.NewFile[User]("test")
2023-07-10 14:27:41 +00:00
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")
}