-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Add Django 5.0 support #9233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Django 5.0 support #9233
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import datetime | ||
import re | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from django import VERSION as django_version | ||
from django.db import DataError, models | ||
from django.test import TestCase | ||
|
||
|
@@ -112,11 +114,15 @@ def test_updated_instance_excluded(self): | |
def test_doesnt_pollute_model(self): | ||
instance = AnotherUniquenessModel.objects.create(code='100') | ||
serializer = AnotherUniquenessSerializer(instance) | ||
assert AnotherUniquenessModel._meta.get_field('code').validators == [] | ||
assert all( | ||
["Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators] | ||
) | ||
|
||
# Accessing data shouldn't effect validators on the model | ||
serializer.data | ||
assert AnotherUniquenessModel._meta.get_field('code').validators == [] | ||
assert all( | ||
["Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators] | ||
) | ||
|
||
def test_related_model_is_unique(self): | ||
data = {'username': 'Existing', 'email': '[email protected]'} | ||
|
@@ -193,15 +199,15 @@ def setUp(self): | |
|
||
def test_repr(self): | ||
serializer = UniquenessTogetherSerializer() | ||
expected = dedent(""" | ||
UniquenessTogetherSerializer(): | ||
id = IntegerField(label='ID', read_only=True) | ||
race_name = CharField(max_length=100, required=True) | ||
position = IntegerField(required=True) | ||
expected = dedent(r""" | ||
UniquenessTogetherSerializer\(\): | ||
id = IntegerField\(label='ID', read_only=True\) | ||
race_name = CharField\(max_length=100, required=True\) | ||
position = IntegerField\(.*required=True\) | ||
class Meta: | ||
validators = [<UniqueTogetherValidator(queryset=UniquenessTogetherModel.objects.all(), fields=('race_name', 'position'))>] | ||
validators = \[<UniqueTogetherValidator\(queryset=UniquenessTogetherModel.objects.all\(\), fields=\('race_name', 'position'\)\)>\] | ||
""") | ||
assert repr(serializer) == expected | ||
assert re.search(expected, repr(serializer)) is not None | ||
|
||
def test_is_not_unique_together(self): | ||
""" | ||
|
@@ -282,13 +288,13 @@ class Meta: | |
read_only_fields = ('race_name',) | ||
|
||
serializer = ReadOnlyFieldSerializer() | ||
expected = dedent(""" | ||
ReadOnlyFieldSerializer(): | ||
id = IntegerField(label='ID', read_only=True) | ||
race_name = CharField(read_only=True) | ||
position = IntegerField(required=True) | ||
expected = dedent(r""" | ||
ReadOnlyFieldSerializer\(\): | ||
id = IntegerField\(label='ID', read_only=True\) | ||
race_name = CharField\(read_only=True\) | ||
position = IntegerField\(.*required=True\) | ||
""") | ||
assert repr(serializer) == expected | ||
assert re.search(expected, repr(serializer)) is not None | ||
|
||
def test_read_only_fields_with_default(self): | ||
""" | ||
|
@@ -366,14 +372,14 @@ class Meta: | |
fields = ['name', 'position'] | ||
|
||
serializer = TestSerializer() | ||
expected = dedent(""" | ||
TestSerializer(): | ||
name = CharField(source='race_name') | ||
position = IntegerField() | ||
expected = dedent(r""" | ||
TestSerializer\(\): | ||
name = CharField\(source='race_name'\) | ||
position = IntegerField\(.*\) | ||
class Meta: | ||
validators = [<UniqueTogetherValidator(queryset=UniquenessTogetherModel.objects.all(), fields=('name', 'position'))>] | ||
validators = \[<UniqueTogetherValidator\(queryset=UniquenessTogetherModel.objects.all\(\), fields=\('name', 'position'\)\)>\] | ||
""") | ||
assert repr(serializer) == expected | ||
assert re.search(expected, repr(serializer)) is not None | ||
|
||
def test_default_validator_with_multiple_fields_with_same_source(self): | ||
class TestSerializer(serializers.ModelSerializer): | ||
|
@@ -411,13 +417,13 @@ class Meta: | |
validators = [] | ||
|
||
serializer = NoValidatorsSerializer() | ||
expected = dedent(""" | ||
NoValidatorsSerializer(): | ||
id = IntegerField(label='ID', read_only=True) | ||
race_name = CharField(max_length=100) | ||
position = IntegerField() | ||
expected = dedent(r""" | ||
NoValidatorsSerializer\(\): | ||
id = IntegerField\(label='ID', read_only=True.*\) | ||
race_name = CharField\(max_length=100\) | ||
position = IntegerField\(.*\) | ||
""") | ||
assert repr(serializer) == expected | ||
assert re.search(expected, repr(serializer)) is not None | ||
|
||
def test_ignore_validation_for_null_fields(self): | ||
# None values that are on fields which are part of the uniqueness | ||
|
@@ -540,16 +546,16 @@ def test_repr(self): | |
# the order of validators isn't deterministic so delete | ||
# fancy_conditions field that has two of them | ||
del serializer.fields['fancy_conditions'] | ||
expected = dedent(""" | ||
UniqueConstraintSerializer(): | ||
id = IntegerField(label='ID', read_only=True) | ||
race_name = CharField(max_length=100, required=True) | ||
position = IntegerField(required=True) | ||
global_id = IntegerField(validators=[<UniqueValidator(queryset=UniqueConstraintModel.objects.all())>]) | ||
expected = dedent(r""" | ||
UniqueConstraintSerializer\(\): | ||
id = IntegerField\(label='ID', read_only=True\) | ||
race_name = CharField\(max_length=100, required=True\) | ||
position = IntegerField\(.*required=True\) | ||
global_id = IntegerField\(.*validators=\[<UniqueValidator\(queryset=UniqueConstraintModel.objects.all\(\)\)>\]\) | ||
class Meta: | ||
validators = [<UniqueTogetherValidator(queryset=<QuerySet [<UniqueConstraintModel: UniqueConstraintModel object (1)>, <UniqueConstraintModel: UniqueConstraintModel object (2)>]>, fields=('race_name', 'position'))>] | ||
validators = \[<UniqueTogetherValidator\(queryset=<QuerySet \[<UniqueConstraintModel: UniqueConstraintModel object \(1\)>, <UniqueConstraintModel: UniqueConstraintModel object \(2\)>\]>, fields=\('race_name', 'position'\)\)>\] | ||
""") | ||
assert repr(serializer) == expected | ||
assert re.search(expected, repr(serializer)) is not None | ||
|
||
def test_unique_together_field(self): | ||
""" | ||
|
@@ -569,15 +575,18 @@ def test_single_field_uniq_validators(self): | |
UniqueConstraint with single field must be transformed into | ||
field's UniqueValidator | ||
""" | ||
# Django 5 includes Max and Min values validators for IntergerField | ||
extra_validators_qty = 2 if django_version[0] >= 5 else 0 | ||
# | ||
serializer = UniqueConstraintSerializer() | ||
assert len(serializer.validators) == 1 | ||
validators = serializer.fields['global_id'].validators | ||
assert len(validators) == 1 | ||
assert len(validators) == 1 + extra_validators_qty | ||
assert validators[0].queryset == UniqueConstraintModel.objects | ||
|
||
validators = serializer.fields['fancy_conditions'].validators | ||
assert len(validators) == 2 | ||
ids_in_qs = {frozenset(v.queryset.values_list(flat=True)) for v in validators} | ||
assert len(validators) == 2 + extra_validators_qty | ||
ids_in_qs = {frozenset(v.queryset.values_list(flat=True)) for v in validators if hasattr(v, "queryset")} | ||
assert ids_in_qs == {frozenset([1]), frozenset([3])} | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usage of regex instead of old string format seems interesting to me. can you share more insight about it? pros and cons regarding this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The need arises from the integer validator generated from Django 5 integer fields. That validator breaks the assertion against the repr. In other tests, there is a conditional on Django version, an "if" to add the extra string to match. I find regex to be more flexible against changes that are not the object of the test - i.e. the correct functionality asserted from the repr in which the integer validator does not play a role. This would also prevent cluttering if, in future versions of Django, similar changes arrive (preventing the discussion for each version).
The cons I find would be the escaping cluttering in the strings because otherwise, it is pretty straight with the current testing suite ("the dedent of strings for testing the repr" way) and not harder to read.