Skip to content

Commit

Permalink
index-test
Browse files Browse the repository at this point in the history
  • Loading branch information
shakty committed Nov 21, 2016
1 parent fdc1350 commit c4e4a87
Showing 1 changed file with 133 additions and 116 deletions.
249 changes: 133 additions & 116 deletions test/test.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ var ids = ['z','x'];//['z','x','c','v'];

var indexable = [
{
id: 1,
painter: "Jesus",
title: "Tea in the desert",
year: 0,
id: 1,
painter: "Jesus",
title: "Tea in the desert",
year: 0,
},
{
id: 2,
id: 2,
painter: "Dali",
title: "Portrait of Paul Eluard",
year: 1929,
Expand Down Expand Up @@ -50,7 +50,7 @@ var indexable = [
title: "Olympia",
year: 1863
},

];

var not_indexable = [
Expand Down Expand Up @@ -96,148 +96,165 @@ var tmp;


describe('NDDB Indexing Operations:', function() {

describe('Importing not-indexable items', function() {
before(function(){
db.importDB(not_indexable);
});
before(function(){
db.importDB(not_indexable);
});

it('should not create the special indexes', function() {
db.painter.should.not.exist;
db.size().should.eql(not_indexable.length);
});
});


describe('Importing indexable items', function() {
before(function(){
db.importDB(indexable);
});
before(function(){
db.importDB(indexable);
});

it('should create the special indexes', function() {
db.painter.should.exist;
db.painter.size().should.be.eql(indexable.length);
});

});

describe('Elements updated in the db should be updated in the indexes', function() {
before(function(){
var j = db.select('painter', '=', 'Jesus').execute().first();
j.painter = 'JSUS';
});

it('updated property \'painter\' should be reflected in the index', function() {
db.painter.get(1).painter.should.be.eql('JSUS');


describe('Elements updated in the db should be updated in the indexes',
function() {

before(function(){
var j = db.select('painter', '=', 'Jesus')
.execute().first();
j.painter = 'JSUS';
});

it('updated property \'painter\' should be reflected in the index',
function() {
db.painter.get(1).painter.should.be.eql('JSUS');
});
});

describe('Rebuilding the indexes multiple times', function() {
before(function(){
db.rebuildIndexes();
db.rebuildIndexes();
});
it('should not change the index', function() {
db.painter.size().should.be.eql(indexable.length);
before(function(){
db.rebuildIndexes();
db.rebuildIndexes();
});

it('should not change the index', function() {
db.painter.size().should.be.eql(indexable.length);
});
it('should not change the view', function() {
db.pview.size().should.be.eql(indexable.length);
it('should not change the view', function() {
db.pview.size().should.be.eql(indexable.length);
});
it('should not change the hash', function() {
JSUS.size(db.phash).should.be.eql(indexable.length);
it('should not change the hash', function() {
JSUS.size(db.phash).should.be.eql(indexable.length);
});
});

describe('#NDDBIndex.update()', function() {
before(function(){
db.painter.update(6, {
painter: 'M.A.N.E.T.'
});
});

it('updated property \'painter\' should be reflected in the index', function() {
var elem = db.select('painter', '=', 'M.A.N.E.T.').execute().fetch();
elem.length.should.be.eql(1);
elem[0].id.should.be.eql(6)
});

it('updated property \'painter\' should update the get index', function() {
db.painter.get(6).painter.should.be.eql('M.A.N.E.T.');
});

before(function(){
db.painter.update(6, {
painter: 'M.A.N.E.T.'
});
});

it('updated property \'painter\' should be reflected in the index',
function() {
var elem = db.select('painter', '=', 'M.A.N.E.T.')
.execute().fetch();
elem.length.should.be.eql(1);
elem[0].id.should.be.eql(6)
});

it('updated property \'painter\' should update the get index',
function() {
db.painter.get(6).painter.should.be.eql('M.A.N.E.T.');
});
});




describe('#NDDBIndex.pop()', function() {
before(function(){
tmp = db.painter.pop(6);
});
it('should return element \'M.A.N.E.T.\'', function() {
tmp.painter.should.be.eql('M.A.N.E.T.');
before(function(){

tmp = db.painter.pop(6);
});

it('should return element \'M.A.N.E.T.\'', function() {
tmp.painter.should.be.eql('M.A.N.E.T.');
});
it('should remove element from index', function() {
db.painter.get(6).should.be.false;
it('should remove element from index', function() {
db.painter.get(6).should.be.false;
});
it('should remove element from the main db too', function() {
db.select('painter', '=', 'M.A.N.E.T.').execute().size().should.be.eql(0);
it('should remove element from the main db too', function() {
db.select('painter', '=', 'M.A.N.E.T.')
.execute().size().should.be.eql(0);
});
it('should remove element from the view too', function() {
db.pview.select('painter', '=', 'M.A.N.E.T.').execute().size().should.be.eql(0);
it('should remove element from the view too', function() {
db.pview.select('painter', '=', 'M.A.N.E.T.')
.execute().size().should.be.eql(0);
});
});

describe('#NDDBIndex.getAllKeys()', function() {
before(function(){
tmp = db.painter.getAllKeys();
});
it('should return all the indexes', function() {
tmp.should.be.eql(['1','2','3','4','5']);
});
before(function(){
tmp = db.painter.getAllKeys();
});

it('should return all the indexes', function() {
tmp.should.be.eql(['1','2','3','4','5']);
});
});

describe('#NDDBIndex.getAllKeyElements()', function() {
before(function(){
tmp = db.painter.getAllKeyElements();
});
it('should return all the indexes', function() {
var o = {}, item, i;
// length -1 : one element removed
for (i = 0; i < indexable.length-1; i++) {
item = indexable[i];
o[item.id] = item;
}
tmp.should.be.eql(o);
});
before(function(){
tmp = db.painter.getAllKeyElements();
});

it('should return all the indexes', function() {
var o = {}, item, i;
// length -1 : one element removed
for (i = 0; i < indexable.length-1; i++) {
item = indexable[i];
o[item.id] = item;
}
tmp.should.be.eql(o);
});
});

describe('#NDDBIndex.update()', function() {
before(function() {
db.painter.update('5', {
id: undefined
});
});

it('updated painter with idx 5 should not be found in index', function() {
var p = db.painter.get('5');
('boolean' === typeof p).should.be.true;
p.should.be.false;
before(function() {
db.painter.update('5', {
id: undefined
});
});

it('updated painter with idx 5 should not be found in index',
function() {
var p = db.painter.get('5');
('boolean' === typeof p).should.be.true;
p.should.be.false;
});

// it('updated painter with idx 5 should not be found in the view',function() {
// db.pview.selexec('id', '=', 5).size().should.be.eql(0);
// db.pview.selexec('painter', '=', 'Monet').size().should.be.eql(1);
// });
//
// it('updated painter with idx 5 should not be found in the hash', function() {
// console.log(db.phash['5'].db);
// });
});

describe('#NDDBIndex.getAllKeys() - item with same id', function() {
before(function() {
db.insert({id: 5, ah: 1});
tmp = db.painter.getAllKeys();
});

it('should not count twice the same id', function() {
tmp.should.be.eql(['1','2','3','4','5']);
});

// it('updated painter with idx 5 should not be found in the view', function() {
// db.pview.selexec('id', '=', 5).size().should.be.eql(0);
// db.pview.selexec('painter', '=', 'Monet').size().should.be.eql(1);
// });
//
// it('updated painter with idx 5 should not be found in the hash', function() {
// console.log(db.phash['5'].db);
// });
});
});

});

0 comments on commit c4e4a87

Please sign in to comment.