-
Notifications
You must be signed in to change notification settings - Fork 10
/
product_test.go
66 lines (61 loc) · 1.77 KB
/
product_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
package woocommerce
import (
"errors"
"github.com/brianvoe/gofakeit/v6"
"github.com/stretchr/testify/assert"
"testing"
)
func TestProductService_All(t *testing.T) {
params := ProductsQueryParams{}
items, _, _, _, err := wooClient.Services.Product.All(params)
if err != nil {
t.Errorf("wooClient.Services.Product.All: %s", err.Error())
} else {
if len(items) > 0 {
mainId = items[0].ID
}
}
}
func TestProductService_One(t *testing.T) {
t.Run("TestProductService_All", TestProductService_All)
product, err := wooClient.Services.Product.One(mainId)
if err != nil {
t.Errorf("wooClient.Services.Product.One: %s", err.Error())
} else {
assert.Equal(t, mainId, product.ID, "product id")
}
}
func TestProductService_CreateUpdateDelete(t *testing.T) {
name := gofakeit.Word()
req := CreateProductRequest{
Name: name,
}
item, err := wooClient.Services.Product.Create(req)
if err != nil {
t.Fatalf("wooClient.Services.Product.Create error: %s", err.Error())
}
productId := item.ID
assert.Equal(t, name, item.Name, "product name")
name = gofakeit.Word()
updateReq := UpdateProductRequest{
Name: name,
}
_, err = wooClient.Services.Product.Update(productId, updateReq)
if err != nil {
t.Fatalf("wooClient.Services.Product.Update error: %s", err.Error())
}
item, err = wooClient.Services.Product.One(productId)
if err != nil {
t.Fatalf("wooClient.Services.Product.One error: %s", err.Error())
}
assert.Equal(t, name, item.Name, "product name")
// Delete
_, err = wooClient.Services.Product.Delete(productId, true)
if err != nil {
t.Fatalf("wooClient.Services.Product.Delete error: %s", err.Error())
}
_, err = wooClient.Services.Product.One(productId)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("wooClient.Services.Product.Delete(%d) failed", productId)
}
}