Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion tests/test_annotations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from tests.test_order_by import CustomOrdering
from django.test import TestCase, Client
from django.contrib.auth.models import User

from binder.json import jsonloads
from .testapp.models import Animal, Caretaker, Zoo
from .testapp.models import Animal, Caretaker, Zoo, Costume


class AnnotationTestCase(TestCase):
Expand Down Expand Up @@ -160,6 +161,44 @@ def test_context_annotation(self):
self.assertEqual(data['data']['name'], 'Harambe')
self.assertEqual(data['data']['prefixed_name'], 'Lady Harambe')

def test_context_annotation_sorting(self):
zoo = Zoo(name='Apenheul')
zoo.save()
harambe = Animal(zoo=zoo, name='Harambe', age=4)
harambe.save()
bokito = Animal(zoo=zoo, name='Bokito', age=3)
bokito.save()

with CustomOrdering(Animal, 'age_product'):
res = self.client.get('/animal/?factor=2')
self.assertEqual(res.status_code, 200)
returned_data = jsonloads(res.content)

data = [x['name'] for x in returned_data['data']]
self.assertEqual(['Bokito', 'Harambe'], data)

with CustomOrdering(Animal, 'age_product'):
# This time the factor is negative, which should give the reverse effect
res = self.client.get('/animal/?factor=-2')
self.assertEqual(res.status_code, 200)
returned_data = jsonloads(res.content)

data = [x['name'] for x in returned_data['data']]
self.assertEqual(['Harambe', 'Bokito'], data)

def test_context_aware_relation_error(self):
zoo = Zoo(name='Apenheul')
zoo.save()
harambe = Animal(zoo=zoo, name='Harambe', age=4)
harambe.save()
bokito = Animal(zoo=zoo, name='Bokito', age=3)
bokito.save()
harambe_costume = Costume(animal=harambe)
bokito_costume = Costume(animal=bokito)

with CustomOrdering(Costume, 'animal.age_product'):
res = self.client.get('/animal/?factor=2')
self.assertEqual(res.status_code, 418)

class IncludeAnnotationsTest(TestCase):

Expand Down
6 changes: 6 additions & 0 deletions tests/testapp/models/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class Concat(Func):
function = 'CONCAT'
output_field = models.TextField()


# From the api docs: an animal with a name.
class Animal(LoadedValuesMixin, BinderModel):
name = models.TextField(max_length=64)
age = models.IntegerField(default=5)
zoo = models.ForeignKey('Zoo', on_delete=models.CASCADE, related_name='animals', blank=True, null=True)
zoo_of_birth = models.ForeignKey('Zoo', on_delete=models.CASCADE, related_name='+', blank=True, null=True) # might've been born outside captivity
caretaker = models.ForeignKey('Caretaker', on_delete=models.PROTECT, related_name='animals', blank=True, null=True)
Expand All @@ -34,3 +36,7 @@ class Annotations:
Value(request.GET.get('animal_name_prefix', 'Sir') + ' '),
F('name'),
))

age_product = ContextAnnotation(lambda request: Value(
Value(request.GET.get('factor')) * F('age')
))