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