Skip to content

Commit 40531a5

Browse files
authored
Enhance test coverage
1 parent 7c0b813 commit 40531a5

13 files changed

+1725
-116
lines changed

src/test/04-transactions.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
import { Database } from "../arangojs";
2+
import { expect } from "chai";
3+
14
describe("Transactions", () => {
5+
let db: Database;
6+
before(done => {
7+
db = new Database({
8+
url: process.env.TEST_ARANGODB_URL || "http://localhost:8529",
9+
arangoVersion: Number(process.env.ARANGO_VERSION || 30000)
10+
});
11+
done();
12+
});
213
describe("database.transaction", () => {
3-
it("is missing tests");
14+
it("should execute a transaction and return the result", done => {
15+
db
16+
.transaction([], "function (params) {return params;}", "test")
17+
.then(result => {
18+
expect(result).to.equal("test");
19+
})
20+
.then(() => done())
21+
.catch(done);
22+
});
423
});
524
});

src/test/06-managing-functions.ts

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,94 @@
1+
import { Database } from "../arangojs";
2+
import { expect } from "chai";
3+
14
describe("Managing functions", () => {
5+
let name = `testdb_${Date.now()}`;
6+
let db: Database;
7+
before(done => {
8+
db = new Database({
9+
url: process.env.TEST_ARANGODB_URL || "http://localhost:8529",
10+
arangoVersion: Number(process.env.ARANGO_VERSION || 30000)
11+
});
12+
db
13+
.createDatabase(name)
14+
.then(() => {
15+
db.useDatabase(name);
16+
done();
17+
})
18+
.catch(done);
19+
});
20+
after(done => {
21+
db.useDatabase("_system");
22+
db
23+
.dropDatabase(name)
24+
.then(() => void done())
25+
.catch(done);
26+
});
227
describe("database.listFunctions", () => {
3-
it("is missing tests");
28+
it("should be empty per default", done => {
29+
db
30+
.listFunctions()
31+
.then(info => {
32+
expect(info).to.have.property("result");
33+
expect(info.result).to.be.instanceof(Array);
34+
expect(info.result).to.be.empty;
35+
})
36+
.then(() => done())
37+
.catch(done);
38+
});
39+
it("should include before created function", done => {
40+
const name = "myfunctions::temperature::celsiustofahrenheit";
41+
const code = "function (celsius) { return celsius * 1.8 + 32; }";
42+
db
43+
.createFunction(name, code)
44+
.then(() => {
45+
return db.listFunctions().then(info => {
46+
expect(info).to.have.property("result");
47+
expect(info.result).to.be.instanceof(Array);
48+
expect(info.result.length).to.equal(1);
49+
expect(info.result[0]).to.eql({
50+
name,
51+
code,
52+
isDeterministic: false
53+
});
54+
});
55+
})
56+
.then(() => done())
57+
.catch(done);
58+
});
459
});
560
describe("database.createFunction", () => {
6-
it("is missing tests");
61+
it("should create a function", done => {
62+
db
63+
.createFunction(
64+
"myfunctions::temperature::celsiustofahrenheit",
65+
"function (celsius) { return celsius * 1.8 + 32; }"
66+
)
67+
.then(info => {
68+
expect(info).to.eql({
69+
code: 200,
70+
error: false
71+
});
72+
})
73+
.then(() => done())
74+
.catch(done);
75+
});
776
});
877
describe("database.dropFunction", () => {
9-
it("is missing tests");
78+
it("should drop a existing function", done => {
79+
const name = "myfunctions::temperature::celsiustofahrenheit";
80+
db
81+
.createFunction(
82+
name,
83+
"function (celsius) { return celsius * 1.8 + 32; }"
84+
)
85+
.then(() => {
86+
return db.dropFunction(name).then(info => {
87+
expect(info).to.have.property("deletedCount", 1);
88+
});
89+
})
90+
.then(() => done())
91+
.catch(done);
92+
});
1093
});
1194
});

src/test/07-routes.ts

Lines changed: 195 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Database } from "../arangojs";
2+
import { DocumentCollection } from "../collection";
23
import { Route } from "../route";
34
import { expect } from "chai";
45

@@ -9,44 +10,226 @@ describe("Arbitrary HTTP routes", () => {
910
});
1011
describe("database.route", () => {
1112
it("returns a Route instance", () => {
12-
let route = db.route();
13+
const route = db.route();
1314
expect(route).to.be.an.instanceof(Route);
1415
});
1516
it("creates a route for the given path", () => {
16-
let path = "/hi";
17-
let route = db.route(path);
17+
const path = "/hi";
18+
const route = db.route(path);
1819
expect((route as any)._path).to.equal(path);
1920
});
2021
it("passes the given headers to the new route", () => {
21-
let route = db.route("/hello", { "x-magic": "awesome" });
22+
const route = db.route("/hello", { "x-magic": "awesome" });
2223
expect((route as any)._headers).to.have.property("x-magic", "awesome");
2324
});
2425
});
2526
});
2627

