Skip to content

Commit

Permalink
fix(product): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMaidurov committed Aug 1, 2024
1 parent f262ecc commit c41e5bb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
16 changes: 12 additions & 4 deletions product/infrastructure/fake/searchservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *SearchService) Search(ctx context.Context, filters ...searchDomain.Filt
NumPages: 10,
NumResults: len(hits),
SelectedFacets: selectedFacets,
SortOptions: mapSortOptions(s.sortConfig),
SortOptions: mapSortOptions(s.sortConfig, filters),
},
Hits: documents,
Suggestion: []searchDomain.Suggestion{},
Expand Down Expand Up @@ -183,7 +183,15 @@ func (s *SearchService) findCurrentPage(filters []searchDomain.Filter) int {
}

// mapSortOptions maps searchperience delivered sort options to corresponding domain objects.
func mapSortOptions(sortConfigs []sortConfig) []searchDomain.SortOption {
func mapSortOptions(sortConfigs []sortConfig, filters []searchDomain.Filter) []searchDomain.SortOption {
lookup := make(map[string]bool, 1) // only one field expected

for _, filter := range filters {
if sortFiler, ok := filter.(*searchDomain.SortFilter); ok {
lookup[sortFiler.Field()] = true // direction always true for that filter name
}
}

result := make([]searchDomain.SortOption, len(sortConfigs))

for i, sortConfig := range sortConfigs {
Expand All @@ -192,8 +200,8 @@ func mapSortOptions(sortConfigs []sortConfig) []searchDomain.SortOption {
Field: sortConfig.Key,
Asc: sortConfig.Asc,
Desc: sortConfig.Desc,
SelectedAsc: false,
SelectedDesc: true,
SelectedAsc: lookup[sortConfig.Asc],
SelectedDesc: lookup[sortConfig.Desc],
}
}

Expand Down
48 changes: 39 additions & 9 deletions product/infrastructure/fake/searchservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@ import (
"path/filepath"
"testing"

"flamingo.me/flamingo/v3/framework/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"flamingo.me/flamingo-commerce/v3/product/domain"
"flamingo.me/flamingo-commerce/v3/product/infrastructure/fake"
searchDomain "flamingo.me/flamingo-commerce/v3/search/domain"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type (
testSortConfig struct {
Key string
Label string
Asc string
Desc string
}
)

func TestSearchService_Search(t *testing.T) {
s := fake.SearchService{}
s.Inject(&fake.ProductService{}, &struct {
LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"`
CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"`
LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"`
CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"`
SortConfig config.Slice `inject:"config:commerce.product.fakeservice.sorting"`
}{})

t.Run("Category Facet", func(t *testing.T) {
Expand Down Expand Up @@ -150,9 +162,9 @@ func TestSearchService_SearchBy(t *testing.T) {
NumResults: 0,
SelectedFacets: []searchDomain.Facet{},
SortOptions: []searchDomain.SortOption{
{Field: "camera", Label: "camera", SelectedDesc: false, SelectedAsc: true},
{Field: "size", Label: "size", SelectedDesc: true, SelectedAsc: false},
{Field: "no-direction", Label: "no-direction", SelectedDesc: false, SelectedAsc: true},
{Field: "camera", Label: "camera", SelectedDesc: false, SelectedAsc: true, Asc: "camera"},
{Field: "size", Label: "size", SelectedDesc: true, SelectedAsc: false, Desc: "size"},
{Field: "no-direction", Label: "no-direction", SelectedDesc: false, SelectedAsc: true, Asc: "no-direction"},
},
},
Hits: []searchDomain.Document{},
Expand Down Expand Up @@ -258,10 +270,28 @@ func TestSearchService_SearchBy(t *testing.T) {
s := new(fake.SearchService).Inject(
new(fake.ProductService),
&struct {
LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"`
CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"`
LiveSearchJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataLiveSearch,optional"`
CategoryFacetItemsJSON string `inject:"config:commerce.product.fakeservice.jsonTestDataCategoryFacetItems,optional"`
SortConfig config.Slice `inject:"config:commerce.product.fakeservice.sorting"`
}{
LiveSearchJSON: tt.fields.liveSearchJSON,
SortConfig: config.Slice{
testSortConfig{
Key: "camera",
Label: "camera",
Asc: "camera",
},
testSortConfig{
Key: "size",
Label: "size",
Desc: "size",
},
testSortConfig{
Key: "no-direction",
Label: "no-direction",
Asc: "no-direction",
},
},
},
)
got, err := s.SearchBy(context.Background(), tt.args.attribute, nil, tt.args.filters...)
Expand Down
4 changes: 2 additions & 2 deletions search/domain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ type (
SelectedDesc bool
// Asc - represents the field that is used to trigger ascending search.
// Deprecated: use "Field" and "SelectedAsc" instead to set which field should be sortable
Asc string
Asc string // Should it be deprecated ?? marked as deprecated in 2019 and used in 2020
// Desc - represents the field that is used to trigger descending search.
// Deprecated: use "Field" and "SelectedDesc" instead to set which field should be sortable
Desc string
Desc string // Should it be deprecated ?? marked as deprecated in 2019 and used in 2020
}

// FacetType for type facets
Expand Down

0 comments on commit c41e5bb

Please sign in to comment.