-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32eeb53
commit 34af138
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
from django import forms | ||
|
||
from colorfield.validators import color_hex_validator | ||
from colorfield.widgets import ColorWidget | ||
|
||
|
||
class ColorField(forms.CharField): | ||
default_validators = [ | ||
color_hex_validator, | ||
] | ||
|
||
def __init__(self, *args, **kwargs): | ||
validator = kwargs.pop("validator", color_hex_validator) | ||
self.default_validators = [validator] | ||
kwargs.setdefault("widget", ColorWidget()) | ||
super().__init__(*args, **kwargs) |
This file contains 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,6 @@ | ||
from django import forms | ||
from colorfield.forms import ColorField | ||
|
||
|
||
class BasicForm(forms.Form): | ||
color = ColorField(initial="#FF0000") |
This file contains 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,54 @@ | ||
from django.test import TestCase | ||
from tests.forms import BasicForm | ||
|
||
|
||
class BasicFormTest(TestCase): | ||
def test_color_field_initial_value(self): | ||
""" | ||
Test that the color field has the correct initial value. | ||
""" | ||
form = BasicForm() | ||
self.assertEqual(form.fields["color"].initial, "#FF0000") | ||
|
||
def test_color_field_valid_data(self): | ||
""" | ||
Test that the form is valid with correct HEX color data. | ||
""" | ||
data = {"color": "#00FF00"} | ||
form = BasicForm(data=data) | ||
self.assertTrue(form.is_valid()) | ||
self.assertEqual(form.cleaned_data["color"], "#00FF00") | ||
|
||
def test_color_field_invalid_data(self): | ||
""" | ||
Test that the form is invalid with incorrect HEX color data. | ||
""" | ||
# Test invalid HEX color (missing #) | ||
data = {"color": "00FF00"} | ||
form = BasicForm(data=data) | ||
self.assertFalse(form.is_valid()) | ||
self.assertIn("color", form.errors) | ||
|
||
# Test invalid HEX color (invalid characters) | ||
data = {"color": "#ZZZZZZ"} | ||
form = BasicForm(data=data) | ||
self.assertFalse(form.is_valid()) | ||
self.assertIn("color", form.errors) | ||
|
||
def test_color_field_empty_data(self): | ||
""" | ||
Test that the form is invalid when the color field is empty. | ||
""" | ||
data = {"color": ""} | ||
form = BasicForm(data=data) | ||
self.assertFalse(form.is_valid()) | ||
self.assertIn("color", form.errors) | ||
|
||
def test_color_field_widget_rendering(self): | ||
""" | ||
Test that the color field widget renders correctly. | ||
""" | ||
form = BasicForm() | ||
rendered_form = form.as_p() | ||
self.assertIn('type="text"', rendered_form) | ||
self.assertIn('value="#FF0000"', rendered_form) |