Skip to content

Commit 756e936

Browse files
committed
Add more robust OH tests for find method
1 parent 839bb9a commit 756e936

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

test/persistence-hooks.suite.js

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ module.exports = function(dataSource, should, connectorCapabilities) {
8080
TestModel.create({name: 'second'}, function(err) {
8181
if (err) return done(err);
8282
var location1 = new GeoPoint({lat: 10.2, lng: 6.7});
83+
var location1 = new GeoPoint({lat: 10.3, lng: 6.8});
8384
GeoModel.create([
8485
{name: 'Rome', location: location1},
86+
{name: 'Tokyo', location: location1},
8587
], function(err) {
8688
done(err);
8789
});
@@ -107,6 +109,21 @@ module.exports = function(dataSource, should, connectorCapabilities) {
107109
});
108110
});
109111

112+
it('triggers the loaded hook multiple times when multiple instances exist', function(done) {
113+
monitorHookExecution();
114+
115+
TestModel.find(function(err, list) {
116+
if (err) return done(err);
117+
118+
hookMonitor.names.should.eql([
119+
'access',
120+
'loaded',
121+
'loaded',
122+
]);
123+
done();
124+
});
125+
});
126+
110127
it('should not trigger hooks, if notify is false', function(done) {
111128
monitorHookExecution();
112129
TestModel.find(
@@ -119,7 +136,8 @@ module.exports = function(dataSource, should, connectorCapabilities) {
119136
});
120137
});
121138

122-
it('triggers correct hooks when near filter is used', function(done) {
139+
it('triggers the loaded hook multiple times when multiple instances exist when near filter is used',
140+
function(done) {
123141
var hookMonitorGeoModel;
124142
hookMonitorGeoModel = new HookMonitor({includeModelName: false});
125143

@@ -134,7 +152,8 @@ module.exports = function(dataSource, should, connectorCapabilities) {
134152
};
135153
GeoModel.find(query, function(err, list) {
136154
if (err) return done(err);
137-
hookMonitorGeoModel.names.should.eql(['access', 'loaded']);
155+
156+
hookMonitorGeoModel.names.should.eql(['access', 'loaded', 'loaded']);
138157
done();
139158
});
140159
});
@@ -151,7 +170,26 @@ module.exports = function(dataSource, should, connectorCapabilities) {
151170

152171
GeoModel.find(query, function(err, list) {
153172
if (err) return done(err);
154-
list.map(get('name')).should.eql(['Berlin']);
173+
list.map(get('name')).should.eql(['Berlin', 'Berlin']);
174+
done();
175+
});
176+
});
177+
178+
it('applies updates to one specific instance from `loaded` hook when near filter is used',
179+
function(done) {
180+
GeoModel.observe('loaded', function(ctx, next) {
181+
if (ctx.data.name === 'Rome')
182+
ctx.data.name = 'Berlin';
183+
next();
184+
});
185+
186+
var query = {
187+
where: {location: {near: '10,5'}},
188+
};
189+
190+
GeoModel.find(query, function(err, list) {
191+
if (err) return done(err);
192+
list.map(get('name')).should.containEql('Berlin', 'Tokyo');
155193
done();
156194
});
157195
});
@@ -169,6 +207,21 @@ module.exports = function(dataSource, should, connectorCapabilities) {
169207
});
170208
});
171209

210+
it('applies updates to one specific instance from `loaded` hook when near filter is not used',
211+
function(done) {
212+
TestModel.observe('loaded', function(ctx, next) {
213+
if (ctx.data.name === 'first')
214+
ctx.data.name = 'Paris';
215+
next();
216+
});
217+
218+
TestModel.find(function(err, list) {
219+
if (err) return done(err);
220+
list.map(get('name')).should.eql(['Paris', 'second']);
221+
done();
222+
});
223+
});
224+
172225
it('should not trigger hooks for geo queries, if notify is false',
173226
function(done) {
174227
monitorHookExecution();

0 commit comments

Comments
 (0)