test_repeat.py 984 B

123456789101112131415161718192021222324
  1. from django.db.models import CharField, Value
  2. from django.db.models.functions import Length, Repeat
  3. from django.test import TestCase
  4. from .models import Author
  5. class RepeatTests(TestCase):
  6. def test_basic(self):
  7. Author.objects.create(name='John', alias='xyz')
  8. tests = (
  9. (Repeat('name', 0), ''),
  10. (Repeat('name', 2), 'JohnJohn'),
  11. (Repeat('name', Length('alias'), output_field=CharField()), 'JohnJohnJohn'),
  12. (Repeat(Value('x'), 3, output_field=CharField()), 'xxx'),
  13. )
  14. for function, repeated_text in tests:
  15. with self.subTest(function=function):
  16. authors = Author.objects.annotate(repeated_text=function)
  17. self.assertQuerysetEqual(authors, [repeated_text], lambda a: a.repeated_text, ordered=False)
  18. def test_negative_number(self):
  19. with self.assertRaisesMessage(ValueError, "'number' must be greater or equal to 0."):
  20. Repeat('name', -1)