mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-29 18:18:57 +03:00
Created abstract model to take care of default values
This commit is contained in:
47
tests/fake_model.py
Normal file
47
tests/fake_model.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from django.contrib.postgres.operations import HStoreExtension
|
||||
from django.db import connection, migrations
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
|
||||
from localized_fields.fields import LocalizedAutoSlugField, LocalizedField
|
||||
from localized_fields.models import LocalizedModel
|
||||
|
||||
MODEL = None
|
||||
|
||||
|
||||
def get_fake_model():
|
||||
"""Creates a fake model to use during unit tests."""
|
||||
|
||||
global MODEL
|
||||
|
||||
if MODEL:
|
||||
return MODEL
|
||||
|
||||
class TestModel(LocalizedModel):
|
||||
"""Model used for testing the :see:LocalizedAutoSlugField."""
|
||||
|
||||
app_label = 'localized_fields'
|
||||
|
||||
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)
|
||||
|
||||
MODEL = TestModel
|
||||
return MODEL
|
||||
@@ -1,12 +1,10 @@
|
||||
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)
|
||||
from localized_fields.fields import LocalizedAutoSlugField
|
||||
|
||||
from .fake_model import get_fake_model
|
||||
|
||||
|
||||
class LocalizedAutoSlugFieldTestCase(TestCase):
|
||||
@@ -20,39 +18,7 @@ class LocalizedAutoSlugFieldTestCase(TestCase):
|
||||
|
||||
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
|
||||
cls.TestModel = get_fake_model()
|
||||
|
||||
def test_populate(self):
|
||||
"""Tests whether the :see:LocalizedAutoSlugField's
|
||||
|
||||
@@ -235,33 +235,6 @@ class LocalizedFieldTestCase(TestCase):
|
||||
assert not LocalizedField().clean(None)
|
||||
assert not LocalizedField().clean(['huh'])
|
||||
|
||||
@staticmethod
|
||||
def test_default_value():
|
||||
"""Tests whether the default value is a :see:LocalizedValue
|
||||
instance."""
|
||||
|
||||
field = LocalizedField()
|
||||
|
||||
assert field.default
|
||||
assert isinstance(field.default, LocalizedValue)
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert not field.default.get(lang_code)
|
||||
|
||||
@staticmethod
|
||||
def test_default_value_override():
|
||||
"""Tests whether the default value of a field
|
||||
can correctly be overriden."""
|
||||
|
||||
default_value = LocalizedValue(get_init_values())
|
||||
field = LocalizedField(default=default_value)
|
||||
|
||||
assert field.default
|
||||
assert isinstance(field.default, LocalizedValue)
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
assert default_value.get(lang_code) == field.default.get(lang_code)
|
||||
|
||||
@staticmethod
|
||||
def test_formfield():
|
||||
"""Tests whether the :see:formfield function
|
||||
|
||||
29
tests/test_localized_model.py
Normal file
29
tests/test_localized_model.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from localized_fields.fields import LocalizedValue
|
||||
|
||||
from .fake_model import get_fake_model
|
||||
|
||||
|
||||
class LocalizedModelTestCase(TestCase):
|
||||
"""Tests whether the :see:LocalizedModel class."""
|
||||
|
||||
TestModel = None
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Creates the test model in the database."""
|
||||
|
||||
super(LocalizedModelTestCase, cls).setUpClass()
|
||||
|
||||
cls.TestModel = get_fake_model()
|
||||
|
||||
@classmethod
|
||||
def test_defaults(cls):
|
||||
"""Tests whether all :see:LocalizedField
|
||||
fields are assigned an empty :see:LocalizedValue
|
||||
instance when the model is instanitiated."""
|
||||
|
||||
obj = cls.TestModel()
|
||||
|
||||
assert isinstance(obj.title, LocalizedValue)
|
||||
Reference in New Issue
Block a user