Skip to content

Commit

Permalink
feat: add search, implement autosuggest endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rkettelerij committed Dec 3, 2024
1 parent 653568a commit b9a94fa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
13 changes: 4 additions & 9 deletions internal/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ func NewSearch(e *engine.Engine, dbConn string, searchIndex string) *Search {
// Suggest autosuggest locations based on user input
func (s *Search) Suggest() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params, err := parseQueryParams(r.URL.Query())
if err != nil {
log.Printf("%v", err)
engine.RenderProblem(engine.ProblemBadRequest, w)
return
}
params := parseQueryParams(r.URL.Query())
searchQuery := params["q"]
delete(params, "q")
format := params["f"]
Expand All @@ -48,7 +43,7 @@ func (s *Search) Suggest() http.HandlerFunc {

log.Printf("crs %s, limit %d, format %s, query %s, params %v", crs, limit, format, searchQuery, params)

suggestions, err := s.datasource.Suggest(r.Context(), r.URL.Query().Get("q"))
suggestions, err := s.datasource.Suggest(r.Context(), searchQuery.(string)) // TODO check before casting
if err != nil {
engine.RenderProblem(engine.ProblemServerError, w, err.Error())
return
Expand All @@ -57,7 +52,7 @@ func (s *Search) Suggest() http.HandlerFunc {
}
}

func parseQueryParams(query url.Values) (map[string]any, error) {
func parseQueryParams(query url.Values) map[string]any {
result := make(map[string]any, len(query))

deepObjectParams := make(map[string]map[string]string)
Expand All @@ -80,7 +75,7 @@ func parseQueryParams(query url.Values) (map[string]any, error) {
for mainKey, subParams := range deepObjectParams {
result[mainKey] = subParams
}
return result, nil
return result
}

func newDatasource(e *engine.Engine, dbConn string, searchIndex string) ds.Datasource {
Expand Down
60 changes: 57 additions & 3 deletions internal/search/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
Expand All @@ -15,6 +18,7 @@ import (
"github.com/PDOK/gomagpie/internal/engine"
"github.com/PDOK/gomagpie/internal/etl"
"github.com/docker/go-connections/nat"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
Expand All @@ -36,7 +40,7 @@ func TestSuggest(t *testing.T) {
}
ctx := context.Background()

// given
// given postgres available
dbPort, postgisContainer, err := setupPostgis(ctx, t)
if err != nil {
t.Error(err)
Expand All @@ -45,20 +49,42 @@ func TestSuggest(t *testing.T) {

dbConn := fmt.Sprintf("postgres://postgres:[email protected]:%d/%s?sslmode=disable", dbPort.Int(), "test_db")

// given empty search index
err = etl.CreateSearchIndex(dbConn, "search_index")
assert.NoError(t, err)

// given imported gpkg
cfg, err := config.NewConfig("internal/etl/testdata/config.yaml")
assert.NoError(t, err)
table := config.FeatureTable{Name: "addresses", FID: "fid", Geom: "geom"}
err = etl.ImportFile(cfg, "search_index", "internal/etl/testdata/addresses-crs84.gpkg", table, 1000, dbConn)
assert.NoError(t, err)

// when/then
// given engine available
e, err := engine.NewEngine("internal/etl/testdata/config.yaml", false, false)
assert.NoError(t, err)

// given server available
rr, ts := createMockServer()
defer ts.Close()

// when perform autosuggest
searchEndpoint := NewSearch(e, dbConn, "search_index")
searchEndpoint.Suggest()
handler := searchEndpoint.Suggest()
req, err := createRequest("http://localhost:8080/search/suggest?q=\"Oudeschild\"")
assert.NoError(t, err)
handler.ServeHTTP(rr, req)

// then
assert.Equal(t, 200, rr.Code)
assert.JSONEq(t, `[
"Barentszstraat, 1792AD <b>Oudeschild</b>",
"Bolwerk, 1792AS <b>Oudeschild</b>",
"Commandeurssingel, 1792AV <b>Oudeschild</b>",
"De Houtmanstraat, 1792BC <b>Oudeschild</b>",
"De Ruyterstraat, 1792AP <b>Oudeschild</b>",
"De Wittstraat, 1792BP <b>Oudeschild</b>"
]`, rr.Body.String())
}

func setupPostgis(ctx context.Context, t *testing.T) (nat.Port, testcontainers.Container, error) {
Expand Down Expand Up @@ -104,3 +130,31 @@ func terminateContainer(ctx context.Context, t *testing.T, container testcontain
t.Fatalf("Failed to terminate container: %s", err.Error())
}
}

func createMockServer() (*httptest.ResponseRecorder, *httptest.Server) {
rr := httptest.NewRecorder()
l, err := net.Listen("tcp", "localhost:9095")
if err != nil {
log.Fatal(err)
}
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
engine.SafeWrite(w.Write, []byte(r.URL.String()))
}))
err = ts.Listener.Close()
if err != nil {
log.Fatal(err)
}
ts.Listener = l
ts.Start()
return rr, ts
}

func createRequest(url string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if req == nil || err != nil {
return req, err
}
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
return req, err
}

0 comments on commit b9a94fa

Please sign in to comment.