2728
describe("Route API", () => {
29+
const name = `testdb_${Date.now()}`;
30+
let db: Database;
31+
let collection: DocumentCollection;
32+
before(done => {
33+
db = new Database({
34+
url: process.env.TEST_ARANGODB_URL || "http://localhost:8529",
35+
arangoVersion: Number(process.env.ARANGO_VERSION || 30000)
36+
});
37+
db
38+
.createDatabase(name)
39+
.then(() => {
40+
db.useDatabase(name);
41+
collection = db.collection(`c_${Date.now()}`);
42+
return collection.create();
43+
})
44+
.then(() => done())
45+
.catch(done);
46+
});
47+
after(done => {
48+
db.useDatabase("_system");
49+
db
50+
.dropDatabase(name)
51+
.then(() => done())
52+
.catch(done);
53+
});
54+
beforeEach(done => {
55+
collection
56+
.truncate()
57+
.then(() => done())
58+
.catch(done);
59+
});
2860
describe("route.route", () => {
29-
it("is missing tests");
61+
it("should concat path", () => {
62+
const route = db.route("/api").route("/version");
63+
expect(route).to.have.property("_path", "/api/version");
64+
});
3065
});
3166
describe("route.get", () => {
32-
it("is missing tests");
67+
it("should be executed using the route path", done => {
68+
db
69+
.route("/_api/version")
70+
.get()
71+
.then(res => {
72+
expect(res).to.have.property("body");
73+
const body = res.body;
74+
expect(body).to.have.property("version");
75+
expect(body).to.have.property("server");
76+
})
77+
.then(() => done())
78+
.catch(done);
79+
});
80+
it("should concat path to route path", done => {
81+
db
82+
.route("/_api")
83+
.get("/version")
84+
.then(res => {
85+
expect(res).to.have.property("body");
86+
const body = res.body;
87+
expect(body).to.have.property("version");
88+
expect(body).to.have.property("server");
89+
})
90+
.then(() => done())
91+
.catch(done);
92+
});
93+
it("should passes query parameters", done => {
94+
db
95+
.route("/_api")
96+
.get("/version", { details: true })
97+
.then(res => {
98+
expect(res).to.have.property("body");
99+
const body = res.body;
100+
expect(body).to.have.property("version");
101+
expect(body).to.have.property("server");
102+
expect(body).to.have.property("details");
103+
})
104+
.then(() => done())
105+
.catch(done);
106+
});
33107
});
34108
describe("route.post", () => {
35-
it("is missing tests");
109+
it("should passes body", done => {
110+
db
111+
.route(`/_api/document/${collection.name}`)
112+
.post({ foo: "bar" })
113+
.then(res => {
114+
expect(res).to.have.property("body");
115+
expect(res.body).to.have.property("_id");
116+
expect(res.body).to.have.property("_key");
117+
expect(res.body).to.have.property("_rev");
118+
})
119+
.then(() => done())
120+
.catch(done);
121+
});
36122
});
37123
describe("route.put", () => {
38-
it("is missing tests");
124+
let documentHandle: String;
125+
beforeEach(done => {
126+
collection
127+
.save({ foo: "bar" })
128+
.then(doc => {
129+
documentHandle = doc._id;
130+
done();
131+
})
132+
.catch(done);
133+
});
134+
it("should passes body", done => {
135+
db
136+
.route(`/_api/document/${documentHandle}`)
137+
.put({ hello: "world" })
138+
.then(res => {
139+
expect(res).to.have.property("body");
140+
expect(res.body).to.have.property("_id");
141+
expect(res.body).to.have.property("_key");
142+
expect(res.body).to.have.property("_rev");
143+
})
144+
.then(() => done())
145+
.catch(done);
146+
});
39147
});
40148
describe("route.patch", () => {
41-
it("is missing tests");
149+
let documentHandle: String;
150+
beforeEach(done => {
151+
collection
152+
.save({ foo: "bar" })
153+
.then(doc => {
154+
documentHandle = doc._id;
155+
done();
156+
})
157+
.catch(done);
158+
});
159+
it("should passes body", done => {
160+
db
161+
.route(`/_api/document/${documentHandle}`)
162+
.patch({ hello: "world" })
163+
.then(res => {
164+
expect(res).to.have.property("body");
165+
expect(res.body).to.have.property("_id");
166+
expect(res.body).to.have.property("_key");
167+
expect(res.body).to.have.property("_rev");
168+
})
169+
.then(() => done())
170+
.catch(done);
171+
});
42172
});
43173
describe("route.delete", () => {
44-
it("is missing tests");
174+
let documentHandle: String;
175+
beforeEach(done => {
176+
collection
177+
.save({ foo: "bar" })
178+
.then(doc => {
179+
documentHandle = doc._id;
180+
done();
181+
})
182+
.catch(done);
183+
});
184+
it("should be executed using the route path", done => {
185+
db
186+
.route(`/_api/document/${documentHandle}`)
187+
.delete()
188+
.then(res => {
189+
expect(res).to.have.property("body");
190+
expect(res.body).to.have.property("_id");
191+
expect(res.body).to.have.property("_key");
192+
expect(res.body).to.have.property("_rev");
193+
})
194+
.then(() => done())
195+
.catch(done);
196+
});
45197
});
46198
describe("route.head", () => {
47-
it("is missing tests");
199+
let documentHandle: String;
200+
beforeEach(done => {
201+
collection
202+
.save({ foo: "bar" })
203+
.then(doc => {
204+
documentHandle = doc._id;
205+
done();
206+
})
207+
.catch(done);
208+
});
209+
it("should be executed using the route path", done => {
210+
db
211+
.route(`/_api/document/${documentHandle}`)
212+
.head()
213+
.then(res => {
214+
expect(res).to.have.property("statusCode", 200);
215+
})
216+
.then(() => done())
217+
.catch(done);
218+
});
48219
});
49220
describe("route.request", () => {
50-
it("is missing tests");
221+
it("should be executed using the route path", done => {
222+
db
223+
.route("/_api/version")
224+
.request("get")
225+
.then(res => {
226+
expect(res).to.have.property("body");
227+
const body = res.body;
228+
expect(body).to.have.property("version");
229+
expect(body).to.have.property("server");
230+
})
231+
.then(() => done())
232+
.catch(done);
233+
});
51234
});
52235
});

0 commit comments

Comments
 (0)