-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_features_test.go
145 lines (121 loc) · 5.17 KB
/
api_features_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gogrowthbook_test
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/JoseFMP/gogrowthbook"
"github.com/JoseFMP/gogrowthbook/models"
"github.com/JoseFMP/gogrowthbook/test_utils"
"github.com/stretchr/testify/assert"
)
// TestCanCreateAndReadFeature
// creates a feature in GrowthBook, and reads it out afterward to double check it was really created
func TestCanCreateAndReadFeature(t *testing.T) {
clientContext := test_utils.GenerateClientCtxRemoteServer(t)
randomNumber := time.Now().UnixMilli()
featureID := fmt.Sprintf("go-growthbook-integration-test-%d", randomNumber)
featureValueType := "string"
featureDescription := fmt.Sprintf(
"This is a feature created during integration tests of go-growthbook lib, remove safely - %d",
randomNumber)
forceRuleDescription := "custom-force-rule-description"
forcedValue := "abc"
forcedEnable := false
forcedType := models.FeatureRuleTypeForce
cfg := gogrowthbook.NewConfiguration()
client := gogrowthbook.NewAPIClient(cfg)
growthBookEnvironmentName := "production"
t.Run("create feature",
func(t *testing.T) {
newFeature := models.PostFeatureRequest{
Id: featureID,
ValueType: featureValueType,
Description: &featureDescription,
Environments: map[string]models.PostFeatureRequestEnvironmentsValue{
growthBookEnvironmentName: {
Enabled: false,
Rules: []*models.FeatureRule{
{
FeatureForceRule: &models.FeatureForceRule{
Type: &forcedType,
Enabled: &forcedEnable,
Value: &forcedValue,
Description: &forceRuleDescription,
},
},
},
},
},
}
createFeature(clientContext, t, newFeature)
})
t.Run("readout feature", func(t *testing.T) {
featureResponse, httpResponse, errReadingOutFeature := client.FeaturesAPI.GetFeature(clientContext, featureID).Execute()
assert.NoErrorf(t, errReadingOutFeature, "There should be no error fetching the feature just created")
assert.NotNil(t, httpResponse)
assert.Equal(t, http.StatusOK, httpResponse.StatusCode)
assert.NotNil(t, featureResponse)
assert.NotNil(t, featureResponse.Feature)
assert.Equal(t, featureResponse.Feature.Id, featureID)
assert.Equal(t, featureResponse.Feature.ValueType, featureValueType)
assert.Equal(t, featureResponse.Feature.Description, featureDescription)
_, hasTargetEnvironment := featureResponse.Feature.Environments[growthBookEnvironmentName]
assert.True(t, hasTargetEnvironment)
assert.Len(t, featureResponse.Feature.Environments[growthBookEnvironmentName].Rules, 1)
assert.NotNil(t, featureResponse.Feature.Environments[growthBookEnvironmentName].Rules[0].FeatureForceRule, 1)
assert.Nil(t, featureResponse.Feature.Environments[growthBookEnvironmentName].Rules[0].FeatureRolloutRule, 1)
assert.Nil(t, featureResponse.Feature.Environments[growthBookEnvironmentName].Rules[0].FeatureExperimentRule, 1)
assert.Nil(t, featureResponse.Feature.Environments[growthBookEnvironmentName].Rules[0].FeatureExperimentRefRule, 1)
assert.Equal(t, *featureResponse.Feature.Environments[growthBookEnvironmentName].Rules[0].FeatureForceRule.Type, models.FeatureRuleType("force"))
assert.Equal(t, *featureResponse.Feature.Environments[growthBookEnvironmentName].Rules[0].FeatureForceRule.Value, forcedValue)
},
)
}
func TestCanReadAllFeatures(t *testing.T) {
clientContext := test_utils.GenerateClientCtxRemoteServer(t)
t.Run("Read with pagination", func(t *testing.T) {
// arrange
randomFeatures := make([]models.PostFeatureRequest, 10)
for i := range randomFeatures {
randomFeatures[i] = *generateRandomFeature()
createFeature(clientContext, t, randomFeatures[i])
time.Sleep(time.Second)
}
clt := gogrowthbook.NewAPIClient(gogrowthbook.NewConfiguration())
// act
resp, httpResp, errDoingReq := clt.FeaturesAPI.ListFeatures(clientContext).Limit(1).Execute()
// assert
assert.NoError(t, errDoingReq)
assert.NotNil(t, resp)
assert.NotNil(t, httpResp)
assert.Equal(t, http.StatusOK, httpResp.StatusCode)
assert.NotNil(t, resp.HasMore)
assert.True(t, resp.HasMore)
assert.NotNil(t, resp.Count)
assert.Equal(t, int32(1), resp.Count)
assert.GreaterOrEqual(t, resp.Total, int32(len(randomFeatures)))
})
}
func createFeature(ctx context.Context, t *testing.T, feature models.PostFeatureRequest) {
cfg := gogrowthbook.NewConfiguration()
client := gogrowthbook.NewAPIClient(cfg)
newFeatureReq := client.FeaturesAPI.PostFeature(ctx).PostFeatureRequest(feature)
responsePayload, httpResponse, errDoingReq := newFeatureReq.Execute()
assert.NoErrorf(t, errDoingReq, "The request should return success", errDoingReq)
assert.NotNil(t, httpResponse)
assert.NotNil(t, responsePayload)
assert.Equalf(t, http.StatusOK, httpResponse.StatusCode, httpResponse.Status)
}
func generateRandomFeature() *models.PostFeatureRequest {
randomizer := fmt.Sprintf("%d", time.Now().Unix())
id := fmt.Sprintf("gogrowthbook-tests-%s", randomizer)
description := "Feature created by tests of package github.com/JoseFMP/gogrowthbook"
return &models.PostFeatureRequest{
Id: id,
ValueType: "string",
Description: &description,
Environments: map[string]models.PostFeatureRequestEnvironmentsValue{},
}
}