Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from microservices-demo/extend-return-message
Browse files Browse the repository at this point in the history
Extend return message
  • Loading branch information
nustiueudinastea authored Jan 20, 2017
2 parents 67797d6 + 240ba89 commit 5279117
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
9 changes: 8 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package payment

import (
"errors"
"fmt"
"time"
)

Expand All @@ -11,7 +12,8 @@ type Service interface {
}

type Authorisation struct {
Authorised bool `json:"authorised"`
Authorised bool `json:"authorised"`
Message string `json:"message"`
}

type Health struct {
Expand Down Expand Up @@ -41,11 +43,16 @@ func (s *service) Authorise(amount float32) (Authorisation, error) {
return Authorisation{}, ErrInvalidPaymentAmount
}
authorised := false
message := "Payment declined"
if amount <= s.declineOverAmount {
authorised = true
message = "Payment authorised"
} else {
message = fmt.Sprintf("Payment declined: amount exceeds %.2f", s.declineOverAmount)
}
return Authorisation{
Authorised: authorised,
Message: message,
}, nil
}

Expand Down
20 changes: 10 additions & 10 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package payment

import (
"testing"
)
import "testing"
import "fmt"

func TestAuthorise(t *testing.T) {
result, _ := NewAuthorisationService(100).Authorise(10)
expected := true
if result.Authorised != expected {
expected := Authorisation{true, "Payment authorised"}
if result != expected {
t.Errorf("Authorise returned unexpected result: got %v want %v",
result.Authorised, expected)
result, expected)
}
}

func TestFailOverCertainAmount(t *testing.T) {
result, _ := NewAuthorisationService(10).Authorise(100)
expected := false
if result.Authorised != expected {
declineAmount := float32(10)
result, _ := NewAuthorisationService(declineAmount).Authorise(100)
expected := Authorisation{false, fmt.Sprintf("Payment declined: amount exceeds %.2f", declineAmount)}
if result != expected {
t.Errorf("Authorise returned unexpected result: got %v want %v",
result.Authorised, expected)
result, expected)
}
}

Expand Down

0 comments on commit 5279117

Please sign in to comment.