mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-29 18:18:57 +03:00
Moved tests to tests/
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
105
tests/test_localized_auto_slug_field.py
Normal file
105
tests/test_localized_auto_slug_field.py
Normal file
@@ -0,0 +1,105 @@
|
||||
from django.conf import settings
|
||||
from django.contrib.postgres.operations import HStoreExtension
|
||||
from django.db import connection, migrations, models
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
from django.test import TestCase
|
||||
from django.utils.text import slugify
|
||||
|
||||
from localized_fields.fields import (LocalizedAutoSlugField, LocalizedField,
|
||||
LocalizedValue)
|
||||
|
||||
|
||||
class LocalizedAutoSlugFieldTestCase(TestCase):
|
||||
"""Tests the :see:LocalizedAutoSlugField class."""
|
||||
|
||||
TestModel = None
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Creates the test model in the database."""
|
||||
|
||||
super(LocalizedAutoSlugFieldTestCase, cls).setUpClass()
|
||||
|
||||
class TestModel(models.Model):
|
||||
"""Model used for testing the :see:LocalizedAutoSlugField."""
|
||||
|
||||
app_label = 'localized_fields'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.title = self.title or LocalizedValue()
|
||||
self.slug = self.slug or LocalizedValue()
|
||||
|
||||
title = LocalizedField()
|
||||
slug = LocalizedAutoSlugField(populate_from='title')
|
||||
|
||||
class TestProject:
|
||||
|
||||
def clone(self, *args, **kwargs):
|
||||
return self
|
||||
|
||||
class TestMigration(migrations.Migration):
|
||||
operations = [
|
||||
HStoreExtension()
|
||||
]
|
||||
|
||||
with connection.schema_editor() as schema_editor:
|
||||
migration_executor = MigrationExecutor(schema_editor.connection)
|
||||
migration_executor.apply_migration(
|
||||
TestProject(),
|
||||
TestMigration('eh', 'localized_fields')
|
||||
)
|
||||
schema_editor.create_model(TestModel)
|
||||
|
||||
cls.TestModel = TestModel
|
||||
|
||||
def test_populate(self):
|
||||
"""Tests whether the :see:LocalizedAutoSlugField's
|
||||
populating feature works correctly."""
|
||||
|
||||
obj = self.TestModel()
|
||||
obj.title.en = 'this is my title'
|
||||
obj.save()
|
||||
|
||||
assert obj.slug.get('en') == slugify(obj.title.en)
|
||||
|
||||
def test_populate_multiple_languages(self):
|
||||
"""Tests whether the :see:LocalizedAutoSlugField's
|
||||
populating feature correctly works for all languages."""
|
||||
|
||||
obj = self.TestModel()
|
||||
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
obj.title.set(lang_code, 'title %s' % lang_name)
|
||||
|
||||
obj.save()
|
||||
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
assert obj.slug.get(lang_code) == 'title-%s' % lang_name.lower()
|
||||
|
||||
def test_unique_slug(self):
|
||||
"""Tests whether the :see:LocalizedAutoSlugField
|
||||
correctly generates unique slugs."""
|
||||
|
||||
obj = self.TestModel()
|
||||
obj.title.en = 'title'
|
||||
obj.save()
|
||||
|
||||
another_obj = self.TestModel()
|
||||
another_obj.title.en = 'title'
|
||||
another_obj.save()
|
||||
|
||||
assert another_obj.slug.en == 'title-1'
|
||||
|
||||
@staticmethod
|
||||
def test_deconstruct():
|
||||
"""Tests whether the :see:deconstruct
|
||||
function properly retains options
|
||||
specified in the constructor."""
|
||||
|
||||
field = LocalizedAutoSlugField(populate_from='title')
|
||||
_, _, _, kwargs = field.deconstruct()
|
||||
|
||||
assert 'populate_from' in kwargs
|
||||
assert kwargs['populate_from'] == field.populate_from
|
||||
86
tests/test_localized_bleach_field.py
Normal file
86
tests/test_localized_bleach_field.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import bleach
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django_bleach.utils import get_bleach_default_options
|
||||
|
||||
from localized_fields.fields import LocalizedBleachField, LocalizedValue
|
||||
|
||||
|
||||
class TestModel:
|
||||
"""Used to declare a bleach-able field on."""
|
||||
|
||||
def __init__(self, value):
|
||||
"""Initializes a new instance of :see:TestModel.
|
||||
|
||||
Arguments:
|
||||
The value to initialize with.
|
||||
"""
|
||||
|
||||
self.value = value
|
||||
|
||||
|
||||
class LocalizedBleachFieldTestCase(TestCase):
|
||||
"""Tests the :see:LocalizedBleachField class."""
|
||||
|
||||
def test_pre_save(self):
|
||||
"""Tests whether the :see:pre_save function
|
||||
bleaches all values in a :see:LocalizedValue."""
|
||||
|
||||
value = self._get_test_value()
|
||||
model, field = self._get_test_model(value)
|
||||
|
||||
bleached_value = field.pre_save(model, False)
|
||||
self._validate(value, bleached_value)
|
||||
|
||||
def test_pre_save_none(self):
|
||||
"""Tests whether the :see:pre_save function
|
||||
works properly when specifying :see:None."""
|
||||
|
||||
model, field = self._get_test_model(None)
|
||||
|
||||
bleached_value = field.pre_save(model, False)
|
||||
assert not bleached_value
|
||||
|
||||
@staticmethod
|
||||
def _get_test_model(value):
|
||||
"""Gets a test model and a artifically
|
||||
constructed :see:LocalizedBleachField
|
||||
instance to test with."""
|
||||
|
||||
model = TestModel(value)
|
||||
|
||||
field = LocalizedBleachField()
|
||||
field.attname = 'value'
|
||||
return model, field
|
||||
|
||||
@staticmethod
|
||||
def _get_test_value():
|
||||
"""Gets a :see:LocalizedValue instance for testing."""
|
||||
|
||||
value = LocalizedValue()
|
||||
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
value.set(lang_code, '<script>%s</script>' % lang_name)
|
||||
|
||||
return value
|
||||
|
||||
@staticmethod
|
||||
def _validate(non_bleached_value, bleached_value):
|
||||
"""Validates whether the specified non-bleached
|
||||
value ended up being correctly bleached.
|
||||
|
||||
Arguments:
|
||||
non_bleached_value:
|
||||
The value before bleaching.
|
||||
|
||||
bleached_value:
|
||||
The value after bleaching.
|
||||
"""
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
expected_value = bleach.clean(
|
||||
non_bleached_value.get(lang_code),
|
||||
get_bleach_default_options()
|
||||
)
|
||||
|
||||
assert bleached_value.get(lang_code) == expected_value
|
||||
246
tests/test_localized_field.py
Normal file
246
tests/test_localized_field.py
Normal file
@@ -0,0 +1,246 @@
|
||||
from django.conf import settings
|
||||
from django.db.utils import IntegrityError
|
||||
from django.test import TestCase
|
||||
from django.utils import translation
|
||||
|
||||
from localized_fields.fields import LocalizedField, LocalizedValue
|
||||
from localized_fields.forms import LocalizedFieldForm
|
||||
|
||||
|
||||
def get_init_values() -> dict:
|
||||
"""Gets a test dictionary containing a key
|
||||
for every language."""
|
||||
|
||||
keys = {}
|
||||
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
keys[lang_code] = 'value in %s' % lang_name
|
||||
|
||||
return keys
|
||||
|
||||
|
||||
class LocalizedValueTestCase(TestCase):
|
||||
"""Tests the :see:LocalizedValue class."""
|
||||
|
||||
@staticmethod
|
||||
def tearDown():
|
||||
"""Assures that the current language
|
||||
is set back to the default."""
|
||||
|
||||
translation.activate(settings.LANGUAGE_CODE)
|
||||
|
||||
@staticmethod
|
||||
def test_init():
|
||||
"""Tests whether the __init__ function
|
||||
of the :see:LocalizedValue class works
|
||||
as expected."""
|
||||
|
||||
keys = get_init_values()
|
||||
value = LocalizedValue(keys)
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert getattr(value, lang_code, None) == keys[lang_code]
|
||||
|
||||
@staticmethod
|
||||
def test_init_default_values():
|
||||
"""Tests wehther the __init__ function
|
||||
of the :see:LocalizedValue accepts the
|
||||
default value or an empty dict properly."""
|
||||
|
||||
value = LocalizedValue()
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert getattr(value, lang_code) is None
|
||||
|
||||
@staticmethod
|
||||
def test_get_explicit():
|
||||
"""Tests whether the the :see:LocalizedValue
|
||||
class's :see:get function works properly
|
||||
when specifying an explicit value."""
|
||||
|
||||
keys = get_init_values()
|
||||
localized_value = LocalizedValue(keys)
|
||||
|
||||
for language, value in keys.items():
|
||||
assert localized_value.get(language) == value
|
||||
|
||||
@staticmethod
|
||||
def test_get_current_language():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's see:get function properly
|
||||
gets the value in the current language."""
|
||||
|
||||
keys = get_init_values()
|
||||
localized_value = LocalizedValue(keys)
|
||||
|
||||
for language, value in keys.items():
|
||||
translation.activate(language)
|
||||
assert localized_value.get() == value
|
||||
|
||||
@staticmethod
|
||||
def test_set():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's see:set function works properly."""
|
||||
|
||||
localized_value = LocalizedValue()
|
||||
|
||||
for language, value in get_init_values():
|
||||
localized_value.set(language, value)
|
||||
assert localized_value.get(language) == value
|
||||
assert getattr(localized_value, language) == value
|
||||
|
||||
@staticmethod
|
||||
def test_str():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's __str__ works properly."""
|
||||
|
||||
keys = get_init_values()
|
||||
localized_value = LocalizedValue(keys)
|
||||
|
||||
for language, value in keys.items():
|
||||
translation.activate(language)
|
||||
assert str(localized_value) == value
|
||||
|
||||
@staticmethod
|
||||
def test_str_fallback():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's __str__'s fallback functionality
|
||||
works properly."""
|
||||
|
||||
test_value = 'myvalue'
|
||||
|
||||
localized_value = LocalizedValue({
|
||||
settings.LANGUAGE_CODE: test_value
|
||||
})
|
||||
|
||||
other_language = settings.LANGUAGES[-1][0]
|
||||
|
||||
# make sure that, by default it returns
|
||||
# the value in the default language
|
||||
assert str(localized_value) == test_value
|
||||
|
||||
# make sure that it falls back to the
|
||||
# primary language when there's no value
|
||||
# available in the current language
|
||||
translation.activate(other_language)
|
||||
assert str(localized_value) == test_value
|
||||
|
||||
# make sure that it's just __str__ falling
|
||||
# back and that for the other language
|
||||
# there's no actual value
|
||||
assert localized_value.get(other_language) != test_value
|
||||
|
||||
|
||||
class LocalizedFieldTestCase(TestCase):
|
||||
"""Tests the :see:LocalizedField class."""
|
||||
|
||||
@staticmethod
|
||||
def test_from_db_value():
|
||||
"""Tests whether the :see:from_db_value function
|
||||
produces the expected :see:LocalizedValue."""
|
||||
|
||||
input_data = get_init_values()
|
||||
localized_value = LocalizedField.from_db_value(input_data)
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert getattr(localized_value, lang_code) == input_data[lang_code]
|
||||
|
||||
@staticmethod
|
||||
def test_from_db_value_none():
|
||||
"""Tests whether the :see:from_db_valuei function
|
||||
correctly handles None values."""
|
||||
|
||||
localized_value = LocalizedField.from_db_value(None)
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert localized_value.get(lang_code) is None
|
||||
|
||||
@staticmethod
|
||||
def test_to_python():
|
||||
"""Tests whether the :see:to_python function
|
||||
produces the expected :see:LocalizedValue."""
|
||||
|
||||
input_data = get_init_values()
|
||||
localized_value = LocalizedField().to_python(input_data)
|
||||
|
||||
for language, value in input_data.items():
|
||||
assert localized_value.get(language) == value
|
||||
|
||||
@staticmethod
|
||||
def test_to_python_none():
|
||||
"""Tests whether the :see:to_python function
|
||||
produces the expected :see:LocalizedValue
|
||||
instance when it is passes None."""
|
||||
|
||||
localized_value = LocalizedField().to_python(None)
|
||||
assert localized_value
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert localized_value.get(lang_code) is None
|
||||
|
||||
@staticmethod
|
||||
def test_to_python_non_dict():
|
||||
"""Tests whether the :see:to_python function produces
|
||||
the expected :see:LocalizedValue when it is
|
||||
passed a non-dictionary value."""
|
||||
|
||||
localized_value = LocalizedField().to_python(list())
|
||||
assert localized_value
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert localized_value.get(lang_code) is None
|
||||
|
||||
@staticmethod
|
||||
def test_get_prep_value():
|
||||
""""Tests whether the :see:get_prep_value function
|
||||
produces the expected dictionary."""
|
||||
|
||||
input_data = get_init_values()
|
||||
localized_value = LocalizedValue(input_data)
|
||||
|
||||
output_data = LocalizedField().get_prep_value(localized_value)
|
||||
|
||||
for language, value in input_data.items():
|
||||
assert language in output_data
|
||||
assert output_data.get(language) == value
|
||||
|
||||
@staticmethod
|
||||
def test_get_prep_value_none():
|
||||
"""Tests whether the :see:get_prep_value function
|
||||
produces the expected output when it is passed None."""
|
||||
|
||||
output_data = LocalizedField().get_prep_value(None)
|
||||
assert not output_data
|
||||
|
||||
@staticmethod
|
||||
def test_get_prep_value_no_localized_value():
|
||||
"""Tests whether the :see:get_prep_value function
|
||||
produces the expected output when it is passed a
|
||||
non-LocalizedValue value."""
|
||||
|
||||
output_data = LocalizedField().get_prep_value(['huh'])
|
||||
assert not output_data
|
||||
|
||||
def test_get_prep_value_clean(self):
|
||||
"""Tests whether the :see:get_prep_value produces
|
||||
None as the output when it is passed an empty, but
|
||||
valid LocalizedValue value but, only when null=True."""
|
||||
|
||||
localized_value = LocalizedValue()
|
||||
|
||||
with self.assertRaises(IntegrityError):
|
||||
LocalizedField(null=False).get_prep_value(localized_value)
|
||||
|
||||
assert not LocalizedField(null=True).get_prep_value(localized_value)
|
||||
assert not LocalizedField().clean(None)
|
||||
assert not LocalizedField().clean(['huh'])
|
||||
|
||||
@staticmethod
|
||||
def test_formfield():
|
||||
"""Tests whether the :see:formfield function
|
||||
correctly returns a valid form."""
|
||||
|
||||
assert isinstance(
|
||||
LocalizedField().formfield(),
|
||||
LocalizedFieldForm
|
||||
)
|
||||
34
tests/test_localized_field_form.py
Normal file
34
tests/test_localized_field_form.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields.forms import LocalizedFieldForm
|
||||
|
||||
|
||||
class LocalizedFieldFormTestCase(TestCase):
|
||||
"""Tests the workings of the :see:LocalizedFieldForm class."""
|
||||
|
||||
@staticmethod
|
||||
def test_init():
|
||||
"""Tests whether the constructor correctly
|
||||
creates a field for every language."""
|
||||
|
||||
form = LocalizedFieldForm()
|
||||
|
||||
for (lang_code, _), field in zip(settings.LANGUAGES, form.fields):
|
||||
assert field.label == lang_code
|
||||
|
||||
if lang_code == settings.LANGUAGE_CODE:
|
||||
assert field.required
|
||||
else:
|
||||
assert not field.required
|
||||
|
||||
@staticmethod
|
||||
def test_compress():
|
||||
"""Tests whether the :see:compress function
|
||||
is working properly."""
|
||||
|
||||
input_value = [lang_name for _, lang_name in settings.LANGUAGES]
|
||||
output_value = LocalizedFieldForm().compress(input_value)
|
||||
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
assert output_value.get(lang_code) == lang_name
|
||||
33
tests/test_localized_field_widget.py
Normal file
33
tests/test_localized_field_widget.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields.fields import LocalizedValue
|
||||
from localized_fields.forms import LocalizedFieldWidget
|
||||
|
||||
|
||||
class LocalizedFieldWidgetTestCase(TestCase):
|
||||
"""Tests the workings of the :see:LocalizedFieldWidget class."""
|
||||
|
||||
@staticmethod
|
||||
def test_widget_creation():
|
||||
"""Tests whether a widget is created for every
|
||||
language correctly."""
|
||||
|
||||
widget = LocalizedFieldWidget()
|
||||
assert len(widget.widgets) == len(settings.LANGUAGES)
|
||||
|
||||
@staticmethod
|
||||
def test_decompress():
|
||||
"""Tests whether a :see:LocalizedValue instance
|
||||
can correctly be "decompressed" over the available
|
||||
widgets."""
|
||||
|
||||
localized_value = LocalizedValue()
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
localized_value.set(lang_code, lang_name)
|
||||
|
||||
widget = LocalizedFieldWidget()
|
||||
decompressed_values = widget.decompress(localized_value)
|
||||
|
||||
for (lang_code, _), value in zip(settings.LANGUAGES, decompressed_values):
|
||||
assert localized_value.get(lang_code) == value
|
||||
Reference in New Issue
Block a user