Skip to content

Commit

Permalink
add more tests, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mangalaman93 committed Nov 5, 2019
1 parent a3dfa56 commit eb3e3a5
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 15 deletions.
143 changes: 142 additions & 1 deletion dgraph/cmd/alpha/upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,6 @@ amount: float .`))
upsert {
query {
u as var(func: has(branch))
}
mutation {
Expand Down Expand Up @@ -2180,3 +2179,145 @@ func TestMultiMutationNoUpsert(t *testing.T) {
require.Equal(t, []string{"1-email", "1-name", "1-works_for"}, resp.Txn.Preds)
testutil.CompareJSON(t, `{"empty": []}`, string(resp.Json))
}

func TestMultipleMutation(t *testing.T) {
require.NoError(t, dropAll())
require.NoError(t, alterSchema(`email: string @index(exact) .`))

m1 := `
upsert {
query {
me(func: eq(email, "email@company.io")) {
v as uid
}
}
mutation @if(not(eq(len(v), 0))) {
set {
uid(v) <name> "not_name" .
uid(v) <email> "not_email@company.io" .
}
}
mutation @if(eq(len(v), 0)) {
set {
_:user <name> "name" .
_:user <email> "email@company.io" .
}
}
}`
mr, err := mutationWithTs(m1, "application/rdf", false, true, 0)
require.NoError(t, err)
require.True(t, len(mr.keys) == 0)
require.True(t, contains(mr.preds, "email"))
require.True(t, contains(mr.preds, "name"))
require.Equal(t, 0, len(mr.vars))

q1 := `
{
q(func: eq(email, "email@company.io")) {
name
}
}`
res, _, err := queryWithTs(q1, "application/graphql+-", "", 0)
expectedRes := `
{
"data": {
"q": [{
"name": "name"
}]
}
}`
require.NoError(t, err)
testutil.CompareJSON(t, res, expectedRes)

// This time the other mutation will get executed
_, err = mutationWithTs(m1, "application/rdf", false, true, 0)
require.NoError(t, err)

q2 := `
{
q(func: eq(email, "not_email@company.io")) {
name
}
}`
res, _, err = queryWithTs(q2, "application/graphql+-", "", 0)
require.NoError(t, err)

expectedRes = `
{
"data": {
"q": [{
"name": "not_name"
}]
}
}`
require.NoError(t, err)
testutil.CompareJSON(t, res, expectedRes)
}

// func TestMultiMutationWithoutIf(t *testing.T) {
// require.NoError(t, dropAll())
// require.NoError(t, alterSchema(`email: string @index(exact) .`))

// m1 := `
// upsert {
// query {
// me(func: eq(email, "email@company.io")) {
// v as uid
// }
// }

// mutation @if(not(eq(len(v), 0))) {
// set {
// uid(v) <name> "not_name" .
// uid(v) <email> "not_email@company.io" .
// }
// }

// mutation @if(eq(len(v), 0)) {
// set {
// _:user <name> "name" .
// _:user <email> "email@company.io" .
// }
// }

// mutation {
// set {
// _:user <name> "name2" .
// _:user <email> "email2@company.io" .
// }
// }

// mutation {
// set {
// _:user <name> "name3" .
// _:user <email> "email3@company.io" .
// }
// }
// }`
// mr, err := mutationWithTs(m1, "application/rdf", false, true, 0)
// require.NoError(t, err)
// require.True(t, len(mr.keys) == 0)
// require.True(t, contains(mr.preds, "email"))
// require.True(t, contains(mr.preds, "name"))
// require.Equal(t, 0, len(mr.vars))

// q1 := `
// {
// q(func: has(email)) {
// name
// }
// }`
// res, _, err := queryWithTs(q1, "application/graphql+-", "", 0)
// expectedRes := `
// {
// "data": {
// "q": [{
// "name": "name"
// }]
// }
// }`
// require.NoError(t, err)
// testutil.CompareJSON(t, res, expectedRes)
// }
19 changes: 11 additions & 8 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,17 @@ func updateMutations(qc *queryContext) {
updateUIDInMutations(gmu, qc)
updateValInMutations(gmu, qc)
}

// Remove condition vars from the response
for _, v := range qc.condVars {
delete(qc.uidRes, v)
}
// Remove empty valued varibales from the response
for n, v := range qc.uidRes {
if len(v) == 0 {
delete(qc.uidRes, n)
}
}
}

// findVars finds all the variables used in mutation block
Expand Down Expand Up @@ -914,12 +925,10 @@ func updateValInMutations(gmu *gql.Mutation, qc *queryContext) {
// * uid(v) -> _:uid(v) -- Otherwise
func updateUIDInMutations(gmu *gql.Mutation, qc *queryContext) {
// usedMutationVars keeps track of variables that are used in mutations.
usedMutationVars := make(map[string]bool)
getNewVals := func(s string) []string {
if strings.HasPrefix(s, "uid(") {
varName := s[4 : len(s)-1]
if uids, ok := qc.uidRes[varName]; ok && len(uids) != 0 {
usedMutationVars[varName] = true
return uids
}

Expand Down Expand Up @@ -973,12 +982,6 @@ func updateUIDInMutations(gmu *gql.Mutation, qc *queryContext) {
}
}
}
for v := range qc.uidRes {
// We only want to return the vars which are used in the mutation.
if _, ok := usedMutationVars[v]; !ok {
delete(qc.uidRes, v)
}
}
gmu.Set = gmuSet
}

Expand Down
12 changes: 6 additions & 6 deletions gql/parser_mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func ParseMutation(mutation string) (req *api.Request, err error) {
func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) {
var req *api.Request
var queryText, condText string
var queryFound, condFound bool
var queryFound bool

// ===>upsert<=== {...}
if !it.Next() {
Expand Down Expand Up @@ -115,10 +115,6 @@ func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) {
// upsert { mutation ===>@if(...)<=== {....} query{...}}
item = it.Item()
if item.Typ == itemUpsertBlockOpContent {
if condFound {
return nil, it.Errorf("Multiple @if directive inside upsert block")
}
condFound = true
condText = item.Val
if !it.Next() {
return nil, it.Errorf("Unexpected end of upsert block")
Expand All @@ -131,7 +127,11 @@ func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) {
return nil, err
}
mu.Cond = condText
req = &api.Request{Mutations: []*api.Mutation{mu}}
if req == nil {
req = &api.Request{Mutations: []*api.Mutation{mu}}
} else {
req.Mutations = append(req.Mutations, mu)
}

// upsert { mutation{...} ===>fragment<==={...}}
case item.Typ == itemUpsertBlockOp && item.Val == "fragment":
Expand Down
32 changes: 32 additions & 0 deletions gql/upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,35 @@ func TestConditionalUpsertErrWrongIf(t *testing.T) {
_, err := ParseMutation(query)
require.Contains(t, err.Error(), "Expected @if, found [@fi]")
}

func TestMultipleMutation(t *testing.T) {
query := `
upsert {
mutation @if(eq(len(m), 1)) {
set {
uid(m) <age> "45" .
}
}
mutation @if(not(eq(len(m), 1))) {
set {
uid(f) <age> "45" .
}
}
mutation {
set {
_:user <age> "45" .
}
}
query {
me(func: eq(age, 34)) @filter(ge(name, "user")) {
uid
}
}
}`
req, err := ParseMutation(query)
require.NoError(t, err)
require.Equal(t, 3, len(req.Mutations))
}

0 comments on commit eb3e3a5

Please sign in to comment.