8000 Moves out relation responsibilities from Mapper class by stalniy · Pull Request #405 · js-data/js-data · GitHub
[go: up one dir, main page]

Skip to content

Moves out relation responsibilities from Mapper class #405

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

Merged
merged 4 commits into from
Apr 24, 2017
Merged

Moves out relation responsibilities from Mapper class #405

merged 4 commits into from
Apr 24, 2017

Conversation

stalniy
Copy link
Contributor
@stalniy stalniy commented Sep 18, 2016

It took a while to understand and fix the code of Mapper.js, so I also plan to add few unit tests related to create/createMany methods and send this a bit earlier just to make you to be familiar with changes. Probably they are not simple to understand, so I will glad to answer any questions you have

@jmdobry jmdobry changed the title Moves out relation responsibilities form Mapper class Moves out relation responsibilities from Mapper class Sep 20, 2016
@codecov-io
Copy link
codecov-io commented Oct 29, 2016

Codecov Report

Merging #405 into master will decrease coverage by 0.09%.
The diff coverage is 97.47%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #405     +/-   ##
=========================================
- Coverage   95.97%   95.87%   -0.1%     
=========================================
  Files          21       21             
  Lines        2508     2520     +12     
=========================================
+ Hits         2407     2416      +9     
- Misses        101      104      +3
Impacted Files Coverage Δ
src/Relation/HasOne.js 87.5% <100%> (+1.78%) ⬆️
src/Relation/HasMany.js 100% <100%> (ø) ⬆️
src/Relation.js 99.03% <100%> (+0.14%) ⬆️
src/Relation/BelongsTo.js 87.5% <83.33%> (-2.5%) ⬇️
src/Mapper.js 97.8% <97.77%> (-0.71%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc6c08b...471c28a. Read the comment docs.

@stalniy
Copy link
Contributor Author
stalniy commented Nov 3, 2016

@jmdobry simplified tests a bit and found a bug, fixed that =)

Plan to continue with tests for Mapper#create and Mapper#createMany

@stalniy
Copy link
Contributor Author
stalniy commented Nov 3, 2016

Also it would be useful if you could describe all relations between classes, and stuff like why we subscribe to all collection/record events, etc.

I mean describe somewhere in doc/wiki

@jmdobry
Copy link
Member
jmdobry commented Nov 10, 2016

Sorry it took me so long to get back to you on this.

Here is where I'm describing the relations, I hope it helps: http://www.js-data.io/v3.0/docs/relations

What's the status of adding more tests for this? (Plus needs to be updated with the latest master)

@stalniy
Copy link
Contributor Author
stalniy commented Nov 10, 2016

There are 2 tests in create.js and all tests in createMany.js files

They are extremely huge. Hard to understand sometime the logic behind the code

@@ -264,7 +264,7 @@ export default Component.extend({
// Here, the currently visted record does not correspond to any record
// in the collection, so (optionally) instantiate this record and insert
// it into the collection
record = this.mapper ? this.mapper.createRecord(record, opts) : record
record = this.mapper && !this.mapper.is(record) ? this.mapper.createRecord(record, opts) : record
Copy link
Member

Choose a reason for hiding this comment

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

This would be a breaking change, as createRecord does more than just wrap the props in the Mapper's Record class. It also checks the record's relations and makes sure have been wrapped too. createRecord doesn't double wrap records, so you don't need the !this.mapper.is(record) check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to my debugging record is always here an instance of mapper and if not then it should be converted through createRecord. JSData supports only 3 ways of model creation: createRecord, createInstance, wrap (all goes through createRecord). All others should be unacceptable. If people want to instantiate model using new it shouldn't work properly and this is what should be noted in docs.

Please correct me if I'm wrong, so then I will change back this

Copy link
Member

Choose a reason for hiding this comment

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

You are correct, but I still think there's a use case you're missing by adding that additional check to the conditional expression. e.g.

Before your change:

let user = store.createRecord('user', { id: 1 });
user.profile = { id: 1, user_id: 1 };
store.is('profile', user.profile); // false
user = store.add('user', user);
store.is('profile', user.profile); // true

after your change:

let user = store.createRecord('user', { id: 1 });
user.profile = { id: 1, user_id: 1 };
store.is('profile', user.profile); // false
user = store.add('user', user);
store.is('profile', user.profile); // false, uh oh!

Copy link
Contributor Author

Ch 8000 oose a reason for hiding this comment

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

I see that make sense. Will revert

}
return undefined

return errors.some(Boolean) ? errors : undefined
Copy link
Member

Choose a reason for hiding this comment

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

Does Babel transpile uses of Array.prototype.some into ES5-compatible code? If not then we can't use it.

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's pity, the same story as in tests doesn't work here =) So, probably I need to change. But again this is only about IE8, does it make sense to care about it? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some
http://caniuse.com/usage-table

