Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 0d05b04

Browse files
authored
refactor(Query): Unify Boolean attributes' handling and testing (#552)
## Boolean attributes handling Although we first thought it would consist in `Move Boolean to boolean in the client`, exploration showed that we need to stick to `Boolean` instead: - Query's boolean attributes can either be _unset_, _true_ or _false_ - The default for each attribute is _not something we can rely on in the library_ (although sometimes it's mentioned in the docstring) Consequently, we need our boolean getters to return either of three values: `Boolean.TRUE`, `Boolean.FALSE`, and `null` if unset. This means **getters should be implemented as `public @nullable Boolean getXXX()`**. Likewise, the user should be able to unset an attribute's value, putting the `Query` in its default state for that attribute. This means **setters should be implemented as `public @nonnull Query setXXX(@nullable Boolean xxx);`**. This is implemented in 530c577, which refactors setters that were not compliant with this reasoning; and 68a955c, which adds appropriate annotations. ## Boolean attributes testing Testing of our boolean attributes was inconsistent, sometimes testing `TRUE` and `FALSE` cases and sometimes only one of them. Adding a test for the `null` case was an opportunity to refactor those. I went the following way: - Extract the Boolean testing logic into [`testBooleanAttribute`](https://github.com/algolia/algoliasearch-client-android/blob/39a4dee0c71eac1db2f8a4a6a53905f1713e9a73/algoliasearch/src/test/java/com/algolia/search/saas/QueryTest.java#L874-L916), a generic method testing all cases of Boolean values for a given attribute - Refactor Boolean tests to always use `testBooleanAttribute`. This makes the tests much more concise, more consistent, without any significant performance drawback (runnning 5 times `QueryTest` before and after this change results in a mean difference of `0.12`s). - Reorganize tests into [regions for attribute types](https://github.com/algolia/algoliasearch-client-android/blob/39a4dee0c71eac1db2f8a4a6a53905f1713e9a73/algoliasearch/src/test/java/com/algolia/search/saas/QueryTest.java#L116). If we like the benefits of this new structure, I would apply it to each other attribute type. - For mixed type attributes that can be Boolean (currently `ignorePlurals`), add assertions for the `null` case and keep it unchanged until further refactoring.
1 parent bab7e95 commit 0d05b04

File tree

3 files changed

+166
-176
lines changed

3 files changed

