Skip to content

Commit 0e51f0b

Browse files
committed
test: Redo tests for the DeleteGlobalField function
1 parent b8cda44 commit 0e51f0b

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

cmd/cs_sdk/api/delete-global-field.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func DeleteGlobalField(uid string, isForced bool) error {
1111
globalField := GetGlobalField(uid)
1212

1313
if globalField == nil {
14-
return errors.New("The global field does not exist")
14+
return errors.New("the global field does not exist")
1515
}
1616

1717
_, err := csSdkRequest(
@@ -25,7 +25,7 @@ func DeleteGlobalField(uid string, isForced bool) error {
2525
return err
2626
}
2727

28-
logger.Info("The global field has been deleted")
28+
logger.Info("the global field has been deleted")
2929

3030
return nil
3131
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package api
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/Dobefu/csb/cmd/cs_sdk"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func setupDeleteGlobalFieldTest() func() {
12+
return func() {
13+
csSdkRequest = cs_sdk.Request
14+
}
15+
}
16+
17+
func TestDeleteGlobalFieldSuccess(t *testing.T) {
18+
cleanup := setupDeleteGlobalFieldTest()
19+
defer cleanup()
20+
21+
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) {
22+
return map[string]interface{}{}, nil
23+
}
24+
25+
err := DeleteGlobalField("Test Name", false)
26+
assert.NoError(t, err)
27+
}
28+
29+
func TestDeleteGlobalFieldErrDoesNotExist(t *testing.T) {
30+
cleanup := setupDeleteGlobalFieldTest()
31+
defer cleanup()
32+
33+
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) {
34+
if method == "GET" {
35+
return nil, errors.New("cannot find global field")
36+
}
37+
38+
return map[string]interface{}{}, nil
39+
}
40+
41+
err := DeleteGlobalField("Bogus", false)
42+
assert.EqualError(t, err, "the global field does not exist")
43+
}
44+
45+
func TestDeleteGlobalFieldErrDelete(t *testing.T) {
46+
cleanup := setupDeleteGlobalFieldTest()
47+
defer cleanup()
48+
49+
csSdkRequest = func(path string, method string, body map[string]interface{}, useManagementToken bool) (map[string]interface{}, error) {
50+
if method == "DELETE" {
51+
return nil, errors.New("cannot find global field")
52+
}
53+
54+
return map[string]interface{}{}, nil
55+
}
56+
57+
err := DeleteGlobalField("Test Name", false)
58+
assert.EqualError(t, err, "cannot find global field")
59+
}

0 commit comments

Comments
 (0)