-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store._find
asserts adapterPayload
not empty
#3916
Merged
bmac
merged 1 commit into
emberjs:master
from
seanpdoyle:sd-assert-adapter-payload-not-empty
Nov 20, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,14 @@ import { | |
|
||
var Promise = Ember.RSVP.Promise; | ||
|
||
function payloadIsNotBlank(adapterPayload) { | ||
if (Ember.isArray(adapterPayload)) { | ||
return true; | ||
} else { | ||
return Object.keys(adapterPayload || {}).length; | ||
} | ||
} | ||
|
||
export function _find(adapter, store, typeClass, id, internalModel, options) { | ||
var snapshot = internalModel.createSnapshot(options); | ||
var promise = adapter.findRecord(store, typeClass, id, snapshot); | ||
|
@@ -24,7 +32,7 @@ export function _find(adapter, store, typeClass, id, internalModel, options) { | |
promise = _guard(promise, _bind(_objectIsAlive, store)); | ||
|
||
return promise.then(function(adapterPayload) { | ||
Ember.assert("You made a request for a " + typeClass.typeClassKey + " with id " + id + ", but the adapter's response did not have any data", adapterPayload); | ||
Ember.assert("You made a `find` request for a " + typeClass.typeClassKey + " with id " + id + ", but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload)); | ||
return store._adapterRun(function() { | ||
var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, id, 'findRecord'); | ||
//TODO Optimize | ||
|
@@ -56,6 +64,7 @@ export function _findMany(adapter, store, typeClass, ids, internalModels) { | |
promise = _guard(promise, _bind(_objectIsAlive, store)); | ||
|
||
return promise.then(function(adapterPayload) { | ||
Ember.assert("You made a `findMany` request for a " + typeClass.typeClassKey + " with ids " + ids + ", but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload)); | ||
return store._adapterRun(function() { | ||
var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, null, 'findMany'); | ||
//TODO Optimize, no need to materialize here | ||
|
@@ -77,6 +86,7 @@ export function _findHasMany(adapter, store, internalModel, link, relationship) | |
promise = _guard(promise, _bind(_objectIsAlive, internalModel)); | ||
|
||
return promise.then(function(adapterPayload) { | ||
Ember.assert("You made a `findHasMany` request for a " + internalModel.modelName + "'s `" + relationship.key + "` relationship, using link " + link + ", but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bmac how can I exercise this assertion? |
||
return store._adapterRun(function() { | ||
var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, null, 'findHasMany'); | ||
//TODO Use a non record creating push | ||
|
@@ -126,6 +136,7 @@ export function _findAll(adapter, store, typeClass, sinceToken, options) { | |
promise = _guard(promise, _bind(_objectIsAlive, store)); | ||
|
||
return promise.then(function(adapterPayload) { | ||
Ember.assert("You made a `findAll` request for " + typeClass.typeClassKey + "records, but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload)); | ||
store._adapterRun(function() { | ||
var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, null, 'findAll'); | ||
//TODO Optimize | ||
|
@@ -172,6 +183,7 @@ export function _queryRecord(adapter, store, typeClass, query) { | |
promise = _guard(promise, _bind(_objectIsAlive, store)); | ||
|
||
return promise.then(function(adapterPayload) { | ||
Ember.assert("You made a `queryRecord` request for " + typeClass.typeClassKey + "records, with query `" + query + "`, but the adapter's response did not have any data", payloadIsNotBlank(adapterPayload)); | ||
var record; | ||
store._adapterRun(function() { | ||
var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, null, 'queryRecord'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,3 +137,27 @@ test("When a single record is requested, and the promise is rejected, the record | |
}); | ||
|
||
}); | ||
|
||
test('When a single record is requested, and the payload is blank', (assert) => { | ||
env.registry.register('adapter:person', DS.Adapter.extend({ | ||
findRecord: () => Ember.RSVP.resolve({}) | ||
})); | ||
|
||
assert.expectAssertion(() => { | ||
run(() => store.find('person', 'the-id')); | ||
}, /the adapter's response did not have any data/); | ||
}); | ||
|
||
test('When multiple records are requested, and the payload is blank', (assert) => { | ||
env.registry.register('adapter:person', DS.Adapter.extend({ | ||
coalesceFindRequests: true, | ||
findMany: () => Ember.RSVP.resolve({}) | ||
})); | ||
|
||
assert.expectAssertion(() => { | ||
run(() => { | ||
store.findRecord('person', '1'); | ||
store.findRecord('person', '2'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bmac 👍 that did the trick. |
||
}, /the adapter's response did not have any data/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmac how can I exercise this assertion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seanpdoyle I would follow the pattern used in this test of stubbing the adapter method to return a value that triggers the assertion then using the
expectAssertion
helper to test that it is called.data/tests/integration/adapter/queries-test.js
Lines 47 to 63 in 1d5dfa1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmac unfortunately, there isn't a
store
operation that maps directly to this one like the others.For instance,
adapter.findRecord
maps tostore.find
,adapter.query
maps tostore.query
(and maybestore.queryRecord
?).In this case, isn't
findMany
used to coalesce find requests?Would I be able to exercise
adapter.findMany
with astore.query({ ids: [1,2,3] })
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmac I've tried the following:
But it didn't raise an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmac which
store
operation maps to afindMany
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you can trigger it by setting
coalesceFindRequests: true
on the adapter then making 2findRecord
requests in the same run loop.