Skip to content

Commit fe0b9f3

Browse files
Test coverage updates
Signed-off-by: Lukasz Gryglicki <[email protected]>
1 parent 2b61d5b commit fe0b9f3

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

tests/py2go/api_test.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"math/rand"
78
"net/http"
89
"os"
910
"sort"
@@ -83,13 +84,15 @@ var (
8384
"gitlab_repos": "repository_name",
8485
"gerrit_repos": "gerrit_url",
8586
}
86-
UserActiveSignatureAPIPath = [2]string{"/v2/user/%s/active-signature", "/v4/user/%s/active-signature"}
87+
UserActiveSignatureAPIPath = [2]string{"/v2/user/%s/active-signature", "/v4/user/%s/active-signature"}
88+
// Optional field: true means the key may be missing in both APIs and still be valid
8789
UserActiveSignatureAPIKeyMapping = map[string]interface{}{
88-
"project_id": nil,
89-
"pull_request_id": nil,
90-
"repository_id": nil,
91-
"return_url": nil,
92-
"user_id": nil,
90+
"project_id": nil,
91+
"pull_request_id": nil,
92+
"repository_id": nil,
93+
"return_url": nil,
94+
"user_id": nil,
95+
"merge_request_id": true,
9396
}
9497
)
9598

@@ -140,6 +143,7 @@ func init() {
140143
PR_ID = iPar
141144
}
142145
}
146+
rand.Seed(time.Now().UnixNano())
143147
}
144148

145149
func tryParseTime(val interface{}) (time.Time, bool) {
@@ -210,12 +214,19 @@ func sortByKey(arr []interface{}, key string) {
210214

211215
func compareNestedFields(t *testing.T, pyData, goData, keyMapping map[string]interface{}, sortMap map[string]string) {
212216
for k, v := range keyMapping {
213-
if v == nil {
217+
bV, bVOK := v.(bool)
218+
if v == nil || bVOK {
214219
Debugf("checking values of '%s'\n", k)
215220
}
216221

217222
pyVal, pyOk := pyData[k]
218223
goVal, goOk := goData[k]
224+
225+
// true means fields are optional (nullable), so if v is true and fileds are missing in both Py and go then this is OK
226+
if bVOK && bV && !pyOk && !goOk {
227+
Debugf("'%s' is not set in both responses, this is ok\n", k)
228+
continue
229+
}
219230
if !pyOk {
220231
t.Errorf("Missing key in Python response: %s", k)
221232
continue
@@ -588,12 +599,18 @@ func TestUserActiveSignatureAPI(t *testing.T) {
588599
projectId := uuid.New().String()
589600
key := "active_signature:" + userId
590601
expire := time.Now().Add(time.Hour).Unix()
591-
value, err := json.Marshal(map[string]interface{}{
602+
iValue := map[string]interface{}{
592603
"user_id": userId,
593604
"project_id": projectId,
594605
"repository_id": fmt.Sprintf("%d", REPO_ID),
595606
"pull_request_id": fmt.Sprintf("%d", PR_ID),
596-
})
607+
}
608+
if rand.Intn(2) == 0 {
609+
mrId := rand.Intn(100)
610+
iValue["merge_request_id"] = fmt.Sprintf("%d", mrId)
611+
iValue["return_url"] = fmt.Sprintf("https://gitlab.com/gitlab-org/gitlab/-/merge_requests/%d", mrId)
612+
}
613+
value, err := json.Marshal(iValue)
597614
if err != nil {
598615
t.Fatalf("failed to marshal value: %+v", err)
599616
}

0 commit comments

Comments
 (0)