MCPcopy Create free account
hub / github.com/apache/kvrocks-controller / TestNamespaceBasics

Function TestNamespaceBasics

server/api/namespace_test.go:50–107  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

48}
49
50func TestNamespaceBasics(t *testing.T) {
51 handler := &NamespaceHandler{s: store.NewClusterStore(engine.NewMock())}
52
53 runCreate := func(t *testing.T, ns string, expectedStatusCode int) {
54 recorder := httptest.NewRecorder()
55 ctx := GetTestContext(recorder)
56 ctx.Request.Body = io.NopCloser(bytes.NewBufferString(fmt.Sprintf("{\"namespace\":\"%s\"}", ns)))
57 handler.Create(ctx)
58 require.Equal(t, expectedStatusCode, recorder.Code)
59 }
60
61 runExists := func(t *testing.T, ns string, expectedStatusCode int) {
62 recorder := httptest.NewRecorder()
63 ctx := GetTestContext(recorder)
64 ctx.Params = []gin.Param{{Key: "namespace", Value: ns}}
65 handler.Exists(ctx)
66 require.Equal(t, expectedStatusCode, recorder.Code)
67 }
68
69 runRemove := func(t *testing.T, ns string, expectedStatusCode int) {
70 recorder := httptest.NewRecorder()
71 ctx := GetTestContext(recorder)
72 ctx.Params = []gin.Param{{Key: "namespace", Value: ns}}
73 handler.Remove(ctx)
74 require.Equal(t, expectedStatusCode, recorder.Code)
75 }
76
77 t.Run("create namespace", func(t *testing.T) {
78 runCreate(t, "test0", http.StatusCreated)
79 runCreate(t, "test1", http.StatusCreated)
80 runCreate(t, "test0", http.StatusConflict)
81 })
82
83 t.Run("exits namespace", func(t *testing.T) {
84 runExists(t, "test0", http.StatusOK)
85 runExists(t, "not-exists", http.StatusNotFound)
86 })
87
88 t.Run("list namespace", func(t *testing.T) {
89 recorder := httptest.NewRecorder()
90 ctx := GetTestContext(recorder)
91 handler.List(ctx)
92 require.Equal(t, http.StatusOK, recorder.Code)
93
94 var rsp struct {
95 Data map[string][]string `json:"data"`
96 }
97 require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &rsp))
98 require.ElementsMatch(t, []string{"test0", "test1"}, rsp.Data["namespaces"])
99 })
100
101 t.Run("remove namespace", func(t *testing.T) {
102 for _, ns := range []string{"test0", "test1"} {
103 runRemove(t, ns, http.StatusNoContent)
104 runRemove(t, ns, http.StatusNotFound)
105 }
106 })
107}

Callers

nothing calls this directly

Calls 8

CreateMethod · 0.95
ExistsMethod · 0.95
RemoveMethod · 0.95
ListMethod · 0.95
NewClusterStoreFunction · 0.92
NewMockFunction · 0.92
GetTestContextFunction · 0.85
EqualMethod · 0.80

Tested by

no test coverage detected