Skip to content

Commit

Permalink
insert/update functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkasun committed Feb 8, 2024
1 parent 84b8e5a commit 4b64d28
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
22 changes: 22 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
ErrNoResults = errors.New("no results found") // ErrNoResults indicates query found no results
ErrInvalidTableName = errors.New("invalid table") // ErrInvalidTableName indicates that specified table does not exist
ErrNoConnection = errors.New("no db connection") //
ErrExists = errors.New("key existss")
db *bbolt.DB
)

Expand Down Expand Up @@ -73,6 +74,27 @@ func Save(value any, key, table string) error {
})
}

// Insert saves a value only if key does not exist
func Insert(value any, key, table string) error {
_, err := Get[any](key, table)
if err == nil {
return ErrExists
}
if errors.Is(err, ErrNoResults) {
return Save(value, key, table)
}
return err
}

// 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) {
return Save(value, key, table)
}
return err
}

// Get retrieves a value for key in specified table
func Get[T any](key, table string) (T, error) {
var value T
Expand Down
55 changes: 54 additions & 1 deletion database_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package boltdb

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -64,9 +65,61 @@ func TestSave(t *testing.T) {
assert.NotNil(t, err)
})
t.Run("valid", func(t *testing.T) {
err := Save("testing", "name", "users")
user := User{
User: "testing",
}
err := Save(user, user.User, "users")
assert.Nil(t, err)
})
deleteTestEntries(t)
err = Close()
assert.Nil(t, err)
}

func TestInsert(t *testing.T) {
err := Initialize("./test.db", tables)
assert.Nil(t, err)
deleteTestEntries(t)
t.Run("valid", func(t *testing.T) {
user := User{
User: "testing",
}
err := Insert(user, user.User, "users")
assert.Nil(t, err)
})
t.Run("exists", func(t *testing.T) {
user := User{
User: "testing",
}
err := Insert(user, user.User, "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)
err = Close()
assert.Nil(t, err)
}
Expand Down

0 comments on commit 4b64d28

Please sign in to comment.