Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
feat(stores): Implement deleteOne method for all stores
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Jul 18, 2016
1 parent 1df2277 commit 9254ee6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
29 changes: 27 additions & 2 deletions src/browser/stores/http.store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, addProviders, async } from '@angular/core/testing';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { Http, BaseRequestOptions, Response } from '@angular/http';
import { Http, BaseRequestOptions, Response, ResponseOptions } from '@angular/http';
import { Injectable, Injector } from '@angular/core';
import { Logger } from '../../common/services/logger.service';
import { LoggerMock } from '../../common/services/logger.service.mock';
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('Http store', () => {

})));

it('Retrieves a single model from http', async(inject([TestHttpStore, MockBackend], (s: TestHttpStore, b: MockBackend) => {
it('Retrieves a single model with http', async(inject([TestHttpStore, MockBackend], (s: TestHttpStore, b: MockBackend) => {

let connection: MockConnection;
b.connections.subscribe((c: MockConnection) => connection = c);
Expand Down Expand Up @@ -130,6 +130,31 @@ describe('Http store', () => {

})));

it('Deletes a single model with http', async(inject([TestHttpStore, MockBackend], (s: TestHttpStore, b: MockBackend) => {

let connection: MockConnection;
b.connections.subscribe((c: MockConnection) => connection = c);

const model = new TestModel({id: 321, name: 'delete-me'});

const testPromise = s.deleteOne(model)
.then((res) => {

expect(res instanceof TestModel)
.toBe(true);
expect(res)
.toEqual(model);
});

connection.mockRespond(new Response(new ResponseOptions({
status: 204,
url: `${process.env.API_BASE}/tests/123`,
})));

return testPromise;

})));

it('Logs error on failed model retrieval', async(inject([TestHttpStore, MockBackend], (s: TestHttpStore, b: MockBackend) => {

let logSpy = spyOn((s as any).logger, 'error');
Expand Down
14 changes: 13 additions & 1 deletion src/browser/stores/http.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export abstract class HttpStore<T extends AbstractModel> extends AbstractStore<T
}

/**
* Get the rest endpoint for the model
* Get the rest endpoint for the model
* @param id
* @returns {string}
*/
Expand Down Expand Up @@ -83,6 +83,18 @@ export abstract class HttpStore<T extends AbstractModel> extends AbstractStore<T
// .catch((error) => this.handleError(error));
}

/**
* @inheritdoc
* @param model
*/
public deleteOne(model: T): Promise<T> {

return this.http.delete(this.endpoint(model.getIdentifier()))
.toPromise()
.then((res: Response) => this.checkStatus(res))
.then(() => model); //@todo flag model as existing
}

/**
* Extract model from the payload
* @param res
Expand Down
10 changes: 0 additions & 10 deletions src/common/services/consoleLogger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,6 @@ describe('Console Logger', () => {

expect(persistSpy)
.toHaveBeenCalled();


try {
//do something that throws error
} catch (e) {
this.logger.error(e.message).silly.debug(e.stack);
}



});

});
Expand Down
12 changes: 12 additions & 0 deletions src/common/stores/mock.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ export abstract class MockStore<T extends AbstractModel> extends AbstractStore<T
return Promise.resolve(model);
}

/**
* Mock seleting model by id
*
* As deleting does not make sense for a mock store, this just stubs the interface by returning
* the model in a resolved promise
* @param model
* @returns {Promise<void>}
*/
public deleteOne(model: T): Promise<T> {
return Promise.resolve(model);
}

}
16 changes: 16 additions & 0 deletions src/common/stores/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ describe('Mock Store', () => {

})));

it('mocks delete action', async(inject([TestClass], (c: TestClass) => {

let shipRef: Ship = null;

return c.shipStore.findOne(1234)
.then((ship: Ship) => {
shipRef = ship;
return c.shipStore.deleteOne(ship)
})
.then((ship: Ship) => {
expect(ship instanceof Ship)
.toBe(true);
});

})));

it('explicitly hydrates from raw data', async(inject([TestClass], (c: TestClass) => {

let data: any = {
Expand Down
6 changes: 5 additions & 1 deletion src/common/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export abstract class AbstractStore<T extends AbstractModel> {
*/
public abstract saveOne(model: T): Promise<T>;

// public abstract deleteOne(id: identifier): Promise<void>;
/**
* Delete the model from the store.
* @param model
*/
public abstract deleteOne(model: T): Promise<T>;

/**
* Find multiple entities using a query for constraints
Expand Down
21 changes: 20 additions & 1 deletion src/server/stores/db.store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TestDatabaseStore extends DatabaseStore<TestModel> {

describe('Database Store', () => {

const repositorySpy = jasmine.createSpyObj('repository', ['findOneById', 'find', 'persist']);
const repositorySpy = jasmine.createSpyObj('repository', ['findOneById', 'find', 'persist', 'remove']);
const dbConnectionSpy = jasmine.createSpyObj('dbConnection', ['getRepository']);
dbConnectionSpy.getRepository.and.returnValue(repositorySpy);

Expand Down Expand Up @@ -122,6 +122,25 @@ describe('Database Store', () => {

})));


it('deletes a single entity from the orm', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {

(db as any).connection = dbConnectionSpy;

const testModelFixture = new TestModel({id: 10});

repositorySpy.remove.and.returnValue(Promise.resolve(testModelFixture));

return store.deleteOne(testModelFixture)
.then((res) => {
expect(repositorySpy.remove)
.toHaveBeenCalledWith(testModelFixture);
expect(res)
.toEqual(testModelFixture);
});

})));

it('retrieves collection of entities from the orm', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {

(db as any).connection = dbConnectionSpy;
Expand Down
21 changes: 16 additions & 5 deletions src/server/stores/db.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export abstract class DatabaseStore<T extends AbstractModel> extends AbstractSto
*/
public getRepository(): Promise<Repository<T>> {
if (!this.repositoryPromise) {
this.repositoryPromise = this.database.getConnection().then((connection: Connection) => connection.getRepository(this.modelStatic))
this.repositoryPromise = this.database.getConnection()
.then((connection: Connection) => connection.getRepository(this.modelStatic))
}

return this.repositoryPromise;
Expand All @@ -49,8 +50,9 @@ export abstract class DatabaseStore<T extends AbstractModel> extends AbstractSto
/**
* @inheritdoc
*/
public initialized():Promise<this> {
return this.getRepository().then(() => this);
public initialized(): Promise<this> {
return this.getRepository()
.then(() => this);
}

/**
Expand All @@ -59,8 +61,8 @@ export abstract class DatabaseStore<T extends AbstractModel> extends AbstractSto
public findOne(id: identifier): Promise<T> {
return this.getRepository()
.then((repo) => repo.findOneById(id))
.then((model:T) => {
if (!model){
.then((model: T) => {
if (!model) {
throw new NotFoundException(`${this.modelStatic.name} not found with id [${id}]`);
}
return model;
Expand Down Expand Up @@ -97,4 +99,13 @@ export abstract class DatabaseStore<T extends AbstractModel> extends AbstractSto
.then((repo) => repo.persist(model));
}

/**
* @inheritdoc
* @param model
*/
public deleteOne(model: T): Promise<T> {
return this.getRepository()
.then((repo) => repo.remove(model));
}

}

0 comments on commit 9254ee6

Please sign in to comment.