| 1 |
import os |
| 2 |
import shutil |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.core.exceptions import ImproperlyConfigured, ValidationError |
| 6 |
from django.core.files import File |
| 7 |
from django.forms import fields_for_model |
| 8 |
from django.test import TestCase |
| 9 |
|
| 10 |
from colorfield.fields import ColorField |
| 11 |
from tests.models import ( |
| 12 |
COLOR_PALETTE, |
| 13 |
Color, |
| 14 |
ColorChoices, |
| 15 |
ColorFieldRGBFormat, |
| 16 |
ColorImageField, |
| 17 |
ColorImageFieldAndDefault, |
| 18 |
ColorImageFieldAndFormat, |
| 19 |
ColorImageFieldRGBFormat, |
| 20 |
ColorInvalidImageField, |
| 21 |
ColorNoImageField, |
| 22 |
ColorNull, |
| 23 |
ColorSamples, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class ColorFieldTestCase(TestCase): |
| 28 |
def setUp(self): |
| 29 |
self.delete_media() |
| 30 |
|
| 31 |
def tearDown(self): |
| 32 |
self.delete_media() |
| 33 |
|
| 34 |
@classmethod |
| 35 |
def delete_media(cls): |
| 36 |
path = settings.MEDIA_ROOT |
| 37 |
if os.path.exists(path): |
| 38 |
shutil.rmtree(path) |
| 39 |
|
| 40 |
@classmethod |
| 41 |
def get_images_input_dir(cls): |
| 42 |
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "images") |
| 43 |
|
| 44 |
@classmethod |
| 45 |
def save_image_to_field_from_path(cls, field, path, save=True): |
| 46 |
path = os.path.join(cls.get_images_input_dir(), path) |
| 47 |
with open(path, "rb") as image: |
| 48 |
name = os.path.basename(path) |
| 49 |
field.save(name, content=File(image), save=save) |
| 50 |
|
| 51 |
def test_model_formfield_doesnt_raise(self): |
| 52 |
""" |
| 53 |
Adding a ColorField to a model should not fail in 2.2LTS. |
| 54 |
""" |
| 55 |
try: |
| 56 |
fields_for_model(Color()) |
| 57 |
except AttributeError: |
| 58 |
self.fail("Raised Attribute Error") |
| 59 |
|
| 60 |
def test_model_formfield_with_samples_and_choices_fails(self): |
| 61 |
""" |
| 62 |
Checks that supplying a ColorField with both samples |
| 63 |
and choices options fails (mutually exclusive). |
| 64 |
""" |
| 65 |
with self.assertRaises(ImproperlyConfigured): |
| 66 |
ColorField(choices=COLOR_PALETTE, samples=COLOR_PALETTE) |
| 67 |
|
| 68 |
def test_clean_field_choices(self): |
| 69 |
""" |
| 70 |
Checks that supplying a ColorField with the samples kwarg works, |
| 71 |
and that it accepts valid values outside the predefined choices. |
| 72 |
""" |
| 73 |
|
| 74 |
obj = ColorChoices() |
| 75 |
obj.color = ColorChoices.COLOR_CHOICES[0][0] |
| 76 |
try: |
| 77 |
obj.full_clean() |
| 78 |
except ValidationError as e: |
| 79 |
self.fail( |
| 80 |
"Failed to assign predefined palette choice " |
| 81 |
f"to ColorField model instance. Message: {e}" |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
other_value = "#35B6A3" |
| 86 |
obj.color = other_value |
| 87 |
with self.assertRaises(ValidationError): |
| 88 |
obj.full_clean() |
| 89 |
|
| 90 |
def test_clean_field_samples(self): |
| 91 |
""" |
| 92 |
Checks that supplying a ColorField with the samples kwarg works, |
| 93 |
and that it accepts valid values outside the predefined choices. |
| 94 |
""" |
| 95 |
|
| 96 |
obj = ColorSamples() |
| 97 |
obj.color = ColorSamples.COLOR_SAMPLES[0][0] |
| 98 |
try: |
| 99 |
obj.full_clean() |
| 100 |
except ValidationError as e: |
| 101 |
self.fail( |
| 102 |
"Failed to assign predefined palette choice " |
| 103 |
f"to ColorField model instance. Message: {e}" |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
other_value = "#35B6A3" |
| 108 |
obj.color = other_value |
| 109 |
try: |
| 110 |
obj.full_clean() |
| 111 |
except ValidationError as e: |
| 112 |
self.fail( |
| 113 |
"Failed to assign value outside palette choices " |
| 114 |
f"to ColorField model instance. Message: {e}" |
| 115 |
) |
| 116 |
|
| 117 |
def test_model_with_null(self): |
| 118 |
obj = ColorNull() |
| 119 |
obj.save() |
| 120 |
self.assertEqual(obj.color, None) |
| 121 |
|
| 122 |
def test_model_with_invalid_image_field_type(self): |
| 123 |
obj = ColorInvalidImageField() |
| 124 |
with self.assertRaises(ImproperlyConfigured): |
| 125 |
obj.save() |
| 126 |
|
| 127 |
def test_model_with_not_existing_image_field(self): |
| 128 |
obj = ColorNoImageField() |
| 129 |
with self.assertRaises(ImproperlyConfigured): |
| 130 |
obj.save() |
| 131 |
|
| 132 |
def test_model_with_image_field_empty(self): |
| 133 |
obj = ColorImageField() |
| 134 |
obj.save() |
| 135 |
self.assertEqual(obj.color, "") |
| 136 |
|
| 137 |
def test_model_with_image_field_empty_and_default(self): |
| 138 |
obj = ColorImageFieldAndDefault() |
| 139 |
obj.save() |
| 140 |
self.assertEqual(obj.color, "#FF0000") |
| 141 |
|
| 142 |
def test_model_with_image(self): |
| 143 |
model_class = ColorImageField |
| 144 |
obj = model_class() |
| 145 |
filename = "django.png" |
| 146 |
self.save_image_to_field_from_path(obj.image, filename) |
| 147 |
obj.save() |
| 148 |
|
| 149 |
self.assertTrue(obj.image.path.endswith(filename)) |
| 150 |
expected_color = "#082D20" |
| 151 |
|
| 152 |
self.assertEqual(obj.color, expected_color) |
| 153 |
|
| 154 |
obj_saved = model_class.objects.get(pk=obj.pk) |
| 155 |
self.assertEqual(obj_saved.color, expected_color) |
| 156 |
|
| 157 |
def test_model_with_image_and_format(self): |
| 158 |
model_class = ColorImageFieldAndFormat |
| 159 |
obj = model_class() |
| 160 |
filename = "django.png" |
| 161 |
self.save_image_to_field_from_path(obj.image, filename) |
| 162 |
obj.save() |
| 163 |
|
| 164 |
self.assertTrue(obj.image.path.endswith(filename)) |
| 165 |
expected_color = "#082D20FF" |
| 166 |
|
| 167 |
self.assertEqual(obj.color, expected_color) |
| 168 |
|
| 169 |
obj_saved = model_class.objects.get(pk=obj.pk) |
| 170 |
self.assertEqual(obj_saved.color, expected_color) |
| 171 |
|
| 172 |
def test_model_rgb_formats(self): |
| 173 |
obj = ColorFieldRGBFormat( |
| 174 |
color_rgb="rgb(123, 123, 123)", |
| 175 |
color_rgba="rgba(128, 199, 255, 0.55)", |
| 176 |
) |
| 177 |
obj.save() |
| 178 |
|
| 179 |
self.assertEqual(obj.color_rgb, "rgb(123, 123, 123)") |
| 180 |
self.assertEqual(obj.color_rgba, "rgba(128, 199, 255, 0.55)") |
| 181 |
|
| 182 |
obj_saved = ColorFieldRGBFormat.objects.get(pk=obj.pk) |
| 183 |
self.assertEqual(obj_saved.color_rgb, "rgb(123, 123, 123)") |
| 184 |
self.assertEqual(obj_saved.color_rgba, "rgba(128, 199, 255, 0.55)") |
| 185 |
|
| 186 |
def test_model_with_image_rgb_format(self): |
| 187 |
obj = ColorImageFieldRGBFormat() |
| 188 |
filename = "django.png" |
| 189 |
self.save_image_to_field_from_path(obj.image, filename) |
| 190 |
obj.save() |
| 191 |
|
| 192 |
self.assertTrue(obj.image.path.endswith(filename)) |
| 193 |
|
| 194 |
self.assertEqual(obj.color_rgb, "rgb(8, 45, 32)") |
| 195 |
self.assertEqual(obj.color_rgba, "rgba(8, 45, 32, 1.0)") |
| 196 |
|
| 197 |
obj_saved = ColorImageFieldRGBFormat.objects.get(pk=obj.pk) |
| 198 |
self.assertEqual(obj_saved.color_rgb, "rgb(8, 45, 32)") |
| 199 |
self.assertEqual(obj_saved.color_rgba, "rgba(8, 45, 32, 1.0)") |