test: add e2e coverage

This commit is contained in:
Peter Steinberger 2026-01-02 19:42:53 +01:00
parent 2bca928e4e
commit 79f853c8bd
3 changed files with 90 additions and 0 deletions

View File

@ -1,4 +1,5 @@
.PHONY: lint test coverage
.PHONY: e2e
lint:
golangci-lint fmt
@ -9,3 +10,6 @@ test:
coverage:
./scripts/check-coverage.sh
e2e:
go test -tags=e2e ./... -run TestE2E

View File

@ -102,3 +102,19 @@ Enforce coverage:
```bash
./scripts/check-coverage.sh
```
### E2E tests (optional)
Real API tests (skipped unless you opt in):
```bash
export GOOGLE_PLACES_API_KEY="..."
go test -tags=e2e ./... -run TestE2E
```
Optional env overrides:
- `GOOGLE_PLACES_E2E_BASE_URL`
- `GOOGLE_PLACES_E2E_QUERY`
- `GOOGLE_PLACES_E2E_LANGUAGE`
- `GOOGLE_PLACES_E2E_REGION`

70
e2e_test.go Normal file
View File

@ -0,0 +1,70 @@
//go:build e2e
// +build e2e
package goplaces
import (
"context"
"os"
"testing"
"time"
)
func TestE2ESearchAndDetails(t *testing.T) {
apiKey := os.Getenv("GOOGLE_PLACES_API_KEY")
if apiKey == "" {
t.Skip("GOOGLE_PLACES_API_KEY not set")
}
query := os.Getenv("GOOGLE_PLACES_E2E_QUERY")
if query == "" {
query = "coffee in Seattle"
}
language := os.Getenv("GOOGLE_PLACES_E2E_LANGUAGE")
if language == "" {
language = "en"
}
region := os.Getenv("GOOGLE_PLACES_E2E_REGION")
if region == "" {
region = "US"
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
client := NewClient(Options{
APIKey: apiKey,
BaseURL: os.Getenv("GOOGLE_PLACES_E2E_BASE_URL"),
Timeout: 10 * time.Second,
})
search, err := client.Search(ctx, SearchRequest{
Query: query,
Limit: 1,
Language: language,
Region: region,
})
if err != nil {
t.Fatalf("search error: %v", err)
}
if len(search.Results) == 0 {
t.Fatalf("expected search results")
}
placeID := search.Results[0].PlaceID
if placeID == "" {
t.Fatalf("expected place id")
}
details, err := client.DetailsWithOptions(ctx, DetailsRequest{
PlaceID: placeID,
Language: language,
Region: region,
})
if err != nil {
t.Fatalf("details error: %v", err)
}
if details.PlaceID == "" {
t.Fatalf("expected details place id")
}
}