Skip to content

Commit

Permalink
Added a test to reproduce the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
delinhabit committed Jul 25, 2015
1 parent bdeb289 commit f53c6c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class Meta:


class BasicModel(RESTFrameworkModel):
text = models.CharField(max_length=100, verbose_name=_("Text comes here"), help_text=_("Text description."))
text = models.CharField(
max_length=100,
verbose_name=_("Text comes here"),
help_text=_("Text description."))


class BaseFilterableItem(RESTFrameworkModel):
Expand Down Expand Up @@ -45,6 +48,9 @@ class ManyToManySource(RESTFrameworkModel):
class ForeignKeyTarget(RESTFrameworkModel):
name = models.CharField(max_length=100)

def get_first_source(self):
return self.sources.first()


class ForeignKeySource(RESTFrameworkModel):
name = models.CharField(max_length=100)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_relations_hyperlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ class Meta:
fields = ('url', 'name', 'sources')


class ForeignKeyTargetCallableSourceSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ForeignKeyTarget
fields = ('url', 'name', 'first_source')

first_source = serializers.HyperlinkedRelatedField(
read_only=True,
source='get_first_source',
view_name='foreignkeysource-detail')


class ForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ForeignKeySource
Expand Down Expand Up @@ -444,3 +455,22 @@ def test_reverse_foreign_key_retrieve_with_null(self):
{'url': 'http://testserver/onetoonetarget/2/', 'name': 'target-2', 'nullable_source': None},
]
self.assertEqual(serializer.data, expected)


class HyperlinkedRelationCallableSourceTests(TestCase):
urls = 'tests.test_relations_hyperlink'

def setUp(self):
self.target = ForeignKeyTarget.objects.create(name='target-1')
ForeignKeySource.objects.create(name='source-1', target=self.target)
ForeignKeySource.objects.create(name='source-2', target=self.target)

def test_relation_field_callable_source(self):
serializer = ForeignKeyTargetCallableSourceSerializer(self.target, context={'request': request})
expected = {
'url': 'http://testserver/foreignkeytarget/1/',
'name': 'target-1',
'first_source': 'http://testserver/foreignkeysource/1/',
}
with self.assertNumQueries(1):
self.assertEqual(serializer.data, expected)

0 comments on commit f53c6c7

Please sign in to comment.