Skip to content

Commit

Permalink
fix(rules): Omitempty the facet values rules ordering (#702)
Browse files Browse the repository at this point in the history
* fix(rules) Omitempty the  of the facet values rules ordering

* To ammend

* Update algolia/search/rendering_content_test.go

Co-authored-by: Samuel Vaillant <samuel.vllnt@gmail.com>

* chore: refactoring tests and docker readme

* fixup! chore: refactoring tests and docker readme

Co-authored-by: Samuel Vaillant <samuel.vllnt@gmail.com>
Co-authored-by: Loïc Say <loic.say@algolia.com>
Co-authored-by: Loïc Say <loic.say@gmail.com>
  • Loading branch information
4 people authored Sep 29, 2022
1 parent 8a305f7 commit f9f9ebb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DOCKER_README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ brew install docker-machine

Then install [VirtualBox](https://www.virtualbox.org/) using [Homebrew Cask](https://github.com/Homebrew/homebrew-cask) to get a driver for your Docker machine
```
$ brew cask install virtualbox
$ brew install --cask virtualbox
```

You may need to enter your password and authorize the application through `System Settings` > `Security & Privacy`.
Expand Down
4 changes: 2 additions & 2 deletions algolia/search/rendering_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FacetOrdering struct {
Facets *FacetsOrder `json:"facets"`

// The ordering of facet values, within an individual list.
Values map[string]FacetValuesOrder `json:"values"`
Values map[string]FacetValuesOrder `json:"values,omitempty"`
}

// Facets ordering rule container
Expand All @@ -24,7 +24,7 @@ type FacetsOrder struct {
// Facet values ordering rule container
type FacetValuesOrder struct {
// Pinned order of facet values.
Order []string `json:"order"`
Order []string `json:"order,omitempty"`

// How to display the remaining items.
SortRemainingBy *SortRule `json:"sortRemainingBy"`
Expand Down
84 changes: 84 additions & 0 deletions algolia/search/rendering_content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func TestUnmarshalRenderingContent(t *testing.T) {
},
"color": {
"order": ["red", "green"]
},
"size": {
"sortRemainingBy": "alpha"
}
}
}
Expand All @@ -29,10 +32,91 @@ func TestUnmarshalRenderingContent(t *testing.T) {
err := json.Unmarshal([]byte(payload), &r)
require.NoError(t, err)
require.Equal(t, r.FacetOrdering.Facets.Order, []string{"brand", "size", "color"})

require.Equal(t, r.FacetOrdering.Values["brand"].Order, []string{"Apple", "Sony", "Samsung"})
require.Equal(t, *r.FacetOrdering.Values["brand"].SortRemainingBy, Alpha)

require.Equal(t, r.FacetOrdering.Values["color"].Order, []string{"red", "green"})
require.Nil(t, r.FacetOrdering.Values["color"].SortRemainingBy)

require.Nil(t, r.FacetOrdering.Values["size"].Order)
}

func TestMarshalRenderingContent(t *testing.T) {
tests := []struct {
name string
input RenderingContent
expected string
}{
{
name: "empty values",
input: RenderingContent{
FacetOrdering: &FacetOrdering{
Facets: &FacetsOrder{
Order: []string{"brand", "size", "color"},
},
Values: nil,
},
},
expected: `{
"facetOrdering": {
"facets": {
"order": [
"brand",
"size",
"color"
]
}
}
}`,
},
{
name: "values with one empty facet value `order`",
input: RenderingContent{
FacetOrdering: &FacetOrdering{
Facets: &FacetsOrder{
Order: []string{"brand", "size", "color"},
},
Values: map[string]FacetValuesOrder{
"brand": NewFacetValuesOrder([]string{"Apple", "Sony", "Samsung"}, Alpha),
"color": NewFacetValuesOrder(nil, Hidden),
},
},
},
expected: `{
"facetOrdering": {
"facets": {
"order": [
"brand",
"size",
"color"
]
},
"values": {
"brand": {
"order": [
"Apple",
"Sony",
"Samsung"
],
"sortRemainingBy": "alpha"
},
"color": {
"sortRemainingBy": "hidden"
}
}
}
}`,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b, err := json.Marshal(test.input)
require.NoError(t, err)
require.JSONEq(t, test.expected, string(b))
})
}
}

func TestBuildRenderingContent(t *testing.T) {
Expand Down

0 comments on commit f9f9ebb

Please sign in to comment.