Repository navigation
Expand file tree
/
Copy pathtest_sync_api.js
More file actions
191 lines (169 loc) · 6.6 KB
/
Copy pathtest_sync_api.js
File metadata and controls
191 lines (169 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const { assert } = require("chai");
const PERSON_IDS = [0, 2, 3, 5, 7, 8, 9, 10];
describe("Query execution", function () {
it("should execute a query synchronously", function () {
const queryResult = conn.querySync(
"MATCH (a:person) RETURN a.ID ORDER BY a.ID"
);
assert.isTrue(queryResult.hasNext());
for (let id of PERSON_IDS) {
const tuple = queryResult.getNextSync();
assert.equal(tuple["a.ID"], id);
}
});
it("should execute a query with multiple statements synchronously", function () {
const queryResults = conn.querySync(
"RETURN 1 AS a; RETURN 2 AS a; RETURN 3 AS a;"
);
for (let i = 1; i <= 3; ++i) {
const queryResult = queryResults[i - 1];
assert.isTrue(queryResult.hasNext());
const tuple = queryResult.getNextSync();
assert.equal(tuple["a"], i);
assert.isFalse(queryResult.hasNext());
}
});
it("should keep child query results usable after closing the parent result", function () {
const queryResults = conn.querySync("RETURN 1 AS first; RETURN 2 AS second;");
const parentResult = queryResults[0];
const childResult = queryResults[1];
const firstChildTuple = childResult.getNextSync();
assert.equal(firstChildTuple["second"], 2);
parentResult.close();
childResult.resetIterator();
const secondChildTuple = childResult.getNextSync();
assert.equal(secondChildTuple["second"], 2);
childResult.resetIterator();
const thirdChildTuple = childResult.getNextSync();
assert.equal(thirdChildTuple["second"], 2);
childResult.close();
});
it("should keep deeper child query results usable after closing earlier results", function () {
const queryResults = conn.querySync(
"RETURN 1 AS first; RETURN 2 AS second; RETURN 3 AS third;"
);
const firstResult = queryResults[0];
const secondResult = queryResults[1];
const thirdResult = queryResults[2];
const firstThirdTuple = thirdResult.getNextSync();
assert.equal(firstThirdTuple["third"], 3);
firstResult.close();
secondResult.close();
thirdResult.resetIterator();
const secondThirdTuple = thirdResult.getNextSync();
assert.equal(secondThirdTuple["third"], 3);
thirdResult.close();
});
it("should allow repeated close on detached query results", function () {
const queryResults = conn.querySync("RETURN 1 AS first; RETURN 2 AS second;");
const parentResult = queryResults[0];
const childResult = queryResults[1];
parentResult.close();
parentResult.close();
childResult.close();
childResult.close();
});
it("should execute a prepared statement synchronously", function () {
const preparedStatement = conn.prepareSync(
"RETURN $a as id"
);
const queryResult = conn.executeSync(
preparedStatement,
{ a: 3 }
);
assert.isTrue(queryResult.hasNext());
const tuple = queryResult.getNextSync();
assert.equal(tuple["id"], 3);
assert.isFalse(queryResult.hasNext());
});
});
describe("Query result", function () {
it("should fail loudly when getAll() is used without await in sync code", async function () {
const queryResult = conn.querySync(
"MATCH (a:person) RETURN a.ID ORDER BY a.ID"
);
const pending = queryResult.getAll();
assert.instanceOf(pending, Promise);
assert.throws(() => JSON.stringify(pending), /getAllSync/);
const rows = await pending;
assert.equal(rows.length, PERSON_IDS.length);
for (let i = 0; i < PERSON_IDS.length; ++i) {
assert.equal(rows[i]["a.ID"], PERSON_IDS[i]);
}
});
it("should get all rows of a query result synchronously", function () {
const queryResult = conn.querySync(
"MATCH (a:person) RETURN a.ID ORDER BY a.ID"
);
const rows = queryResult.getAllSync();
assert.equal(rows.length, PERSON_IDS.length);
for (let i = 0; i < PERSON_IDS.length; ++i) {
assert.equal(rows[i]["a.ID"], PERSON_IDS[i]);
}
});
it("should get column names of a query result synchronously", function () {
const queryResult = conn.querySync(
"MATCH (a:person) RETURN a.ID, a.fName ORDER BY a.ID"
);
const columnNames = queryResult.getColumnNamesSync();
assert.deepEqual(columnNames, ["a.ID", "a.fName"]);
});
it("should get column data types of a query result synchronously", function () {
const queryResult = conn.querySync(
"MATCH (a:person) RETURN a.ID, a.fName ORDER BY a.ID"
);
const columnDataTypes = queryResult.getColumnDataTypesSync();
assert.deepEqual(columnDataTypes, ["INT64", "STRING"]);
});
it("should get query summary of a query result synchronously", function () {
const queryResult = conn.querySync(
"MATCH (a:person) RETURN a.ID, a.fName ORDER BY a.ID"
);
const querySummary = queryResult.getQuerySummarySync();
assert.isTrue(querySummary.hasOwnProperty("compilingTime"));
assert.isTrue(querySummary.hasOwnProperty("executionTime"));
assert.isNumber(querySummary.executionTime);
assert.isNumber(querySummary.compilingTime);
assert.isTrue(querySummary.executionTime >= 0);
assert.isTrue(querySummary.compilingTime >= 0);
});
});
describe("Synchronous initialization", function () {
it("should initialize a database synchronously", function () {
const database = new lbug.Database(":memory:", 1 << 28 /* 256 MB */);
database.initSync();
assert.isTrue(database._isInitialized);
assert.isFalse(database._isClosed);
database.closeSync();
assert.isTrue(database._isClosed);
});
it("should initialize a connection synchronously", function () {
const connection = new lbug.Connection(db);
connection.initSync();
assert.isTrue(connection._isInitialized);
assert.isFalse(connection._isClosed);
connection.closeSync();
assert.isTrue(connection._isClosed);
});
it("should perform initialization automatically for lazily-constructed database and connection", function () {
const database = new lbug.Database(":memory:", 1 << 28 /* 256 MB */);
const connection = new lbug.Connection(database);
assert.isFalse(database._isInitialized);
assert.isFalse(connection._isInitialized);
const queryResult = connection.querySync(
"RETURN 1 AS a, 'hello' AS b"
);
assert.isTrue(database._isInitialized);
assert.isTrue(connection._isInitialized);
const rows = queryResult.getAllSync();
assert.equal(rows.length, 1);
assert.equal(rows[0]["a"], 1);
assert.equal(rows[0]["b"], "hello");
queryResult.close();
connection.closeSync();
assert.isTrue(connection._isClosed);
assert.isFalse(database._isClosed);
database.closeSync();
assert.isTrue(database._isClosed);
});
});