search_queries_geo_bounding_box_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 TestGeoBoundingBoxQueryIncomplete(t *testing.T) {
  10. q := NewGeoBoundingBoxQuery("pin.location")
  11. q = q.TopLeft(40.73, -74.1)
  12. // no bottom and no right here
  13. q = q.Type("memory")
  14. src, err := q.Source()
  15. if err == nil {
  16. t.Fatal("expected error")
  17. }
  18. if src != nil {
  19. t.Fatal("expected empty source")
  20. }
  21. }
  22. func TestGeoBoundingBoxQuery(t *testing.T) {
  23. q := NewGeoBoundingBoxQuery("pin.location")
  24. q = q.TopLeft(40.73, -74.1)
  25. q = q.BottomRight(40.01, -71.12)
  26. q = q.Type("memory")
  27. src, err := q.Source()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. data, err := json.Marshal(src)
  32. if err != nil {
  33. t.Fatalf("marshaling to JSON failed: %v", err)
  34. }
  35. got := string(data)
  36. expected := `{"geo_bounding_box":{"pin.location":{"bottom_right":[-71.12,40.01],"top_left":[-74.1,40.73]},"type":"memory"}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestGeoBoundingBoxQueryWithGeoPoint(t *testing.T) {
  42. q := NewGeoBoundingBoxQuery("pin.location")
  43. q = q.TopLeftFromGeoPoint(GeoPointFromLatLon(40.73, -74.1))
  44. q = q.BottomRightFromGeoPoint(GeoPointFromLatLon(40.01, -71.12))
  45. src, err := q.Source()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. data, err := json.Marshal(src)
  50. if err != nil {
  51. t.Fatalf("marshaling to JSON failed: %v", err)
  52. }
  53. got := string(data)
  54. expected := `{"geo_bounding_box":{"pin.location":{"bottom_right":[-71.12,40.01],"top_left":[-74.1,40.73]}}}`
  55. if got != expected {
  56. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  57. }
  58. }