Skip to content

Commit 430512d

Browse files
authored
Add tests for query generation (#1595)
Separates out spanner query generation so it can be tested independently from the query results (follow up to comments on #1591) Currently the tests use the same examples as query_test.go Also adds sorting to node filters for deterministic responses
1 parent f5d58db commit 430512d

35 files changed

+1136
-242
lines changed

internal/server/spanner/dsutil.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import (
2323
"log"
2424
"sort"
2525
"strconv"
26-
"strings"
2726

28-
"cloud.google.com/go/spanner"
2927
pb "github.com/datacommonsorg/mixer/internal/proto"
3028
pbv2 "github.com/datacommonsorg/mixer/internal/proto/v2"
3129
"github.com/datacommonsorg/mixer/internal/server/pagination"
@@ -43,8 +41,8 @@ const (
4341
CHAIN = "+"
4442
// Used for Facet responses with an entity expression.
4543
ENTITY_PLACEHOLDER = ""
46-
WHERE = "\nWHERE "
47-
AND = "\nAND "
44+
WHERE = "\n\t\tWHERE\n\t\t\t"
45+
AND = "\n\t\t\tAND "
4846
)
4947

5048
// Select options for Observation.
@@ -241,26 +239,6 @@ func isObservationRequest(qo *queryOptions) bool {
241239
return qo.date && qo.value
242240
}
243241

244-
func buildBaseObsStatement(variables []string, entities []string) spanner.Statement {
245-
stmt := spanner.Statement{
246-
SQL: statements.getObs,
247-
Params: map[string]interface{}{},
248-
}
249-
250-
filters := []string{}
251-
if len(variables) > 0 {
252-
stmt.Params["variables"] = variables
253-
filters = append(filters, statements.selectVariableDcids)
254-
}
255-
if len(entities) > 0 {
256-
stmt.Params["entities"] = entities
257-
filters = append(filters, statements.selectEntityDcids)
258-
}
259-
stmt.SQL += WHERE + strings.Join(filters, AND)
260-
261-
return stmt
262-
}
263-
264242
func filterTimeSeriesByDate(ts *TimeSeries, date string) {
265243
switch date {
266244
case "":
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GRAPH DCGraph MATCH (m:Node
2+
WHERE
3+
m.subject_id IN ('FireIncidentTypeEnum','FoodTypeEnum'))<-[e:Edge]-(n:Node)
4+
RETURN
5+
m.subject_id,
6+
e.predicate,
7+
e.provenance,
8+
n.value,
9+
n.bytes,
10+
n.name,
11+
n.types
12+
ORDER BY
13+
subject_id,
14+
predicate,
15+
value,
16+
provenance
17+
LIMIT 5001
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GRAPH DCGraph MATCH (m:Node
2+
WHERE
3+
m.subject_id IN ('Aadhaar','Monthly_Average_RetailPrice_Electricity_Residential','foo'))-[e:Edge]->(n:Node)
4+
RETURN
5+
m.subject_id,
6+
e.predicate,
7+
e.provenance,
8+
n.value,
9+
n.bytes,
10+
n.name,
11+
n.types
12+
ORDER BY
13+
subject_id,
14+
predicate,
15+
value,
16+
provenance
17+
LIMIT 5001
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
GRAPH DCGraph MATCH (m:Node
2+
WHERE
3+
m.subject_id IN ('StatisticalVariable'))<-[e:Edge
4+
WHERE
5+
e.predicate IN ('typeOf')]-(n:Node)
6+
RETURN
7+
m.subject_id,
8+
e.predicate,
9+
e.provenance,
10+
n.value,
11+
n.bytes,
12+
n.name,
13+
n.types
14+
ORDER BY
15+
subject_id,
16+
predicate,
17+
value,
18+
provenance
19+
LIMIT 5001
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GRAPH DCGraph MATCH ANY (m:Node
2+
WHERE
3+
m.subject_id IN ('dc/g/UN'))<-[e:Edge
4+
WHERE
5+
e.predicate = 'specializationOf']-{1,10}(n:Node)
6+
RETURN
7+
m.subject_id,
8+
'specializationOf+' AS predicate,
9+
'' AS provenance,
10+
n.value,
11+
n.bytes,
12+
n.name,
13+
n.types
14+
ORDER BY
15+
subject_id,
16+
value
17+
LIMIT 5001
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
GRAPH DCGraph MATCH (m:Node
2+
WHERE
3+
m.subject_id IN ('EarthquakeEvent'))<-[e:Edge
4+
WHERE
5+
e.predicate IN ('domainIncludes','naturalHazardType')]-(n:Node)
6+
RETURN
7+
m.subject_id,
8+
e.predicate,
9+
e.provenance,
10+
n.value,
11+
n.bytes,
12+
n.name,
13+
n.types
14+
ORDER BY
15+
subject_id,
16+
predicate,
17+
value,
18+
provenance
19+
LIMIT 5001
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GRAPH DCGraph MATCH ANY (m:Node
2+
WHERE
3+
m.subject_id IN ('dc/g/Farm_FarmInventoryStatus'))<-[e:Edge
4+
WHERE
5+
e.predicate = 'specializationOf']-{1,10}(n:Node)
6+
RETURN
7+
m.subject_id,
8+
'specializationOf+' AS predicate,
9+
'' AS provenance,
10+
n.value,
11+
n.bytes,
12+
n.name,
13+
n.types
14+
ORDER BY
15+
subject_id,
16+
value
17+
LIMIT 5001
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
GRAPH DCGraph MATCH (m:Node
2+
WHERE
3+
m.subject_id IN ('Farm'))<-[e:Edge]-(n:Node)
4+
,(n)-[filter0:Edge
5+
WHERE
6+
filter0.predicate = 'farmInventoryType'
7+
AND filter0.object_id IN ('Melon','mxuMmhySOejKGXRXFbMXdorKlNV934EOop6b21kOJGw=')]->
8+
,(n)-[filter1:Edge
9+
WHERE
10+
filter1.predicate = 'name'
11+
AND filter1.object_id IN ('Area of Farm: Melon','xblU8pfFl5m+cg9tsR1EsW19+PLlpqfNhwYkFu0mgzE=')]->
12+
RETURN
13+
m.subject_id,
14+
n.subject_id AS value,
15+
e.predicate,
16+
e.provenance
17+
NEXT MATCH (n)
18+
WHERE
19+
n.subject_id = value
20+
RETURN
21+
subject_id,
22+
predicate,
23+
provenance,
24+
n.value,
25+
n.bytes,
26+
n.name,
27+
n.types
28+
ORDER BY
29+
subject_id,
30+
predicate,
31+
value,
32+
provenance
33+
LIMIT 5001
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
GRAPH DCGraph MATCH (m:Node
2+
WHERE
3+
m.subject_id IN ('EarthquakeEvent'))<-[e:Edge
4+
WHERE
5+
e.predicate IN ('domainIncludes')]-(n:Node)
6+
RETURN
7+
m.subject_id,
8+
e.predicate,
9+
e.provenance,
10+
n.value,
11+
n.bytes,
12+
n.name,
13+
n.types
14+
ORDER BY
15+
subject_id,
16+
predicate,
17+
value,
18+
provenance
19+
LIMIT 5001
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
GRAPH DCGraph MATCH (m:Node
2+
WHERE
3+
m.subject_id IN ('foo OR 1=1;'))<-[e:Edge
4+
WHERE
5+
e.predicate IN ('foo OR 1=1;')]-(n:Node)
6+
,(n)-[filter0:Edge
7+
WHERE
8+
filter0.predicate = 'foo OR 1=1;'
9+
AND filter0.object_id IN ('foo OR 1=1;','OG7012T2qe10jzYRBvG6dgUEx5fj7uIxT+RkGvxpn/U=')]->
10+
RETURN
11+
m.subject_id,
12+
n.subject_id AS value,
13+
e.predicate,
14+
e.provenance
15+
NEXT MATCH (n)
16+
WHERE
17+
n.subject_id = value
18+
RETURN
19+
subject_id,
20+
predicate,
21+
provenance,
22+
n.value,
23+
n.bytes,
24+
n.name,
25+
n.types
26+
ORDER BY
27+
subject_id,
28+
predicate,
29+
value,
30+
provenance
31+
LIMIT 5001

0 commit comments

Comments
 (0)