-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler_test.go
More file actions
152 lines (116 loc) · 3.32 KB
/
handler_test.go
File metadata and controls
152 lines (116 loc) · 3.32 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package function
import (
"fmt"
"os"
"testing"
)
func Test_parseUint64Value_whenNotSet(t *testing.T) {
_, err := parseUint64Value("missing_value")
if err == nil {
t.Error("Expected error")
}
}
func Test_parseUint64Value_whenSet(t *testing.T) {
expected := uint64(1000)
name := "test_value"
if err := os.Setenv(name, fmt.Sprintf("%d", expected)); err != nil {
t.Error(err)
}
got, err := parseUint64Value(name)
if err != nil {
t.Error(err)
}
if expected != got {
t.Errorf("Expected: %d, got: %d", expected, got)
}
}
func Test_parseUint64Value_whenSetInvalid(t *testing.T) {
name := "test_value"
if err := os.Setenv(name, "NaN"); err != nil {
t.Error(err)
}
_, err := parseUint64Value(name)
if err == nil {
t.Error("Expected error")
}
}
func Test_calculateInvocationsCost_whenExact(t *testing.T) {
invocations := uint64(100)
costPerUnitInvocations := uint64(10)
unitInvocations := uint64(1000)
expected := uint64(1)
got := calculateInvocationsCost(invocations, costPerUnitInvocations, unitInvocations)
if expected != got {
t.Errorf("Expected: %d, got: %d", expected, got)
}
}
func Test_calculateInvocationsCost_whenRounded(t *testing.T) {
invocations := uint64(199)
costPerUnitInvocations := uint64(10)
unitInvocations := uint64(1000)
expected := uint64(1)
got := calculateInvocationsCost(invocations, costPerUnitInvocations, unitInvocations)
if expected != got {
t.Errorf("Expected: %d, got: %d", expected, got)
}
}
func Test_NewFunctionBalance_whenBalanceZero(t *testing.T) {
credit := uint64(0)
invocations := uint64(5)
costPerUnitInvocations := uint64(10)
unitInvocations := uint64(100)
bonusInvocations := uint64(50)
expected := FunctionBalance{
Balance: 0,
Invocations: 45,
}
got := NewFunctionBalance(credit, invocations, costPerUnitInvocations, unitInvocations, bonusInvocations)
if expected != got {
t.Errorf("Expected: %d, got: %d", expected, got)
}
}
func Test_NewFunctionBalance_whenBalancePositive(t *testing.T) {
credit := uint64(100)
invocations := uint64(100)
costPerUnitInvocations := uint64(10)
unitInvocations := uint64(1000)
bonusInvocations := uint64(50)
expected := FunctionBalance{
Balance: 99,
Invocations: 9850,
}
got := NewFunctionBalance(credit, invocations, costPerUnitInvocations, unitInvocations, bonusInvocations)
if expected != got {
t.Errorf("Expected: %d, got: %d", expected, got)
}
}
func Test_NewFunctionBalance_whenBalanceNegative(t *testing.T) {
credit := uint64(9)
invocations := uint64(100)
costPerUnitInvocations := uint64(10)
unitInvocations := uint64(100)
bonusInvocations := uint64(50)
expected := FunctionBalance{
Balance: 0,
Invocations: 40,
}
got := NewFunctionBalance(credit, invocations, costPerUnitInvocations, unitInvocations, bonusInvocations)
if expected != got {
t.Errorf("Expected: %d, got: %d", expected, got)
}
}
func Test_NewFunctionBalance_whenRemainingInvocationsNegative(t *testing.T) {
credit := uint64(4)
invocations := uint64(100)
costPerUnitInvocations := uint64(10)
unitInvocations := uint64(100)
bonusInvocations := uint64(50)
expected := FunctionBalance{
Balance: 0,
Invocations: 0,
}
got := NewFunctionBalance(credit, invocations, costPerUnitInvocations, unitInvocations, bonusInvocations)
if expected != got {
t.Errorf("Expected: %d, got: %d", expected, got)
}
}