Improved tests

This commit is contained in:
Eduard Urbach 2023-07-08 12:13:29 +02:00
parent e0a5fa281b
commit fee6a812d6
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

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