fix: handle rating-count rendering fallback and add regressions
Some checks failed
ci / test (push) Has been cancelled

This commit is contained in:
Peter Steinberger 2026-02-14 05:11:52 +01:00
parent de52de72c1
commit 39a4f09700
6 changed files with 60 additions and 24 deletions

1
.gitignore vendored
View File

@ -47,4 +47,3 @@ go.work.sum
bin/
dist/
tmp/
goplaces-patched

View File

@ -44,6 +44,7 @@ func TestSearchSuccess(t *testing.T) {
"formattedAddress": "123 Street",
"location": {"latitude": 1.23, "longitude": 4.56},
"rating": 4.7,
"userRatingCount": 532,
"priceLevel": "PRICE_LEVEL_MODERATE",
"types": ["cafe"],
"currentOpeningHours": {"openNow": true}
@ -95,6 +96,9 @@ func TestSearchSuccess(t *testing.T) {
if result.PriceLevel == nil || *result.PriceLevel != 2 {
t.Fatalf("unexpected price level: %#v", result.PriceLevel)
}
if result.UserRatingCount == nil || *result.UserRatingCount != 532 {
t.Fatalf("unexpected user rating count: %#v", result.UserRatingCount)
}
if result.OpenNow == nil || *result.OpenNow != true {
t.Fatalf("unexpected openNow: %#v", result.OpenNow)
}
@ -287,6 +291,7 @@ func TestNearbySearchSuccess(t *testing.T) {
"formattedAddress": "123 Street",
"location": {"latitude": 1.23, "longitude": 4.56},
"rating": 4.7,
"userRatingCount": 42,
"priceLevel": "PRICE_LEVEL_MODERATE",
"types": ["cafe"],
"currentOpeningHours": {"openNow": true}
@ -312,6 +317,9 @@ func TestNearbySearchSuccess(t *testing.T) {
if len(response.Results) != 1 {
t.Fatalf("expected 1 result, got %d", len(response.Results))
}
if response.Results[0].UserRatingCount == nil || *response.Results[0].UserRatingCount != 42 {
t.Fatalf("unexpected user rating count: %#v", response.Results[0].UserRatingCount)
}
if response.NextPageToken != "next" {
t.Fatalf("unexpected token: %s", response.NextPageToken)
}
@ -383,6 +391,7 @@ func TestDetailsSuccess(t *testing.T) {
"formattedAddress": "Central",
"location": {"latitude": 10, "longitude": 20},
"rating": 4.2,
"userRatingCount": 1234,
"priceLevel": "PRICE_LEVEL_FREE",
"types": ["park"],
"regularOpeningHours": {"weekdayDescriptions": ["Mon: 9-5"]},
@ -405,6 +414,9 @@ func TestDetailsSuccess(t *testing.T) {
if place.PlaceID != "place-123" {
t.Fatalf("unexpected id: %s", place.PlaceID)
}
if place.UserRatingCount == nil || *place.UserRatingCount != 1234 {
t.Fatalf("unexpected user rating count: %#v", place.UserRatingCount)
}
if place.OpenNow == nil || *place.OpenNow != false {
t.Fatalf("unexpected openNow")
}

View File

@ -68,12 +68,12 @@ func mapPlaceDetails(place placeItem) PlaceDetails {
Rating: place.Rating,
UserRatingCount: place.UserRatingCount,
PriceLevel: mapPriceLevel(place.PriceLevel),
Types: place.Types,
Phone: place.NationalPhoneNumber,
Website: place.WebsiteURI,
Hours: weekdayDescriptions(place.RegularOpeningHours),
OpenNow: openNow(place.CurrentOpeningHours),
Reviews: mapReviews(place.Reviews),
Photos: mapPhotos(place.Photos),
Types: place.Types,
Phone: place.NationalPhoneNumber,
Website: place.WebsiteURI,
Hours: weekdayDescriptions(place.RegularOpeningHours),
OpenNow: openNow(place.CurrentOpeningHours),
Reviews: mapReviews(place.Reviews),
Photos: mapPhotos(place.Photos),
}
}

View File

@ -301,7 +301,7 @@ func writeLocation(out *bytes.Buffer, color Color, loc *goplaces.LatLng) {
}
func writeRating(out *bytes.Buffer, color Color, rating *float64, userRatingCount *int, priceLevel *int) {
if rating == nil && priceLevel == nil {
if rating == nil && userRatingCount == nil && priceLevel == nil {
return
}
parts := make([]string, 0, 3)
@ -311,6 +311,8 @@ func writeRating(out *bytes.Buffer, color Color, rating *float64, userRatingCoun
ratingStr += fmt.Sprintf(" (%d)", *userRatingCount)
}
parts = append(parts, ratingStr)
} else if userRatingCount != nil {
parts = append(parts, fmt.Sprintf("%d ratings", *userRatingCount))
}
if priceLevel != nil {
parts = append(parts, fmt.Sprintf("$%d", *priceLevel))

View File

@ -12,17 +12,19 @@ import (
func TestRenderSearch(t *testing.T) {
open := true
level := 2
ratingCount := 532
response := goplaces.SearchResponse{
Results: []goplaces.PlaceSummary{
{
PlaceID: "abc",
Name: "Cafe",
Address: "123 Street",
Location: &goplaces.LatLng{Lat: 1, Lng: 2},
Rating: floatPtr(4.5),
PriceLevel: &level,
Types: []string{"cafe", "coffee_shop"},
OpenNow: &open,
PlaceID: "abc",
Name: "Cafe",
Address: "123 Street",
Location: &goplaces.LatLng{Lat: 1, Lng: 2},
Rating: floatPtr(4.5),
UserRatingCount: &ratingCount,
PriceLevel: &level,
Types: []string{"cafe", "coffee_shop"},
OpenNow: &open,
},
},
NextPageToken: "next",
@ -35,6 +37,9 @@ func TestRenderSearch(t *testing.T) {
if !strings.Contains(output, "Rating") {
t.Fatalf("missing rating")
}
if !strings.Contains(output, "4.5 (532)") {
t.Fatalf("missing rating count")
}
if !strings.Contains(output, "Open now") {
t.Fatalf("missing open now")
}
@ -43,6 +48,24 @@ func TestRenderSearch(t *testing.T) {
}
}
func TestRenderSearchRatingCountOnly(t *testing.T) {
ratingCount := 12
response := goplaces.SearchResponse{
Results: []goplaces.PlaceSummary{
{
PlaceID: "abc",
Name: "Cafe",
UserRatingCount: &ratingCount,
},
},
}
output := renderSearch(NewColor(false), response)
if !strings.Contains(output, "12 ratings") {
t.Fatalf("missing rating count-only output: %s", output)
}
}
func TestRenderSearchEmpty(t *testing.T) {
output := renderSearch(NewColor(false), goplaces.SearchResponse{})
if !strings.Contains(output, "No results") {

View File

@ -104,13 +104,13 @@ type PlaceDetails struct {
Rating *float64 `json:"rating,omitempty"`
UserRatingCount *int `json:"user_rating_count,omitempty"`
PriceLevel *int `json:"price_level,omitempty"`
Types []string `json:"types,omitempty"`
Phone string `json:"phone,omitempty"`
Website string `json:"website,omitempty"`
Hours []string `json:"hours,omitempty"`
OpenNow *bool `json:"open_now,omitempty"`
Reviews []Review `json:"reviews,omitempty"`
Photos []Photo `json:"photos,omitempty"`
Types []string `json:"types,omitempty"`
Phone string `json:"phone,omitempty"`
Website string `json:"website,omitempty"`
Hours []string `json:"hours,omitempty"`
OpenNow *bool `json:"open_now,omitempty"`
Reviews []Review `json:"reviews,omitempty"`
Photos []Photo `json:"photos,omitempty"`
}
// LocationResolveRequest resolves a text location into place candidates.