-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Duplicate error msg fix for IP address field #9647
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
Open
Daksh2000
wants to merge
4
commits into
encode:master
Choose a base branch
from
Daksh2000:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3681824
Duplicate error msg fix for IP address field
dakshbhayana11811137 08aba54
WIP
dakshbhayana11811137 ec927c9
Refactored Test cases for GenericIPAddress Field
dakshbhayana11811137 6128db6
Refactored Test Cases with additional models and Serializers
dakshbhayana11811137 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from django.db import models | ||
from django.test import TestCase | ||
|
||
from rest_framework import serializers | ||
from rest_framework.exceptions import ValidationError | ||
|
||
|
||
# Define the model | ||
class TestModel(models.Model): | ||
address = models.GenericIPAddressField(protocol="both") | ||
|
||
class Meta: | ||
app_label = "main" | ||
|
||
|
||
class TestSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = TestModel | ||
fields = "__all__" | ||
|
||
|
||
# Define the serializer in setUp | ||
class TestSerializerTestCase(TestCase): | ||
def setUp(self): | ||
"""Initialize serializer class.""" | ||
self.serializer_class = TestSerializer | ||
|
||
def test_invalid_ipv4_for_ipv4_field(self): | ||
"""Test that an invalid IPv4 raises only an IPv4-related error.""" | ||
TestModel._meta.get_field("address").protocol = "IPv4" # Set field to IPv4 only | ||
invalid_data = {"address": "invalid-ip"} | ||
serializer = self.serializer_class(data=invalid_data) | ||
|
||
with self.assertRaises(ValidationError) as context: | ||
serializer.is_valid(raise_exception=True) | ||
|
||
self.assertEqual( | ||
str(context.exception.detail["address"][0]), | ||
"Enter a valid IPv4 address." | ||
) | ||
|
||
def test_invalid_ipv6_for_ipv6_field(self): | ||
"""Test that an invalid IPv6 raises only an IPv6-related error.""" | ||
TestModel._meta.get_field("address").protocol = "IPv6" # Set field to IPv6 only | ||
invalid_data = {"address": "invalid-ip"} | ||
serializer = self.serializer_class(data=invalid_data) | ||
|
||
with self.assertRaises(ValidationError) as context: | ||
serializer.is_valid(raise_exception=True) | ||
|
||
self.assertEqual( | ||
str(context.exception.detail["address"][0]), | ||
"Enter a valid IPv6 address." | ||
) | ||
|
||
def test_invalid_both_protocol(self): | ||
"""Test that an invalid IP raises a combined error message when protocol is both.""" | ||
TestModel._meta.get_field("address").protocol = "both" # Allow both IPv4 & IPv6 | ||
invalid_data = {"address": "invalid-ip"} | ||
serializer = self.serializer_class(data=invalid_data) | ||
|
||
with self.assertRaises(ValidationError) as context: | ||
serializer.is_valid(raise_exception=True) | ||
|
||
self.assertEqual( | ||
str(context.exception.detail["address"][0]), | ||
"Enter a valid IPv4 or IPv6 address." | ||
) | ||
|
||
def test_valid_ipv4(self): | ||
"""Test that a valid IPv4 passes validation.""" | ||
TestModel._meta.get_field("address").protocol = "IPv4" | ||
valid_data = {"address": "192.168.1.1"} | ||
serializer = self.serializer_class(data=valid_data) | ||
self.assertTrue(serializer.is_valid()) | ||
|
||
def test_valid_ipv6(self): | ||
"""Test that a valid IPv6 passes validation.""" | ||
TestModel._meta.get_field("address").protocol = "IPv6" | ||
valid_data = {"address": "2001:db8::ff00:42:8329"} | ||
serializer = self.serializer_class(data=valid_data) | ||
self.assertTrue(serializer.is_valid()) | ||
|
||
def test_valid_ipv4_for_both_protocol(self): | ||
"""Test that a valid IPv4 is accepted when protocol is 'both'.""" | ||
TestModel._meta.get_field("address").protocol = "both" | ||
valid_data = {"address": "192.168.1.1"} | ||
serializer = self.serializer_class(data=valid_data) | ||
self.assertTrue(serializer.is_valid()) | ||
|
||
def test_valid_ipv6_for_both_protocol(self): | ||
"""Test that a valid IPv6 is accepted when protocol is 'both'.""" | ||
TestModel._meta.get_field("address").protocol = "both" | ||
valid_data = {"address": "2001:db8::ff00:42:8329"} | ||
serializer = self.serializer_class(data=valid_data) | ||
self.assertTrue(serializer.is_valid()) |
Oops, something went wrong.
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.
I would have expected this test to be next to the existin ones:
django-rest-framework/tests/test_model_serializer.py
Line 411 in 28d0261
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.
Thanks for pointing out the existing test cases @browniebroke , I did some changes and incorporated my test cases within the same class, and did some minor changes along with it.