search_queries_geo_polygon_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestGeoPolygonQuery(t *testing.T) {
  10. q := NewGeoPolygonQuery("person.location")
  11. q = q.AddPoint(40, -70)
  12. q = q.AddPoint(30, -80)
  13. point, err := GeoPointFromString("20,-90")
  14. if err != nil {
  15. t.Fatalf("GeoPointFromString failed: %v", err)
  16. }
  17. q = q.AddGeoPoint(point)
  18. src, err := q.Source()
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. data, err := json.Marshal(src)
  23. if err != nil {
  24. t.Fatalf("marshaling to JSON failed: %v", err)
  25. }
  26. got := string(data)
  27. expected := `{"geo_polygon":{"person.location":{"points":[{"lat":40,"lon":-70},{"lat":30,"lon":-80},{"lat":20,"lon":-90}]}}}`
  28. if got != expected {
  29. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  30. }
  31. }
  32. func TestGeoPolygonQueryFromGeoPoints(t *testing.T) {
  33. q := NewGeoPolygonQuery("person.location")
  34. q = q.AddGeoPoint(&GeoPoint{Lat: 40, Lon: -70})
  35. q = q.AddGeoPoint(GeoPointFromLatLon(30, -80))
  36. point, err := GeoPointFromString("20,-90")
  37. if err != nil {
  38. t.Fatalf("GeoPointFromString failed: %v", err)
  39. }
  40. q = q.AddGeoPoint(point)
  41. src, err := q.Source()
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. data, err := json.Marshal(src)
  46. if err != nil {
  47. t.Fatalf("marshaling to JSON failed: %v", err)
  48. }
  49. got := string(data)
  50. expected := `{"geo_polygon":{"person.location":{"points":[{"lat":40,"lon":-70},{"lat":30,"lon":-80},{"lat":20,"lon":-90}]}}}`
  51. if got != expected {
  52. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  53. }
  54. }