Skip to content

Commit

Permalink
Add method and tests for removing config
Browse files Browse the repository at this point in the history
Added config remove method and associated unit tests
  • Loading branch information
danesparza committed Jun 12, 2017
1 parent b99a5a8 commit 0ba5f00
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
29 changes: 29 additions & 0 deletions data/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ func (store ConfigDB) Set(configItem ConfigItem) (ConfigItem, error) {
return retval, err
}

// Remove removes the config item
func (store ConfigDB) Remove(configName string) error {

// If there is no config name, throw an error:
if strings.TrimSpace(configName) == "" {
return errors.New("Config name can't be blank")
}

// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
if err != nil {
return err
}

// Update the database:
err = db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("configItems"))
if err != nil {
return err
}

// Remove the item:
return b.Delete([]byte(configName))
})

return err
}

// Get fetches a config item
func (store ConfigDB) Get(configName string) (ConfigItem, error) {
// Our return item:
Expand Down
77 changes: 77 additions & 0 deletions data/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,80 @@ settings:
t.Errorf("GetAll failed: Couldn't find the config item '%s'", configName)
}
}

// Config set get remove get should work
func TestConfig_Set_ThenRemove_Successful(t *testing.T) {
// Arrange
filename := "testing.db"
defer os.Remove(filename)
defer viper.Reset()

db := data.ConfigDB{
Database: filename}

// Try storing some config items:
ct1 := data.ConfigItem{
Name: "TestItem1",
Value: "Value1"}

ct2 := data.ConfigItem{
Name: "TestItem2",
Value: "Value2"}

queryName := "TestItem1"
expectedValue := "Value1"

// Act (set)
db.Set(ct1)
db.Set(ct2)
response, err := db.Get(queryName)

// Assert (set)
if err != nil {
t.Errorf("Set then remove failed: Should have set a config item without error: %s", err)
}

if response.Value != expectedValue {
t.Errorf("Set then Get failed: Should have returned the value '%s' but returned '%s' instead", ct2.Value, response.Value)
}

if response.Name != queryName {
t.Errorf("Set then Get failed: Should have returned the value '%s' but returned '%s' instead", queryName, response.Name)
}

// Act (remove)
err = db.Remove(queryName)
if err != nil {
t.Errorf("Set then remove failed: Should have removed a config item without error: %s", err)
}

response, err = db.Get(queryName)

// Assert (remove)
if err != nil {
t.Errorf("Set then remove failed: Should have removed a config item without error: %s", err)
}

if response.Value != "" {
t.Errorf("Set then remove failed: Should not have returned an item")
}
}

// Config set get remove get should work
func TestConfig_Remove_ItemDoesntExist_NoErrors(t *testing.T) {
// Arrange
filename := "testing.db"
defer os.Remove(filename)
defer viper.Reset()

db := data.ConfigDB{
Database: filename}

// NO VALUES

// Act
err := db.Remove("somebogusname")
if err != nil {
t.Errorf("Set then remove failed: Should have removed a config item without error: %s", err)
}
}

0 comments on commit 0ba5f00

Please sign in to comment.