11/*
2- * Copyright 2025 Hypermode Inc. and Contributors
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
2+ * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
3+ * SPDX-License-Identifier: Apache-2.0
154 */
165
176package modusgraph_test
@@ -73,10 +62,16 @@ func TestClientSimpleGet(t *testing.T) {
7362 }
7463}
7564
65+ type GeoLocation struct {
66+ Type string `json:"type"`
67+ Coord []float64 `json:"coordinates"`
68+ }
69+
7670type QueryTestRecord struct {
77- Name string `json:"name,omitempty" dgraph:"index=exact,term unique"`
78- Age int `json:"age,omitempty"`
79- BirthDate time.Time `json:"birthDate,omitzero"`
71+ Name string `json:"name,omitempty" dgraph:"index=exact,term unique"`
72+ Age int `json:"age,omitempty" dgraph:"index=int"`
73+ BirthDate time.Time `json:"birthDate,omitzero"`
74+ Location * GeoLocation `json:"location,omitempty" dgraph:"type=geo index=geo"`
8075
8176 UID string `json:"uid,omitempty"`
8277 DType []string `json:"dgraph.type,omitempty"`
@@ -100,6 +95,18 @@ func TestClientQuery(t *testing.T) {
10095 },
10196 }
10297
98+ locations := [][]float64 {
99+ {- 122.4194 , 37.7749 }, // San Francisco, USA
100+ {2.2945 , 48.8584 }, // Paris (Eiffel Tower), France
101+ {- 74.0060 , 40.7128 }, // New York City, USA
102+ {- 0.1276 , 51.5072 }, // London, UK
103+ {139.7690 , 35.6804 }, // Tokyo, Japan
104+ {77.2090 , 28.6139 }, // New Delhi, India
105+ {31.2357 , 30.0444 }, // Cairo, Egypt
106+ {151.2093 , - 33.8688 }, // Sydney, Australia
107+ {- 43.1729 , - 22.9068 }, // Rio de Janeiro, Brazil
108+ {116.4074 , 39.9042 }, // Beijing, China
109+ }
103110 for _ , tc := range testCases {
104111 t .Run (tc .name , func (t * testing.T ) {
105112 if tc .skip {
@@ -117,6 +124,10 @@ func TestClientQuery(t *testing.T) {
117124 Name : fmt .Sprintf ("Test Entity %d" , i ),
118125 Age : 30 + i ,
119126 BirthDate : birthDate .AddDate (0 , 0 , i ),
127+ Location : & GeoLocation {
128+ Type : "Point" ,
129+ Coord : locations [i ],
130+ },
120131 }
121132 }
122133 ctx := context .Background ()
@@ -140,6 +151,7 @@ func TestClientQuery(t *testing.T) {
140151 require .Equal (t , result [i ].Name , fmt .Sprintf ("Test Entity %d" , i ), "Name should match" )
141152 require .Equal (t , result [i ].Age , 30 + i , "Age should match" )
142153 require .Equal (t , result [i ].BirthDate , birthDate .AddDate (0 , 0 , i ), "BirthDate should match" )
154+ require .Equal (t , result [i ].Location .Coord , locations [i ], "Location coordinates should match" )
143155 }
144156 })
145157
@@ -156,6 +168,33 @@ func TestClientQuery(t *testing.T) {
156168 }
157169 })
158170
171+ t .Run ("QueryWithGeoFilters" , func (t * testing.T ) {
172+ var result []QueryTestRecord
173+ err := client .Query (ctx , QueryTestRecord {}).
174+ Filter (`(near(location, [2.2946, 48.8585], 1000))` ). // just a few meters from the Eiffel Tower
175+ Nodes (& result )
176+ require .NoError (t , err , "Query should succeed" )
177+ require .Len (t , result , 1 , "Should have 1 entity" )
178+ require .Equal (t , result [0 ].Name , "Test Entity 1" , "Name should match" )
179+
180+ var rawResult struct {
181+ Data []QueryTestRecord `json:"q"`
182+ }
183+ parisQuery := `query {
184+ q(func: within(location, [[[2.2945, 48.8584], [2.2690, 48.8800], [2.3300, 48.9000],
185+ [2.4100, 48.8800], [2.4150, 48.8300], [2.3650, 48.8150],
186+ [2.3000, 48.8100], [2.2600, 48.8350], [2.2945, 48.8584]]])) {
187+ uid
188+ name
189+ }
190+ }`
191+ resp , err := client .QueryRaw (ctx , parisQuery , nil )
192+ require .NoError (t , err , "Query should succeed" )
193+ require .NoError (t , json .Unmarshal (resp , & rawResult ), "Failed to unmarshal response" )
194+ require .Len (t , rawResult .Data , 1 , "Should have 1 entity" )
195+ require .Equal (t , rawResult .Data [0 ].Name , "Test Entity 1" , "Name should match" )
196+ })
197+
159198 t .Run ("QueryWithPagination" , func (t * testing.T ) {
160199 var result []QueryTestRecord
161200 count , err := client .Query (ctx , QueryTestRecord {}).First (5 ).NodesAndCount (& result )
@@ -182,7 +221,8 @@ func TestClientQuery(t *testing.T) {
182221 Data []QueryTestRecord `json:"q"`
183222 }
184223 resp , err := client .QueryRaw (ctx ,
185- `query { q(func: type(QueryTestRecord), orderasc: age) { uid name age birthDate }}` )
224+ `query { q(func: type(QueryTestRecord), orderasc: age) { uid name age birthDate }}` ,
225+ nil )
186226 require .NoError (t , err , "Query should succeed" )
187227 require .NoError (t , json .Unmarshal (resp , & result ), "Failed to unmarshal response" )
188228 require .Len (t , result .Data , 10 , "Should have 10 entities" )
@@ -192,6 +232,21 @@ func TestClientQuery(t *testing.T) {
192232 require .Equal (t , result .Data [i ].BirthDate , birthDate .AddDate (0 , 0 , i ), "BirthDate should match" )
193233 }
194234 })
235+
236+ t .Run ("QueryRawWithVars" , func (t * testing.T ) {
237+ var result struct {
238+ Data []QueryTestRecord `json:"q"`
239+ }
240+ resp , err := client .QueryRaw (ctx ,
241+ `query older_than_inclusive($1: int) { q(func: ge(age, $1)) { uid name age }}` ,
242+ map [string ]string {"$1" : "38" })
243+ require .NoError (t , err , "Query should succeed" )
244+ require .NoError (t , json .Unmarshal (resp , & result ), "Failed to unmarshal response" )
245+ require .Len (t , result .Data , 2 , "Should have 2 entities" )
246+ for i := range 2 {
247+ require .GreaterOrEqual (t , result .Data [i ].Age , 38 , "Age should be greater than or equal to 38" )
248+ }
249+ })
195250 })
196251 }
197252}
0 commit comments