-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
181 lines (163 loc) · 6.07 KB
/
index.test.js
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
const ComfyDB = require( "./index" );
test( "connects to MongoDB", async () => {
await ComfyDB.Connect();
expect( ComfyDB.IsConnected() ).toBe( true );
});
test( "gets undefined for non-existent values", async () => {
expect( await ComfyDB.Get( "nonexistent" ) ).toBeUndefined();
});
test( "saves and gets values", async () => {
await ComfyDB.Store( "test", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "test" ) ).name ).toBe( "Instafluff" );
});
test( "deletes value", async () => {
await ComfyDB.Delete( "test" );
expect( await ComfyDB.Get( "test" ) ).toBeUndefined();
});
test( "sets _id", async () => {
await ComfyDB.Store( "identifier", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "identifier" ) )._id ).toBeDefined();
});
test( "adds createdAt", async () => {
await ComfyDB.Store( "created", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "created" ) ).createdAt ).toBeDefined();
});
test( "adds updatedAt", async () => {
await ComfyDB.Store( "updated", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "updated" ) ).updatedAt ).toBeDefined();
});
test( "search returns all results", async () => {
for( let i = 0; i < 5; i++ ) {
await ComfyDB.Store( `test${i}`, { value: `test${i}` } );
}
let results = await ComfyDB.Search();
for( let i = 0; i < 5; i++ ) {
expect( ( await ComfyDB.Get( `test${i}` ) ).value ).toBe( `test${i}` );
}
});
test( "search returns correct results", async () => {
for( let i = 0; i < 10; i++ ) {
await ComfyDB.Store( `ignore${i}`, { value: `ignore${i}` } );
}
for( let i = 0; i < 5; i++ ) {
await ComfyDB.Store( `search${i}`, { value: `search${i}` } );
}
let results = await ComfyDB.Search( { where: { value: { contains: "search" } } } );
expect( results.length ).toBe( 5 );
for( let i = 0; i < 5; i++ ) {
expect( results[ i ].value ).toBe( `search${i}` );
}
});
test( "sorts results ascending", async () => {
for( let i = 5; i > 0; i-- ) {
await ComfyDB.Store( `ascending${i}`, { value: `ascending${i}` } );
}
let results = await ComfyDB.Search( { sortBy: "value", sort: "asc", where: { value: { contains: "ascending" } } } );
expect( results.length ).toBe( 5 );
for( let i = 0; i < 5; i++ ) {
expect( results[ i ].value ).toBe( `ascending${1 + i}` );
}
});
test( "sorts results descending", async () => {
for( let i = 0; i < 5; i++ ) {
await ComfyDB.Store( `descending${i}`, { value: `descending${i}` } );
}
let results = await ComfyDB.Search( { sortBy: "value", sort: "desc", where: { value: { contains: "descending" } } } );
expect( results.length ).toBe( 5 );
for( let i = 0; i < 5; i++ ) {
expect( results[ i ].value ).toBe( `descending${4 - i}` );
}
});
test( "start offsets results", async () => {
for( let i = 0; i < 10; i++ ) {
await ComfyDB.Store( `start${i}`, { value: `start${i}` } );
}
let results = await ComfyDB.Search( { start: 5, where: { value: { contains: "start" } } } );
expect( results.length ).toBe( 5 );
for( let i = 0; i < 5; i++ ) {
expect( results[ i ].value ).toBe( `start${i + 5}` );
}
});
test( "limit limits results", async () => {
for( let i = 0; i < 10; i++ ) {
await ComfyDB.Store( `limit${i}`, { value: `limit${i}` } );
}
let results = await ComfyDB.Search( { limit: 5, where: { value: { contains: "limit" } } } );
expect( results.length ).toBe( 5 );
for( let i = 0; i < 5; i++ ) {
expect( results[ i ].value ).toBe( `limit${i}` );
}
});
test( "count is accurate", async () => {
for( let i = 0; i < 10; i++ ) {
await ComfyDB.Store( `count${i}`, { value: `count${i}` } );
}
let result = await ComfyDB.Count( { where: { value: { contains: "count" } } } );
expect( result ).toBe( 10 );
});
test( "increment adds amount", async () => {
for( let i = 0; i < 10; i++ ) {
await ComfyDB.Store( `skipincrement${i}`, { value: 5 } );
}
for( let i = 0; i < 5; i++ ) {
await ComfyDB.Store( `increment${i}`, { value: 5 } );
}
await ComfyDB.Increment( "value", { by: 5, where: { key: { startsWith: "increment" } } } );
let results = await ComfyDB.Search( { where: { key: { contains: "increment" } } } );
expect( results.length ).toBe( 15 );
for( let i = 0; i < 15; i++ ) {
if( results[ i ].key.startsWith( "skip" ) ) {
expect( results[ i ].value ).toBe( 5 );
}
else {
expect( results[ i ].value ).toBe( 10 );
}
}
});
test( "decrement subtracts amount", async () => {
for( let i = 0; i < 10; i++ ) {
await ComfyDB.Store( `skipdecrement${i}`, { value: 5 } );
}
for( let i = 0; i < 5; i++ ) {
await ComfyDB.Store( `decrement${i}`, { value: 5 } );
}
await ComfyDB.Decrement( "value", { by: 5, where: { key: { startsWith: "decrement" } } } );
let results = await ComfyDB.Search( { where: { key: { contains: "decrement" } } } );
expect( results.length ).toBe( 15 );
for( let i = 0; i < 15; i++ ) {
if( results[ i ].key.startsWith( "skip" ) ) {
expect( results[ i ].value ).toBe( 5 );
}
else {
expect( results[ i ].value ).toBe( 0 );
}
}
});
var ComfyMongo = null;
var testFinishResolve = null;
beforeAll( () => {
return new Promise( ( resolve, reject ) => {
ComfyMongo = require( "comfy-mongo" )();
ComfyMongo.on( "output", ( data ) => {
// console.log( data );
});
ComfyMongo.on( "error", ( err ) => {
// console.log( err );
});
ComfyMongo.on( "ready", async () => {
console.log( "[ComfyDB] Ready..." );
resolve();
});
ComfyMongo.on( "exit", ( code ) => {
console.log( "[ComfyDB] Exit:", code );
testFinishResolve();
});
});
});
afterAll(() => {
return new Promise( async ( resolve, reject ) => {
testFinishResolve = resolve;
await ComfyDB.DeleteAll();
ComfyMongo.shutdown();
});
});