/
handler_opp_test.go
93 lines (88 loc) · 2.88 KB
/
handler_opp_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
package isumm
import (
"net/http"
"net/url"
"reflect"
"testing"
"appengine/aetest"
)
const (
typeStr = "1"
valueStr = "2.23"
dateStr = "30/12/2015"
firstOpDateStr = "31/12/2015"
)
func TestOpHandler_AddSucess(t *testing.T) {
c, err := aetest.NewContext(&aetest.Options{StronglyConsistentDatastore: true})
if err != nil {
t.Fatal(err)
}
defer c.Close()
// The test has two operations. One is added at investment creation and the other is added via handler.
firstOp, _ := NewOperationFromString(typeStr, valueStr, firstOpDateStr)
secondOp, _ := NewOperationFromString(typeStr, valueStr, dateStr)
// Adding investment along with first operation.
inv := &Investment{Name: "testInv", Ops: Operations{firstOp}}
if err := PutInvestment(c, inv); err != nil {
t.Fatal(err)
}
// This should add an operation that is exactly like second operation.
form := url.Values{OpsParamInv: {inv.Key}, OpsParamType: {typeStr}, OpsParamValue: {valueStr}, OpsParamDate: {dateStr}}
if err := handleOp(c, &http.Request{Form: form}); err != nil {
t.Fatalf("handleOp want nil got:%q", err)
}
gotInv, err := GetInvestment(c, inv.Key)
if err != nil {
t.Errorf("getInvestment returned an error: %q", err)
}
// Even though firstOp happened latter, operations must be chronologically sorted.
if !reflect.DeepEqual(firstOp, gotInv.Ops[0]) { // There is already one operation inserted.
t.Errorf("want:%v got:%v", firstOp, gotInv.Ops[0])
}
if !reflect.DeepEqual(secondOp, gotInv.Ops[1]) { // There is already one operation inserted.
t.Errorf("want:%v got:%v", secondOp, gotInv.Ops[1])
}
}
func TestOpHandler_AddFailure(t *testing.T) {
c, err := aetest.NewContext(&aetest.Options{StronglyConsistentDatastore: true})
if err != nil {
t.Fatal(err)
}
defer c.Close()
op, _ := NewOperationFromString(typeStr, valueStr, dateStr)
inv := &Investment{Name: "testInv", Ops: Operations{op}}
if err := PutInvestment(c, inv); err != nil {
t.Fatal(err)
}
testCases := []struct {
desc, k, t, v, d string
action string
statusCode int
}{
{
desc: "invalid key",
k: "", t: typeStr, v: valueStr, d: dateStr,
statusCode: http.StatusPreconditionFailed,
},
{
desc: "invalid date",
k: inv.Key, t: typeStr, v: valueStr, d: "",
statusCode: http.StatusPreconditionFailed,
},
{
desc: "invalid investment",
k: "1", t: typeStr, v: valueStr, d: dateStr,
statusCode: http.StatusInternalServerError,
},
}
for _, testCase := range testCases {
form := url.Values{OpsParamInv: {testCase.k}, OpsParamType: {testCase.t}, OpsParamValue: {testCase.v}, OpsParamDate: {testCase.d}}
gotErr := handleOp(c, &http.Request{Form: form})
if gotErr == nil {
t.Errorf("want error got nil. Test case: %+v", testCase.desc)
}
if testCase.statusCode != gotErr.Code {
t.Errorf("(%s) status code want:%d got:%d", testCase.desc, testCase.statusCode, gotErr.Code)
}
}
}