2023-07-05 15:23:50 +00:00
|
|
|
package ocean_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.akyoto.dev/go/assert"
|
|
|
|
"git.akyoto.dev/go/ocean"
|
|
|
|
)
|
|
|
|
|
2023-07-06 12:10:12 +00:00
|
|
|
type User struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
func TestCollectionGet(t *testing.T) {
|
2023-07-06 12:27:48 +00:00
|
|
|
users, err := ocean.New[User]("test", "User")
|
2023-07-05 21:59:49 +00:00
|
|
|
assert.Nil(t, err)
|
2023-07-06 12:10:12 +00:00
|
|
|
defer users.Clear()
|
2023-07-05 21:59:49 +00:00
|
|
|
|
2023-07-06 12:10:12 +00:00
|
|
|
users.Set("1", &User{Name: "User 1"})
|
2023-07-05 20:02:16 +00:00
|
|
|
user, exists := users.Get("1")
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
assert.True(t, exists)
|
|
|
|
assert.NotNil(t, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInteraction(t *testing.T) {
|
2023-07-06 12:27:48 +00:00
|
|
|
users, err := ocean.New[User]("test", "User")
|
2023-07-05 21:59:49 +00:00
|
|
|
assert.Nil(t, err)
|
2023-07-06 12:10:12 +00:00
|
|
|
defer users.Clear()
|
2023-07-05 20:02:16 +00:00
|
|
|
|
|
|
|
assert.True(t, !users.Exists("1"))
|
|
|
|
assert.True(t, !users.Exists("2"))
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-06 12:10:12 +00:00
|
|
|
users.Set("1", &User{Name: "User 1"})
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
assert.True(t, users.Exists("1"))
|
|
|
|
assert.True(t, !users.Exists("2"))
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-06 12:10:12 +00:00
|
|
|
users.Set("2", &User{Name: "User 1"})
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
assert.True(t, users.Exists("1"))
|
|
|
|
assert.True(t, users.Exists("2"))
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
users.Delete("1")
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
assert.True(t, !users.Exists("1"))
|
|
|
|
assert.True(t, users.Exists("2"))
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
users.Delete("2")
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-05 20:02:16 +00:00
|
|
|
assert.True(t, !users.Exists("1"))
|
|
|
|
assert.True(t, !users.Exists("2"))
|
2023-07-05 15:23:50 +00:00
|
|
|
}
|
2023-07-06 12:10:12 +00:00
|
|
|
|
|
|
|
func TestCollectionAll(t *testing.T) {
|
2023-07-06 12:27:48 +00:00
|
|
|
users, err := ocean.New[User]("test", "User")
|
2023-07-06 12:10:12 +00:00
|
|
|
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)
|
|
|
|
}
|