All test cases passing + added test cases for copy

This commit is contained in:
Henry 2015-11-03 17:39:05 -08:00 committed by Andrew Henry
parent cbd21212d1
commit bd1c3cb7da
3 changed files with 63 additions and 82 deletions

View File

@ -77,7 +77,7 @@
"description": "Provides a service for copying objects", "description": "Provides a service for copying objects",
"implementation": "services/CopyService.js", "implementation": "services/CopyService.js",
"depends": ["$q", "creationService", "policyService", "depends": ["$q", "creationService", "policyService",
"persistenceService"] "persistenceService", "now"]
}, },
{ {
"key": "locationService", "key": "locationService",

View File

@ -35,11 +35,12 @@ define(
* @memberof platform/entanglement * @memberof platform/entanglement
* @implements {platform/entanglement.AbstractComposeService} * @implements {platform/entanglement.AbstractComposeService}
*/ */
function CopyService($q, creationService, policyService, persistenceService) { function CopyService($q, creationService, policyService, persistenceService, now) {
this.$q = $q; this.$q = $q;
this.creationService = creationService; this.creationService = creationService;
this.policyService = policyService; this.policyService = policyService;
this.persistenceService = persistenceService; this.persistenceService = persistenceService;
this.now = now;
} }
CopyService.prototype.validate = function (object, parentCandidate) { CopyService.prototype.validate = function (object, parentCandidate) {
@ -95,16 +96,15 @@ define(
} }
delete modelClone.model.composition; delete modelClone.model.composition;
delete modelClone.model.location; delete modelClone.model.location;
delete modelClone.model.persisted;
delete modelClone.model.modified;
return $q.when(originalObject.useCapability('composition')).then(function(composees){ return $q.when(originalObject.useCapability('composition')).then(function(composees){
console.log("composees: " + composees);
return (composees || []).reduce(function(promise, composee){ return (composees || []).reduce(function(promise, composee){
console.log("inside reduce");
//If the object is composed of other //If the object is composed of other
// objects, chain a promise.. // objects, chain a promise..
return promise.then(function(){ return promise.then(function(){
// ...to recursively copy it (and its children) // ...to recursively copy it (and its children)
return copy(composee, originalObject).then(function(composeeClone){ return copy(composee, originalObject).then(function(composeeClone){
console.log("Composing clone");
//Once copied, associate each cloned //Once copied, associate each cloned
// composee with its parent clone // composee with its parent clone
composeeClone.model.location = modelClone.id; composeeClone.model.location = modelClone.id;
@ -113,7 +113,6 @@ define(
}); });
});}, $q.when(undefined) });}, $q.when(undefined)
).then(function (){ ).then(function (){
console.log("Adding clone to list");
//Add the clone to the list of clones that will //Add the clone to the list of clones that will
//be returned by this function //be returned by this function
clones.push(modelClone); clones.push(modelClone);
@ -122,7 +121,6 @@ define(
}); });
}; };
return copy(domainObject, parent).then(function(){ return copy(domainObject, parent).then(function(){
console.log("Done cloning, returning");
return clones; return clones;
}); });
} }
@ -140,13 +138,13 @@ define(
var persisted = 0, var persisted = 0,
self = this; self = this;
return function(objectClones) { return function(objectClones) {
console.log("Persisting " + objectClones.length + " clones");
return self.$q.all(objectClones.map(function(clone, index){ return self.$q.all(objectClones.map(function(clone, index){
clone.model.persisted = self.now();
return self.persistenceService.createObject(clone.persistenceSpace, clone.id, clone.model) return self.persistenceService.createObject(clone.persistenceSpace, clone.id, clone.model)
.then(function(){ .then(function(){
progress && progress("copying", objectClones.length, ++persisted); progress && progress("copying", objectClones.length, ++persisted);
}); });
})).then(function(qall){ })).then(function(){
return objectClones return objectClones
}); });
} }
@ -170,8 +168,10 @@ define(
return self.persistenceService return self.persistenceService
.updateObject(parentClone.persistenceSpace, parentClone.id, parentClone.model) .updateObject(parentClone.persistenceSpace, parentClone.id, parentClone.model)
.then(function(){return parent.getCapability('composition').add(parentClone.id)}) .then(function(){return parent.getCapability("composition").add(parentClone.id)})
.then(function(){return parent.getCapability("persistence").persist()}); .then(function(){return parent.getCapability("persistence").persist()})
.then(function(){return parentClone});
// Ensure the clone of the original domainObject is returned
} }
} }
@ -181,8 +181,8 @@ define(
* @param domainObject * @param domainObject
* @param parent * @param parent
* @param progress * @param progress
* @returns a promise that will be completed when the duplication is * @returns a promise that will be completed with the clone of
* successful, otherwise an error is thrown. * domainObject when the duplication is successful.
*/ */
CopyService.prototype.perform = function (domainObject, parent, progress) { CopyService.prototype.perform = function (domainObject, parent, progress) {
var $q = this.$q; var $q = this.$q;

View File

@ -130,12 +130,13 @@ define(
createObjectPromise, createObjectPromise,
copyService, copyService,
mockPersistenceService, mockPersistenceService,
mockNow,
object, object,
newParent, newParent,
copyResult, copyResult,
copyFinished, copyFinished,
persistObjectPromise, persistObjectPromise,
persistenceCapability; parentPersistenceCapability;
beforeEach(function () { beforeEach(function () {
creationService = jasmine.createSpyObj( creationService = jasmine.createSpyObj(
@ -154,13 +155,18 @@ define(
mockPersistenceService.createObject.andReturn(persistObjectPromise); mockPersistenceService.createObject.andReturn(persistObjectPromise);
mockPersistenceService.updateObject.andReturn(persistObjectPromise); mockPersistenceService.updateObject.andReturn(persistObjectPromise);
persistenceCapability = jasmine.createSpyObj( parentPersistenceCapability = jasmine.createSpyObj(
"persistence", "persistence",
[ "persist", "getSpace" ] [ "persist", "getSpace" ]
); );
persistenceCapability.persist.andReturn(persistObjectPromise); parentPersistenceCapability.persist.andReturn(persistObjectPromise);
persistenceCapability.getSpace.andReturn("testSpace"); parentPersistenceCapability.getSpace.andReturn("testSpace");
mockNow = jasmine.createSpyObj("mockNow", ["now"]);
mockNow.now.andCallFake(function(){
return 1234;
})
}); });
@ -170,7 +176,8 @@ define(
name: 'object', name: 'object',
id: 'abc', id: 'abc',
model: { model: {
name: 'some object' name: 'some object',
persisted: mockNow.now()
} }
}); });
newParent = domainObjectFactory({ newParent = domainObjectFactory({
@ -180,7 +187,7 @@ define(
composition: [] composition: []
}, },
capabilities: { capabilities: {
persistence: persistenceCapability persistence: parentPersistenceCapability
} }
}); });
mockQ = jasmine.createSpyObj('mockQ', ['when', 'all', 'reject']); mockQ = jasmine.createSpyObj('mockQ', ['when', 'all', 'reject']);
@ -193,26 +200,15 @@ define(
return synchronousPromise(result); return synchronousPromise(result);
}); });
copyService = new CopyService(mockQ, creationService, policyService, mockPersistenceService); copyService = new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
copyResult = copyService.perform(object, newParent); copyResult = copyService.perform(object, newParent);
copyFinished = jasmine.createSpy('copyFinished'); copyFinished = jasmine.createSpy('copyFinished');
copyResult.then(copyFinished); copyResult.then(copyFinished);
}); });
/**
* Test invalidated. Copy service no longer uses creation service.
*/
/*it("uses creation service", function () {
expect(creationService.createObject)
.toHaveBeenCalledWith(jasmine.any(Object), newParent);
expect(createObjectPromise.then)
.toHaveBeenCalledWith(jasmine.any(Function));
});*/
it("uses persistence service", function () { it("uses persistence service", function () {
expect(mockPersistenceService.createObject) expect(mockPersistenceService.createObject)
.toHaveBeenCalledWith(persistenceCapability, jasmine.any(String), object.getModel()); .toHaveBeenCalledWith(parentPersistenceCapability, jasmine.any(String), object.getModel());
expect(persistObjectPromise.then) expect(persistObjectPromise.then)
.toHaveBeenCalledWith(jasmine.any(Function)); .toHaveBeenCalledWith(jasmine.any(Function));
@ -301,62 +297,51 @@ define(
id: '456', id: '456',
model: { model: {
composition: [] composition: []
},
capabilities: {
composition: compositionCapability,
persistence: parentPersistenceCapability
} }
}); });
createObjectPromise = synchronousPromise(newObject); createObjectPromise = synchronousPromise(newObject);
creationService.createObject.andReturn(createObjectPromise); creationService.createObject.andReturn(createObjectPromise);
copyService = new CopyService(mockQ, creationService, policyService, mockPersistenceService); copyService = new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
console.log("Before perform");
//compositionPromise.then.andReturn(synchronousPromise([childObject]));
//mockQ.when.andReturn(compositionPromise);
copyResult = copyService.perform(object, newParent);
console.log("After perform");
//compositionPromise.then.mostRecentCall.args[0]([childObject]);
copyFinished = jasmine.createSpy('copyFinished');
copyResult.then(copyFinished);
}); });
/** describe("the cloning process", function(){
* Test no longer valid due to creation service not beforeEach(function() {
* being used copyResult = copyService.perform(object, newParent);
*/ copyFinished = jasmine.createSpy('copyFinished');
/*it("uses creation service", function () { copyResult.then(copyFinished);
expect(creationService.createObject) });
.toHaveBeenCalledWith(jasmine.any(Object), newParent); /**
* This is testing that the number of calls to the
* backend is kept to a minimum
*/
it("makes only n+2 persistence calls for n copied" +
" objects", function () {
expect(mockPersistenceService.createObject.calls.length).toEqual(2);
expect(mockPersistenceService.updateObject.calls.length).toEqual(1);
expect(parentPersistenceCapability.persist).toHaveBeenCalled();
});
expect(createObjectPromise.then) it("copies object and children in a bottom-up" +
.toHaveBeenCalledWith(jasmine.any(Function)); " fashion", function () {
});*/ expect(mockPersistenceService.createObject.calls[0].args[2].name).toEqual(childObject.model.name)
expect(mockPersistenceService.createObject.calls[1].args[2].name).toEqual(object.model.name)
});
it("uses persistence service", function () { it("returns a promise", function () {
//Need a better way of testing duplication here. expect(copyResult.then).toBeDefined();
expect(mockPersistenceService.createObject) expect(copyFinished).toHaveBeenCalled();
.toHaveBeenCalled(); });
});
/*
//Test is no longer relevant
it("clears model composition", function () {
var newModel = creationService
.createObject
.mostRecentCall
.args[0];
expect(newModel.composition.length).toBe(0); it("clears modified and sets persisted", function () {
expect(newModel.name).toBe('some object'); expect(copyFinished.mostRecentCall.args[0].model.modified).toBeUndefined();
});*/ expect(copyFinished.mostRecentCall.args[0].model.persisted).toBe(mockNow.now());
});
it("recursively clones it's children", function () {
//TODO: This is a valid test, but needs rewritten
/*expect(creationService.createObject.calls.length).toBe(1);
expect(compositionCapability.invoke).toHaveBeenCalled();
compositionPromise.then.mostRecentCall.args[0]([childObject]);
expect(creationService.createObject.calls.length).toBe(2);*/
});
it("returns a promise", function () {
expect(copyResult.then).toBeDefined();
expect(copyFinished).toHaveBeenCalled();
}); });
}); });
@ -378,7 +363,7 @@ define(
it("throws an error", function () { it("throws an error", function () {
var copyService = var copyService =
new CopyService(mockQ, creationService, policyService, mockPersistenceService); new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
function perform() { function perform() {
copyService.perform(object, newParent); copyService.perform(object, newParent);
@ -391,10 +376,6 @@ define(
expect(perform).toThrow(); expect(perform).toThrow();
}); });
}); });
/**
* Additional tests:
* - Created and persisted dates
*/
}); });
}); });