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

Improve test setup #92

Merged
merged 32 commits into from
Sep 1, 2016
Merged

Conversation

aars
Copy link
Collaborator

@aars aars commented Aug 25, 2016

This PR is intended to review/discuss changes.

@@ -116,7 +116,7 @@ const Resource = Ember.Object.extend(ResourceOperationsMixin, {
@method toString
*/
toString() {
let type = this.get('type') || 'null';
let type = singularize(this.get('type')) || 'null';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This makes toStrings more predictable and easier to test.

@aars
Copy link
Collaborator Author

aars commented Aug 25, 2016

Cleans and improves some/most tests. Replaces a lot of handwritten strings (relationUrl etc) to test against with references to the actual urls from the used fixtures/mockdata.

Adds actual calling of promise.then() in a lot of tests. I'm unclear on why this was already done on some tests but not on others. Anyway, if there ever is a mock-server to test against this will actually become async and important.

Uses models defined in dummy app (I'm not sure if I broke that dummy app with the changes in models. Haven't looked at that yet).

const adapter = this.subject({type: 'posts', url: '/posts'});
sandbox.stub(adapter, 'fetch', function () { return RSVP.Promise.resolve(null); });
let resource = this.container.lookup('model:post').create(postMock.data);
sandbox.stub(adapter, 'cacheRemove', function () {});
Ember.run(function() {
Ember.run(() => { // Why is this called inside a Ember RunLoop? Does that imply async?
Copy link
Owner

Choose a reason for hiding this comment

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

I forget when I needed the run loop, but typically I'm expecting some backburner queues to be processed.

I'm pretty sure it was to be sure https://github.com/pixelhandler/ember-jsonapi-resources/blob/master/addon/adapters/application.js#L188 resource.destroy(); is completed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since theoretically stuff could break within the runloop I'll use another assert.expect() and async() here so we know when it does.

@pixelhandler
Copy link
Owner

@aars I like the changes. I replied to a few questions in the code comments. All the tests pass, (using ember try as well).

Perhaps we merge in after addressing the questions in the code comments.

@aars
Copy link
Collaborator Author

aars commented Aug 29, 2016

@pixelhandler Replies and updates. Will continue with other tests now.

Please have a look at my changes to find(), the PR diff makes it a bit confusing since it's actually a complete rewrite of the function. https://github.com/aars/ember-jsonapi-resources/blob/improve-test-setup/addon/adapters/application.js#L48-L69

I ended up not even testing for number. Just casting to string whenever an apparent id is found. I think this covers everything (including undefined and 0 and "0")

@aars
Copy link
Collaborator Author

aars commented Aug 29, 2016

@pixelhandler Regarding casting ids to strings. If I could direct your attention at this code, written by you: https://github.com/pixelhandler/ember-jsonapi-resources/blob/master/tests/unit/mixins/resource-operations-test.js

It uses numbers as ids everywhere. You use it consistently in this file, so no test is broken... but... this is incorrect usage of the lib/spec, in your lib, by you. It will break as soon as you, per spec, try to look up a relation by id using a string.

Can I use this to strengthen my argument about easy pitfalls and annoying users? 😝

PS: The code in this test creates invalid JSONAPI payloads to the server as well, since ids are never transformed.

@pixelhandler
Copy link
Owner

@aars when we cast to string for fetching, we should also cast to string when we deserialize too.

if (options.id) {
return this.findOne(options.id, options.query);
// Collect id and query from options (if given).
// ids are cast to string to allow number input.
Copy link
Owner

Choose a reason for hiding this comment

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

perhaps instead of "to allow" we could say to "ensure id is a Strings (which spec requires)"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Many better.

@aars
Copy link
Collaborator Author

aars commented Aug 30, 2016

@pixelhandler Casting to string when deserializing is on the other side of "the spectrum" though, as in; the current goal is to improve JSONAPI compliancy for "users" of this library, programmers (people) who feed it data, where mistakes like numerical ids can easily happen. On the other side is the server, which should never have sent a numerical id to begin with. If we're casting to string when deserializing we're only adding in support for broken JSONAPI endpoints... No? What do you think?

Not that I'm really against it. I would however add a warning message about the server sending invalid payloads (edit: I am now against it! 😄 see some line comment)

@@ -189,6 +221,10 @@ test('#findRelated', function(assert) {
});
});

// TODO: This test is broken in an odd fashion. Ends up somehow calling `addRelationship`
Copy link
Owner

Choose a reason for hiding this comment

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

This was addressed in PR #94

@pixelhandler
Copy link
Owner

@aars casting in create is good for developers and perhaps a warning if server sends ids and numbers could help. The think is the expectation of this library is that that server follows the JSONAPI 1.0 spec. It may not be necessary to assert that the IDs in a JSON payload are strings.

Here is where the deserialization begins, https://github.com/pixelhandler/ember-jsonapi-resources/blob/master/addon/mixins/fetch.js#L148-L150 And in the serializer we could add an assert for the id type in here https://github.com/pixelhandler/ember-jsonapi-resources/blob/master/addon/serializers/application.js#L208-L233 but seems fine to just trust that the server is doing it's part.

@aars
Copy link
Collaborator Author

aars commented Aug 30, 2016

I'm on my second sip of coffee, I wasn't fully awake when I wrote about deserialize supporting broken servers. Still am not, but as you noticed and commented, the casting to string in create is enough to also handle casting when/after deserializing. If we're not adding code specific to deserializing, it's no longer fitting to give a warning about numerical-ids from the server. I'd say we're done with that question. (edit: as in, we trust the server. Or, that's not our problem. We can still handle it)

@aars
Copy link
Collaborator Author

aars commented Aug 30, 2016

I have rebased/merged. I will squash the commits later today.

edit: I'm having a horrible time rebasing/squashing this. Can we consider this PR and commit history for merging?

If you prefer these commits squash I'll cherry-pick them on a new branch and create a new PR.

@aars
Copy link
Collaborator Author

aars commented Sep 1, 2016

When all is 🎉 I have this PR ready to fix/improve working with relationship resource types: realxsnl#1 (goes on top of this PR).

@pixelhandler
Copy link
Owner

@aars there is a "Squash and merge" option so I can just do that, no need for you to squash.

@pixelhandler pixelhandler merged commit 62386a5 into pixelhandler:master Sep 1, 2016
@aars aars deleted the improve-test-setup branch September 23, 2016 10:10
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