+166
-176
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/Query.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,12 @@ public Query(@NonNull Query other) {
289289
* search but not engine.
290290
*/
291291
public @NonNull
292-
Query setAdvancedSyntax(Boolean enabled) {
292+
Query setAdvancedSyntax(@Nullable Boolean enabled) {
293293
return set(KEY_ADVANCED_SYNTAX, enabled);
294294
}
295295

296-
public Boolean getAdvancedSyntax() {
296+
public @Nullable
297+
Boolean getAdvancedSyntax() {
297298
return parseBoolean(get(KEY_ADVANCED_SYNTAX));
298299
}
299300

@@ -304,11 +305,12 @@ public Boolean getAdvancedSyntax() {
304305
* Defaults to true.
305306
*/
306307
public @NonNull
307-
Query setAllowTyposOnNumericTokens(Boolean enabled) {
308+
Query setAllowTyposOnNumericTokens(@Nullable Boolean enabled) {
308309
return set(KEY_ALLOW_TYPOS_ON_NUMERIC_TOKENS, enabled);
309310
}
310311

311-
public Boolean getAllowTyposOnNumericTokens() {
312+
public @Nullable
313+
Boolean getAllowTyposOnNumericTokens() {
312314
return parseBoolean(get(KEY_ALLOW_TYPOS_ON_NUMERIC_TOKENS));
313315
}
314316

@@ -318,11 +320,12 @@ public Boolean getAllowTyposOnNumericTokens() {
318320
* @param enabled If set to true, the results will return queryID which is needed for sending click | conversion events. Defaults to false.
319321
*/
320322
public @NonNull
321-
Query setClickAnalytics(Boolean enabled) {
323+
Query setClickAnalytics(@Nullable Boolean enabled) {
322324
return set(KEY_CLICK_ANALYTICS, enabled);
323325
}
324326

325-
public Boolean getClickAnalytics() {
327+
public @Nullable
328+
Boolean getClickAnalytics() {
326329
return parseBoolean(get(KEY_CLICK_ANALYTICS));
327330
}
328331

@@ -333,11 +336,12 @@ public Boolean getClickAnalytics() {
333336
* analytics feature. Defaults to true.
334337
*/
335338
public @NonNull
336-
Query setAnalytics(Boolean enabled) {
339+
Query setAnalytics(@Nullable Boolean enabled) {
337340
return set(KEY_ANALYTICS, enabled);
338341
}
339342

340-
public Boolean getAnalytics() {
343+
public @Nullable
344+
Boolean getAnalytics() {
341345
return parseBoolean(get(KEY_ANALYTICS));
342346
}
343347

@@ -381,11 +385,12 @@ LatLng getAroundLatLng() {
381385
* geolocation)
382386
*/
383387
public @NonNull
384-
Query setAroundLatLngViaIP(Boolean enabled) {
388+
Query setAroundLatLngViaIP(@Nullable Boolean enabled) {
385389
return set(KEY_AROUND_LAT_LNG_VIA_IP, enabled);
386390
}
387391

388-
public Boolean getAroundLatLngViaIP() {
392+
public @Nullable
393+
Boolean getAroundLatLngViaIP() {
389394
return parseBoolean(get(KEY_AROUND_LAT_LNG_VIA_IP));
390395
}
391396

@@ -611,7 +616,7 @@ Boolean getFacetingAfterDistinct() {
611616
* @see <a href="https://www.algolia.com/doc/api-client/android/parameters/#facetingafterdistinct">facetingAfterDistinct's documentation</a>
612617
*/
613618
public @NonNull
614-
Query setFacetingAfterDistinct(Boolean enabled) {
619+
Query setFacetingAfterDistinct(@Nullable Boolean enabled) {
615620
return set(KEY_FACETING_AFTER_DISTINCT, enabled);
616621
}
617622

@@ -648,11 +653,11 @@ String getFilters() {
648653
* attribute.
649654
*/
650655
public @NonNull
651-
Query setGetRankingInfo(Boolean enabled) {
656+
Query setGetRankingInfo(@Nullable Boolean enabled) {
652657
return set(KEY_GET_RANKING_INFO, enabled);
653658
}
654659

655-
public Boolean getGetRankingInfo() {
660+
public @Nullable Boolean getGetRankingInfo() {
656661
return parseBoolean(get(KEY_GET_RANKING_INFO));
657662
}
658663

@@ -820,7 +825,7 @@ public int hashCode() {
820825
* car/cars will be considered as equals). Defaults to false.
821826
*/
822827
public @NonNull
823-
Query setIgnorePlurals(boolean enabled) {
828+
Query setIgnorePlurals(@Nullable Boolean enabled) {
824829
return set(KEY_IGNORE_PLURALS, enabled);
825830
}
826831

@@ -1277,11 +1282,11 @@ public Integer getPage() {
12771282
* Algolia dashboard. When `false`, the search query is excluded from percentile computation.
12781283
*/
12791284
public @NonNull
1280-
Query setPercentileComputation(boolean enabled) {
1285+
Query setPercentileComputation(@Nullable Boolean enabled) {
12811286
return set(KEY_PERCENTILE_COMPUTATION, enabled);
12821287
}
12831288

1284-
public Boolean getPercentileComputation() {
1289+
public @Nullable Boolean getPercentileComputation() {
12851290
return parseBoolean(get(KEY_PERCENTILE_COMPUTATION));
12861291
}
12871292

@@ -1372,11 +1377,12 @@ RemoveWordsIfNoResults getRemoveWordsIfNoResults() {
13721377
* to true.
13731378
*/
13741379
public @NonNull
1375-
Query setReplaceSynonymsInHighlight(Boolean enabled) {
1380+
Query setReplaceSynonymsInHighlight(@Nullable Boolean enabled) {
13761381
return set(KEY_REPLACE_SYNONYMS_IN_HIGHLIGHT, enabled);
13771382
}
13781383

1379-
public Boolean getReplaceSynonymsInHighlight() {
1384+
public @Nullable
1385+
Boolean getReplaceSynonymsInHighlight() {
13801386
return parseBoolean(get(KEY_REPLACE_SYNONYMS_IN_HIGHLIGHT));
13811387
}
13821388

@@ -1389,11 +1395,12 @@ public Boolean getReplaceSynonymsInHighlight() {
13891395
* only array items that matched at least partially are highlighted/snippeted.
13901396
*/
13911397
public @NonNull
1392-
Query setRestrictHighlightAndSnippetArrays(boolean restrict) {
1398+
Query setRestrictHighlightAndSnippetArrays(@Nullable Boolean restrict) {
13931399
return set(KEY_RESTRICT_HIGHLIGHT_AND_SNIPPET, restrict);
13941400
}
13951401

1396-
public Boolean getRestrictHighlightAndSnippetArrays() {
1402+
public @Nullable
1403+
Boolean getRestrictHighlightAndSnippetArrays() {
13971404
return parseBoolean(get(KEY_RESTRICT_HIGHLIGHT_AND_SNIPPET));
13981405
}
13991406

@@ -1476,11 +1483,12 @@ public SortFacetValuesBy getSortFacetValuesBy() {
14761483
* @param enabled False means that the total score of a record is the maximum score of an individual filter. Setting it to true changes the total score by adding together the scores of each filter found. Defaults to false.
14771484
*/
14781485
public @NonNull
1479-
Query setSumOrFiltersScores(Boolean enabled) {
1486+
Query setSumOrFiltersScores(@Nullable Boolean enabled) {
14801487
return set(KEY_SUM_OR_FILTERS_SCORES, enabled);
14811488
}
14821489

1483-
public Boolean getSumOrFiltersScores() {
1490+
public @Nullable
1491+
Boolean getSumOrFiltersScores() {
14841492
return parseBoolean(get(KEY_SUM_OR_FILTERS_SCORES));
14851493
}
14861494

@@ -1491,11 +1499,12 @@ public Boolean getSumOrFiltersScores() {
14911499
* configuration. Defaults to true.
14921500
*/
14931501
public @NonNull
1494-
Query setSynonyms(Boolean enabled) {
1502+
Query setSynonyms(@Nullable Boolean enabled) {
14951503
return set(KEY_SYNONYMS, enabled);
14961504
}
14971505

1498-
public Boolean getSynonyms() {
1506+
public @Nullable
1507+
Boolean getSynonyms() {
14991508
return parseBoolean(get(KEY_SYNONYMS));
15001509
}
15011510

@@ -1554,11 +1563,12 @@ ExactOnSingleWordQuery getExactOnSingleWordQuery() {
15541563
* Defaults to true.
15551564
*/
15561565
public @NonNull
1557-
Query setEnableRules(Boolean enabled) {
1566+
Query setEnableRules(@Nullable Boolean enabled) {
15581567
return set(KEY_ENABLE_RULES, enabled);
15591568
}
15601569

1561-
public Boolean getEnableRules() {
1570+
public @Nullable
1571+
Boolean getEnableRules() {
15621572
return parseBoolean(get(KEY_ENABLE_RULES));
15631573
}
15641574

algoliasearch/src/main/java/com/algolia/search/saas/places/PlacesQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ PlacesQuery setAroundLatLngViaIP(Boolean enabled) {
115115
return set(KEY_AROUND_LAT_LNG_VIA_IP, enabled);
116116
}
117117

118-
public Boolean getAroundLatLngViaIP() {
118+
public @Nullable Boolean getAroundLatLngViaIP() {
119119
return parseBoolean(get(KEY_AROUND_LAT_LNG_VIA_IP));
120120
}
121121

0 commit comments

Comments
 (0)