@ -9,6 +9,7 @@ const Datastore = require('../lib/datastore')
const Persistence = require ( '../lib/persistence' )
const Persistence = require ( '../lib/persistence' )
const storage = require ( '../lib/storage' )
const storage = require ( '../lib/storage' )
const { execFile , fork } = require ( 'child_process' )
const { execFile , fork } = require ( 'child_process' )
const Readable = require ( 'stream' ) . Readable
const { assert } = chai
const { assert } = chai
chai . should ( )
chai . should ( )
@ -41,101 +42,151 @@ describe('Persistence', function () {
] , done )
] , done )
} )
} )
it ( 'Every line represents a document' , function ( ) {
it ( 'Every line represents a document' , function ( done ) {
const now = new Date ( )
const now = new Date ( )
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { _id : '3' , nested : { today : now } } )
model . serialize ( { _id : '3' , nested : { today : now } } )
const treatedData = d . persistence . treatRawData ( rawData ) . data
const stream = new Readable ( )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
stream . push ( rawData )
treatedData . length . should . equal ( 3 )
stream . push ( null )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '2' , hello : 'world' } )
d . persistence . treatRawStream ( stream , function ( err , result ) {
assert . deepStrictEqual ( treatedData [ 2 ] , { _id : '3' , nested : { today : now } } )
const treatedData = result . data
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
treatedData . length . should . equal ( 3 )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '2' , hello : 'world' } )
assert . deepStrictEqual ( treatedData [ 2 ] , { _id : '3' , nested : { today : now } } )
done ( )
} )
} )
} )
it ( 'Badly formatted lines have no impact on the treated data' , function ( ) {
it ( 'Badly formatted lines have no impact on the treated data' , function ( done ) {
const now = new Date ( )
const now = new Date ( )
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
'garbage\n' +
'garbage\n' +
model . serialize ( { _id : '3' , nested : { today : now } } )
model . serialize ( { _id : '3' , nested : { today : now } } )
const treatedData = d . persistence . treatRawData ( rawData ) . data
const stream = new Readable ( )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
stream . push ( rawData )
treatedData . length . should . equal ( 2 )
stream . push ( null )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , nested : { today : now } } )
d . persistence . treatRawStream ( stream , function ( err , result ) {
console . log ( err )
var treatedData = result . data
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
treatedData . length . should . equal ( 2 )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , nested : { today : now } } )
done ( )
} )
} )
} )
it ( 'Well formatted lines that have no _id are not included in the data' , function ( ) {
it ( 'Well formatted lines that have no _id are not included in the data' , function ( done ) {
const now = new Date ( )
const now = new Date ( )
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { nested : { today : now } } )
model . serialize ( { nested : { today : now } } )
const treatedData = d . persistence . treatRawData ( rawData ) . data
const stream = new Readable ( )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
stream . push ( rawData )
treatedData . length . should . equal ( 2 )
stream . push ( null )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '2' , hello : 'world' } )
d . persistence . treatRawStream ( stream , function ( err , result ) {
var treatedData = result . data
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
treatedData . length . should . equal ( 2 )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '2' , hello : 'world' } )
done ( )
} )
} )
} )
it ( 'If two lines concern the same doc (= same _id), the last one is the good version' , function ( ) {
it ( 'If two lines concern the same doc (= same _id), the last one is the good version' , function ( done ) {
const now = new Date ( )
const now = new Date ( )
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { _id : '1' , nested : { today : now } } )
model . serialize ( { _id : '1' , nested : { today : now } } )
const treatedData = d . persistence . treatRawData ( rawData ) . data
const stream = new Readable ( )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
stream . push ( rawData )
treatedData . length . should . equal ( 2 )
stream . push ( null )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , nested : { today : now } } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '2' , hello : 'world' } )
d . persistence . treatRawStream ( stream , function ( err , result ) {
var treatedData = result . data
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
treatedData . length . should . equal ( 2 )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , nested : { today : now } } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '2' , hello : 'world' } )
done ( )
} )
} )
} )
it ( 'If a doc contains $$deleted: true, that means we need to remove it from the data' , function ( ) {
it ( 'If a doc contains $$deleted: true, that means we need to remove it from the data' , function ( done ) {
const now = new Date ( )
const now = new Date ( )
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { _id : '2' , hello : 'world' } ) + '\n' +
model . serialize ( { _id : '1' , $$deleted : true } ) + '\n' +
model . serialize ( { _id : '1' , $$deleted : true } ) + '\n' +
model . serialize ( { _id : '3' , today : now } )
model . serialize ( { _id : '3' , today : now } )
const treatedData = d . persistence . treatRawData ( rawData ) . data
const stream = new Readable ( )
stream . push ( rawData )
stream . push ( null )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
d . persistence . treatRawStream ( stream , function ( err , result ) {
treatedData . length . should . equal ( 2 )
var treatedData = result . data
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '2' , hello : 'world' } )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , today : now } )
treatedData . length . should . equal ( 2 )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '2' , hello : 'world' } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , today : now } )
done ( )
} )
} )
} )
it ( 'If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before' , function ( ) {
it ( 'If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before' , function ( done ) {
const now = new Date ( )
const now = new Date ( )
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
model . serialize ( { _id : '2' , $$deleted : true } ) + '\n' +
model . serialize ( { _id : '2' , $$deleted : true } ) + '\n' +
model . serialize ( { _id : '3' , today : now } )
model . serialize ( { _id : '3' , today : now } )
const treatedData = d . persistence . treatRawData ( rawData ) . data
const stream = new Readable ( )
stream . push ( rawData )
stream . push ( null )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
d . persistence . treatRawStream ( stream , function ( err , result ) {
treatedData . length . should . equal ( 2 )
var treatedData = result . data
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , today : now } )
treatedData . length . should . equal ( 2 )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , today : now } )
done ( )
} )
} )
} )
it ( 'If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options' , function ( ) {
it ( 'If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options' , function ( done ) {
const now = new Date ( )
const now = new Date ( )
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
const rawData = model . serialize ( { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } ) + '\n' +
model . serialize ( { $$indexCreated : { fieldName : 'test' , unique : true } } ) + '\n' +
model . serialize ( { $$indexCreated : { fieldName : 'test' , unique : true } } ) + '\n' +
model . serialize ( { _id : '3' , today : now } )
model . serialize ( { _id : '3' , today : now } )
const treatedData = d . persistence . treatRawData ( rawData ) . data
const stream = new Readable ( )
const indexes = d . persistence . treatRawData ( rawData ) . indexes
stream . push ( rawData )
stream . push ( null )
Object . keys ( indexes ) . length . should . equal ( 1 )
d . persistence . treatRawStream ( stream , function ( err , result ) {
assert . deepStrictEqual ( indexes . test , { fieldName : 'test' , unique : true } )
var treatedData = result . data
var indexes = result . indexes
Object . keys ( indexes ) . length . should . equal ( 1 )
assert . deepEqual ( indexes . test , { fieldName : "test" , unique : true } )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
treatedData . sort ( function ( a , b ) { return a . _id - b . _id } )
treatedData . length . should . equal ( 2 )
treatedData . length . should . equal ( 2 )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 0 ] , { _id : '1' , a : 2 , ages : [ 1 , 5 , 12 ] } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , today : now } )
assert . deepStrictEqual ( treatedData [ 1 ] , { _id : '3' , today : now } )
done ( )
} )
} )
} )
it ( 'Compact database on load' , function ( done ) {
it ( 'Compact database on load' , function ( done ) {