Let me know if you want to revert this back

Copy link
Member

Choose a reason for hiding this comment

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

Need to change as JSData v3 declares than it requires no higher than ES5 support in order to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for being a pain, but every/some methods are part of ES5.

http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.17

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But anyway this is up to you. If you say don't use some/every because of IE8 then I will change this

Copy link
Member

Choose a reason for hiding this comment

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

Ah you are correct, I don't know why I was thinking every and some were introduced in the ES2015 spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way, I've just found out that Array.isArray is also a part of ES5 spec :) http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.3.2

assert.equal(user, undefined, 'user was not created')
assert.equal(props[User.idAttribute], undefined, 'props does NOT have an id')
})
it('should disallow extra props', async function () {
Copy link
Member

Choose a reason for hiding this comment

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

Did this test just get totally deleted? It needs to stay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes deleted this test because the only thing which we need to test here is that Schema#validate is called and the rest of schema related checks should be done in schema tests. Otherwise you need to duplicate not only disallowed props but all kind of checks (required, minlength, min/max, propsPatterns, etc). All that tests were fixed in #417.

Responsibility of this tests is just to test creation of resources, not their validation. Plus there is a small set of tests which checks what happens if record is invalid, you can find them by searching describe('when invalid object is passed'

So, does it make sense now?

Copy link
Member

Choose a reason for hiding this comment

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

Well, maybe this test needs to get moved into the integration test suite, as it's testing a more complex code path than what the unit tests cover.

@jmdobry
Copy link
Member
jmdobry commented Dec 6, 2016

Needs to be updated with master.

@stalniy
Copy link
Contributor Author
stalniy commented Dec 18, 2016

@jmdobry

  • rebased on latest master
  • added support for relation presets in tests in order to simplify tests' setup

The only thing left is to move should disallow extra props test into integration tests

@stalniy
Copy link
Contributor Author
stalniy commented Dec 18, 2016

I think that refactoring of mapper/createMany.test.js will be done in a separate PR because the logic related to DataStore should be extracted from mapper's tests. And I will proceed doing this the PR will be hard to review.

It's even now pretty big

Update:
everything is done but #418 should be merged first. And then I will add few more tests for belongsTo relationship in this PR

@stalniy
Copy link
Contributor Author
stalniy commented Jan 18, 2017

@jmdobry happy you are back :)

For some reason nyc fails:

> nyc --require babel-core/register --require babel-polyfill --cache mocha  --recursive -t 20000 -R dot && nyc report --reporter=html

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: spawn mocha ENOENT
    at exports._errnoException (util.js:1026:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:359:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:606:11)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

Could you please take a look?

@stalniy
Copy link
Contributor Author
stalniy commented Mar 16, 2017

@jmdobry looks like everything is fine now. Can we merge this? So, I will be able to proceed on the next steps

@stalniy
Copy link
Contributor Author
stalniy commented Apr 11, 2017

@jmdobry any news?

@jmdobry jmdobry merged commit 009e7bf into js-data:master Apr 24, 2017
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.

3 participants
0