Skip to content

Commit

Permalink
Fixed DefaultTextNormaliser and added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaytotomato committed Jul 26, 2024
1 parent 530b2be commit a701d13
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
26 changes: 25 additions & 1 deletion src/main/java/com/tomaytotomato/SearchLocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,31 @@ private List<Location> getTopMatchingLocations(Map<Country, Integer> countryHits
return List.of(locationMapper.toLocation(topState));
}
} else {
return List.of(locationMapper.toLocation(topCountry));
var locationBuilder = Location.builder();

locationBuilder.countryName(topCountry.getName());
locationBuilder.countryId(topCountry.getId());
locationBuilder.countryIso2Code(topCountry.getIso2());
locationBuilder.countryIso3Code(topCountry.getIso3());
locationBuilder.latitude(topCountry.getLatitude());
locationBuilder.longitude(topCountry.getLongitude());

if (!Objects.isNull(topState)) {
locationBuilder.stateName(topState.getName());
locationBuilder.stateCode(topState.getStateCode());
locationBuilder.stateId(topState.getId());
locationBuilder.latitude(topState.getLatitude());
locationBuilder.longitude(topState.getLongitude());
}

if (!Objects.isNull(topCity)) {
locationBuilder.city(topCity.getName());
locationBuilder.cityId(topCity.getId());
locationBuilder.latitude(topCity.getLatitude());
locationBuilder.longitude(topCity.getLongitude());
}

return List.of(locationBuilder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public String normalise(String text) {
if (Objects.isNull(text) || text.isEmpty()) {
throw new IllegalArgumentException("Text cannot be null or empty");
}
return text.trim().toLowerCase().replaceAll("\\s+", " ");
return text.trim()
.toLowerCase()
.replaceAll("\\p{Punct}", "")
.replaceAll("\\s+", " ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ void normalise_WhenTextHasMultipleSpacesBetweenWords_ThenReturnLowercasedTextWit
assertThat(result).isEqualTo("hello world");
}

@Description("Normalise, when text contains punctuation, then return lowercased text with punctuation intact")
@Description("Normalise, when text contains punctuation, then return lowercased text without punctuation")
@Test
void normalise_WhenTextContainsPunctuation_ThenReturnLowercasedTextWithPunctuationIntact() {
// When
var result = textNormaliser.normalise("Hello, World!");
var result = textNormaliser.normalise("San Francisco, CA, USA");

// Then
assertThat(result).isEqualTo("hello, world!");
assertThat(result).isEqualTo("san francisco ca usa");
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/tomaytotomato/usecase/SearchLocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,26 @@ void search_WhenTextContainsStateAndCountryName_ThenReturnSingleMatch(String tex
.extracting("countryName")
.containsOnly(countryName);
}


@Test
void search_WhenTextContainsCountryStateandCityName_ThenReturnSingleMatch() {

// When Then
assertThat(searchLocationService.search("San Francisco, CA, USA")).isNotEmpty().hasSize(1)
.extracting("countryName", "countryIso2Code", "countryIso3Code", "stateName", "city")
.containsExactly(tuple("United States", "US", "USA", "California", "San Francisco"));

assertThat(searchLocationService.search("KY, Glasgow USA")).isNotEmpty().hasSize(1)
.extracting("countryName", "countryIso2Code", "countryIso3Code", "stateName", "city")
.containsExactly(tuple("United States", "US", "USA", "Kentucky", "Glasgow"));

assertThat(searchLocationService.search("Glasgow Kentucky USA")).isNotEmpty().hasSize(1)
.extracting("countryName", "countryIso2Code", "countryIso3Code", "stateName", "city")
.containsExactly(tuple("United States", "US", "USA", "Kentucky", "Glasgow"));

assertThat(searchLocationService.search("Glasgow Scotland")).isNotEmpty().hasSize(1)
.extracting("countryName", "countryIso2Code", "countryIso3Code", "stateName", "city")
.containsExactly(tuple("United Kingdom", "GB", "GBR", "Scotland", "Glasgow"));
}
}

0 comments on commit a701d13

Please sign in to comment.