From 96ddc77cfcfdfe7730dfbd5725b4bbf20b4a638a Mon Sep 17 00:00:00 2001 From: Swen Kooij Date: Mon, 26 Jun 2017 12:44:09 +0300 Subject: [PATCH] Fake models now have generated names --- tests/fake_model.py | 19 ++++++++++++------- tests/test_bulk.py | 1 - tests/test_expressions.py | 2 -- tests/test_file_field.py | 1 - tests/test_model.py | 1 - tests/test_slug_fields.py | 11 ----------- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/tests/fake_model.py b/tests/fake_model.py index dc8cf8e..46526af 100644 --- a/tests/fake_model.py +++ b/tests/fake_model.py @@ -1,3 +1,5 @@ +import uuid + from django.db import connection, migrations from django.db.migrations.executor import MigrationExecutor from django.contrib.postgres.operations import HStoreExtension @@ -5,24 +7,27 @@ from django.contrib.postgres.operations import HStoreExtension 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 = { - 'app_label': 'localized_fields', + 'app_label': 'tests', '__module__': __name__, - '__name__': name + '__name__': name, + 'Meta': type('Meta', (object,), meta_options) } if fields: attributes.update(fields) + model = type(name, (model_base,), attributes) - model = type(name, (LocalizedModel,), attributes) 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.""" - model = define_fake_model(name, fields) + model = define_fake_model(fields, model_base, meta_options) class TestProject: @@ -39,7 +44,7 @@ def get_fake_model(name='TestModel', fields=None): with connection.schema_editor() as schema_editor: migration_executor = MigrationExecutor(schema_editor.connection) migration_executor.apply_migration( - TestProject(), TestMigration('eh', 'localized_fields')) + TestProject(), TestMigration('eh', 'postgres_extra')) schema_editor.create_model(model) diff --git a/tests/test_bulk.py b/tests/test_bulk.py index e62f732..0d5cafc 100644 --- a/tests/test_bulk.py +++ b/tests/test_bulk.py @@ -16,7 +16,6 @@ class LocalizedBulkTestCase(TestCase): a :see:LocalizedUniqueSlugField in the model.""" model = get_fake_model( - 'BulkSlugInsertModel', { 'name': LocalizedField(), 'slug': LocalizedUniqueSlugField(populate_from='name', include_time=True), diff --git a/tests/test_expressions.py b/tests/test_expressions.py index 91a2c98..8abc57a 100644 --- a/tests/test_expressions.py +++ b/tests/test_expressions.py @@ -24,14 +24,12 @@ class LocalizedExpressionsTestCase(TestCase): 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') diff --git a/tests/test_file_field.py b/tests/test_file_field.py index 7bab6d7..00a8975 100644 --- a/tests/test_file_field.py +++ b/tests/test_file_field.py @@ -30,7 +30,6 @@ class LocalizedFileFieldTestCase(TestCase): super().setUpClass() cls.FileFieldModel = get_fake_model( - 'LocalizedFileFieldTestModel', { 'file': LocalizedFileField(), } diff --git a/tests/test_model.py b/tests/test_model.py index 974b975..54f7ef7 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -18,7 +18,6 @@ class LocalizedModelTestCase(TestCase): super(LocalizedModelTestCase, cls).setUpClass() cls.TestModel = get_fake_model( - 'LocalizedModelTestCase', { 'title': LocalizedField() } diff --git a/tests/test_slug_fields.py b/tests/test_slug_fields.py index 2c563e2..e543cb4 100644 --- a/tests/test_slug_fields.py +++ b/tests/test_slug_fields.py @@ -29,7 +29,6 @@ class LocalizedSlugFieldTestCase(TestCase): super(LocalizedSlugFieldTestCase, cls).setUpClass() cls.AutoSlugModel = get_fake_model( - 'LocalizedAutoSlugFieldTestModel', { 'title': LocalizedField(), 'name': models.CharField(max_length=255), @@ -38,7 +37,6 @@ class LocalizedSlugFieldTestCase(TestCase): ) cls.MagicSlugModel = get_fake_model( - 'LocalizedUniqueSlugFieldTestModel', { 'title': LocalizedField(), 'name': models.CharField(max_length=255), @@ -78,7 +76,6 @@ class LocalizedSlugFieldTestCase(TestCase): title = 'myuniquetitle' PkModel = get_fake_model( - 'PkModel', { 'title': LocalizedField(), '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.""" NoChangeSlugModel = get_fake_model( - 'NoChangeSlugModel', { 'title': LocalizedField(), 'slug': LocalizedUniqueSlugField(populate_from='title', include_time=True) @@ -190,7 +186,6 @@ class LocalizedSlugFieldTestCase(TestCase): fields works correctly.""" model = get_fake_model( - '_test_populate_multiple_from_fields_' + str(field_type), { 'title': LocalizedField(), 'name': models.CharField(max_length=255), @@ -214,14 +209,12 @@ class LocalizedSlugFieldTestCase(TestCase): fields works correctly.""" model_fk = get_fake_model( - '_test_populate_multiple_from_fields_fk_other_' + str(field_type), { 'name': LocalizedField(), } ) model = get_fake_model( - '_test_populate_multiple_from_fields_fk_' + str(field_type), { 'title': LocalizedField(), 'other': models.ForeignKey(model_fk), @@ -230,10 +223,6 @@ class LocalizedSlugFieldTestCase(TestCase): ) 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() for lang_code, lang_name in settings.LANGUAGES: