-
Notifications
You must be signed in to change notification settings - Fork 1
/
testutil.go
106 lines (83 loc) · 2.86 KB
/
testutil.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
package graphql
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"strings"
"testing"
"unicode"
"flamingo.me/flamingo-commerce/v3/test/integrationtest/projecttest/helper"
"github.com/gavv/httpexpect/v2"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)
const (
replacementScale = 2
)
func LoadGraphQL(t *testing.T, name string, replacements map[string]string) string {
t.Helper()
content, err := os.ReadFile(path.Join("testdata", name+".graphql"))
if err != nil {
t.Fatal(err)
}
repl := make([]string, replacementScale*len(replacements))
i := 0
for key, val := range replacements {
repl[i] = fmt.Sprintf("###%s###", key)
repl[i+1] = val
i += 2
}
replacer := strings.NewReplacer(repl...)
return replacer.Replace(string(content))
}
// PrepareCart adds a simple product via graphQl
func PrepareCart(t *testing.T, e *httpexpect.Expect) {
t.Helper()
helper.GraphQlRequest(t, e, LoadGraphQL(t, "cart_add_to_cart", map[string]string{"MARKETPLACE_CODE": "fake_simple", "DELIVERY_CODE": "delivery"})).Expect().Status(http.StatusOK)
}
// PrepareCartWithPaymentSelection adds a simple product via graphQl
func PrepareCartWithPaymentSelection(t *testing.T, expect *httpexpect.Expect, paymentMethod string, marketPlaceCode *string) {
t.Helper()
code := "fake_simple"
if marketPlaceCode != nil {
code = *marketPlaceCode
}
helper.GraphQlRequest(t, expect, LoadGraphQL(t, "cart_add_to_cart", map[string]string{"MARKETPLACE_CODE": code, "DELIVERY_CODE": "delivery"})).Expect().Status(http.StatusOK)
helper.GraphQlRequest(t, expect, LoadGraphQL(t, "update_payment_selection", map[string]string{"PAYMENT_METHOD": paymentMethod})).Expect().Status(http.StatusOK)
}
func GetValue(response *httpexpect.Response, queryName, key string) *httpexpect.Value {
return response.JSON().Object().Value("data").Object().Value(queryName).Object().Value(key)
}
func GetArray(response *httpexpect.Response, queryName string) *httpexpect.Array {
return response.JSON().Object().Value("data").Object().Value(queryName).Array()
}
func AssertResponseForExpectedState(t *testing.T, response *httpexpect.Response, expectedState map[string]interface{}) {
t.Helper()
data := make(map[string]interface{})
require.NoError(t, json.Unmarshal([]byte(response.Body().Raw()), &data))
var theData interface{}
var ok bool
if theData, ok = data["data"]; !ok || theData == nil {
t.Fatalf("no data in response: %s", response.Body().Raw())
return
}
data, ok = theData.(map[string]interface{})
if !ok {
t.Fatalf("no data in response: %s", response.Body().Raw())
return
}
if diff := cmp.Diff(data, expectedState); diff != "" {
t.Errorf("diff mismatch (-want +got):\\n%s", diff)
}
}
// SpaceMap strips all whitespace from given string
func SpaceMap(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, str)
}