From 7eca2f1f962852e1a8a36fcde81d6a7b16eaa578 Mon Sep 17 00:00:00 2001 From: Benjamin Cutler Date: Mon, 4 Dec 2023 14:44:08 -0700 Subject: [PATCH] fix Django 3.2 --- tests/test_auth.py | 14 +++++++++++--- tests/test_prize.py | 9 ++++++++- tests/test_speedrun.py | 10 ++++++++++ tracker/models/event.py | 15 ++++++++++++++- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 41747ed4c..d4bd748cf 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,5 +1,7 @@ import urllib.parse +from unittest import skipIf +import django import post_office.models from django.contrib.auth import get_user_model from django.test import RequestFactory, TestCase, override_settings @@ -70,6 +72,11 @@ def test_register_inactive_user(self): ) self.assertContains(resp, 'An e-mail has been sent to your address.') + # TODO: remove skip when 3.2 no longer supported + @skipIf( + django.VERSION < (4, 1), + 'assertFormError requires response object until Django 4.1', + ) def test_register_active_user(self): AuthUser.objects.create( username='existinguser', email='test@email.com', is_active=True @@ -78,8 +85,9 @@ def test_register_active_user(self): reverse('tracker:register'), data={'email': 'test@email.com'} ) self.assertFormError( - resp, - 'form', + resp.context['form'], 'email', - 'This email is already registered. Please log in, (or reset your password if you forgot it).', + [ + 'This email is already registered. Please log in, (or reset your password if you forgot it).' + ], ) diff --git a/tests/test_prize.py b/tests/test_prize.py index 884248a3d..9ebc3eb2f 100644 --- a/tests/test_prize.py +++ b/tests/test_prize.py @@ -1,8 +1,10 @@ import datetime import random from decimal import Decimal +from unittest import skipIf from unittest.mock import patch +import django import post_office.models import pytz from dateutil.parser import parse as parse_date @@ -1467,6 +1469,11 @@ def test_prize_key_import_action(self): reverse('admin:tracker_prize_key_import', args=(self.prize_with_keys.id,)), ) + # TODO: remove skip when 3.2 no longer supported + @skipIf( + django.VERSION < (4, 1), + 'assertFormError requires response object until Django 4.1', + ) def test_prize_key_import_form(self): keys = ['dead-beef-dead-beef-123%d' % i for i in range(5)] response = self.client.get( @@ -1521,7 +1528,7 @@ def test_prize_key_import_form(self): {'keys': keys[0]}, ) self.assertFormError( - response, 'form', 'keys', ['At least one key already exists.'] + response.context['form'], 'keys', ['At least one key already exists.'] ) def test_prize_winner_admin(self): diff --git a/tests/test_speedrun.py b/tests/test_speedrun.py index 39017d0a1..a60257d6f 100755 --- a/tests/test_speedrun.py +++ b/tests/test_speedrun.py @@ -1,6 +1,8 @@ import datetime import random +from unittest import skipIf +import django import pytz from django.contrib.auth.models import User from django.core.exceptions import ValidationError @@ -313,6 +315,10 @@ def test_start_run(self): self.assertEqual(self.run1.run_time, '0:41:20') self.assertEqual(self.run1.setup_time, '0:09:40') + @skipIf( + django.VERSION < (4, 1), + 'assertFormError requires response object until Django 4.1', + ) def test_invalid_time(self): from tracker.admin.forms import StartRunForm @@ -329,6 +335,10 @@ def test_invalid_time(self): self.assertFalse(form.is_valid()) self.assertFormError(form, None, StartRunForm.Errors.invalid_start_time) + @skipIf( + django.VERSION < (4, 1), + 'assertFormError requires response object until Django 4.1', + ) def test_anchor_drift(self): from tracker.admin.forms import StartRunForm diff --git a/tracker/models/event.py b/tracker/models/event.py index ceb1b9ed0..a4d8afb21 100644 --- a/tracker/models/event.py +++ b/tracker/models/event.py @@ -21,6 +21,19 @@ from .fields import TimestampField from .util import LatestEvent +# TODO: remove when 3.10 is oldest supported version + +try: + from itertools import pairwise +except ImportError: + + def pairwise(iterable): + # pairwise('ABCDEFG') --> AB BC CD DE EF FG + a, b = itertools.tee(iterable) + next(b, None) + return zip(a, b) + + __all__ = [ 'Event', 'PostbackURL', @@ -547,7 +560,7 @@ def clean(self): 'order': 'Next anchor in the order would occur before this one' } ) - for c, n in itertools.pairwise( + for c, n in pairwise( itertools.chain( [self], SpeedRun.objects.filter(