-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreateProduct_test.go
97 lines (71 loc) · 3 KB
/
createProduct_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
package service
import (
"errors"
"testing"
"github.com/sebajax/go-vertical-slice-architecture/internal/product"
"github.com/sebajax/go-vertical-slice-architecture/internal/product/mock"
"github.com/stretchr/testify/assert"
)
func TestCreateProductService_CreateProduct_Success(t *testing.T) {
var insertedId int64 = 1
// Create a new Product
p, _ := product.New("test_product", "test_sku", product.Laptop, 1200)
// Create a mock ProductRepository instance
mockProductRepository := &mock.ProductRepository{}
// Set expectations on the mock
mockProductRepository.EXPECT().GetBySku(p.Sku).Return(nil, false, nil)
mockProductRepository.EXPECT().Save(p).Return(insertedId, nil)
// Pass the mock as a dependency
productService := NewCreateProductService(mockProductRepository)
// Call the method being tested
id, err := productService.CreateProduct(p)
// Assert the result
assert.NoError(t, err)
assert.Equal(t, insertedId, id)
// Assert that the method was called exactly once or not called
mockProductRepository.AssertCalled(t, "GetBySku", p.Sku)
mockProductRepository.AssertCalled(t, "Save", p)
// Assert that the expectations were met
mockProductRepository.AssertExpectations(t)
}
func TestCreateProductService_CreateProduct_GetBySku_Failure(t *testing.T) {
// Create a new Product
p, _ := product.New("test_product", "test_sku", product.Laptop, 1200)
// Create a mock ProductRepository instance
mockProductRepository := &mock.ProductRepository{}
// Set expectations on the mock
mockProductRepository.EXPECT().GetBySku(p.Sku).Return(p, true, nil)
// Pass the mock as a dependency
productService := NewCreateProductService(mockProductRepository)
// Call the method being tested
id, err := productService.CreateProduct(p)
// Assert the result
assert.Error(t, err)
assert.Equal(t, int64(0), id)
// Assert that the method was called exactly once or not called
mockProductRepository.AssertCalled(t, "GetBySku", p.Sku)
mockProductRepository.AssertNotCalled(t, "Save")
// Assert that the expectations were met
mockProductRepository.AssertExpectations(t)
}
func TestCreateProductService_CreateProduct_Save_Failure(t *testing.T) {
// Create a new Product
p, _ := product.New("test_product", "test_sku", product.Laptop, 1200)
// Create a mock ProductRepository instance
mockProductRepository := &mock.ProductRepository{}
// Set expectations on the mock
mockProductRepository.EXPECT().GetBySku(p.Sku).Return(nil, false, nil)
mockProductRepository.EXPECT().Save(p).Return(0, errors.New("DB ERROR"))
// Pass the mock as a dependency
productService := NewCreateProductService(mockProductRepository)
// Call the method being tested
id, err := productService.CreateProduct(p)
// Assert the result
assert.Error(t, err)
assert.Equal(t, int64(0), id)
// Assert that the method was called exactly once or not called
mockProductRepository.AssertCalled(t, "GetBySku", p.Sku)
mockProductRepository.AssertCalled(t, "Save", p)
// Assert that the expectations were met
mockProductRepository.AssertExpectations(t)
}