-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathproducts_test.go
170 lines (133 loc) · 3.55 KB
/
products_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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//go:build unit
// +build unit
package domain
import (
"context"
"errors"
"testing"
"github.com/aws-samples/serverless-go-demo/store"
"github.com/aws-samples/serverless-go-demo/types"
"github.com/aws-samples/serverless-go-demo/types/mocks"
"github.com/golang/mock/gomock"
)
func TestGetProductNotFound(t *testing.T) {
memoryStore := store.NewMemoryStore()
domain := NewProductsDomain(memoryStore)
product, err := domain.GetProduct(context.Background(), "1")
if err != nil {
t.Errorf("GetProduct returned an error: %w", err)
}
if product != nil {
t.Error("GetProduct returned unexpected product")
}
}
func TestGetExistingProduct(t *testing.T) {
ctx := context.Background()
memoryStore := store.NewMemoryStore()
memoryStore.Put(ctx, types.Product{
Id: "iXR",
Name: "iPhone XML",
Price: 0.123,
})
domain := NewProductsDomain(memoryStore)
product, err := domain.GetProduct(context.Background(), "iXR")
if err != nil {
t.Errorf("GetProduct returned an error: %w", err)
}
if product == nil {
t.Errorf("GetProduct returned nil object")
return
}
if product.Name != "iPhone XML" {
t.Errorf("GetProduct returned wrong product name")
}
if product.Price != 0.123 {
t.Errorf("GetProduct returned wrong price")
}
}
func TestGetInternalStoreError(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := context.Background()
store := mocks.NewMockStore(ctrl)
store.EXPECT().
Get(ctx, gomock.Eq("1")).
Return(nil, errors.New("internal error"))
domain := NewProductsDomain(store)
product, err := domain.GetProduct(ctx, "1")
if product != nil {
t.Error("Got unexpected product")
}
if err == nil {
t.Error("Expecting an error to be returned")
return
}
if err.Error() != "internal error" {
t.Errorf("Got unexpected error: %s", err)
}
}
func TestAllProductsWithInvalidNext(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := context.Background()
store := mocks.NewMockStore(ctrl)
store.EXPECT().
All(ctx, gomock.Nil()).
AnyTimes()
domain := NewProductsDomain(store)
t.Parallel()
t.Run("with nil 'next'", func(t *testing.T) {
domain.AllProducts(ctx, nil)
})
t.Run("with empty 'next'", func(t *testing.T) {
next := ""
domain.AllProducts(ctx, &next)
})
t.Run("with empty spaces 'next'", func(t *testing.T) {
next := " "
domain.AllProducts(ctx, &next)
})
}
func TestAllProductsInternalStoreError(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := context.Background()
store := mocks.NewMockStore(ctrl)
store.EXPECT().
All(ctx, gomock.All()).
Return(types.ProductRange{}, errors.New("internal error"))
domain := NewProductsDomain(store)
_, err := domain.AllProducts(ctx, nil)
if err == nil {
t.Error("Expecting an error to be returned")
return
}
if err.Error() != "internal error" {
t.Errorf("Got unexpected error: %s", err)
}
}
func TestAllProducts(t *testing.T) {
memoryStore := store.NewMemoryStore()
domain := NewProductsDomain(memoryStore)
ctx := context.Background()
t.Run("with an empty store", func(t *testing.T) {
productRange, err := domain.AllProducts(ctx, nil)
if err != nil {
t.Errorf("Got unexpected error: %w", err)
}
if len(productRange.Products) != 0 {
t.Errorf("Got unexpected products")
}
})
t.Run("with products on the store", func(t *testing.T) {
memoryStore.Put(ctx, types.Product{
Id: "iXR",
Name: "iPhone XML",
Price: 0.123,
})
productRange, err := domain.AllProducts(ctx, nil)
if err != nil {
t.Errorf("Got unexpected error: %w", err)
}
if len(productRange.Products) != 1 {
t.Errorf("Got unexpected products")
}
})
}