Fake models now have generated names

This commit is contained in:
Swen Kooij 2017-06-26 12:44:09 +03:00
parent 51fc6959d2
commit 96ddc77cfc
6 changed files with 12 additions and 23 deletions

View File

@ -1,3 +1,5 @@
import uuid
from django.db import connection, migrations from django.db import connection, migrations
from django.db.migrations.executor import MigrationExecutor from django.db.migrations.executor import MigrationExecutor
from django.contrib.postgres.operations import HStoreExtension from django.contrib.postgres.operations import HStoreExtension
@ -5,24 +7,27 @@ from django.contrib.postgres.operations import HStoreExtension
from localized_fields.models import LocalizedModel from localized_fields.models import LocalizedModel
def define_fake_model(name='TestModel', fields=None): def define_fake_model(fields=None, model_base=LocalizedModel, meta_options={}):
name = str(uuid.uuid4()).replace('-', '')[:8]
attributes = { attributes = {
'app_label': 'localized_fields', 'app_label': 'tests',
'__module__': __name__, '__module__': __name__,
'__name__': name '__name__': name,
'Meta': type('Meta', (object,), meta_options)
} }
if fields: if fields:
attributes.update(fields) attributes.update(fields)
model = type(name, (model_base,), attributes)
model = type(name, (LocalizedModel,), attributes)
return model return model
def get_fake_model(name='TestModel', fields=None): def get_fake_model(fields=None, model_base=LocalizedModel, meta_options={}):
"""Creates a fake model to use during unit tests.""" """Creates a fake model to use during unit tests."""
model = define_fake_model(name, fields) model = define_fake_model(fields, model_base, meta_options)
class TestProject: class TestProject:
@ -39,7 +44,7 @@ def get_fake_model(name='TestModel', fields=None):
with connection.schema_editor() as schema_editor: with connection.schema_editor() as schema_editor:
migration_executor = MigrationExecutor(schema_editor.connection) migration_executor = MigrationExecutor(schema_editor.connection)
migration_executor.apply_migration( migration_executor.apply_migration(
TestProject(), TestMigration('eh', 'localized_fields')) TestProject(), TestMigration('eh', 'postgres_extra'))
schema_editor.create_model(model) schema_editor.create_model(model)

View File

@ -16,7 +16,6 @@ class LocalizedBulkTestCase(TestCase):
a :see:LocalizedUniqueSlugField in the model.""" a :see:LocalizedUniqueSlugField in the model."""
model = get_fake_model( model = get_fake_model(
'BulkSlugInsertModel',
{ {
'name': LocalizedField(), 'name': LocalizedField(),
'slug': LocalizedUniqueSlugField(populate_from='name', include_time=True), 'slug': LocalizedUniqueSlugField(populate_from='name', include_time=True),

View File

@ -24,14 +24,12 @@ class LocalizedExpressionsTestCase(TestCase):
super(LocalizedExpressionsTestCase, cls).setUpClass() super(LocalizedExpressionsTestCase, cls).setUpClass()
cls.TestModel1 = get_fake_model( cls.TestModel1 = get_fake_model(
'LocalizedExpressionsTestCase2',
{ {
'name': models.CharField(null=False, blank=False, max_length=255), 'name': models.CharField(null=False, blank=False, max_length=255),
} }
) )
cls.TestModel2 = get_fake_model( cls.TestModel2 = get_fake_model(
'LocalizedExpressionsTestCase1',
{ {
'text': LocalizedField(), 'text': LocalizedField(),
'other': models.ForeignKey(cls.TestModel1, related_name='features') 'other': models.ForeignKey(cls.TestModel1, related_name='features')

View File

@ -30,7 +30,6 @@ class LocalizedFileFieldTestCase(TestCase):
super().setUpClass() super().setUpClass()
cls.FileFieldModel = get_fake_model( cls.FileFieldModel = get_fake_model(
'LocalizedFileFieldTestModel',
{ {
'file': LocalizedFileField(), 'file': LocalizedFileField(),
} }

View File

@ -18,7 +18,6 @@ class LocalizedModelTestCase(TestCase):
super(LocalizedModelTestCase, cls).setUpClass() super(LocalizedModelTestCase, cls).setUpClass()
cls.TestModel = get_fake_model( cls.TestModel = get_fake_model(
'LocalizedModelTestCase',
{ {
'title': LocalizedField() 'title': LocalizedField()
} }

View File

@ -29,7 +29,6 @@ class LocalizedSlugFieldTestCase(TestCase):
super(LocalizedSlugFieldTestCase, cls).setUpClass() super(LocalizedSlugFieldTestCase, cls).setUpClass()
cls.AutoSlugModel = get_fake_model( cls.AutoSlugModel = get_fake_model(
'LocalizedAutoSlugFieldTestModel',
{ {
'title': LocalizedField(), 'title': LocalizedField(),
'name': models.CharField(max_length=255), 'name': models.CharField(max_length=255),
@ -38,7 +37,6 @@ class LocalizedSlugFieldTestCase(TestCase):
) )
cls.MagicSlugModel = get_fake_model( cls.MagicSlugModel = get_fake_model(
'LocalizedUniqueSlugFieldTestModel',
{ {
'title': LocalizedField(), 'title': LocalizedField(),
'name': models.CharField(max_length=255), 'name': models.CharField(max_length=255),
@ -78,7 +76,6 @@ class LocalizedSlugFieldTestCase(TestCase):
title = 'myuniquetitle' title = 'myuniquetitle'
PkModel = get_fake_model( PkModel = get_fake_model(
'PkModel',
{ {
'title': LocalizedField(), 'title': LocalizedField(),
'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True) 'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True)
@ -96,7 +93,6 @@ class LocalizedSlugFieldTestCase(TestCase):
"""Tests whether slugs are not re-generated if not needed.""" """Tests whether slugs are not re-generated if not needed."""
NoChangeSlugModel = get_fake_model( NoChangeSlugModel = get_fake_model(
'NoChangeSlugModel',
{ {
'title': LocalizedField(), 'title': LocalizedField(),
'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True) 'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True)
@ -190,7 +186,6 @@ class LocalizedSlugFieldTestCase(TestCase):
fields works correctly.""" fields works correctly."""
model = get_fake_model( model = get_fake_model(
'_test_populate_multiple_from_fields_' + str(field_type),
{ {
'title': LocalizedField(), 'title': LocalizedField(),
'name': models.CharField(max_length=255), 'name': models.CharField(max_length=255),
@ -214,14 +209,12 @@ class LocalizedSlugFieldTestCase(TestCase):
fields works correctly.""" fields works correctly."""
model_fk = get_fake_model( model_fk = get_fake_model(
'_test_populate_multiple_from_fields_fk_other_' + str(field_type),
{ {
'name': LocalizedField(), 'name': LocalizedField(),
} }
) )
model = get_fake_model( model = get_fake_model(
'_test_populate_multiple_from_fields_fk_' + str(field_type),
{ {
'title': LocalizedField(), 'title': LocalizedField(),
'other': models.ForeignKey(model_fk), 'other': models.ForeignKey(model_fk),
@ -230,10 +223,6 @@ class LocalizedSlugFieldTestCase(TestCase):
) )
other = model_fk.objects.create(name={settings.LANGUAGE_CODE: 'swen'}) other = model_fk.objects.create(name={settings.LANGUAGE_CODE: 'swen'})
# for lang_code, lang_name in settings.LANGUAGES:
# other.name.set(lang_code, 'swen')
# other.save()
obj = model() obj = model()
for lang_code, lang_name in settings.LANGUAGES: for lang_code, lang_name in settings.LANGUAGES: