Skip to content

Commit

Permalink
Merge pull request #6 from devilcove/bug/update
Browse files Browse the repository at this point in the history
check for existing value during update
  • Loading branch information
mattkasun authored Apr 10, 2024
2 parents 9577741 + 00a7898 commit f91289e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 64 deletions.
5 changes: 3 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func Insert(value any, key, table string) error {
// Update save a value only if key already exists
func Update(value any, key, table string) error {
_, err := Get[any](key, table)
if errors.Is(err, ErrExists) {
if err == nil {
return Save(value, key, table)
}
return err
return ErrExists
}

// Get retrieves a value for key in specified table
Expand Down Expand Up @@ -138,6 +138,7 @@ func GetAll[T any](table string) ([]T, error) {

// Delete deletes the entry in table corresponding to key
func Delete[T any](key, table string) error {
//verify table exists
if _, err := Get[T](key, table); err != nil {
return err
}
Expand Down
164 changes: 102 additions & 62 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package boltdb

import (
"errors"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,20 +12,19 @@ import (
var tables = []string{"users", "keys", "tables"}

type User struct {
User string
Name string
Pass string
}

type Other struct {
Name string
Foo int
Bar string
Foo int
}

func TestInitialize(t *testing.T) {
t.Log(tables)
//tables := []string{"users", "keys", "networks"}
t.Run("valid", func(t *testing.T) {
err := Initialize("./test.db", tables)
err := testInit()
assert.Nil(t, err)
err = Close()
assert.Nil(t, err)
Expand All @@ -35,7 +36,7 @@ func TestInitialize(t *testing.T) {
}

func TestClose(t *testing.T) {
err := Initialize("./test.db", tables)
err := testInit()
assert.Nil(t, err)
t.Run("open", func(t *testing.T) {
err = Close()
Expand All @@ -48,7 +49,7 @@ func TestClose(t *testing.T) {
}

func TestSave(t *testing.T) {
err := Initialize("./test.db", tables)
err := testInit()
assert.Nil(t, err)
t.Run("noSuchTable", func(t *testing.T) {
err := Save("testing", "key", "nosuchtable")
Expand All @@ -66,68 +67,49 @@ func TestSave(t *testing.T) {
})
t.Run("valid", func(t *testing.T) {
user := User{
User: "testing",
Name: "testing",
}
err := Save(user, user.User, "users")
err := Save(user, user.Name, "users")
assert.Nil(t, err)
})
deleteTestEntries(t)
err = deleteTestEntries()
assert.Nil(t, err)
err = Close()
assert.Nil(t, err)
}

func TestInsert(t *testing.T) {
err := Initialize("./test.db", tables)
err := testInit()
assert.Nil(t, err)
err = deleteTestEntries()
assert.Nil(t, err)
deleteTestEntries(t)

t.Run("valid", func(t *testing.T) {
user := User{
User: "testing",
Name: "testing",
}
err := Insert(user, user.User, "users")
err := Insert(user, user.Name, "users")
assert.Nil(t, err)
})
t.Run("exists", func(t *testing.T) {
user := User{
User: "testing",
Name: "testing",
}
err := Insert(user, user.User, "users")
err := Insert(user, user.Name, "users")
assert.True(t, errors.Is(err, ErrExists))
})
deleteTestEntries(t)
err = Close()
assert.Nil(t, err)
}

func TestUpdate(t *testing.T) {
err := Initialize("./test.db", tables)
assert.Nil(t, err)
deleteTestEntries(t)
t.Run("does not exist", func(t *testing.T) {
user := User{
User: "testing",
}
err := Update(user, user.User, "users")
assert.True(t, errors.Is(err, ErrNoResults))
})
t.Run("existing", func(t *testing.T) {
user := User{
User: "testing",
}
err := Save(user, user.User, "users")
assert.Nil(t, err)
err = Update(user, user.User, "users")
assert.Nil(t, err)
})
deleteTestEntries(t)
//deleteTestEntries(t)
err = Close()
assert.Nil(t, err)
}

func TestGetOne(t *testing.T) {
err := Initialize("./test.db", tables)
//err := Initialize("./test.db", tables)
err := testInit()
assert.Nil(t, err)
err = deleteTestEntries()
assert.Nil(t, err)
deleteTestEntries(t)
t.Log(err)
t.Run("noSuchTable", func(t *testing.T) {
value, err := Get[User]("first", "nosuchtable")
assert.Equal(t, User{}, value)
Expand All @@ -152,18 +134,16 @@ func TestGetOne(t *testing.T) {
t.Run("valid", func(t *testing.T) {
value, err := Get[User]("first", "users")
assert.Nil(t, err)
assert.Equal(t, "first", value.User)
assert.Equal(t, "first", value.Name)
assert.Equal(t, "password", value.Pass)
})
deleteTestEntries(t)
err = Close()
assert.Nil(t, err)
}

func TestGetAll(t *testing.T) {
err := Initialize("./test.db", tables)
err := testInit()
assert.Nil(t, err)
err = deleteTestEntries()
assert.Nil(t, err)
deleteTestEntries(t)
t.Run("noSuchTable", func(t *testing.T) {
value, err := GetAll[User]("nosuchtable")
assert.Equal(t, []User(nil), value)
Expand All @@ -178,16 +158,51 @@ func TestGetAll(t *testing.T) {
t.Run("valid", func(t *testing.T) {
value, err := GetAll[User]("users")
assert.Nil(t, err)
assert.Equal(t, "first", value[0].User)
assert.Equal(t, "first", value[0].Name)
assert.Equal(t, "password", value[0].Pass)
})
deleteTestEntries(t)
//deleteTestEntries(t)
err = Close()
assert.Nil(t, err)
}

func TestUpdate(t *testing.T) {
err := testInit()
assert.Nil(t, err)
err = deleteTestEntries()
assert.Nil(t, err)
t.Run("does not exist", func(t *testing.T) {
user := User{
Name: "testing",
}
err := Update(user, user.Name, "users")
assert.True(t, errors.Is(err, ErrExists))
})
t.Run("existing", func(t *testing.T) {
user := User{
Name: "testing",
}
err := Save(user, user.Name, "users")
assert.Nil(t, err)
user2 := User{
Name: "test2",
Pass: "nopass",
}
err = Update(user2, user.Name, "users")
assert.Nil(t, err)
user, err = Get[User](user.Name, "users")
assert.Nil(t, err)
assert.Equal(t, user2.Name, user.Name)
})
//deleteTestEntries(t)
err = Close()
assert.Nil(t, err)
}

func TestDelete(t *testing.T) {
err := Initialize("./test.db", tables)
err := testInit()
assert.Nil(t, err)
err = deleteTestEntries()
assert.Nil(t, err)
t.Run("nonexistentTable", func(t *testing.T) {
err := Delete[User]("first", "tabledoesnotexist")
Expand All @@ -202,7 +217,7 @@ func TestDelete(t *testing.T) {
err := Delete[User]("first", "users")
assert.Nil(t, err)
})
deleteTestEntries(t)
//deleteTestEntries(t)
err = Close()
assert.Nil(t, err)
}
Expand All @@ -211,26 +226,51 @@ func createTestEntries(t *testing.T) {
t.Helper()
users := []User{
{
User: "first",
Name: "first",
Pass: "password",
},
{
User: "second",
Name: "second",
Pass: "testing",
},
}
for _, user := range users {
err := Save(user, user.User, "users")
err := Save(user, user.Name, "users")
assert.Nil(t, err)
}
}

func deleteTestEntries(t *testing.T) {
t.Helper()
func deleteTestEntries() error {
//t.Helper()
values, err := GetAll[User]("users")
assert.Nil(t, err)
if err != nil {
//if errors.Is(err, os.ErrNotExist) || errors.Is(err, ErrNoResults) {
//return nil
//}
if strings.Contains(err.Error(), "no results") {
return nil
}
return err
}
for _, value := range values {
err := Delete[User](value.User, "users")
assert.Nil(t, err)
if err := Delete[User](value.Name, "users"); err != nil {
if strings.Contains(err.Error(), "no results") {
return nil
}
return err
}
}
return nil
}

func testInit() error {
if err := os.Remove("./test.db"); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
}
if err := Initialize("./test.db", tables); err != nil {
return err
}
return nil
}

0 comments on commit f91289e

Please sign in to comment.