Skip to content

Commit

Permalink
Merge 735f04c into 27731ea
Browse files Browse the repository at this point in the history
  • Loading branch information
tprebs committed Sep 28, 2021
2 parents 27731ea + 735f04c commit 2085b95
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
- [Delete](#delete)
- [Delete Query](#delete-query)
- [Delete Node](#delete-node)
- [Delete Edge](#delete-edge)
- [Delete Edge](#delete-edges)
- [Development](#development)

## Installation
Expand Down Expand Up @@ -551,6 +551,8 @@ count, err := tx.Get(&users).
fmt.Println(count)
```

Note: `Query.query` will only be applied to the count query if `Query.Cascade` is provided as node filters do not affect the overall count unless cascaded.

#### Custom Scanning Query results

You can alternatively specify a different destination for your query results, by passing it as a parameter to the `Node` or `Nodes`.
Expand Down
8 changes: 8 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ func (q *Query) nodes(jsonData []byte, dst interface{}) error {
func (q *Query) NodesAndCount() (count int, err error) {
tx := TxnContext{txn: q.tx, ctx: q.ctx}

var qr string
// only apply the query if the result will be cascaded
if q.cascade != nil {
qr = q.query
}

pagedResult := PagedResults{}
query := tx.Query(
&Query{
Expand All @@ -435,6 +441,8 @@ func (q *Query) NodesAndCount() (count int, err error) {
rootFunc: q.rootFunc,
model: q.model,
filter: q.filter,
query: qr,
cascade: q.cascade,
},
&Query{
name: "result",
Expand Down
49 changes: 39 additions & 10 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package dgman

import (
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -35,7 +36,7 @@ type TestModel struct {

type TestEdge struct {
UID string `json:"uid"`
Level string `json:"level,omitempty"`
Level string `json:"level,omitempty" dgraph:"index=term"`
DType []string `json:"dgraph.type,omitempty"`
}

Expand Down Expand Up @@ -520,7 +521,12 @@ func TestGetNodesAndCount(t *testing.T) {
models = append(models, &TestModel{
Name: fmt.Sprintf("wildan %d", i%10),
Address: "Beverly Hills",
Age: i,
Edges: []TestEdge{
{
Level: strconv.Itoa(i),
},
},
Age: i,
}, &TestModel{
Name: fmt.Sprintf("alex %d", i%10),
Address: "New York",
Expand All @@ -534,16 +540,39 @@ func TestGetNodesAndCount(t *testing.T) {
return
}

result := []*TestModel{}
t.Run("get nodes and count", func(t *testing.T) {
result := []*TestModel{}

tx = NewReadOnlyTxn(c)
count, err := tx.Get(&result).Filter(`anyofterms(name, "wildan")`).First(3).NodesAndCount()
if err != nil {
t.Error(err)
}
tx = NewReadOnlyTxn(c)
count, err := tx.Get(&result).Filter(`anyofterms(name, "wildan")`).First(3).NodesAndCount()
if err != nil {
t.Error(err)
}

assert.Len(t, result, 3)
assert.Equal(t, 5, count)
})

t.Run("get nodes and count with cascade", func(t *testing.T) {
result := []*TestModel{}

query := `
{
uid
edges @filter(eq(level,0,1,2,3)){
uid
}
}
`
tx = NewReadOnlyTxn(c)
count, err := tx.Get(&result).Filter(`anyofterms(name, "wildan")`).First(3).Query(query).Cascade("edges").NodesAndCount()
if err != nil {
t.Error(err)
}

assert.Len(t, result, 3)
assert.Equal(t, 5, count)
assert.Len(t, result, 3)
assert.Equal(t, 4, count)
})
}

func TestExpandAll(t *testing.T) {
Expand Down

0 comments on commit 2085b95

Please sign in to comment.