This repository has been archived by the owner on May 8, 2020. It is now read-only.
/
db.store.spec.ts
225 lines (158 loc) · 6.75 KB
/
db.store.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { Logger } from '../../common/services/logger.service';
import { Injectable, Injector } from '@angular/core';
import { inject, addProviders, async } from '@angular/core/testing';
import { LoggerMock } from '../../common/services/logger.service.mock';
import { Database } from '../services/database.service';
import { DatabaseStore } from './db.store';
import { AbstractModel } from '../../common/models/model';
import { Primary } from '../../common/models/types/primary.decorator';
import { StoredProperty } from '../../common/models/types/storedProperty.decorator';
import { NotFoundException } from '../exeptions/exceptions';
import { Collection } from '../../common/models/collection';
import { DatabaseMock } from '../services/database.service.mock';
import Spy = jasmine.Spy;
class TestModel extends AbstractModel {
@Primary()
id: number;
@StoredProperty()
name: string;
}
@Injectable()
class TestDatabaseStore extends DatabaseStore<TestModel> {
constructor(injector: Injector, database: Database, loggerBase: Logger) {
super(TestModel, injector, database, loggerBase);
}
}
describe('Database Store', () => {
const repositorySpy = jasmine.createSpyObj('repository', ['findOneById', 'find', 'persist', 'remove']);
const dbConnectionSpy = jasmine.createSpyObj('dbConnection', ['getRepository']);
dbConnectionSpy.getRepository.and.returnValue(repositorySpy);
const providers = [
TestDatabaseStore,
{provide: Logger, useClass: LoggerMock},
{provide: Database, useClass: DatabaseMock},
];
beforeEach(() => {
addProviders(providers);
});
it('initializes the store with a reference to the static model', inject([TestDatabaseStore], (store: TestDatabaseStore) => {
expect((store as any).modelStatic)
.toEqual(TestModel);
}));
it('retrieves a reference to the orm repository', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {
(db as any).connection = dbConnectionSpy;
return store.getRepository()
.then((repo) => {
expect(repo)
.toEqual(repositorySpy);
expect(dbConnectionSpy.getRepository)
.toHaveBeenCalledTimes(1);
//verify repo is cached by re-calling and asserting no more calls to getRepository
return store.getRepository()
.then(() => {
expect(repo)
.toEqual(repositorySpy);
expect(dbConnectionSpy.getRepository)
.toHaveBeenCalledTimes(1);
});
});
})));
it('provides initialized method so callees can defer actions', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {
(db as any).connection = dbConnectionSpy;
return store.initialized()
.then((res) => {
expect(res)
.toEqual(store);
});
})));
it('retrieves 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.findOneById.and.returnValue(Promise.resolve(testModelFixture));
return store.findOne(testModelFixture.id)
.then((res) => {
expect(repositorySpy.findOneById)
.toHaveBeenCalledWith(testModelFixture.id);
expect(res)
.toEqual(testModelFixture);
});
})));
it('rejects retrieval with not found exception when there is no model', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {
(db as any).connection = dbConnectionSpy;
repositorySpy.findOneById.and.returnValue(Promise.resolve(null));
return store.findOne(123)
.catch((err: Error) => {
expect(err instanceof NotFoundException)
.toBe(true);
expect(err.message)
.toEqual('TestModel not found with id [123]');
});
})));
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;
const testModelsFixture = [new TestModel({id: 12})];
repositorySpy.find.and.returnValue(Promise.resolve(testModelsFixture));
return store.findMany()
.then((res: any) => {
expect(repositorySpy.find)
.toHaveBeenCalledWith({});
expect(res instanceof Collection)
.toBe(true);
expect(res[0])
.toEqual(testModelsFixture[0]);
});
})));
it('rejects retrieval with not found exception when there is no models', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {
(db as any).connection = dbConnectionSpy;
repositorySpy.find.and.returnValue(Promise.resolve([]));
return store.findMany()
.catch((err: any) => {
expect(repositorySpy.find)
.toHaveBeenCalledWith({});
expect(err instanceof NotFoundException)
.toBe(true);
expect(err.message)
.toEqual('No TestModel found with query params [undefined]');
});
})));
it('persists a single entity to the orm', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {
(db as any).connection = dbConnectionSpy;
const testModelFixture = new TestModel({id: 10});
repositorySpy.persist.and.returnValue(Promise.resolve(testModelFixture));
return store.saveOne(testModelFixture)
.then((res) => {
expect(repositorySpy.persist)
.toHaveBeenCalledWith(testModelFixture);
expect(res)
.toEqual(testModelFixture);
});
})));
it('checks if entity exists in the orm', async(inject([TestDatabaseStore, Database], (store: TestDatabaseStore, db: Database) => {
(db as any).connection = dbConnectionSpy;
const testModelFixture = new TestModel({id: 10});
repositorySpy.findOneById.and.returnValue(Promise.resolve(testModelFixture));
return store.hasOne(testModelFixture)
.then((res) => {
expect(res)
.toBe(true);
repositorySpy.findOneById.and.returnValue(Promise.reject(new Error('not found')));
return store.hasOne(testModelFixture);
})
.then((res) => {
expect(res)
.toBe(false);
});
})));
});