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,6 +22,8 @@ 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"})
t.Run("All", func(t *testing.T) {
count := 0 count := 0
for range users.All() { for range users.All() {
@ -29,16 +31,9 @@ func TestAll(t *testing.T) {
} }
assert.Equal(t, count, 3) assert.Equal(t, count, 3)
} })
func TestFilter(t *testing.T) { t.Run("Filter", func(t *testing.T) {
users, err := ocean.New[User]("test")
assert.Nil(t, err)
defer users.Clear()
users.Set("1", &User{Name: "User 1"})
users.Set("2", &User{Name: "User 2"})
users.Set("3", &User{Name: "User 3"})
count := 0 count := 0
f := func(user *User) bool { f := func(user *User) bool {
@ -50,76 +45,23 @@ func TestFilter(t *testing.T) {
} }
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")
assert.Nil(t, err)
defer users.Clear()
users.Set("1", &User{Name: "User 1"})
user, err := users.Get("1") user, err := users.Get("1")
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, user) assert.NotNil(t, user)
} assert.Equal(t, user.Name, "User 1")
})
func TestInteraction(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"))
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"})
t.Run("Exists", func(t *testing.T) {
assert.True(t, users.Exists("1")) assert.True(t, users.Exists("1"))
assert.True(t, users.Exists("2")) assert.True(t, users.Exists("2"))
assert.True(t, users.Exists("3"))
assert.True(t, !users.Exists("4"))
})
users.Delete("1") t.Run("Parallel Get and Set", func(t *testing.T) {
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 TestPersistence(t *testing.T) {
users, err := ocean.New[User]("test")
assert.Nil(t, err)
defer users.Clear()
users.Set("1", &User{Name: "User 1"})
users.Set("2", &User{Name: "User 2"})
again, err := ocean.New[User]("test")
assert.Nil(t, err)
user1, err := again.Get("1")
assert.Nil(t, err)
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{} wg := sync.WaitGroup{}
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
@ -140,6 +82,51 @@ func TestParallel(t *testing.T) {
} }
wg.Wait() wg.Wait()
})
t.Run("Persistence", func(t *testing.T) {
again, err := ocean.New[User]("test")
assert.Nil(t, err)
user, err := again.Get("1")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.Equal(t, user.Name, "User 1")
user, err = again.Get("2")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.Equal(t, user.Name, "User 2")
user, err = again.Get("3")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.Equal(t, user.Name, "User 3")
})
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"))
users.Delete("1")
assert.True(t, !users.Exists("1"))
assert.True(t, users.Exists("2"))
assert.True(t, users.Exists("3"))
users.Delete("2")
assert.True(t, !users.Exists("1"))
assert.True(t, !users.Exists("2"))
assert.True(t, users.Exists("3"))
users.Delete("3")
assert.True(t, !users.Exists("1"))
assert.True(t, !users.Exists("2"))
assert.True(t, !users.Exists("3"))
})
} }
func BenchmarkGet(b *testing.B) { func BenchmarkGet(b *testing.B) {