Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for Upsert block #68

Merged
merged 2 commits into from
Jun 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 165 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func ExampleTxn_Mutate() {
// While setting an object if a struct has a Uid then its properties in the graph are updated
// else a new node is created.
// In the example below new nodes for Alice, Bob and Charlie and school are created (since they
// dont have a Uid).
// don't have a Uid).
p := Person{
Name: "Alice",
Age: 26,
Expand Down Expand Up @@ -1029,7 +1029,6 @@ func ExampleTxn_Discard() {
}

txn := dg.NewTxn()

_, err = txn.Mutate(ctx, &api.Mutation{
SetNquads: []byte(`_:a <name> "Alice" .`),
})
Expand All @@ -1054,3 +1053,167 @@ func ExampleTxn_Discard() {
fmt.Printf(string(resp.Json))
// Output: {"q":[]}
}

func ExampleTxn_Mutate_upsert() {
dg, cancel := getDgraphClient()
defer cancel()

ctx, toCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer toCancel()

// Warn: Cleaning up the database
if err := dg.Alter(ctx, &api.Operation{DropAll: true}); err != nil {
log.Fatal("The drop all operation should have succeeded")
}

op := &api.Operation{}
op.Schema = `
name: string .
email: string @index(exact) .
`
if err := dg.Alter(ctx, op); err != nil {
log.Fatal(err)
}

m1 := `
_:n1 <name> "user" .
_:n1 <email> "user@dgraphO.io" .
`
mu := &api.Mutation{
SetNquads: []byte(m1),
CommitNow: true,
}
if _, err := dg.NewTxn().Mutate(ctx, mu); err != nil {
log.Fatal(err)
}

mu.Query = `
query {
me(func: eq(email, "user@dgraphO.io")) {
v as uid
}
}
`
m2 := `uid(v) <email> "user@dgraph.io" .`
mu.SetNquads = []byte(m2)

// Update email only if matching uid found.
if _, err := dg.NewTxn().Mutate(ctx, mu); err != nil {
log.Fatal(err)
}

query := `
{
me(func: eq(email, "user@dgraph.io")) {
name
email
}
}
`
resp, err := dg.NewTxn().Query(ctx, query)
if err != nil {
log.Fatal(err)
}

// resp.Json contains the updated value.
fmt.Println(string(resp.Json))
// Output: {"me":[{"name":"user","email":"user@dgraph.io"}]}
}

func ExampleTxn_Mutate_upsertJSON() {
dg, cancel := getDgraphClient()
defer cancel()

// Warn: Cleaning up the database
ctx := context.Background()
if err := dg.Alter(ctx, &api.Operation{DropAll: true}); err != nil {
log.Fatal(err)
}

type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Email string `json:"email,omitempty"`
Friends []Person `json:"friend,omitempty"`
}

op := &api.Operation{Schema: `email: string @index(exact) @upsert .`}
if err := dg.Alter(context.Background(), op); err != nil {
log.Fatal(err)
}

// Create and query the user using Upsert block
mu := &api.Mutation{CommitNow: true}
mu.Query = `
{
me(func: eq(email, "user@dgraph.io")) {
...fragmentA
}
}

fragment fragmentA {
v as uid
}
`
pb, err := json.Marshal(Person{Uid: "uid(v)", Name: "Wrong", Email: "user@dgraph.io"})
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
if _, err := dg.NewTxn().Mutate(ctx, mu); err != nil {
log.Fatal(err)
}

// Fix the name and add age
pb, err = json.Marshal(Person{Uid: "uid(v)", Name: "user", Age: 35})
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
if _, err := dg.NewTxn().Mutate(ctx, mu); err != nil {
log.Fatal(err)
}

q := `
{
Me(func: has(email)) {
age
name
email
}
}
`
resp, err := dg.NewReadOnlyTxn().Query(ctx, q)
if err != nil {
log.Fatal("The query should have succeeded")
}

type Root struct {
Me []Person `json:"me"`
}
var r Root
if err := json.Unmarshal(resp.Json, &r); err != nil {
log.Fatal(err)
}
fmt.Println(string(resp.Json))

// Delete the user now
mu.SetJson = nil
dgo.DeleteEdges(mu, "uid(v)", "age", "name", "email")
if _, err := dg.NewTxn().Mutate(ctx, mu); err != nil {
log.Fatal(err)
}

resp, err = dg.NewReadOnlyTxn().Query(ctx, q)
if err != nil {
log.Fatal("The query should have succeeded")
}
if err := json.Unmarshal(resp.Json, &r); err != nil {
log.Fatal(err)
}

fmt.Println(string(resp.Json))
// Output: {"Me":[{"age":35,"name":"user","email":"user@dgraph.io"}]}
// {"Me":[]}
}