Added tests for custom db-backend

This commit is contained in:
Swen Kooij
2017-02-02 14:36:22 +02:00
parent 5ba9be4aeb
commit 1ef02616ec
5 changed files with 162 additions and 42 deletions

View File

@@ -1,38 +1,46 @@
from django.db import connection, migrations
from localized_fields import LocalizedModel
from django.db.migrations.executor import MigrationExecutor
from django.contrib.postgres.operations import HStoreExtension
from localized_fields import LocalizedModel
def get_fake_model(name='TestModel', fields={}):
"""Creates a fake model to use during unit tests."""
def define_fake_model(name='TestModel', fields=None):
attributes = {
'app_label': 'localized_fields',
'__module__': __name__,
'__name__': name
}
attributes.update(fields)
TestModel = type(name, (LocalizedModel,), attributes)
if fields:
attributes.update(fields)
model = type(name, (LocalizedModel,), attributes)
return model
def get_fake_model(name='TestModel', fields=None):
"""Creates a fake model to use during unit tests."""
model = define_fake_model(name, fields)
class TestProject:
def clone(self, *args, **kwargs):
def clone(self, *_args, **_kwargs):
return self
@property
def apps(self):
return self
class TestMigration(migrations.Migration):
operations = [
HStoreExtension()
]
operations = [HStoreExtension()]
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', 'localized_fields'))
schema_editor.create_model(TestModel)
schema_editor.create_model(model)
return TestModel
return model

87
tests/test_db_backend.py Normal file
View File

@@ -0,0 +1,87 @@
from unittest import mock
from django.db import connection
from django.conf import settings
from django.test import TestCase
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from localized_fields import LocalizedField, get_language_codes
from .fake_model import define_fake_model
from django.apps import apps
class DBBackendTestCase(TestCase):
"""Tests the custom database back-end."""
@staticmethod
def test_hstore_extension_enabled():
"""Tests whether the `hstore` extension was
enabled automatically."""
with connection.cursor() as cursor:
cursor.execute((
'SELECT count(*) FROM pg_extension '
'WHERE extname = \'hstore\''
))
assert cursor.fetchone()[0] == 1
@classmethod
def test_migration_create_drop_model(cls):
"""Tests whether models containing a :see:LocalizedField
with a `uniqueness` constraint get created properly,
with the contraints in the database."""
model = define_fake_model('NewModel', {
'title': LocalizedField(uniqueness=get_language_codes())
})
# create the model in db and verify the indexes are being created
with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
with connection.schema_editor() as schema_editor:
schema_editor.create_model(model)
create_index_calls = [
call for call in execute.mock_calls if 'CREATE UNIQUE INDEX' in str(call)
]
assert len(create_index_calls) == len(settings.LANGUAGES)
# delete the model in the db and verify the indexes are being deleted
with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(model)
drop_index_calls = [
call for call in execute.mock_calls if 'DROP INDEX' in str(call)
]
assert len(drop_index_calls) == len(settings.LANGUAGES)
@classmethod
def test_migration_alter_field(cls):
"""Tests whether the back-end correctly removes and
adds `uniqueness` constraints when altering a :see:LocalizedField."""
define_fake_model('ExistingModel', {
'title': LocalizedField(uniqueness=get_language_codes())
})
app_config = apps.get_app_config('tests')
with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
with connection.schema_editor() as schema_editor:
dynmodel = app_config.get_model('ExistingModel')
schema_editor.alter_field(
dynmodel,
dynmodel._meta.fields[1],
dynmodel._meta.fields[1]
)
index_calls = [
call for call in execute.mock_calls
if 'INDEX' in str(call) and 'title' in str(call)
]
assert len(index_calls) == len(settings.LANGUAGES) * 2