feat(be): implement options for boltdb

This commit is contained in:
fiftin 2024-03-10 19:00:15 +01:00
parent 710c7df275
commit 522513b375
2 changed files with 17 additions and 7 deletions

View File

@ -12,7 +12,7 @@ func (d *BoltDb) SetOption(key string, value string) error {
Value: value,
}
_, err := d.GetOption(key)
_, err := d.getOption(key)
if errors.Is(err, db.ErrNotFound) {
_, err = d.createObject(-1, db.OptionProps, opt)
@ -24,9 +24,21 @@ func (d *BoltDb) SetOption(key string, value string) error {
return err
}
func (d *BoltDb) GetOption(key string) (value string, err error) {
func (d *BoltDb) getOption(key string) (value string, err error) {
var option db.Option
err = d.getObject(-1, db.OptionProps, strObjectID(key), &option)
value = option.Value
return
}
func (d *BoltDb) GetOption(key string) (value string, err error) {
var option db.Option
err = d.getObject(-1, db.OptionProps, strObjectID(key), &option)
value = option.Value
if errors.Is(err, db.ErrNotFound) {
err = nil
}
return
}

View File

@ -1,18 +1,16 @@
package bolt
import (
"errors"
"github.com/ansible-semaphore/semaphore/db"
"testing"
)
func TestGetOption(t *testing.T) {
store := CreateTestStore()
_, err := store.GetOption("unknown_option")
val, err := store.GetOption("unknown_option")
if !errors.Is(err, db.ErrNotFound) {
t.Fatal("Result must be nil for non-existent option")
if err != nil && val != "" {
t.Fatal("Result must be empty string for non-existent option")
}
}