mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-29 18:18:57 +03:00
Merge branch 'master' of https://github.com/SectorLabs/django-localized-fields into extra-fields
This commit is contained in:
13
tests/data.py
Normal file
13
tests/data.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
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
|
||||
@@ -2,7 +2,7 @@ from django.db import connection, migrations
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
from django.contrib.postgres.operations import HStoreExtension
|
||||
|
||||
from localized_fields import LocalizedModel
|
||||
from localized_fields.models import LocalizedModel
|
||||
|
||||
|
||||
def define_fake_model(name='TestModel', fields=None):
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import bleach
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django_bleach.utils import get_bleach_default_options
|
||||
import bleach
|
||||
|
||||
from localized_fields import LocalizedBleachField, LocalizedValue
|
||||
from localized_fields.fields import LocalizedBleachField
|
||||
from localized_fields.value import LocalizedValue
|
||||
|
||||
|
||||
class TestModel:
|
||||
45
tests/test_bulk.py
Normal file
45
tests/test_bulk.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields.fields import LocalizedField, LocalizedUniqueSlugField
|
||||
|
||||
from .fake_model import get_fake_model
|
||||
|
||||
|
||||
class LocalizedBulkTestCase(TestCase):
|
||||
"""Tests bulk operations with data structures provided
|
||||
by the django-localized-fields library."""
|
||||
|
||||
@staticmethod
|
||||
def test_localized_bulk_insert():
|
||||
"""Tests whether bulk inserts work properly when using
|
||||
a :see:LocalizedUniqueSlugField in the model."""
|
||||
|
||||
model = get_fake_model(
|
||||
'BulkSlugInsertModel',
|
||||
{
|
||||
'name': LocalizedField(),
|
||||
'slug': LocalizedUniqueSlugField(populate_from='name', include_time=True),
|
||||
'score': models.IntegerField()
|
||||
}
|
||||
)
|
||||
|
||||
to_create = [
|
||||
model(name={'en': 'english name 1', 'ro': 'romanian name 1'}, score=1),
|
||||
model(name={'en': 'english name 2', 'ro': 'romanian name 2'}, score=2),
|
||||
model(name={'en': 'english name 3', 'ro': 'romanian name 3'}, score=3)
|
||||
]
|
||||
|
||||
model.objects.bulk_create(to_create)
|
||||
assert model.objects.all().count() == 3
|
||||
|
||||
for obj in to_create:
|
||||
obj_db = model.objects.filter(
|
||||
name__en=obj.name.en,
|
||||
name__ro=obj.name.ro,
|
||||
score=obj.score
|
||||
).first()
|
||||
|
||||
assert obj_db
|
||||
assert len(obj_db.slug.en) >= len(obj_db.name.en)
|
||||
assert len(obj_db.slug.ro) >= len(obj_db.name.ro)
|
||||
86
tests/test_expressions.py
Normal file
86
tests/test_expressions.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from django.test import TestCase
|
||||
from django.db import models
|
||||
from django.utils import translation
|
||||
from django.conf import settings
|
||||
from django.contrib.postgres.aggregates import ArrayAgg
|
||||
|
||||
from localized_fields.fields import LocalizedField
|
||||
from localized_fields.value import LocalizedValue
|
||||
from localized_fields.expressions import LocalizedRef
|
||||
|
||||
from .fake_model import get_fake_model
|
||||
|
||||
|
||||
class LocalizedExpressionsTestCase(TestCase):
|
||||
"""Tests whether expressions properly work with :see:LocalizedField."""
|
||||
|
||||
TestModel1 = None
|
||||
TestModel2 = None
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Creates the test model in the database."""
|
||||
|
||||
super(LocalizedExpressionsTestCase, cls).setUpClass()
|
||||
|
||||
cls.TestModel1 = get_fake_model(
|
||||
'LocalizedExpressionsTestCase2',
|
||||
{
|
||||
'name': models.CharField(null=False, blank=False, max_length=255),
|
||||
}
|
||||
)
|
||||
|
||||
cls.TestModel2 = get_fake_model(
|
||||
'LocalizedExpressionsTestCase1',
|
||||
{
|
||||
'text': LocalizedField(),
|
||||
'other': models.ForeignKey(cls.TestModel1, related_name='features')
|
||||
}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def test_localized_ref(cls):
|
||||
"""Tests whether the :see:LocalizedRef expression properly works."""
|
||||
|
||||
obj = cls.TestModel1.objects.create(name='bla bla')
|
||||
for i in range(0, 10):
|
||||
cls.TestModel2.objects.create(
|
||||
text=LocalizedValue(dict(en='text_%d_en' % i, ro='text_%d_ro' % i, nl='text_%d_nl' % i)),
|
||||
other=obj
|
||||
)
|
||||
|
||||
def create_queryset(ref):
|
||||
return (
|
||||
cls.TestModel1.objects
|
||||
.annotate(mytexts=ref)
|
||||
.values_list('mytexts', flat=True)
|
||||
)
|
||||
|
||||
# assert that it properly selects the currently active language
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
translation.activate(lang_code)
|
||||
queryset = create_queryset(LocalizedRef('features__text'))
|
||||
|
||||
for index, value in enumerate(queryset):
|
||||
assert translation.get_language() in value
|
||||
assert str(index) in value
|
||||
|
||||
# ensure that the default language is used in case no
|
||||
# language is active at all
|
||||
translation.deactivate_all()
|
||||
queryset = create_queryset(LocalizedRef('features__text'))
|
||||
for index, value in enumerate(queryset):
|
||||
assert settings.LANGUAGE_CODE in value
|
||||
assert str(index) in value
|
||||
|
||||
# ensures that overriding the language works properly
|
||||
queryset = create_queryset(LocalizedRef('features__text', 'ro'))
|
||||
for index, value in enumerate(queryset):
|
||||
assert 'ro' in value
|
||||
assert str(index) in value
|
||||
|
||||
# ensures that using this in combination with ArrayAgg works properly
|
||||
queryset = create_queryset(ArrayAgg(LocalizedRef('features__text', 'ro'))).first()
|
||||
assert isinstance(queryset, list)
|
||||
for value in queryset:
|
||||
assert 'ro' in value
|
||||
158
tests/test_field.py
Normal file
158
tests/test_field.py
Normal file
@@ -0,0 +1,158 @@
|
||||
import json
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.utils import IntegrityError
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields.fields import LocalizedField
|
||||
from localized_fields.forms import LocalizedFieldForm
|
||||
from localized_fields.value import LocalizedValue
|
||||
|
||||
from .data import get_init_values
|
||||
|
||||
|
||||
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_value 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
|
||||
|
||||
def test_from_db_value_none_return_none(self):
|
||||
"""Tests whether the :see:from_db_value function
|
||||
correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL
|
||||
is set to True."""
|
||||
|
||||
with self.settings(LOCALIZED_FIELDS_EXPERIMENTAL=True):
|
||||
localized_value = LocalizedField.from_db_value(None)
|
||||
|
||||
assert localized_value 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_non_json():
|
||||
"""Tests whether the :see:to_python function
|
||||
properly handles a string that is not JSON."""
|
||||
|
||||
localized_value = LocalizedField().to_python('my value')
|
||||
assert localized_value.get() == 'my 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_to_python_str():
|
||||
"""Tests whether the :see:to_python function produces
|
||||
the expected :see:LocalizedValue when it is
|
||||
passed serialized string value."""
|
||||
|
||||
serialized_str = json.dumps(get_init_values())
|
||||
localized_value = LocalizedField().to_python(serialized_str)
|
||||
assert isinstance(localized_value, LocalizedValue)
|
||||
|
||||
for language, value in get_init_values().items():
|
||||
assert localized_value.get(language) == value
|
||||
assert getattr(localized_value, language) == value
|
||||
|
||||
@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
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields import LocalizedFieldForm
|
||||
from localized_fields.forms import LocalizedFieldForm
|
||||
|
||||
|
||||
class LocalizedFieldFormTestCase(TestCase):
|
||||
@@ -1,288 +0,0 @@
|
||||
from django.conf import settings
|
||||
from django.db.utils import IntegrityError
|
||||
from django.test import TestCase
|
||||
from django.utils import translation
|
||||
|
||||
from localized_fields import LocalizedField, LocalizedFieldForm, LocalizedValue
|
||||
|
||||
|
||||
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_default_language():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's see:get function properly
|
||||
gets the value in the default language."""
|
||||
|
||||
keys = get_init_values()
|
||||
localized_value = LocalizedValue(keys)
|
||||
|
||||
for language, _ in keys.items():
|
||||
translation.activate(language)
|
||||
assert localized_value.get() == keys[settings.LANGUAGE_CODE]
|
||||
|
||||
@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_eq():
|
||||
"""Tests whether the __eq__ operator
|
||||
of :see:LocalizedValue works properly."""
|
||||
|
||||
a = LocalizedValue({'en': 'a', 'ar': 'b'})
|
||||
b = LocalizedValue({'en': 'a', 'ar': 'b'})
|
||||
|
||||
assert a == b
|
||||
|
||||
b.en = 'b'
|
||||
assert a != b
|
||||
|
||||
@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
|
||||
|
||||
@staticmethod
|
||||
def test_deconstruct():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's :see:deconstruct function works properly."""
|
||||
|
||||
keys = get_init_values()
|
||||
value = LocalizedValue(keys)
|
||||
|
||||
path, args, kwargs = value.deconstruct()
|
||||
|
||||
assert args[0] == keys
|
||||
|
||||
@staticmethod
|
||||
def test_construct_string():
|
||||
"""Tests whether the :see:LocalizedValue's constructor
|
||||
assumes the primary language when passing a single string."""
|
||||
|
||||
value = LocalizedValue('beer')
|
||||
assert value.get(settings.LANGUAGE_CODE) == 'beer'
|
||||
|
||||
|
||||
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_value 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
|
||||
|
||||
def test_from_db_value_none_return_none(self):
|
||||
"""Tests whether the :see:from_db_value function
|
||||
correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL
|
||||
is set to True."""
|
||||
|
||||
with self.settings(LOCALIZED_FIELDS_EXPERIMENTAL=True):
|
||||
localized_value = LocalizedField.from_db_value(None)
|
||||
|
||||
assert localized_value 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
|
||||
)
|
||||
@@ -7,10 +7,11 @@ from django import forms
|
||||
from django.test import TestCase, override_settings
|
||||
from django.core.files.base import File, ContentFile
|
||||
from django.core.files import temp as tempfile
|
||||
from localized_fields import LocalizedFileField, LocalizedValue
|
||||
from localized_fields.fields import LocalizedFileField
|
||||
from localized_fields.value import LocalizedValue
|
||||
from localized_fields.fields.localized_file_field import LocalizedFieldFile
|
||||
from localized_fields.forms import LocalizedFileFieldForm
|
||||
from localized_fields.localized_value import LocalizedFileValue
|
||||
from localized_fields.value import LocalizedFileValue
|
||||
from localized_fields.widgets import LocalizedFileWidget
|
||||
from .fake_model import get_fake_model
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields import LocalizedField, LocalizedValue
|
||||
from localized_fields.fields import LocalizedField
|
||||
from localized_fields.value import LocalizedValue
|
||||
|
||||
from .fake_model import get_fake_model
|
||||
|
||||
@@ -33,7 +34,6 @@ class LocalizedModelTestCase(TestCase):
|
||||
|
||||
assert isinstance(obj.title, LocalizedValue)
|
||||
|
||||
|
||||
@classmethod
|
||||
def test_model_init_kwargs(cls):
|
||||
"""Tests whether all :see:LocalizedField
|
||||
@@ -4,10 +4,14 @@ from django import forms
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.db.utils import IntegrityError
|
||||
from localized_fields import (LocalizedField, LocalizedAutoSlugField,
|
||||
LocalizedUniqueSlugField)
|
||||
from django.utils.text import slugify
|
||||
|
||||
from localized_fields.fields import (
|
||||
LocalizedField,
|
||||
LocalizedAutoSlugField,
|
||||
LocalizedUniqueSlugField
|
||||
)
|
||||
|
||||
from .fake_model import get_fake_model
|
||||
|
||||
|
||||
164
tests/test_value.py
Normal file
164
tests/test_value.py
Normal file
@@ -0,0 +1,164 @@
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.utils import translation
|
||||
|
||||
from localized_fields.value import LocalizedValue
|
||||
|
||||
from .data import get_init_values
|
||||
|
||||
|
||||
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 whether 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_init_array():
|
||||
"""Tests whether the __init__ function
|
||||
of :see:LocalizedValue properly handles an
|
||||
array.
|
||||
|
||||
Arrays can be passed to LocalizedValue as
|
||||
a result of a ArrayAgg operation."""
|
||||
|
||||
value = LocalizedValue(['my value'])
|
||||
assert value.get(settings.LANGUAGE_CODE) == 'my value'
|
||||
|
||||
@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_default_language():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's see:get function properly
|
||||
gets the value in the default language."""
|
||||
|
||||
keys = get_init_values()
|
||||
localized_value = LocalizedValue(keys)
|
||||
|
||||
for language, _ in keys.items():
|
||||
translation.activate(language)
|
||||
assert localized_value.get() == keys[settings.LANGUAGE_CODE]
|
||||
|
||||
@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_eq():
|
||||
"""Tests whether the __eq__ operator
|
||||
of :see:LocalizedValue works properly."""
|
||||
|
||||
a = LocalizedValue({'en': 'a', 'ar': 'b'})
|
||||
b = LocalizedValue({'en': 'a', 'ar': 'b'})
|
||||
|
||||
assert a == b
|
||||
|
||||
b.en = 'b'
|
||||
assert a != b
|
||||
|
||||
@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
|
||||
|
||||
@staticmethod
|
||||
def test_deconstruct():
|
||||
"""Tests whether the :see:LocalizedValue
|
||||
class's :see:deconstruct function works properly."""
|
||||
|
||||
keys = get_init_values()
|
||||
value = LocalizedValue(keys)
|
||||
|
||||
path, args, kwargs = value.deconstruct()
|
||||
|
||||
assert args[0] == keys
|
||||
|
||||
@staticmethod
|
||||
def test_construct_string():
|
||||
"""Tests whether the :see:LocalizedValue's constructor
|
||||
assumes the primary language when passing a single string."""
|
||||
|
||||
value = LocalizedValue('beer')
|
||||
assert value.get(settings.LANGUAGE_CODE) == 'beer'
|
||||
@@ -1,7 +1,8 @@
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields import LocalizedFieldWidget, LocalizedValue
|
||||
from localized_fields.value import LocalizedValue
|
||||
from localized_fields.widgets import LocalizedFieldWidget
|
||||
|
||||
|
||||
class LocalizedFieldWidgetTestCase(TestCase):
|
||||
Reference in New Issue
Block a user