forked from avinoamr/dbstream-memory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
80 lines (68 loc) · 2.5 KB
/
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
var test = require( "dbstream/test" );
var memory = require( "./memory" );
var stream = require( "stream" );
var assert = require( "assert" );
var util = require( "util" );
describe( "Memory", function () {
it( "Insert", function ( done ) {
var data = {};
var connection = memory.connect( data );
var cursor = new connection.Cursor();
cursor.write({ name: "Hello" })
cursor.write({ name: "World" })
cursor.on( "finish", function() {
var values = Object.keys( data ).map( function ( id ) {
return data[ id ];
});
assert.equal( values.length, 2 );
assert.equal( values[ 0 ].name, "Hello" );
assert.equal( values[ 1 ].name, "World" );
assert( values[ 0 ].id ); // id is generated
assert( values[ 1 ].id );
done();
}).end();
});
it( "Upsert", function ( done ) {
var data = { 1: { name: "Hello", id: 1 }, 2: { name: "World", id: 2 } };
var connection = memory.connect( data );
var cursor = new connection.Cursor();
cursor.write({ id: 2, name: "Foo" });
cursor.write({ id: 3, name: "Bar" });
cursor.on( "finish", function() {
assert.deepEqual( data, {
1: { name: "Hello", id: 1 },
2: { name: "Foo", id: 2 },
3: { name: "Bar", id: 3 }
})
done();
}).end();
});
it( "Remove", function ( done ) {
var data = { 1: { name: "Hello", id: 1 }, 2: { name: "World", id: 2 } };
var connection = memory.connect( data );
var cursor = new connection.Cursor();
cursor.remove({ id: 1 })
cursor.on( "finish", function () {
assert.deepEqual( data, {
2: { name: "World", id: 2 }
});
done();
}).end();
});
it( "Read", function ( done ) {
var data = { 1: { name: "Hello", id: 1 }, 2: { name: "World", id: 2 } };
var connection = memory.connect( data );
var cursor = new connection.Cursor();
var found = [];
cursor.find({})
.on( "data", function ( obj ) { found.push( obj ) } )
.on( "end", function () {
assert.deepEqual( found, [
{ name: "Hello", id: 1 },
{ name: "World", id: 2 }
])
done();
})
});
it( "Implements the dbstream API", test( memory.connect() ) );
});