flamingo.me/flamingo-commerce/v3@v3.11.0/category/interfaces/controller/querycommand_test.go (about) 1 package controller_test 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "flamingo.me/flamingo-commerce/v3/category/domain/mocks" 9 "flamingo.me/flamingo-commerce/v3/category/interfaces/controller" 10 categoryMocks "flamingo.me/flamingo-commerce/v3/category/interfaces/controller/mocks" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/mock" 14 15 "flamingo.me/flamingo-commerce/v3/category/domain" 16 productApplication "flamingo.me/flamingo-commerce/v3/product/application" 17 searchDomain "flamingo.me/flamingo-commerce/v3/search/domain" 18 "flamingo.me/flamingo-commerce/v3/search/utils" 19 ) 20 21 func TestDefaultCommandHandler_Execute_SearchService(t *testing.T) { 22 type args struct { 23 categoryService domain.CategoryService 24 searchServiceResult *productApplication.SearchResult 25 searchServiceError error 26 } 27 28 categoryDataFixture := &domain.CategoryData{CategoryName: "test", CategoryCode: "test"} 29 catService := mocks.NewCategoryService(t) 30 catService.EXPECT().Tree(mock.Anything, mock.Anything).Return(&domain.TreeData{}, nil) 31 catService.EXPECT().Get(mock.Anything, mock.Anything).Return(categoryDataFixture, nil) 32 33 tests := []struct { 34 name string 35 args args 36 request controller.Request 37 wantViewData *controller.Result 38 wantViewRedirect *controller.RedirectResult 39 wantViewError error 40 }{ 41 { 42 name: "successful product search results in success response", 43 args: args{ 44 categoryService: catService, 45 searchServiceResult: &productApplication.SearchResult{}, 46 searchServiceError: nil, 47 }, 48 request: controller.Request{ 49 Code: "test", 50 Name: "test", 51 }, 52 wantViewData: &controller.Result{ 53 ProductSearchResult: &productApplication.SearchResult{}, 54 Category: categoryDataFixture, 55 CategoryTree: &domain.TreeData{}, 56 SearchMeta: searchDomain.SearchMeta{}, 57 PaginationInfo: utils.PaginationInfo{}, 58 }, 59 }, 60 { 61 name: "not found error results in not found error response", 62 args: args{ 63 categoryService: catService, 64 searchServiceResult: nil, 65 searchServiceError: searchDomain.ErrNotFound, 66 }, 67 request: controller.Request{ 68 Code: "test", 69 Name: "test", 70 }, 71 wantViewError: searchDomain.ErrNotFound, 72 }, 73 { 74 name: "error results in internal server error response", 75 args: args{ 76 categoryService: catService, 77 searchServiceResult: nil, 78 searchServiceError: errors.New("other"), 79 }, 80 request: controller.Request{ 81 Code: "test", 82 Name: "test", 83 }, 84 wantViewError: errors.New("other"), 85 }, 86 { 87 name: "redirect if name is wrong", 88 args: args{ 89 categoryService: catService, 90 searchServiceResult: nil, 91 searchServiceError: nil, 92 }, 93 request: controller.Request{ 94 Code: "test", 95 Name: "testt", 96 }, 97 wantViewRedirect: &controller.RedirectResult{Code: "test", Name: "test"}, 98 }, 99 } 100 101 for _, tt := range tests { 102 t.Run(tt.name, func(t *testing.T) { 103 104 productSearchService := new(categoryMocks.ProductSearchService) 105 productSearchService.EXPECT().Find(mock.Anything, mock.Anything).Return(tt.args.searchServiceResult, tt.args.searchServiceError) 106 107 commandHandler := controller.QueryHandlerImpl{} 108 commandHandler.Inject(tt.args.categoryService, productSearchService) 109 110 gotViewData, gotRedirect, gotError := commandHandler.Execute(context.Background(), tt.request) 111 112 a := assert.New(t) 113 114 a.Equal(tt.wantViewError, gotError) 115 a.Equal(tt.wantViewRedirect, gotRedirect) 116 a.Equal(tt.wantViewData, gotViewData) 117 }) 118 } 119 }