From a701d13cd1570bf1b8277b5e2cb30609793ae3bb Mon Sep 17 00:00:00 2001 From: tomaytotomato <971697+tomaytotomato@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:52:01 +0100 Subject: [PATCH] Fixed DefaultTextNormaliser and added more tests --- .../tomaytotomato/SearchLocationService.java | 26 ++++++++++++++++++- .../normaliser/DefaultTextNormaliser.java | 5 +++- .../normaliser/DefaultTextNormaliserTest.java | 6 ++--- .../usecase/SearchLocationTest.java | 22 ++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/tomaytotomato/SearchLocationService.java b/src/main/java/com/tomaytotomato/SearchLocationService.java index 5487e16..6c86a3e 100644 --- a/src/main/java/com/tomaytotomato/SearchLocationService.java +++ b/src/main/java/com/tomaytotomato/SearchLocationService.java @@ -419,7 +419,31 @@ private List getTopMatchingLocations(Map 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()); } } diff --git a/src/main/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliser.java b/src/main/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliser.java index 3d5790a..5d56b6b 100644 --- a/src/main/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliser.java +++ b/src/main/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliser.java @@ -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+", " "); } } diff --git a/src/test/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliserTest.java b/src/test/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliserTest.java index 6febd5f..94e6ebc 100644 --- a/src/test/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliserTest.java +++ b/src/test/java/com/tomaytotomato/text/normaliser/DefaultTextNormaliserTest.java @@ -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"); } } diff --git a/src/test/java/com/tomaytotomato/usecase/SearchLocationTest.java b/src/test/java/com/tomaytotomato/usecase/SearchLocationTest.java index baa52ef..454c1f8 100644 --- a/src/test/java/com/tomaytotomato/usecase/SearchLocationTest.java +++ b/src/test/java/com/tomaytotomato/usecase/SearchLocationTest.java @@ -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")); + } } \ No newline at end of file