Skip to content

Commit

Permalink
Merge pull request #12 from RyaWcksn/DEV-0001
Browse files Browse the repository at this point in the history
feat(Product): Add unit test
  • Loading branch information
RyaWcksn authored Apr 8, 2023
2 parents 1d80426 + 0725d1c commit 13196b3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: Pull request checking

on:
push:
pull_request:
branches: [ "master" ]
branches:
- master

jobs:
Unit testing:
Expand Down
8 changes: 7 additions & 1 deletion apis/v1/services/service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,11 @@ func (s *ServiceImpl) GetBuyerOrderList(ctx context.Context) (orderList *[]entit

// GetProducts implements IService
func (s *ServiceImpl) GetProducts(ctx context.Context) (productList *[]entities.ProductListEntity, err error) {
panic("unimplemented")
products, err := s.productImpl.GetAllProducts(ctx)
if err != nil {
s.log.Errorf("[ERR] Err from domain := %v", err)
return nil, err
}

return products, nil
}
108 changes: 88 additions & 20 deletions apis/v1/services/service_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,43 +615,111 @@ func TestServiceImpl_GetBuyerOrderList(t *testing.T) {
}

func TestServiceImpl_GetProducts(t *testing.T) {
type fields struct {
cfg configs.Config
buyerImpl repositories.IBuyer
sellerImpl repositories.ISeller
orderImpl repositories.IOrder
productImpl repositories.IProduct
log logger.ILogger
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()

productMock := repositories.NewMockIProduct(ctrl)
log := logger.New("", "", "")

cfg := configs.Cfg

ctx := context.Background()
ctx = context.WithValue(ctx, "id", "1")

type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
wantMock func()
wantProductList *[]entities.ProductListEntity
wantErr bool
}{
// TODO: Add test cases.
{
name: "Success",
args: args{
ctx: ctx,
},
wantMock: func() {
productMock.EXPECT().GetAllProducts(gomock.Any()).Return(
&[]entities.ProductListEntity{
{
Id: 1,
ProductName: "HG Dynames Gundam",
Description: "HG Dynames Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
{
Id: 2,
ProductName: "HG Kyrios Gundam",
Description: "HG Kyrios Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
{
Id: 3,
ProductName: "HG Exia Gundam",
Description: "HG Exia Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
{
Id: 4,
ProductName: "HG Virtue Gundam",
Description: "HG Virtue Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
},
nil,
)
},
wantProductList: &[]entities.ProductListEntity{
{
Id: 1,
ProductName: "HG Dynames Gundam",
Description: "HG Dynames Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
{
Id: 2,
ProductName: "HG Kyrios Gundam",
Description: "HG Kyrios Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
{
Id: 3,
ProductName: "HG Exia Gundam",
Description: "HG Exia Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
{
Id: 4,
ProductName: "HG Virtue Gundam",
Description: "HG Virtue Gundam from Kidou Senshi Gundam 00",
Price: "180000",
Seller: 1,
},
},
wantErr: false,
},
}
for _, tt := range tests {
tt.wantMock()
t.Run(tt.name, func(t *testing.T) {
s := &ServiceImpl{
cfg: tt.fields.cfg,
buyerImpl: tt.fields.buyerImpl,
sellerImpl: tt.fields.sellerImpl,
orderImpl: tt.fields.orderImpl,
productImpl: tt.fields.productImpl,
log: tt.fields.log,
}
s := NewServiceImpl().WithConfig(*cfg).WithLog(log).WithProduct(productMock)
gotProductList, err := s.GetProducts(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("ServiceImpl.GetProducts() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("ServiceImpl.GetProductsList() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotProductList, tt.wantProductList) {
t.Errorf("ServiceImpl.GetProducts() = %v, want %v", gotProductList, tt.wantProductList)
t.Errorf("ServiceImpl.GetProductsList() = %v, want %v", gotProductList, tt.wantProductList)
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion cov.out
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:173.2,173.19 1 1
github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:177.103,182.16 4 1
github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:182.16,185.3 2 0
github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:187.2,187.20 1 1
github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:191.111,192.24 1 0
github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:191.111,193.16 2 1
github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:193.16,196.3 2 0
github.com/RyaWcksn/ecommerce/apis/v1/services/service_impl.go:198.2,198.22 1 1
github.com/RyaWcksn/ecommerce/apis/v1/services/service_mock.go:28.61,32.2 3 0
github.com/RyaWcksn/ecommerce/apis/v1/services/service_mock.go:35.59,37.2 1 0
github.com/RyaWcksn/ecommerce/apis/v1/services/service_mock.go:40.121,46.2 5 0
Expand Down

0 comments on commit 13196b3

Please sign in to comment.