diff --git a/Collection_test.go b/Collection_test.go index ecc8269..f2dd91a 100644 --- a/Collection_test.go +++ b/Collection_test.go @@ -14,7 +14,7 @@ type User struct { Name string `json:"name"` } -func TestAll(t *testing.T) { +func TestCollection(t *testing.T) { users, err := ocean.New[User]("test") assert.Nil(t, err) defer users.Clear() @@ -22,124 +22,111 @@ func TestAll(t *testing.T) { users.Set("1", &User{Name: "User 1"}) users.Set("2", &User{Name: "User 2"}) users.Set("3", &User{Name: "User 3"}) - count := 0 - for range users.All() { - count++ - } + t.Run("All", func(t *testing.T) { + count := 0 - assert.Equal(t, count, 3) -} + for range users.All() { + count++ + } -func TestFilter(t *testing.T) { - users, err := ocean.New[User]("test") - assert.Nil(t, err) - defer users.Clear() + assert.Equal(t, count, 3) + }) - users.Set("1", &User{Name: "User 1"}) - users.Set("2", &User{Name: "User 2"}) - users.Set("3", &User{Name: "User 3"}) - count := 0 + t.Run("Filter", func(t *testing.T) { + count := 0 - f := func(user *User) bool { - return user.Name == "User 2" - } + f := func(user *User) bool { + return user.Name == "User 2" + } - for range users.Filter(f) { - count++ - } + for range users.Filter(f) { + count++ + } - assert.Equal(t, count, 1) -} + assert.Equal(t, count, 1) + }) -func TestGet(t *testing.T) { - users, err := ocean.New[User]("test") - assert.Nil(t, err) - defer users.Clear() + t.Run("Get", func(t *testing.T) { + user, err := users.Get("1") + assert.Nil(t, err) + assert.NotNil(t, user) + assert.Equal(t, user.Name, "User 1") + }) - users.Set("1", &User{Name: "User 1"}) - user, err := users.Get("1") + t.Run("Exists", func(t *testing.T) { + assert.True(t, users.Exists("1")) + assert.True(t, users.Exists("2")) + assert.True(t, users.Exists("3")) + assert.True(t, !users.Exists("4")) + }) - assert.Nil(t, err) - assert.NotNil(t, user) -} + t.Run("Parallel Get and Set", func(t *testing.T) { + wg := sync.WaitGroup{} -func TestInteraction(t *testing.T) { - users, err := ocean.New[User]("test") - assert.Nil(t, err) - defer users.Clear() + for i := 0; i < 100; i++ { + wg.Add(2) - assert.True(t, !users.Exists("1")) - assert.True(t, !users.Exists("2")) + go func() { + defer wg.Done() + user, err := users.Get("1") + assert.Nil(t, err) + assert.NotNil(t, user) + assert.Equal(t, user.Name, "User 1") + }() - users.Set("1", &User{Name: "User 1"}) + go func() { + defer wg.Done() + users.Set("1", &User{Name: "User 1"}) + }() + } - assert.True(t, users.Exists("1")) - assert.True(t, !users.Exists("2")) + wg.Wait() + }) - users.Set("2", &User{Name: "User 1"}) + t.Run("Persistence", func(t *testing.T) { + again, err := ocean.New[User]("test") + assert.Nil(t, err) - assert.True(t, users.Exists("1")) - assert.True(t, users.Exists("2")) + user, err := again.Get("1") + assert.Nil(t, err) + assert.NotNil(t, user) + assert.Equal(t, user.Name, "User 1") - users.Delete("1") + user, err = again.Get("2") + assert.Nil(t, err) + assert.NotNil(t, user) + assert.Equal(t, user.Name, "User 2") - assert.True(t, !users.Exists("1")) - assert.True(t, users.Exists("2")) + user, err = again.Get("3") + assert.Nil(t, err) + assert.NotNil(t, user) + assert.Equal(t, user.Name, "User 3") + }) - users.Delete("2") + t.Run("Delete", func(t *testing.T) { + assert.True(t, users.Exists("1")) + assert.True(t, users.Exists("2")) + assert.True(t, users.Exists("3")) - assert.True(t, !users.Exists("1")) - assert.True(t, !users.Exists("2")) -} + users.Delete("1") -func TestPersistence(t *testing.T) { - users, err := ocean.New[User]("test") - assert.Nil(t, err) - defer users.Clear() + assert.True(t, !users.Exists("1")) + assert.True(t, users.Exists("2")) + assert.True(t, users.Exists("3")) - users.Set("1", &User{Name: "User 1"}) - users.Set("2", &User{Name: "User 2"}) + users.Delete("2") - again, err := ocean.New[User]("test") - assert.Nil(t, err) + assert.True(t, !users.Exists("1")) + assert.True(t, !users.Exists("2")) + assert.True(t, users.Exists("3")) - user1, err := again.Get("1") - assert.Nil(t, err) + users.Delete("3") - user2, err := again.Get("2") - assert.Nil(t, err) - - assert.NotNil(t, user1) - assert.NotNil(t, user2) -} - -func TestParallel(t *testing.T) { - users, err := ocean.New[User]("test") - assert.Nil(t, err) - defer users.Clear() - - users.Set("1", &User{Name: "User 1"}) - wg := sync.WaitGroup{} - - for i := 0; i < 100; i++ { - wg.Add(2) - - go func() { - defer wg.Done() - user, err := users.Get("1") - assert.Nil(t, err) - assert.NotNil(t, user) - assert.Equal(t, user.Name, "User 1") - }() - - go func() { - defer wg.Done() - users.Set("1", &User{Name: "User 1"}) - }() - } - - wg.Wait() + assert.True(t, !users.Exists("1")) + assert.True(t, !users.Exists("2")) + assert.True(t, !users.Exists("3")) + }) } func BenchmarkGet(b *testing.B) {