Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryo Chijiiwa committed Aug 2, 2019
1 parent c3a6e4c commit ad83e22
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ class Meta:
'cats', 'friendly_cats', 'bad_cats'
)

def filter_queryset(self, query):
return query.exclude(name='Atlantis')

users = DynamicRelationField(
'UserSerializer',
source='user_set',
many=True,
deferred=True)
deferred=True
)
user_count = CountField('users', required=False, deferred=True)
address = DynamicField(source='blob', required=False, deferred=True)
cats = DynamicRelationField(
Expand Down
45 changes: 45 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,51 @@ def test_get_with_request_filters_and_requires(self):
}]
}, json.loads(response.content.decode('utf-8')))

def test_implicit_vs_explicit_prefetch(self):
"""
LocationSerializer has a built-in filter to hide Atlantis.
UserSerializer can explicitly include Location, and it can also
implicitly require Location through the `number_of_cats` field.
This test ensures that LocationSerializer.filter_queryset() is
being respected regardless of whether `User.location` is being
included implicitly or explicitly.
"""
atlantis = Location.objects.create(name='Atlantis')
atlantian = User.objects.create(
name='Atlantian',
last_name='Human',
location=atlantis
)
Cat.objects.create(
name='Gato',
home=atlantis,
backup_home=self.fixture.locations[0],
)

url = (
'/users/%s/?'
'include[]=number_of_cats&'
'include[]=location.'
) % atlantian.pk
response1 = self._get_json(url)

url = (
'/users/%s/?'
'include[]=number_of_cats&'
'exclude[]=location'
) % atlantian.pk
response2 = self._get_json(url)

# Atlantis is hidden, therefore its cats are also hidden
self.assertEqual(
response1['user']['number_of_cats'],
0
)
self.assertEqual(
response1['user']['number_of_cats'],
response2['user']['number_of_cats']
)

def test_boolean_filters_on_boolean_field(self):
# create one dead user
User.objects.create(name='Dead', last_name='Mort', is_dead=True)
Expand Down

0 comments on commit ad83e22

Please sign in to comment.