forked from microservices-demo/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
41 lines (36 loc) · 1.11 KB
/
service_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
package payment
import "testing"
import "fmt"
func TestAuthorise(t *testing.T) {
result, _ := NewAuthorisationService(100).Authorise(10)
expected := Authorisation{true, "Payment authorised"}
if result != expected {
t.Errorf("Authorise returned unexpected result: got %v want %v",
result, expected)
}
}
func TestFailOverCertainAmount(t *testing.T) {
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, expected)
}
}
func TestFailIfAmountIsZero(t *testing.T) {
_, err := NewAuthorisationService(10).Authorise(0)
_, ok := err.(error)
if !ok {
t.Errorf("Authorise returned unexpected result: got %v want %v",
err, "Zero payment")
}
}
func TestFailIfAmountNegative(t *testing.T) {
_, err := NewAuthorisationService(10).Authorise(-1)
_, ok := err.(error)
if !ok {
t.Errorf("Authorise returned unexpected result: got %v want %v",
err, "Negative payment")
}
}