Skip to content

Commit 64dfa8c

Browse files
committed
Fix wrapping of proto classes and simplify test cases
1 parent bdb9110 commit 64dfa8c

File tree

10 files changed

+51
-38
lines changed

10 files changed

+51
-38
lines changed

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class DgraphClient {
4343
this.debug(`Alter request:\n${stringifyMessage(op)}`);
4444

4545
const c = this.anyClient();
46-
const pl = <types.Payload>(await c.alter(op));
46+
const pl = types.createPayload(await c.alter(op));
4747
this.debug(`Alter response:\n${stringifyMessage(pl)}`);
4848

4949
return pl;

src/txn.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import * as messages from "../generated/api_pb";
33
import { DgraphClient } from "./client";
44
import { ERR_ABORTED, ERR_FINISHED } from "./errors";
55
import * as types from "./types";
6-
import { isAbortedError, isConflictError, mergeLinReads, stringifyMessage } from "./util";
6+
import {
7+
isAbortedError,
8+
isConflictError,
9+
mergeLinReads,
10+
stringifyMessage,
11+
} from "./util";
712

813
/**
914
* Txn is a single atomic transaction.
@@ -69,7 +74,7 @@ export class Txn {
6974
this.dc.debug(`Query request:\n${stringifyMessage(req)}`);
7075

7176
const c = this.dc.anyClient();
72-
const res = <types.Response>(await c.query(req));
77+
const res = types.createResponse(await c.query(req));
7378
this.mergeContext(res.getTxn());
7479
this.dc.debug(`Query response:\n${stringifyMessage(res)}`);
7580

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export class Payload extends messages.Payload {
5757
}
5858
}
5959

60+
export function createPayload(oldPayload: messages.Payload): Payload {
61+
return messages.Payload.deserializeBinaryFromReader(
62+
new Payload(),
63+
new jspb.BinaryReader(oldPayload.serializeBinary()),
64+
);
65+
}
66+
6067
// Query classes.
6168

6269
/**
@@ -108,6 +115,13 @@ export class Response extends messages.Response {
108115
}
109116
}
110117

118+
export function createResponse(oldResponse: messages.Response): Response {
119+
return messages.Response.deserializeBinaryFromReader(
120+
new Response(),
121+
new jspb.BinaryReader(oldResponse.serializeBinary()),
122+
);
123+
}
124+
111125
// Mutate classes.
112126

113127
/**

tests/integration/acctUpsert.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as dgraph from "../../src";
2-
import { strToU8, u8ToStr } from "../../src/util";
32

43
import { setSchema, setup } from "../helper";
54

@@ -43,7 +42,7 @@ async function tryUpsert(account: Account): Promise<void> {
4342
const res = await txn.query(q);
4443
const resJson: {
4544
find: { uid: string }[];
46-
} = JSON.parse(u8ToStr(res.getJson_asU8())); // tslint:disable-line no-unsafe-any
45+
} = res.getJson(); // tslint:disable-line no-unsafe-any
4746
expect(resJson.find.length).toBeLessThanOrEqual(1);
4847

4948
let mu: dgraph.Mutation;
@@ -52,11 +51,11 @@ async function tryUpsert(account: Account): Promise<void> {
5251
uid = resJson.find[0].uid;
5352
} else {
5453
mu = new dgraph.Mutation();
55-
mu.setSetNquads(strToU8(`
54+
mu.setSetNquads(`
5655
_:acct <first> "${account.first}" .
5756
_:acct <last> "${account.last}" .
5857
_:acct <age> "${account.age}"^^<xs:int> .
59-
`));
58+
`);
6059

6160
const ag = await txn.mutate(mu);
6261
uid = ag.getUidsMap().get("acct");
@@ -65,7 +64,7 @@ async function tryUpsert(account: Account): Promise<void> {
6564

6665
mu = new dgraph.Mutation();
6766
// Time used here is in milliseconds.
68-
mu.setSetNquads(strToU8(`<${uid}> <when> "${new Date().getTime()}"^^<xs:int> .`));
67+
mu.setSetNquads(`<${uid}> <when> "${new Date().getTime()}"^^<xs:int> .`);
6968
await txn.mutate(mu);
7069

7170
await txn.commit();
@@ -142,7 +141,7 @@ async function checkIntegrity(): Promise<void> {
142141

143142
const data: {
144143
all: Account[];
145-
} = JSON.parse(u8ToStr(res.getJson_asU8())); // tslint:disable-line no-unsafe-any
144+
} = res.getJson(); // tslint:disable-line no-unsafe-any
146145

147146
const accountSet: { [key: string]: boolean } = {};
148147
for (const account of data.all) {

tests/integration/bank.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as dgraph from "../../src";
2-
import { strToU8, u8ToStr } from "../../src/util";
32

43
import { setSchema, setup, wait } from "../helper";
54

@@ -32,7 +31,7 @@ async function createAccounts(): Promise<void> {
3231

3332
const txn = client.newTxn();
3433
const mu = new dgraph.Mutation();
35-
mu.setSetJson(strToU8(JSON.stringify(accounts)));
34+
mu.setSetJson(accounts);
3635
const ag = await txn.mutate(mu);
3736
await txn.commit();
3837

@@ -58,7 +57,7 @@ async function runTotal(): Promise<void> {
5857
}
5958
}`);
6059
// tslint:disable-next-line no-unsafe-any
61-
expect(JSON.parse(u8ToStr(res.getJson_asU8())).total[0].bal).toBe(uids.length * initialBalance);
60+
expect(res.getJson().total[0].bal).toBe(uids.length * initialBalance);
6261

6362
// tslint:disable-next-line no-console
6463
console.log(`Runs: ${runs}, Aborts: ${aborts}, Total Time: ${new Date().getTime() - startStatus} ms`);
@@ -94,14 +93,14 @@ async function runTxn(): Promise<void> {
9493
const accountsWithUid: {
9594
uid: string;
9695
bal: number;
97-
}[] = JSON.parse(u8ToStr(res.getJson_asU8())).both; // tslint:disable-line no-unsafe-any
96+
}[] = res.getJson().both; // tslint:disable-line no-unsafe-any
9897
expect(accountsWithUid).toHaveLength(2);
9998

10099
accountsWithUid[0].bal += 5;
101100
accountsWithUid[1].bal -= 5;
102101

103102
const mu = new dgraph.Mutation();
104-
mu.setSetJson(strToU8(JSON.stringify(accountsWithUid)));
103+
mu.setSetJson(accountsWithUid);
105104
await txn.mutate(mu);
106105
await txn.commit();
107106
} finally {

tests/integration/conflict.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as dgraph from "../../src";
2-
import { strToU8 } from "../../src/util";
32

43
import { setup } from "../helper";
54

@@ -10,13 +9,13 @@ describe("conflict", () => {
109
const txn1 = client.newTxn();
1110

1211
let mu = new dgraph.Mutation();
13-
mu.setSetNquads(strToU8('_:alice <name> "Alice" .'));
12+
mu.setSetNquads('_:alice <name> "Alice" .');
1413
const ag = await txn1.mutate(mu);
1514
const uid = ag.getUidsMap().get("alice");
1615

1716
const txn2 = client.newTxn();
1817
mu = new dgraph.Mutation();
19-
mu.setSetNquads(strToU8(`<${uid}> <name> "Alice" .`));
18+
mu.setSetNquads(`<${uid}> <name> "Alice" .`);
2019
await txn2.mutate(mu);
2120

2221
const p1 = txn1.commit();

tests/integration/delete.spec.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as dgraph from "../../src";
2-
import { strToU8, u8ToStr } from "../../src/util";
32

43
import { setSchema, setup } from "../helper";
54

@@ -8,7 +7,7 @@ describe("delete", () => {
87
const client = await setup();
98

109
let mu = new dgraph.Mutation();
11-
mu.setSetNquads(strToU8('_:alice <name> "Alice" .'));
10+
mu.setSetNquads('_:alice <name> "Alice" .');
1211
mu.setCommitNow(true);
1312
const ag = await client.newTxn().mutate(mu);
1413
const uid = ag.getUidsMap().get("alice");
@@ -20,24 +19,24 @@ describe("delete", () => {
2019
}`;
2120
let res = await client.newTxn().query(q);
2221
// tslint:disable-next-line no-unsafe-any
23-
expect(JSON.parse(u8ToStr(res.getJson_asU8())).find_bob[0].name).toEqual("Alice");
22+
expect(res.getJson().find_bob[0].name).toEqual("Alice");
2423

2524
mu = new dgraph.Mutation();
26-
mu.setDelNquads(strToU8(`<${uid}> * * .`));
25+
mu.setDelNquads(`<${uid}> * * .`);
2726
mu.setCommitNow(true);
2827
await client.newTxn().mutate(mu);
2928

3029
res = await client.newTxn().query(q);
3130
// tslint:disable-next-line no-unsafe-any
32-
expect(JSON.parse(u8ToStr(res.getJson_asU8())).find_bob).toHaveLength(0);
31+
expect(res.getJson().find_bob).toHaveLength(0);
3332
});
3433

3534
it("should delete edges", async () => {
3635
const client = await setup();
3736
await setSchema(client, "age: int .\nmarried: bool .");
3837

3938
let mu = new dgraph.Mutation();
40-
mu.setSetJson(strToU8(JSON.stringify({
39+
mu.setSetJson({
4140
name: "Alice",
4241
age: 26,
4342
loc: "Riley Street",
@@ -57,7 +56,7 @@ describe("delete", () => {
5756
age: 29,
5857
},
5958
],
60-
})));
59+
});
6160
mu.setCommitNow(true);
6261
const ag = await client.newTxn().mutate(mu);
6362
const uid = ag.getUidsMap().get("blank-0");
@@ -82,7 +81,7 @@ describe("delete", () => {
8281
}`;
8382
let res = await client.newTxn().query(q);
8483
// tslint:disable-next-line no-unsafe-any
85-
expect(JSON.parse(u8ToStr(res.getJson_asU8())).me[0].friends.length).toBe(2);
84+
expect(res.getJson().me[0].friends.length).toBe(2);
8685

8786
mu = new dgraph.Mutation();
8887
dgraph.deleteEdges(mu, uid, "friends");
@@ -91,6 +90,6 @@ describe("delete", () => {
9190

9291
res = await client.newTxn().query(q);
9392
// tslint:disable-next-line no-unsafe-any
94-
expect(JSON.parse(u8ToStr(res.getJson_asU8())).me[0].friends).toBeFalsy();
93+
expect(res.getJson().me[0].friends).toBeFalsy();
9594
});
9695
});

tests/integration/mutate.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as dgraph from "../../src";
2-
import { u8ToStr } from "../../src/util";
32

43
import { setSchema, setup } from "../helper";
54

@@ -39,6 +38,6 @@ describe("mutate", () => {
3938
const res = await txn.query(query);
4039
await txn.commit();
4140

42-
expect(u8ToStr(res.getJson_asU8())).toEqual('{"me":[{"name":"ok 200"},{"name":"ok 300"},{"name":"ok 400"}]}');
41+
expect(res.getJson()).toEqual({ me: [{ name: "ok 200" }, { name: "ok 300" }, { name: "ok 400" }] });
4342
});
4443
});

tests/txn.spec.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as dgraph from "../src";
2-
import { strToU8, u8ToStr } from "../src/util";
32

43
import { setSchema, setup } from "./helper";
54

@@ -18,7 +17,7 @@ describe("txn", () => {
1817

1918
const mu = new dgraph.Mutation();
2019
mu.setCommitNow(true);
21-
mu.setSetNquads(strToU8('_:alice <name> "Alice" .'));
20+
mu.setSetNquads('_:alice <name> "Alice" .');
2221
await client.newTxn().mutate(mu);
2322
});
2423

@@ -31,7 +30,7 @@ describe("txn", () => {
3130
);
3231
let resJson: {
3332
me: { name: string }[];
34-
} = JSON.parse(u8ToStr(res.getJson_asU8())); // tslint:disable-line no-unsafe-any
33+
} = res.getJson(); // tslint:disable-line no-unsafe-any
3534
expect(resJson.me).toHaveLength(1);
3635
expect(resJson.me[0].name).toEqual("Alice");
3736

@@ -42,7 +41,7 @@ describe("txn", () => {
4241
$b: true, // non-string properties are ignored
4342
},
4443
);
45-
resJson = JSON.parse(u8ToStr(res.getJson_asU8())); // tslint:disable-line no-unsafe-any
44+
resJson = res.getJson(); // tslint:disable-line no-unsafe-any
4645
expect(resJson.me).toHaveLength(1);
4746
expect(resJson.me[0].name).toEqual("Alice");
4847
});
@@ -56,7 +55,7 @@ describe("txn", () => {
5655
);
5756
const resJson: {
5857
me: { name: string }[];
59-
} = JSON.parse(u8ToStr(res.getJson_asU8())); // tslint:disable-line no-unsafe-any
58+
} = res.getJson(); // tslint:disable-line no-unsafe-any
6059
expect(resJson.me).toHaveLength(0);
6160
});
6261

@@ -83,7 +82,7 @@ describe("txn", () => {
8382
await txn.commit();
8483

8584
const mu = new dgraph.Mutation();
86-
mu.setSetNquads(strToU8('_:alice <name> "Alice" .'));
85+
mu.setSetNquads('_:alice <name> "Alice" .');
8786
const p = txn.mutate(mu);
8887
await expect(p).rejects.toBe(dgraph.ERR_FINISHED);
8988
});
@@ -93,7 +92,7 @@ describe("txn", () => {
9392

9493
const mu = new dgraph.Mutation();
9594
// There is an error in the mutation NQuad.
96-
mu.setSetNquads(strToU8('alice <name> "Alice" .'));
95+
mu.setSetNquads('alice <name> "Alice" .');
9796
const p1 = txn.mutate(mu);
9897
await expect(p1).rejects.toBeDefined();
9998

@@ -120,7 +119,7 @@ describe("txn", () => {
120119
const txn = client.newTxn();
121120

122121
const mu = new dgraph.Mutation();
123-
mu.setSetNquads(strToU8('_:alice <name> "Alice" .'));
122+
mu.setSetNquads('_:alice <name> "Alice" .');
124123
mu.setCommitNow(true);
125124
await txn.mutate(mu);
126125

@@ -147,7 +146,7 @@ describe("txn", () => {
147146
const txn = client.newTxn();
148147

149148
const mu = new dgraph.Mutation();
150-
mu.setSetNquads(strToU8('_:alice <name> "Alice" .'));
149+
mu.setSetNquads('_:alice <name> "Alice" .');
151150
mu.setCommitNow(true);
152151
await txn.mutate(mu);
153152

tests/types.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as dgraph from "../src";
22
import { b64ToStr, strToB64, strToU8, u8ToStr } from "../src/util";
33

44
const obj = { k1: "v1" };
5-
const setNquads = "_:alice <name> \"Alice\"";
5+
const setNquads = '_:alice <name> "Alice"';
66
const delNquads = "<0x7569> * * .";
77

88
describe("types", () => {

0 commit comments

Comments
 (0)