-
Notifications
You must be signed in to change notification settings - Fork 299
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
Do not lazy load forward relations #461
Do not lazy load forward relations #461
Conversation
Codecov Report
@@ Coverage Diff @@
## master #461 +/- ##
==========================================
+ Coverage 93.44% 93.73% +0.29%
==========================================
Files 56 56
Lines 3217 3255 +38
==========================================
+ Hits 3006 3051 +45
+ Misses 211 204 -7
Continue to review full report at Codecov.
|
Could you elaborate what you mean with |
The quoted part is only an explanation for why I added the check I will try to submit a test case to cover these changes. |
a7c0766
to
bba4f6e
Compare
A test case was added. |
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.
Implementation looks good. Just a few comments on the testing front.
Thanks for working on this.
example/tests/test_serializers.py
Outdated
@@ -30,6 +32,32 @@ def setUp(self): | |||
Author.objects.create(name=name, email='{}@example.org'.format(name)) | |||
) | |||
|
|||
def test_forward_relationship_not_loaded_when_not_included(self): | |||
MockRequest = namedtuple('Request', ['query_params']) |
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.
Maybe better to use from rest_framework.test import APIRequestFactory
. There is an example in unit/test_pagination.py
how to use it.
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.
Thanks!
example/tests/test_serializers.py
Outdated
fields = '__all__' | ||
|
||
def to_representation(self, instance): | ||
raise Exception('to_representation of BlogSerializer was called') |
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.
the disadvantage of this way of testing is that this code will actually never be covered. A bit odd when test code is not covered and the long term goal of DJA would be to have 100% coverage.
So I would prefer if the already existing serializers in example/serializers.py are used for this. to_representation
would need to be mocked and assert_not_called could be used to check that to_representation has not been called.
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 didn't think about it in this way, that you also want full coverage on test code itself. Interesting.
Anyway it is nicer to have a proper assert, so I changed it according to your suggestion.
example/tests/test_serializers.py
Outdated
} | ||
|
||
serializer = EntrySerializer(context={'request': request_without_includes}) | ||
serializer.to_representation(self.entry) |
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 think it would also be good to assert the result.
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 agree. I added another test to validate the result.
return ret | ||
|
||
def _get_field_representation(self, field, instance): | ||
request = self.context.get('request', None) |
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.
None is the default return value when using get
. So seems to be a bit cleaner to use self.context.get('request')
.
👍 to extract this logic into its own method.
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.
Changed it. Good suggestion!
4239178
to
64f8ae3
Compare
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.
Looks good.
One small comment + rebase to master + add yourself to AUTHROS + changelog entry and I think this is ready for merging.
example/tests/test_serializers.py
Outdated
result.pop('created_at') | ||
result.pop('modified_at') | ||
|
||
expected = OrderedDict( |
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.
ordering is not relevant in an assertion so I think the code would be more readable using normal dict.
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.
Makes sense. I will also change it to assertDictEqual.
64f8ae3
to
7186b0c
Compare
If the forward relations are included, the renderer will break, so this optimization only works if it is not included. fixup! Do not lazy load forward relations fixup! Do not lazy load forward relations fixup! Do not lazy load forward relations fixup! Do not lazy load forward relations fixup! Do not lazy load forward relations Include test case Small refactoring Make compatible with Python 2.X fixup! Make compatible with Python 2.X Fix travis Use isort Implement suggested changes Make import backward compatible Make travis happy
7186b0c
to
91bb86d
Compare
If the forward relations are included, the renderer will break, so this optimization only works if it is not included.
Description of the Change
Fixes a copy paste error, which resulted in an optimization not being applied.
Checklist
CHANGELOG.md
AUTHORS