Skip to content

Commit

Permalink
Update DeleteConfigKey to return old value
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Jun 17, 2019
1 parent e7e7d8a commit e66a24c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions config.go
Expand Up @@ -70,12 +70,12 @@ func (c *Client) SetConfigValue(ctx context.Context, node, section, key, value s
}

// DeleteConfigKey deletes the configuration key and associated value from the
// specified node.
// specified node. It returns the old value.
//
// See http://docs.couchdb.org/en/stable/api/server/configuration.html#delete--_node-node-name-_config-section-key
func (c *Client) DeleteConfigKey(ctx context.Context, node, section, key string) error {
func (c *Client) DeleteConfigKey(ctx context.Context, node, section, key string) (string, error) {
if configer, ok := c.driverClient.(driver.Configer); ok {
return configer.DeleteConfigKey(ctx, node, section, key)
}
return configNotImplemented
return "", configNotImplemented
}
27 changes: 16 additions & 11 deletions config_test.go
Expand Up @@ -224,6 +224,7 @@ func TestDeleteConfigKey(t *testing.T) {
type tst struct {
client *Client
node, section, key string
expected string
status int
err string
}
Expand All @@ -235,35 +236,39 @@ func TestDeleteConfigKey(t *testing.T) {
})
tests.Add("error", tst{
client: &Client{driverClient: &mock.Configer{
DeleteConfigKeyFunc: func(_ context.Context, _, _, _ string) error {
return errors.New("conf error")
DeleteConfigKeyFunc: func(_ context.Context, _, _, _ string) (string, error) {
return "", errors.New("conf error")
},
}},
status: http.StatusInternalServerError,
err: "conf error",
})
tests.Add("success", tst{
client: &Client{driverClient: &mock.Configer{
DeleteConfigKeyFunc: func(_ context.Context, node, section, key string) error {
DeleteConfigKeyFunc: func(_ context.Context, node, section, key string) (string, error) {
if node != "foo" {
return errors.Errorf("Unexpected node: %s", node)
return "", errors.Errorf("Unexpected node: %s", node)
}
if section != "foo" {
return errors.Errorf("Unexpected section: %s", section)
return "", errors.Errorf("Unexpected section: %s", section)
}
if key != "baz" {
return errors.Errorf("Unexpected key: %s", key)
return "", errors.Errorf("Unexpected key: %s", key)
}
return nil
return "old", nil
},
}},
node: "foo",
section: "foo",
key: "baz",
node: "foo",
section: "foo",
key: "baz",
expected: "old",
})

tests.Run(t, func(t *testing.T, test tst) {
err := test.client.DeleteConfigKey(context.Background(), test.node, test.section, test.key)
result, err := test.client.DeleteConfigKey(context.Background(), test.node, test.section, test.key)
testy.StatusError(t, test.err, test.status, err)
if d := diff.Interface(test.expected, result); d != nil {
t.Error(d)
}
})
}
2 changes: 1 addition & 1 deletion driver/config.go
Expand Up @@ -13,5 +13,5 @@ type Configer interface {
ConfigSection(ctx context.Context, node, section string) (ConfigSection, error)
ConfigValue(ctx context.Context, node, section, key string) (string, error)
SetConfigValue(ctx context.Context, node, section, key, value string) (string, error)
DeleteConfigKey(ctx context.Context, node, section, key string) error
DeleteConfigKey(ctx context.Context, node, section, key string) (string, error)
}
4 changes: 2 additions & 2 deletions mock/client.go
Expand Up @@ -159,7 +159,7 @@ type Configer struct {
ConfigSectionFunc func(context.Context, string, string) (driver.ConfigSection, error)
ConfigValueFunc func(context.Context, string, string, string) (string, error)
SetConfigValueFunc func(context.Context, string, string, string, string) (string, error)
DeleteConfigKeyFunc func(context.Context, string, string, string) error
DeleteConfigKeyFunc func(context.Context, string, string, string) (string, error)
}

var _ driver.Configer = &Configer{}
Expand All @@ -185,6 +185,6 @@ func (c *Configer) SetConfigValue(ctx context.Context, node, section, key, value
}

// DeleteConfigKey calls c.DeleteConfigKeyFunc
func (c *Configer) DeleteConfigKey(ctx context.Context, node, section, key string) error {
func (c *Configer) DeleteConfigKey(ctx context.Context, node, section, key string) (string, error) {
return c.DeleteConfigKeyFunc(ctx, node, section, key)
}

0 comments on commit e66a24c

Please sign in to comment.