Skip to content

Commit e45ebce

Browse files
Add test for GetAllFeatureFlags
1 parent f38e84e commit e45ebce

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

dao/inmemory_impl_mock.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ type InMemoryMockDao struct {
2222

2323
// GetFlags return all the flags
2424
func (m *InMemoryMockDao) GetFlags(ctx context.Context) ([]model.FeatureFlag, daoErr.DaoError) {
25+
if ctx.Value("error") != nil {
26+
if err, ok := ctx.Value("error").(daoErr.DaoErrorCode); ok {
27+
return nil, daoErr.NewDaoError(err, fmt.Errorf("error on get flags"))
28+
}
29+
return nil, daoErr.NewDaoError(daoErr.UnknownError, fmt.Errorf("error on get flags"))
30+
}
2531
return m.flags, nil
2632
}
2733

@@ -82,3 +88,7 @@ func (m *InMemoryMockDao) Ping() daoErr.DaoError {
8288
func (m *InMemoryMockDao) OnPingReturnError(v bool) {
8389
m.errorOnPing = v
8490
}
91+
92+
func (m *InMemoryMockDao) SetFlags(flags []model.FeatureFlag) {
93+
m.flags = flags
94+
}

handler/flags_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package handler
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/go-feature-flag/app-api/dao"
7+
daoErr "github.com/go-feature-flag/app-api/dao/err"
8+
"github.com/go-feature-flag/app-api/model"
9+
"github.com/go-feature-flag/app-api/testutils"
10+
"github.com/labstack/echo/v4"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
"net/http"
14+
"net/http/httptest"
15+
"testing"
16+
"time"
17+
)
18+
19+
func TestFlagsHandler_GetAllFeatureFlags(t *testing.T) {
20+
type test struct {
21+
name string
22+
ctx context.Context
23+
flags []model.FeatureFlag
24+
expectedHTTPCode int
25+
expectedBody string
26+
}
27+
tests := []test{
28+
{
29+
name: "should return an empty array of flags if there are no flags",
30+
ctx: context.Background(),
31+
expectedHTTPCode: http.StatusOK,
32+
flags: make([]model.FeatureFlag, 0),
33+
expectedBody: "[]\n",
34+
},
35+
{
36+
name: "should return a flag with a default rule",
37+
ctx: context.Background(),
38+
expectedHTTPCode: http.StatusOK,
39+
flags: []model.FeatureFlag{
40+
{
41+
ID: "926214f3-80c1-46e6-a913-b2d40b92a932",
42+
Name: "flag1",
43+
Description: testutils.String("description1"),
44+
Variations: &map[string]*interface{}{
45+
"variation1": testutils.Interface("A"),
46+
"variation2": testutils.Interface("B"),
47+
},
48+
LastModifiedBy: "foo",
49+
LastUpdatedDate: time.Unix(1729849827, 0),
50+
CreatedDate: time.Unix(1729849827, 0),
51+
DefaultRule: &model.Rule{
52+
VariationResult: testutils.String("variation1"),
53+
},
54+
},
55+
},
56+
expectedBody: "[{\"id\":\"926214f3-80c1-46e6-a913-b2d40b92a932\",\"name\":\"flag1\",\"createdDate\":\"2024-10-25T11:50:27+02:00\",\"lastUpdatedDate\":\"2024-10-25T11:50:27+02:00\",\"LastModifiedBy\":\"foo\",\"description\":\"description1\",\"type\":\"\",\"variations\":{\"variation1\":\"A\",\"variation2\":\"B\"},\"defaultRule\":{\"id\":\"\",\"variation\":\"variation1\"}}]\n",
57+
},
58+
{
59+
name: "should return a 500 if an error occured ",
60+
ctx: context.WithValue(context.Background(), "error", daoErr.UnknownError),
61+
expectedHTTPCode: http.StatusInternalServerError,
62+
flags: make([]model.FeatureFlag, 0),
63+
expectedBody: "{\"errorDetails\":\"error on get flags\",\"code\":500}\n",
64+
},
65+
}
66+
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
e := echo.New()
70+
mockDao, err := dao.NewInMemoryMockDao()
71+
require.NoError(t, err)
72+
mockDao.SetFlags(tt.flags)
73+
74+
h := NewFlagAPIHandler(mockDao)
75+
req := httptest.NewRequestWithContext(tt.ctx, http.MethodGet, "/v1/flags", nil)
76+
rec := httptest.NewRecorder()
77+
c := e.NewContext(req, rec)
78+
79+
require.NoError(t, h.GetAllFeatureFlags(c))
80+
assert.Equal(t, tt.expectedHTTPCode, rec.Code)
81+
fmt.Println(rec.Body.String())
82+
assert.JSONEq(t, tt.expectedBody, rec.Body.String())
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)