Skip to content

Commit

Permalink
Refapp-1309 - bug fixes + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wkrop committed Jul 20, 2023
1 parent 0ae7a24 commit 04acbca
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 20 deletions.
3 changes: 2 additions & 1 deletion pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ func (t *TestCase) validateExpectsLastIfAll(res *resty.Response) (bool, []error)
return t.validateExpectsLastIfAllArrayResults(res)
}

failedExpects := make([]error, 0, len(t.ExpectLastIfAll))

for i, expect := range t.ExpectLastIfAll {
failedExpects := make([]error, 0, len(t.ExpectLastIfAll))
ok, err := t.validateExpect(expect, res)

switch i {
Expand Down
117 changes: 98 additions & 19 deletions pkg/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,60 @@ import (

var (
// standingOrder JSON queries
numberOfPaymentsJSON = "Data.StandingOrder.#.NumberOfPayments"
finalPaymentDateTime = "Data.StandingOrder.#.FinalPaymentDateTime"
finalPaymentAmount = "Data.StandingOrder.#.FinalPaymentAmount"

matchNumberOfPayments = Match{Description: "test", JSON: numberOfPaymentsJSON}
matchNoNumberOfPayments = Match{Description: "test", JSONNotPresent: numberOfPaymentsJSON}

matchFinalPaymentDateTime = Match{Description: "test", JSON: finalPaymentDateTime}
matchNoFinalPaymentDateTime = Match{Description: "test", JSONNotPresent: finalPaymentDateTime}

matchFinalPaymentAmount = Match{Description: "test", JSON: finalPaymentAmount}
bulkNumberOfPaymentsJSON = "Data.StandingOrder.#.NumberOfPayments"
bulkFinalPaymentDateTime = "Data.StandingOrder.#.FinalPaymentDateTime"
bulkFinalPaymentAmount = "Data.StandingOrder.#.FinalPaymentAmount"
numberOfPaymentsJSON = "Data.Initiation.NumberOfPayments"
finalPaymentDateTime = "Data.Initiation.FinalPaymentDateTime"
finalPaymentAmount = "Data.Initiation.FinalPaymentAmount"

matchBulkNumberOfPayments = Match{Description: "test", JSON: bulkNumberOfPaymentsJSON}
matchBulkNoNumberOfPayments = Match{Description: "test", JSONNotPresent: bulkNumberOfPaymentsJSON}
matchNumberOfPayments = Match{Description: "test", JSON: numberOfPaymentsJSON}
matchNoNumberOfPayments = Match{Description: "test", JSONNotPresent: numberOfPaymentsJSON}

matchBulkFinalPaymentDateTime = Match{Description: "test", JSON: bulkFinalPaymentDateTime}
matchBulkNoFinalPaymentDateTime = Match{Description: "test", JSONNotPresent: bulkFinalPaymentDateTime}
matchFinalPaymentDateTime = Match{Description: "test", JSON: finalPaymentDateTime}
matchNoFinalPaymentDateTime = Match{Description: "test", JSONNotPresent: finalPaymentDateTime}

matchBulkFinalPaymentAmount = Match{Description: "test", JSON: bulkFinalPaymentAmount}
matchFinalPaymentAmount = Match{Description: "test", JSON: finalPaymentAmount}

tc1 = TestCase{ExpectLastIfAll: []Expect{
{Matches: []Match{matchNumberOfPayments}},
{Matches: []Match{matchFinalPaymentDateTime}},
{Matches: []Match{matchBulkNumberOfPayments}},
{Matches: []Match{matchBulkFinalPaymentDateTime}},
{StatusCode: 403},
},
Validator: schema.NewNullValidator(),
ExpectArrayResults: true,
}

tc2 = TestCase{ExpectLastIfAll: []Expect{
{Matches: []Match{matchBulkNoNumberOfPayments}},
{Matches: []Match{matchBulkNoFinalPaymentDateTime}},
{Matches: []Match{matchBulkFinalPaymentAmount}},
{StatusCode: 403},
},
Validator: schema.NewNullValidator(),
ExpectArrayResults: true,
}

tc3 = TestCase{ExpectLastIfAll: []Expect{
{Matches: []Match{matchNumberOfPayments}},
{Matches: []Match{matchFinalPaymentDateTime}},
{StatusCode: 403},
},
Validator: schema.NewNullValidator(),
}

tc4 = TestCase{ExpectLastIfAll: []Expect{
{Matches: []Match{matchNoNumberOfPayments}},
{Matches: []Match{matchNoFinalPaymentDateTime}},
{Matches: []Match{matchFinalPaymentAmount}},
{StatusCode: 403},
},
Validator: schema.NewNullValidator(),
ExpectArrayResults: true,
Validator: schema.NewNullValidator(),
}
)

Expand Down Expand Up @@ -260,7 +285,7 @@ func TestGetReplacementField(t *testing.T) {

func TestPassingExpectsLastIfAll(t *testing.T) {
for i, body := range okTestCasesStandingOrders {
t.Run(fmt.Sprintf("Standing Orders - OK test case %d", i), func(t *testing.T) {
t.Run(fmt.Sprintf("Standing Orders - OK test case Fails when NumberOfPayments, FinalPaymentDateTime are not specified and FinalPaymentAmount is specified.: %d", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(200, "OK", body)
result, errs := tc1.Validate(resp, emptyContext)
assert.True(t, result)
Expand All @@ -269,7 +294,7 @@ func TestPassingExpectsLastIfAll(t *testing.T) {
}

for i, body := range badTestCasesStandingOrders {
t.Run(fmt.Sprintf("Standing Orders - Bad test case %d with the expected status code", i), func(t *testing.T) {
t.Run(fmt.Sprintf("Standing Orders - Bad test case Fails when NumberOfPayments, FinalPaymentDateTime are not specified and FinalPaymentAmount is specified.: %d with the expected status code", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(403, "Forbidden", body)
result, errs := tc1.Validate(resp, emptyContext)
assert.True(t, result)
Expand All @@ -278,7 +303,7 @@ func TestPassingExpectsLastIfAll(t *testing.T) {
}

for i, body := range okTestCasesStandingOrders2 {
t.Run(fmt.Sprintf("Standing Orders - OK test case %d", i), func(t *testing.T) {
t.Run(fmt.Sprintf("Standing Orders - OK test case Fails when NumberOfPayments, FinalPaymentDateTime are not specified and FinalPaymentAmount is specified.: %d", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(200, "OK", body)
result, errs := tc2.Validate(resp, emptyContext)
assert.True(t, result)
Expand All @@ -287,13 +312,49 @@ func TestPassingExpectsLastIfAll(t *testing.T) {
}

for i, body := range badTestCasesStandingOrders2 {
t.Run(fmt.Sprintf("Standing Orders - Bad test case %d with the expected status code", i), func(t *testing.T) {
t.Run(fmt.Sprintf("Standing Orders - Bad test case Fails when NumberOfPayments, FinalPaymentDateTime are not specified and FinalPaymentAmount is specified.: %d with the expected status code", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(403, "Forbidden", body)
result, errs := tc2.Validate(resp, emptyContext)
assert.True(t, result)
assert.Empty(t, errs)
})
}

for i, body := range okTestCaseDomesticStandingOrders1 {
t.Run(fmt.Sprintf("Domestic Standing Orders - OK test case Fails when NumberOfPayments, FinalPaymentDateTime are specified at the time.: %d", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(200, "OK", body)
result, errs := tc3.Validate(resp, emptyContext)
assert.True(t, result)
assert.Empty(t, errs)
})
}

for i, body := range badTestCaseDomesticStandingOrders1 {
t.Run(fmt.Sprintf("Domestic Standing Orders - Bad test case Fails when NumberOfPayments, FinalPaymentDateTime are specified at the time.: %d with the expected status code", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(403, "Forbidden", body)
result, errs := tc3.Validate(resp, emptyContext)
assert.True(t, result)
assert.Empty(t, errs)
})
}

for i, body := range okTestCaseDomesticStandingOrders2 {
t.Run(fmt.Sprintf("Domestic Standing Orders - OK test case Fails when NumberOfPayments, FinalPaymentDateTime are not specified and FinalPaymentAmount is specified.: %d", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(200, "OK", body)
result, errs := tc4.Validate(resp, emptyContext)
assert.True(t, result)
assert.Empty(t, errs)
})
}

for i, body := range badTestCaseDomesticStandingOrders2 {
t.Run(fmt.Sprintf("Domestic Standing Orders - Bad test case Fails when NumberOfPayments, FinalPaymentDateTime are not specified and FinalPaymentAmount is specified.: %d with the expected status code", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(403, "Forbidden", body)
result, errs := tc4.Validate(resp, emptyContext)
assert.True(t, result)
assert.Empty(t, errs)
})
}
}

func TestFailingExpectsLastIfAll(t *testing.T) {
Expand All @@ -314,4 +375,22 @@ func TestFailingExpectsLastIfAll(t *testing.T) {
assert.NotEmpty(t, errs)
})
}

for i, body := range badTestCaseDomesticStandingOrders1 {
t.Run(fmt.Sprintf("Domestic Standing Orders - Bad test case Fails when NumberOfPayments, FinalPaymentDateTime are specified at the time.: %d with the unexpected status code", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(200, "Forbidden", body)
result, errs := tc3.Validate(resp, emptyContext)
assert.False(t, result)
assert.NotEmpty(t, errs)
})
}

for i, body := range badTestCaseDomesticStandingOrders2 {
t.Run(fmt.Sprintf("Domestic Standing Orders - Bad test case Fails when NumberOfPayments, FinalPaymentDateTime are not specified and FinalPaymentAmount is specified.: %d with the unexpected status code", i), func(t *testing.T) {
resp := test.CreateHTTPResponse(200, "Forbidden", body)
result, errs := tc4.Validate(resp, emptyContext)
assert.False(t, result)
assert.NotEmpty(t, errs)
})
}
}
106 changes: 106 additions & 0 deletions pkg/model/testcase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,112 @@ var (
}
}`,
}

okTestCaseDomesticStandingOrders1 = []string{
`{
"Data": {
"Initiation": {
"NumberOfPayments": "5"
}
}
}`,
`{
"Data": {
"Initiation": {
"FinalPaymentDateTime": "1981-03-20T06:06:06+00:00"
}
}
}`,
`{
"Data": {
"Initiation": {
"Frequency": "EvryDay"
}
}
}`,
}

badTestCaseDomesticStandingOrders1 = []string{
`{
"Data": {
"Initiation": {
"NumberOfPayments": "5",
"FinalPaymentDateTime": "1981-03-20T06:06:06+00:00"
}
}
}`,
}

okTestCaseDomesticStandingOrders2 = []string{
`{
"Data": {
"Initiation": {
"Frequency": "EvryDay"
}
}
}`,
`{
"Data": {
"Initiation": {
"FinalPaymentDateTime": "1981-03-20T06:06:06+00:00",
"FinalPaymentAmount": {
"Amount": "7.00",
"Currency": "GBP"
}
}
}
}`,
`{
"Data": {
"Initiation": {
"NumberOfPayments": "5",
"FinalPaymentDateTime": "1981-03-20T06:06:06+00:00",
"FinalPaymentAmount": {
"Amount": "7.00",
"Currency": "GBP"
}
}
}
}`,
`{
"Data": {
"Initiation": {
"NumberOfPayments": "5",
"FinalPaymentAmount": {
"Amount": "7.00",
"Currency": "GBP"
}
}
}
}`,
`{
"Data": {
"Initiation": {
"NumberOfPayments": "5"
}
}
}`,
`{
"Data": {
"Initiation": {
"FinalPaymentDateTime": "1981-03-20T06:06:06+00:00"
}
}
}`,
}

badTestCaseDomesticStandingOrders2 = []string{
`{
"Data": {
"Initiation": {
"FinalPaymentAmount": {
"Amount": "7.00",
"Currency": "GBP"
}
}
}
}`,
}
)

// Reads a single testcase from json bytes
Expand Down

0 comments on commit 04acbca

Please sign in to comment.