-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutiny_test.go
127 lines (110 loc) · 4.37 KB
/
mutiny_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
package mutiny_test
import (
"encoding/json"
"fmt"
"github.com/Kansuler/mutiny"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)
// Example payload
type Payload struct {
CountryCode mutiny.PossibleValues `json:"country_code"`
Currency mutiny.PossibleValues `json:"currency"`
BankAccount mutiny.PossibleValues `json:"bank_account"`
}
func (p Payload) SetCountryCode(value mutiny.AssignedValue) Payload {
p.CountryCode = mutiny.SelectValue(p.CountryCode, value)
return p
}
func (p Payload) SetCurrency(value mutiny.AssignedValue) Payload {
p.Currency = mutiny.SelectValue(p.Currency, value)
return p
}
func (p Payload) SetBankAccount(value mutiny.AssignedValue) Payload {
p.BankAccount = mutiny.SelectValue(p.BankAccount, value)
return p
}
type MutinyTestSuite struct {
suite.Suite
}
func JSONEq(suite *MutinyTestSuite, expected, actual string) bool {
var expectedJSONAsInterface, actualJSONAsInterface interface{}
if err := json.Unmarshal([]byte(expected), &expectedJSONAsInterface); err != nil {
suite.T().Error(fmt.Sprintf("Expected value ('%s') is not valid json.\nJSON parsing error: '%s'", expected, err.Error()))
return false
}
if err := json.Unmarshal([]byte(actual), &actualJSONAsInterface); err != nil {
suite.T().Error(fmt.Sprintf("Input ('%s') needs to be valid json.\nJSON parsing error: '%s'", actual, err.Error()))
return false
}
return assert.ObjectsAreEqual(expectedJSONAsInterface, actualJSONAsInterface)
}
func (suite *MutinyTestSuite) TestPayload() {
payload := Payload{
CountryCode: mutiny.PossibleValues{
Pass: []any{"SE"},
Fail: []any{"FEK"},
Erroneous: []any{" ", true, 123},
},
Currency: mutiny.PossibleValues{
Pass: []any{"EUR", "SEK"},
Erroneous: []any{"123", " "},
Fail: []any{"DKK"},
},
BankAccount: mutiny.PossibleValues{
Pass: []any{
json.RawMessage(`{"type":"iban","account_number":"SE4550000000058398257466"}`),
},
Fail: []any{
json.RawMessage(`{"type":"national-DK","account_number":"4200874917","bank_code":"0040"}`),
},
Erroneous: []any{},
},
}
expectedResults := []string{
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":\"SE\", \"currency\":\"EUR\"}",
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":\"SE\", \"currency\":\"SEK\"}",
}
units, err := mutiny.Riot(payload)
suite.NoError(err)
suite.Len(units, 2)
for _, unit := range units {
var match bool
for index, expected := range expectedResults {
if JSONEq(suite, expected, string(unit.RequestBody)) {
match = true
expectedResults = append(expectedResults[:index], expectedResults[index+1:]...)
break
}
}
suite.Truef(match, "Unexpected request body: %s", string(unit.RequestBody))
}
suite.Zerof(len(expectedResults), "expected test to consume all expected results")
expectedResults = []string{
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":\" \", \"currency\":\"EUR\"}",
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":true, \"currency\":\"EUR\"}",
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":123, \"currency\":\"EUR\"}",
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":\" \", \"currency\":\"SEK\"}",
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":true, \"currency\":\"SEK\"}",
"{\"bank_account\":{\"account_number\":\"SE4550000000058398257466\", \"type\":\"iban\"}, \"country_code\":123, \"currency\":\"SEK\"}",
}
units, err = mutiny.Riot(payload.SetCountryCode(mutiny.Erroneous))
suite.NoError(err)
suite.Len(units, 6)
for _, unit := range units {
var match bool
for index, expected := range expectedResults {
if JSONEq(suite, expected, string(unit.RequestBody)) {
match = true
expectedResults = append(expectedResults[:index], expectedResults[index+1:]...)
break
}
}
suite.Truef(match, "Unexpected request body: %s", string(unit.RequestBody))
}
suite.Zerof(len(expectedResults), "expected test to consume all expected results")
}
func TestMutinyTestSuite(t *testing.T) {
suite.Run(t, new(MutinyTestSuite))
}