70 lines
1.3 KiB
Go

package ocean_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/ocean"
)
type User struct {
Name string `json:"name"`
}
func TestCollectionGet(t *testing.T) {
users, err := ocean.New[User]("test", "User")
assert.Nil(t, err)
defer users.Clear()
users.Set("1", &User{Name: "User 1"})
user, exists := users.Get("1")
assert.True(t, exists)
assert.NotNil(t, user)
}
func TestInteraction(t *testing.T) {
users, err := ocean.New[User]("test", "User")
assert.Nil(t, err)
defer users.Clear()
assert.True(t, !users.Exists("1"))
assert.True(t, !users.Exists("2"))
users.Set("1", &User{Name: "User 1"})
assert.True(t, users.Exists("1"))
assert.True(t, !users.Exists("2"))
users.Set("2", &User{Name: "User 1"})
assert.True(t, users.Exists("1"))
assert.True(t, users.Exists("2"))
users.Delete("1")
assert.True(t, !users.Exists("1"))
assert.True(t, users.Exists("2"))
users.Delete("2")
assert.True(t, !users.Exists("1"))
assert.True(t, !users.Exists("2"))
}
func TestCollectionAll(t *testing.T) {
users, err := ocean.New[User]("test", "User")
assert.Nil(t, err)
defer users.Clear()
users.Set("1", &User{Name: "User 1"})
users.Set("2", &User{Name: "User 2"})
count := 0
for range users.All() {
count++
}
assert.Equal(t, count, 2)
}