Skip to content

Commit c0525f0

Browse files
committed
add tests and fixes to ensure that memberships are removed on entity deletion.
1 parent 3ab83a5 commit c0525f0

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

lib/level-storage.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,55 @@ LevelStorage.prototype.updateEntityPromise = function (entity_id, entity_type, d
266266
//deletes the entity with the given id and all its attributes
267267
LevelStorage.prototype.deleteEntityPromise = function (entity_id, entity_type) {
268268
console.log("arguments for deleteEntity leveldb " + JSON.stringify(arguments));
269-
var pk = buildEntityPk(entity_id, entity_type);
270-
return deleteSomething("entities", pk, transaction(this.entities), true);
269+
270+
function rollback(t1, t2, callback) {
271+
t1.rollback(t2.rollback(callback));
272+
}
273+
var that = this;
274+
var group, entity;
275+
return new Promise(function (resolve, reject) {
276+
var pk = buildEntityPk(entity_id, entity_type);
277+
var t_groups = transaction(that.groups);
278+
var t_entities = transaction(that.entities);
279+
readSomething("entity", pk, t_entities, false)
280+
.then(function (entity) {
281+
var groups = entity.groups;
282+
var readGroups = [];
283+
if (groups && groups.length > 0) {
284+
groups.forEach(function (group_pk) {
285+
readGroups.push(readSomething("groups", group_pk, t_groups, false));
286+
});
287+
return Promise.all(readGroups);
288+
} else {
289+
return Promise.resolve([]); //return an empty set of entities so that the promise chain keeps going :)
290+
}
291+
}).then(function (groups) {
292+
var ps = [];
293+
groups.forEach(function (g) {
294+
ps.push(new Promise(function (re, rej) {
295+
g.entities = g.entities.filter(function (v) {
296+
return (v.type !== entity_type || v.id !== entity_id);
297+
});
298+
updateSomething("groups", buildGroupPk(g.group_name, g.owner), g, t_groups, false).then(re, rej);
299+
}));
300+
});
301+
return Promise.all(ps);
302+
}).then(function (res) {
303+
console.log("finished updating groups by removing entity from their attributes");
304+
console.log("attempting to delete entity " + JSON.stringify(pk));
305+
return deleteSomething("entities", pk, t_entities, false);
306+
}).then(function () {
307+
t_entities.commit(function () {
308+
t_groups.commit(function () {
309+
resolve();
310+
});
311+
});
312+
}).catch(function rej(reason) {
313+
console.log('level storage rejecting ' + reason);
314+
return rollback(t_entities, t_groups, reject.bind(this, reason));
315+
});
316+
});
317+
271318
};
272319

273320
LevelStorage.prototype.createGroupPromise = function (group_name, owner) {

tests/level-storage-test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ describe('LevelStorage', function () {
768768
});
769769
});
770770

771-
it('should remove group references in entities after deleting it', function (done) {
771+
it('should remove group references in entities after deleting it from the group', function (done) {
772772
var storage = createLevelStorage();
773773
var owner = "1";
774774
var entity_id = "2";
@@ -807,6 +807,39 @@ describe('LevelStorage', function () {
807807
});
808808
});
809809

810+
it('should remove group references in entities after deleting the entity altogether', function (done) {
811+
var storage = createLevelStorage();
812+
var owner = "1";
813+
var entity_id = "2";
814+
var entity_type = "user";
815+
var data = {
816+
"name": "string",
817+
"token": "123"
818+
};
819+
var group;
820+
var group_name = "mygroup";
821+
storage.createGroupPromise(group_name, owner)
822+
.then(function (g) {
823+
group = g;
824+
return storage.createEntityPromise(entity_id, entity_type, owner, data)
825+
})
826+
.then(function (entity) {
827+
return storage.addEntityToGroupPromise(group.group_name, group.owner, entity_id, entity_type);
828+
}).then(function (result) {
829+
return storage.deleteEntityPromise(entity_id, entity_type);
830+
}).then(function (result) {
831+
return storage.readGroupPromise(group.group_name, group.owner);
832+
}).then(function (g) {
833+
if (g.entities) {
834+
if (g.entities.length === 0)
835+
storage.cleanDb(done);
836+
837+
}
838+
}).catch(function reject(error) {
839+
throw error;
840+
});
841+
});
842+
810843
it('should remove entity from group as well as the group reference in the entity after an entity has been removed from a group', function (done) {
811844
var storage = createLevelStorage();
812845
var owner = "1";

0 commit comments

Comments
 (0)