From 9cb0c2f13cc9032c252d535d8fb993e0cfad3c67 Mon Sep 17 00:00:00 2001 From: knokko Date: Wed, 2 Dec 2020 10:42:07 +0100 Subject: [PATCH] My first attempt to write tests for soring on the values of context-aware annotations --- tests/test_annotations.py | 41 +++++++++++++++++++++++++++++++++- tests/testapp/models/animal.py | 6 +++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/tests/test_annotations.py b/tests/test_annotations.py index 0984f4e6..86178c1a 100644 --- a/tests/test_annotations.py +++ b/tests/test_annotations.py @@ -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): @@ -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): diff --git a/tests/testapp/models/animal.py b/tests/testapp/models/animal.py index 85d844e0..c5880532 100644 --- a/tests/testapp/models/animal.py +++ b/tests/testapp/models/animal.py @@ -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) @@ -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') + ))