-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb_discount_offers_test.go
56 lines (49 loc) · 1.27 KB
/
db_discount_offers_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
package main
import (
"errors"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewDiscountID(t *testing.T) {
last := uint(len(discountOffers))
exceeded := last + 1
exceededErr := eDiscountIDOutOfRange{exceeded}
type testcase struct {
s string
id uint
err error
}
verify := func(tc *testcase, id DiscountID, err error, testname string) {
assert.Equal(t, DiscountID(tc.id), id, testname)
if errors.Is(tc.err, assert.AnError) {
assert.Error(t, err)
} else {
assert.ErrorIs(t, err, tc.err)
}
}
testcases := []testcase{
{"0", 0, nil},
{strconv.FormatUint(uint64(last), 32), last, nil},
{strconv.FormatUint(uint64(exceeded), 32), 0, exceededErr},
{"a string", 0, assert.AnError},
{"-1", 0, assert.AnError},
}
for _, tc := range testcases {
id, err := NewDiscountID(tc.s)
verify(&tc, id, err, "regular")
verify(&tc, id, id.UnmarshalCSV(tc.s), "CSV")
verify(&tc, id, id.UnmarshalJSON([]byte(tc.s)), "JSON")
}
}
func TestDiscountRoundupValue(t *testing.T) {
assert.Equal(t,
DiscountRoundupValue(1000, 40, 60, (1/3.0)), 20.0, "Full round-up",
)
assert.Equal(t,
DiscountRoundupValue(10, 40, 60, (1/3.0)), 10.0, "Limited round-up",
)
assert.Equal(t,
DiscountRoundupValue(0, 40, 60, (1/3.0)), 0.0, "No round-up",
)
}