Skip to content
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
merged 1 commit into from
Nov 20, 2015

Conversation

seanpdoyle
Copy link
Contributor

Currently, Store._find only asserts that the adapterPayload is
non-null (i.e. {} is a valid payload).

This commit forces the adapterPayload to be non-empty.

When TDDing with Mirage, adapterPayload is often {}, which sneaks
through the asserts and ends up as a non-descriptive:

Assertion Failed: You must include an 'id' for undefined in an object
passed to 'push'

Which is triggered in Store._pushInternalModel,
which is a few method invocations away from the source of the issue.

@seanpdoyle seanpdoyle force-pushed the sd-assert-adapter-payload-not-empty branch 2 times, most recently from 1012f3b to 2460ada Compare November 10, 2015 18:47
@@ -77,6 +82,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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that could use a better assertion. What adapter are you using?
--@bmac

What would you suggest?

@bmac
Copy link
Member

bmac commented Nov 11, 2015

These assertions look great @seanpdoyle. Do you mind adding tests to make sure the assertions are called when the payload is {}? Let me know if you need help doing figuring out where the test goes.

@seanpdoyle
Copy link
Contributor Author

@bmac I think I'll need some guidance.

I'm surprised that these assertions caused test failures to begin with.

@seanpdoyle seanpdoyle force-pushed the sd-assert-adapter-payload-not-empty branch 2 times, most recently from a95adcd to bfed007 Compare November 13, 2015 02:31
@@ -56,6 +60,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));
Copy link
Contributor Author

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?

Copy link
Member

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.

test("The store asserts when query is made and the adapter responses with a single record.", function(assert) {
env = setupStore({ person: Person, adapter: DS.RESTAdapter });
store = env.store;
adapter = env.adapter;
adapter.query = function(store, type, query, recordArray) {
assert.equal(type, Person, "the query method is called with the correct type");
return Ember.RSVP.resolve({ people: { id: 1, name: "Peter Wagenet" } });
};
assert.expectAssertion(function() {
Ember.run(function() {
store.query('person', { page: 1 });
});
}, /The response to store.query is expected to be an array but it was a single record/);
});

Copy link
Contributor Author

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 to store.find, adapter.query maps to store.query (and maybe store.queryRecord?).

In this case, isn't findMany used to coalesce find requests?

Would I be able to exercise adapter.findMany with a store.query({ ids: [1,2,3] })?

Copy link
Contributor Author

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:

adapter.findMany = () => Ember.RSVP.resolve({});

assert.expectAssertion(() => {
  run(() => store.query('person', { ids: [1, 2] }));
}, /the adapter's response did not have any data/);

But it didn't raise an error.

Copy link
Contributor Author

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 a findMany?

Copy link
Member

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 2 findRecord requests in the same run loop.

assert.expectAssertion(() => {
  run(() => {
    store.findRecord('person', 1);
    store.findRecord('person', 2);
  });
}, /the adapter's response did not have any data/);

@seanpdoyle seanpdoyle force-pushed the sd-assert-adapter-payload-not-empty branch 4 times, most recently from 352ff57 to 2e5cfae Compare November 13, 2015 05:23
Currently, [Store._find][find] only asserts that the `adapterPayload` is
non-null (i.e. `{}` is a valid payload).

This commit forces the `adapterPayload` to be non-empty.

When TDDing with Mirage, `adapterPayload` is often `{}`, which sneaks
through the asserts and ends up as a non-descriptive:

```
Assertion Failed: You must include an 'id' for undefined in an object
passed to 'push'
```

Which is triggered in [Store._pushInternalModel][_pushInternalModel],
which is a few method invocations away from the source of the issue.

[_pushInternalModel]: https://github.com/emberjs/data/blob/f1ccad8ab9356dd51f44d63585c27d8bb4ec9c3a/packages/ember-data/lib/system/store.js#L1709
[find]: https://github.com/emberjs/data/blob/f1ccad8ab9356dd51f44d63585c27d8bb4ec9c3a/packages/ember-data/lib/system/store/finders.js#L27
@seanpdoyle seanpdoyle force-pushed the sd-assert-adapter-payload-not-empty branch from 2e5cfae to a51bea9 Compare November 13, 2015 15:25
run(() => {
store.findRecord('person', '1');
store.findRecord('person', '2');
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmac 👍 that did the trick.

@seanpdoyle
Copy link
Contributor Author

@bmac what's stopping this from being merged?

Is there anything else I can do?

@bmac
Copy link
Member

bmac commented Nov 20, 2015

Sorry, @seanpdoyle I've been very busy recently and hadn't had a chance to review this pr. Looking at it now.

bmac added a commit that referenced this pull request Nov 20, 2015
…t-empty

`Store._find` asserts `adapterPayload` not empty
@bmac bmac merged commit 6ab1756 into emberjs:master Nov 20, 2015
@bmac
Copy link
Member

bmac commented Nov 20, 2015

Thanks @seanpdoyle. Sorry for the delay.

@seanpdoyle seanpdoyle deleted the sd-assert-adapter-payload-not-empty branch November 20, 2015 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants