Skip to content

Commit c1ad859

Browse files
Adding tests for upsert with graphql variables (#101)
1 parent f8ad980 commit c1ad859

File tree

2 files changed

+190
-37
lines changed

2 files changed

+190
-37
lines changed

tests/integration/delete.spec.ts

Lines changed: 120 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,116 @@ import * as dgraph from "../../src";
22

33
import { setSchema, setup } from "../helper";
44

5+
let client: dgraph.DgraphClient;
6+
7+
async function performUpsert(mu: dgraph.Mutation, query: string, vars: object) {
8+
const req = new dgraph.Request();
9+
req.addMutations(mu);
10+
req.setQuery(query);
11+
req.setCommitNow(true);
12+
if (vars !== undefined) {
13+
const varsMap = req.getVarsMap();
14+
req.setQuery(query);
15+
Object.keys(vars).forEach((key: string) => {
16+
varsMap.set(key, vars[key]);
17+
});
18+
}
19+
await expect(client.newTxn().doRequest(req)).resolves.toBeDefined();
20+
}
21+
22+
async function performMutation(mu: dgraph.Mutation, blankNodeLabel: string): Promise<string> {
23+
mu.setCommitNow(true);
24+
const res = await client.newTxn().mutate(mu);
25+
const uid = res.getUidsMap().get(blankNodeLabel);
26+
expect(uid).not.toEqual("");
27+
return uid;
28+
}
29+
30+
async function performJsonMutation(jsonObj: object, blankNodeLabel: string): Promise<string> {
31+
const mu = new dgraph.Mutation();
32+
mu.setSetJson(jsonObj);
33+
return performMutation(mu, blankNodeLabel);
34+
}
35+
36+
async function performNquadMutation(nquads: string, blankNodeLabel: string): Promise<string> {
37+
const mu = new dgraph.Mutation();
38+
mu.setSetNquads(nquads);
39+
return performMutation(mu, blankNodeLabel);
40+
}
41+
42+
async function performNquadDeletion(nquads: string, blankNodeLabel: string): Promise<string> {
43+
const mu = new dgraph.Mutation();
44+
mu.setDelNquads(nquads);
45+
return performMutation(mu, blankNodeLabel);
46+
}
47+
48+
async function checkIntegrity(updatedProfile: Object, query: string, vars?: object) {
49+
const res = await client.newTxn().queryWithVars(query, vars);
50+
const receivedObject = res.getJson().all[0];
51+
expect(receivedObject).toEqual(updatedProfile);
52+
}
53+
54+
async function upsertDeletionWithVars(): Promise<void> {
55+
const jsonObj = {
56+
uid: "_:prashant",
57+
name: "Prashant",
58+
"dgraph.type": "Person",
59+
};
60+
await performJsonMutation(jsonObj, "prashant");
61+
const expectedObj = {
62+
name: "Prashant",
63+
};
64+
const query = `{
65+
all(func: has(name)) {
66+
name
67+
}
68+
}`;
69+
await checkIntegrity(expectedObj, query);
70+
const deleteJsonObj = {
71+
uid: "uid(user)",
72+
};
73+
const query2 = `query all($userName: string) {
74+
user as all(func: eq(name, $userName))
75+
}`;
76+
const vars = {
77+
$userName: "Prashant",
78+
};
79+
const mu = new dgraph.Mutation();
80+
mu.setDeleteJson(deleteJsonObj);
81+
await performUpsert(mu, query2, vars);
82+
await checkIntegrity(undefined, query);
83+
}
84+
585
describe("delete", () => {
686
it("should delete node", async () => {
7-
const client = await setup();
87+
client = await setup();
888

9-
let mu = new dgraph.Mutation();
10-
mu.setSetNquads('_:alice <name> "Alice" .');
11-
mu.setCommitNow(true);
12-
const ag = await client.newTxn().mutate(mu);
13-
const uid = ag.getUidsMap().get("alice");
89+
const nquads = '_:alice <name> "Alice" .';
90+
const uid = await performNquadMutation(nquads, "alice");
1491

1592
const q = `{
16-
find_bob(func: uid(${uid})) {
93+
all(func: uid(${uid})) {
1794
name
1895
}
1996
}`;
20-
let res = await client.newTxn().query(q);
21-
// tslint:disable-next-line no-unsafe-any
22-
expect(res.getJson().find_bob[0].name).toEqual("Alice");
97+
const expectedJson = {
98+
name: "Alice",
99+
};
100+
await checkIntegrity(expectedJson, q);
23101

24-
mu = new dgraph.Mutation();
25-
mu.setDelNquads(`<${uid}> <name> * .`);
26-
mu.setCommitNow(true);
27-
await client.newTxn().mutate(mu);
102+
const nquads2 = `<${uid}> <name> * .`;
103+
await performNquadDeletion(nquads2, uid.toString());
28104

29-
res = await client.newTxn().query(q);
105+
const res = await client.newTxn().query(q);
30106
// tslint:disable-next-line no-unsafe-any
31-
expect(res.getJson().find_bob).toHaveLength(0);
107+
expect(res.getJson().all).toHaveLength(0);
32108
});
33109

34110
it("should delete edges", async () => {
35-
const client = await setup();
111+
client = await setup();
36112
await setSchema(client, "age: int .\nmarried: bool .");
37113

38-
let mu = new dgraph.Mutation();
39-
mu.setSetJson({
114+
const jsonObj = {
40115
uid: "_:alice",
41116
name: "Alice",
42117
age: 26,
@@ -57,40 +132,48 @@ describe("delete", () => {
57132
age: 29,
58133
},
59134
],
60-
});
61-
mu.setCommitNow(true);
62-
const ag = await client.newTxn().mutate(mu);
63-
const uid = ag.getUidsMap().get("alice");
135+
};
136+
const uid = await performJsonMutation(jsonObj, "alice");
64137

65-
const q = `{
66-
me(func: uid(${uid})) {
67-
uid
138+
const expectedJson = jsonObj;
139+
// tslint:disable-next-line no-dynamic-delete no-string-literal
140+
delete expectedJson["uid"];
141+
const query = `{
142+
all(func: uid(${uid})) {
68143
name
69144
age
70145
loc
71146
married
72-
friends {
73-
uid
147+
schools {
74148
name
75-
age
76149
}
77-
schools {
78-
uid
150+
friends {
79151
name
152+
age
80153
}
81154
}
82155
}`;
83-
let res = await client.newTxn().query(q);
84-
// tslint:disable-next-line no-unsafe-any
85-
expect(res.getJson().me[0].friends.length).toBe(2);
156+
await checkIntegrity(expectedJson, query);
86157

87-
mu = new dgraph.Mutation();
158+
const mu = new dgraph.Mutation();
88159
dgraph.deleteEdges(mu, uid, "friends");
89160
mu.setCommitNow(true);
90161
await client.newTxn().mutate(mu);
91162

92-
res = await client.newTxn().query(q);
163+
const res = await client.newTxn().query(query);
93164
// tslint:disable-next-line no-unsafe-any
94-
expect(res.getJson().me[0].friends).toBeFalsy();
165+
expect(res.getJson().all[0].friends).toBeFalsy();
166+
});
167+
168+
it("should delete a node with upsert using graphql", async () => {
169+
client = await setup();
170+
await setSchema(client, `
171+
name: string @index(hash) .
172+
173+
type Person {
174+
name: string
175+
}
176+
`);
177+
await upsertDeletionWithVars();
95178
});
96179
});

tests/integration/upsert.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,66 @@ async function checkUpsertIntegrity(): Promise<void> {
313313
expect(receivedObject).toEqual(expectedObject);
314314
}
315315

316+
async function doInsertUpsertWithVars(): Promise<void> {
317+
await performMutation(profiles[0]);
318+
const jsonObj = {
319+
uid: "uid(person)",
320+
name: "Prashant Shahi",
321+
email: "prashantshahi@dgraph.io",
322+
age: "24",
323+
};
324+
const query = `query q($email: string) {
325+
person as q(func: eq(email, $email))
326+
}`;
327+
const vars = {
328+
$email: "prashant@dgraph.io",
329+
};
330+
331+
const txn = client.newTxn();
332+
const mu = new dgraph.Mutation();
333+
mu.setSetJson(jsonObj);
334+
335+
const req = new dgraph.Request();
336+
const varsMap = req.getVarsMap();
337+
req.setQuery(query);
338+
Object.keys(vars).forEach((key: string) => {
339+
varsMap.set(key, vars[key]);
340+
});
341+
req.addMutations(mu);
342+
req.setCommitNow(true);
343+
344+
try {
345+
// Update account only if matching uid found.
346+
const response = await txn.doRequest(req);
347+
const uid = response.getUidsMap().get("uid(person)");
348+
expect(uid).not.toEqual("");
349+
} finally {
350+
await txn.discard();
351+
}
352+
353+
const updatedProfile: Profile = {
354+
name: "Prashant Shahi",
355+
email: "prashantshahi@dgraph.io",
356+
age: 24,
357+
};
358+
const query2 = `query q($email: string) {
359+
all(func: eq(email, $email)) {
360+
name
361+
email
362+
age
363+
}
364+
}`;
365+
const vars2 = {
366+
$email: "prashantshahi@dgraph.io",
367+
};
368+
const res = await client.newTxn().queryWithVars(query2, vars2);
369+
const data: {
370+
all: Profile[];
371+
} = res.getJson(); // tslint:disable-line no-unsafe-any
372+
const receivedObject: Profile = data.all[0];
373+
expect(receivedObject).toEqual(updatedProfile);
374+
}
375+
316376
describe("upsert using doRequest", () => {
317377
it("update existing data with upsert", async () => {
318378
client = await setup();
@@ -334,6 +394,16 @@ describe("upsert using doRequest", () => {
334394
await doInsertUpsert();
335395
});
336396

397+
it("create new data with GraphQL variables", async () => {
398+
client = await setup();
399+
await setSchema(client, `
400+
name: string @index(term) .
401+
email: string @index(exact) .
402+
age: int @index(int) .
403+
`);
404+
await doInsertUpsertWithVars();
405+
});
406+
337407
it("successfully performs upsert loadset", async () => {
338408
client = await setup();
339409
await setSchema(client, `

0 commit comments

Comments
 (0)