1- import { ArangoError } from "../error" ;
2- import { Database } from "../arangojs" ;
31import { expect } from "chai" ;
2+ import { Database } from "../arangojs" ;
3+ import { ArangoError } from "../error" ;
44
55const range = ( n : number ) : number [ ] => Array . from ( Array ( n ) . keys ( ) ) ;
6- const ARANGO_VERSION = Number ( process . env . ARANGO_VERSION || 30000 ) ;
6+ const ARANGO_VERSION = Number ( process . env . ARANGO_VERSION || 30400 ) ;
7+ const it2x = ARANGO_VERSION < 30000 ? it : it . skip ;
78
89describe ( "Manipulating databases" , function ( ) {
910 // create database takes 11s in a standard cluster
10- this . timeout ( 20000 ) ;
11+ this . timeout ( 20000 ) ;
1112
1213 let db : Database ;
1314 beforeEach ( ( ) => {
@@ -16,6 +17,9 @@ describe("Manipulating databases", function() {
1617 arangoVersion : ARANGO_VERSION
1718 } ) ;
1819 } ) ;
20+ afterEach ( ( ) => {
21+ db . close ( ) ;
22+ } ) ;
1923 describe ( "database.useDatabase" , ( ) => {
2024 it ( "updates the database name" , ( ) => {
2125 const name = "example" ;
@@ -31,169 +35,122 @@ describe("Manipulating databases", function() {
3135 } ) ;
3236 describe ( "database.createDatabase" , ( ) => {
3337 let name = `testdb_${ Date . now ( ) } ` ;
34- afterEach ( done => {
38+ afterEach ( async ( ) => {
3539 db . useDatabase ( "_system" ) ;
36- db
37- . dropDatabase ( name )
38- . then ( ( ) => void done ( ) )
39- . catch ( done ) ;
40+ await db . dropDatabase ( name ) ;
4041 } ) ;
41- it ( "creates a database with the given name" , done => {
42- db
43- . createDatabase ( name )
44- . then ( ( ) => {
45- db . useDatabase ( name ) ;
46- return db . get ( ) ;
47- } )
48- . then ( info => {
49- expect ( info . name ) . to . equal ( name ) ;
50- done ( ) ;
51- } )
52- . catch ( done ) ;
42+ it ( "creates a database with the given name" , async ( ) => {
43+ await db . createDatabase ( name ) ;
44+ db . useDatabase ( name ) ;
45+ const info = await db . get ( ) ;
46+ expect ( info . name ) . to . equal ( name ) ;
5347 } ) ;
5448 it ( "adds the given users to the database" ) ;
5549 } ) ;
5650 describe ( "database.get" , ( ) => {
57- it ( "fetches the database description if the database exists" , done => {
58- db
59- . get ( )
60- . then ( info => {
61- expect ( info . name ) . to . equal ( db . name ) ;
62- expect ( db . name ) . to . equal ( "_system" ) ;
63- done ( ) ;
64- } )
65- . catch ( done ) ;
51+ it ( "fetches the database description if the database exists" , async ( ) => {
52+ const info = await db . get ( ) ;
53+ expect ( info . name ) . to . equal ( db . name ) ;
54+ expect ( db . name ) . to . equal ( "_system" ) ;
6655 } ) ;
67- it ( "fails if the database does not exist" , done => {
56+ it ( "fails if the database does not exist" , async ( ) => {
6857 db . useDatabase ( "__does_not_exist__" ) ;
69- db
70- . get ( )
71- . then (
72- ( ) => Promise . reject ( new Error ( "Should not succeed" ) ) ,
73- err => {
74- expect ( err ) . to . be . an . instanceof ( ArangoError ) ;
75- done ( ) ;
76- }
77- )
78- . catch ( done ) ;
58+ try {
59+ await db . get ( ) ;
60+ } catch ( e ) {
61+ expect ( e ) . to . be . an . instanceof ( ArangoError ) ;
62+ return ;
63+ }
64+ expect . fail ( "should not succeed" ) ;
7965 } ) ;
8066 } ) ;
8167 describe ( "database.listDatabases" , ( ) => {
82- it ( "returns a list of all databases" , done => {
83- db
84- . listDatabases ( )
85- . then ( databases => {
86- expect ( databases ) . to . be . an . instanceof ( Array ) ;
87- expect ( databases . indexOf ( "_system" ) ) . to . be . greaterThan ( - 1 ) ;
88- done ( ) ;
89- } )
90- . catch ( done ) ;
68+ it ( "returns a list of all databases" , async ( ) => {
69+ const databases = await db . listDatabases ( ) ;
70+ expect ( databases ) . to . be . an . instanceof ( Array ) ;
71+ expect ( databases . indexOf ( "_system" ) ) . to . be . greaterThan ( - 1 ) ;
9172 } ) ;
9273 } ) ;
9374 describe ( "database.listUserDatabases" , ( ) => {
9475 it ( "returns a list of databases accessible to the active user" ) ;
9576 } ) ;
9677 describe ( "database.dropDatabase" , ( ) => {
9778 let name = `testdb_${ Date . now ( ) } ` ;
98- beforeEach ( done => {
99- db
100- . createDatabase ( name )
101- . then ( ( ) => void done ( ) )
102- . catch ( done ) ;
79+ beforeEach ( async ( ) => {
80+ await db . createDatabase ( name ) ;
10381 } ) ;
104- it ( "deletes the given database from the server" , done => {
105- db
106- . dropDatabase ( name )
107- . then ( ( ) => new Database ( ) . useDatabase ( name ) . get ( ) )
108- . then ( ( ) => Promise . reject ( new Error ( "Should not succeed" ) ) , ( ) => null )
109- . then ( ( ) => void done ( ) )
110- . catch ( done ) ;
82+ it ( "deletes the given database from the server" , async ( ) => {
83+ await db . dropDatabase ( name ) ;
84+ let temp = new Database ( ) . useDatabase ( name ) ;
85+ try {
86+ await temp . get ( ) ;
87+ } catch ( e ) {
88+ return ;
89+ } finally {
90+ temp . close ( ) ;
91+ }
92+ expect . fail ( "should not succeed" ) ;
11193 } ) ;
11294 } ) ;
11395 describe ( "database.truncate" , ( ) => {
11496 let name = `testdb_${ Date . now ( ) } ` ;
11597 let nonSystemCollections = range ( 4 ) . map ( i => `c_${ Date . now ( ) } _${ i } ` ) ;
11698 let systemCollections = range ( 4 ) . map ( i => `_c_${ Date . now ( ) } _${ i } ` ) ;
117- beforeEach ( done => {
118- db
119- . createDatabase ( name )
120- . then ( ( ) => {
121- db . useDatabase ( name ) ;
122- return Promise . all ( [
123- ...nonSystemCollections . map ( name => {
124- let collection = db . collection ( name ) ;
125- return collection
126- . create ( )
127- . then ( ( ) => collection . save ( { _key : "example" } ) ) ;
128- } ) ,
129- ...systemCollections . map ( name => {
130- let collection = db . collection ( name ) ;
131- return collection
132- . create ( { isSystem : true } )
133- . then ( ( ) => collection . save ( { _key : "example" } ) ) ;
134- } )
135- ] ) ;
99+ beforeEach ( async ( ) => {
100+ await db . createDatabase ( name ) ;
101+ db . useDatabase ( name ) ;
102+ await Promise . all ( [
103+ ...nonSystemCollections . map ( async name => {
104+ let collection = db . collection ( name ) ;
105+ await collection . create ( ) ;
106+ return await collection . save ( { _key : "example" } ) ;
107+ } ) ,
108+ ...systemCollections . map ( async name => {
109+ let collection = db . collection ( name ) ;
110+ await collection . create ( { isSystem : true } ) ;
111+ return await collection . save ( { _key : "example" } ) ;
136112 } )
137- . then ( ( ) => void done ( ) )
138- . catch ( done ) ;
113+ ] ) ;
139114 } ) ;
140- afterEach ( done => {
115+ afterEach ( async ( ) => {
141116 db . useDatabase ( "_system" ) ;
142- db
143- . dropDatabase ( name )
144- . then ( ( ) => void done ( ) )
145- . catch ( done ) ;
117+ await db . dropDatabase ( name ) ;
146118 } ) ;
147- it ( "removes all documents from all non-system collections in the database" , done => {
148- db
149- . truncate ( )
150- . then ( ( ) => {
151- return Promise . all ( [
152- ...nonSystemCollections . map ( name =>
153- db
154- . collection ( name )
155- . document ( "example" )
156- . then (
157- doc =>
158- Promise . reject (
159- new Error ( `Expected document to be destroyed: ${ doc . _id } ` )
160- ) ,
161- err => expect ( err ) . to . be . an . instanceof ( ArangoError )
162- )
163- ) ,
164- ...systemCollections . map ( name =>
165- db . collection ( name ) . document ( "example" )
166- )
167- ] ) ;
168- } )
169- . then ( ( ) => void done ( ) )
170- . catch ( done ) ;
119+ it ( "removes all documents from all non-system collections in the database" , async ( ) => {
120+ await db . truncate ( ) ;
121+ await Promise . all ( [
122+ ...nonSystemCollections . map ( async name => {
123+ let doc ;
124+ try {
125+ doc = await db . collection ( name ) . document ( "example" ) ;
126+ } catch ( e ) {
127+ expect ( e ) . to . be . an . instanceof ( ArangoError ) ;
128+ return ;
129+ }
130+ expect . fail ( `Expected document to be destroyed: ${ doc . _id } ` ) ;
131+ } ) ,
132+ ...systemCollections . map ( name =>
133+ db . collection ( name ) . document ( "example" )
134+ )
135+ ] ) ;
171136 } ) ;
172- if ( ARANGO_VERSION < 30000 ) {
173- it ( "additionally truncates system collections if explicitly passed false" , done => {
174- db
175- . truncate ( false )
176- . then ( ( ) => {
177- return Promise . all (
178- nonSystemCollections . map ( name =>
179- db
180- . collection ( name )
181- . document ( "example" )
182- . then (
183- doc =>
184- Promise . reject (
185- new Error (
186- `Expected document to be destroyed: ${ doc . _id } `
187- )
188- ) ,
189- err => expect ( err ) . to . be . an . instanceof ( ArangoError )
190- )
191- )
192- ) ;
137+ it2x (
138+ "additionally truncates system collections if explicitly passed false" ,
139+ async ( ) => {
140+ await db . truncate ( false ) ;
141+ await Promise . all (
142+ nonSystemCollections . map ( async name => {
143+ let doc ;
144+ try {
145+ doc = await db . collection ( name ) . document ( "example" ) ;
146+ } catch ( e ) {
147+ expect ( e ) . to . be . an . instanceof ( ArangoError ) ;
148+ return ;
149+ }
150+ expect . fail ( `Expected document to be destroyed: ${ doc . _id } ` ) ;
193151 } )
194- . then ( ( ) => void done ( ) )
195- . catch ( done ) ;
196- } ) ;
197- }
152+ ) ;
153+ }
154+ ) ;
198155 } ) ;
199156} ) ;
0 commit comments