Skip to content

Commit f9f9ebb

Browse files
clemfromspacesamoussLoïc Sayloicsay
authored
fix(rules): Omitempty the facet values rules ordering (#702)
* fix(rules) Omitempty the of the facet values rules ordering * To ammend * Update algolia/search/rendering_content_test.go Co-authored-by: Samuel Vaillant <[email protected]> * chore: refactoring tests and docker readme * fixup! chore: refactoring tests and docker readme Co-authored-by: Samuel Vaillant <[email protected]> Co-authored-by: Loïc Say <[email protected]> Co-authored-by: Loïc Say <[email protected]>
1 parent 8a305f7 commit f9f9ebb

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

DOCKER_README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ brew install docker-machine
1818

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

2424
You may need to enter your password and authorize the application through `System Settings` > `Security & Privacy`.

algolia/search/rendering_content.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type FacetOrdering struct {
1212
Facets *FacetsOrder `json:"facets"`
1313

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

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

2929
// How to display the remaining items.
3030
SortRemainingBy *SortRule `json:"sortRemainingBy"`

algolia/search/rendering_content_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func TestUnmarshalRenderingContent(t *testing.T) {
2121
},
2222
"color": {
2323
"order": ["red", "green"]
24+
},
25+
"size": {
26+
"sortRemainingBy": "alpha"
2427
}
2528
}
2629
}
@@ -29,10 +32,91 @@ func TestUnmarshalRenderingContent(t *testing.T) {
2932
err := json.Unmarshal([]byte(payload), &r)
3033
require.NoError(t, err)
3134
require.Equal(t, r.FacetOrdering.Facets.Order, []string{"brand", "size", "color"})
35+
3236
require.Equal(t, r.FacetOrdering.Values["brand"].Order, []string{"Apple", "Sony", "Samsung"})
3337
require.Equal(t, *r.FacetOrdering.Values["brand"].SortRemainingBy, Alpha)
38+
3439
require.Equal(t, r.FacetOrdering.Values["color"].Order, []string{"red", "green"})
3540
require.Nil(t, r.FacetOrdering.Values["color"].SortRemainingBy)
41+
42+
require.Nil(t, r.FacetOrdering.Values["size"].Order)
43+
}
44+
45+
func TestMarshalRenderingContent(t *testing.T) {
46+
tests := []struct {
47+
name string
48+
input RenderingContent
49+
expected string
50+
}{
51+
{
52+
name: "empty values",
53+
input: RenderingContent{
54+
FacetOrdering: &FacetOrdering{
55+
Facets: &FacetsOrder{
56+
Order: []string{"brand", "size", "color"},
57+
},
58+
Values: nil,
59+
},
60+
},
61+
expected: `{
62+
"facetOrdering": {
63+
"facets": {
64+
"order": [
65+
"brand",
66+
"size",
67+
"color"
68+
]
69+
}
70+
}
71+
}`,
72+
},
73+
{
74+
name: "values with one empty facet value `order`",
75+
input: RenderingContent{
76+
FacetOrdering: &FacetOrdering{
77+
Facets: &FacetsOrder{
78+
Order: []string{"brand", "size", "color"},
79+
},
80+
Values: map[string]FacetValuesOrder{
81+
"brand": NewFacetValuesOrder([]string{"Apple", "Sony", "Samsung"}, Alpha),
82+
"color": NewFacetValuesOrder(nil, Hidden),
83+
},
84+
},
85+
},
86+
expected: `{
87+
"facetOrdering": {
88+
"facets": {
89+
"order": [
90+
"brand",
91+
"size",
92+
"color"
93+
]
94+
},
95+
"values": {
96+
"brand": {
97+
"order": [
98+
"Apple",
99+
"Sony",
100+
"Samsung"
101+
],
102+
"sortRemainingBy": "alpha"
103+
},
104+
"color": {
105+
"sortRemainingBy": "hidden"
106+
}
107+
}
108+
}
109+
}`,
110+
},
111+
}
112+
113+
for _, test := range tests {
114+
t.Run(test.name, func(t *testing.T) {
115+
b, err := json.Marshal(test.input)
116+
require.NoError(t, err)
117+
require.JSONEq(t, test.expected, string(b))
118+
})
119+
}
36120
}
37121

38122
func TestBuildRenderingContent(t *testing.T) {

0 commit comments

Comments
 (0)