Skip to content

Commit

Permalink
Merge pull request #109 from paulocheque/ddf_check
Browse files Browse the repository at this point in the history
New ddf_check_models method
  • Loading branch information
paulocheque committed Jan 4, 2020
2 parents a0539a6 + f594db5 commit 837731e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ compile:

test:
# Run specific test:
# ARGS=pytest django_dynamic_fixture.tests.FILE::CLASS::METHOD make test
clear ; env/bin/pytest --create-db --reuse-db --no-migrations ${ARGS}
# TESTS=pytest django_dynamic_fixture.tests.FILE::CLASS::METHOD make test
clear ; env/bin/pytest --create-db --reuse-db --no-migrations ${TESTS}
# clear ; time env/bin/tox --parallel all -e django111-py27
# clear ; time env/bin/tox --parallel all -e django20-py37

Expand All @@ -57,13 +57,16 @@ config_postgres:
psql -c "ALTER USER ddf_user WITH SUPERUSER;" -U postgres

test_postgres:
clear ; env/bin/pytest --reuse-db --no-migrations --ds=settings_postgres
# TESTS=pytest django_dynamic_fixture.tests.FILE::CLASS::METHOD make test_postgres
clear ; env/bin/pytest --reuse-db --no-migrations --ds=settings_postgres ${TESTS}

test_mysql:
clear ; env/bin/pytest --reuse-db --no-migrations --ds=settings_mysql
# TESTS=pytest django_dynamic_fixture.tests.FILE::CLASS::METHOD make test_mysql
clear ; env/bin/pytest --reuse-db --no-migrations --ds=settings_mysql ${TESTS}

cov:
clear ; env/bin/pytest --create-db --reuse-db --no-migrations --cov=django_dynamic_fixture
# TESTS=pytest django_dynamic_fixture.tests.FILE::CLASS::METHOD make cov
clear ; env/bin/pytest --create-db --reuse-db --no-migrations --cov=django_dynamic_fixture ${TESTS}

coveralls:
clear ; env/bin/coveralls
Expand Down
1 change: 1 addition & 0 deletions django_dynamic_fixture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django_dynamic_fixture.global_settings import DDF_DEFAULT_DATA_FIXTURE, DDF_FILL_NULLABLE_FIELDS, DDF_NUMBER_OF_LAPS, \
DDF_IGNORE_FIELDS, DDF_VALIDATE_MODELS, \
DDF_DEBUG_MODE, DDF_FIELD_FIXTURES
from django_dynamic_fixture.script_ddf_checkings import ddf_check_models


if not django_greater_than('1.10'):
Expand Down
4 changes: 3 additions & 1 deletion django_dynamic_fixture/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ class CustomDjangoFieldMultipleInheritance(CustomDjangoFieldMixin, models.Intege


class NewField(models.Field):
pass
# Avoid OperationalError("table has no column named ...") errors
def db_type(self, connection):
return 'char(25)'


class ModelWithCustomFields(models.Model):
Expand Down
46 changes: 46 additions & 0 deletions django_dynamic_fixture/script_ddf_checkings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.db import transaction

from django_dynamic_fixture.django_helper import get_apps, get_models_of_an_app


def color(color, string):
return '\033[1;{}m{}\033[0m'.format(color, string)

def white(string):
return color('37', string)

def red(string):
return color('91', string)

def green(string):
return color('92', string)


def ddf_check_models(application_labels=[], exclude_application_labels=[]):
from django_dynamic_fixture import get

succeeded = {}
errors = {}
for app_label in get_apps(application_labels, exclude_application_labels):
models = get_models_of_an_app(app_label)
for model_class in models:
ref = '{}.{}'.format(app_label, model_class.__name__)
try:
with transaction.atomic():
get(model_class)
succeeded[ref] = None
except Exception as e:
errors[ref] = '[{}] {}'.format(type(e), str(e))

# Print report
print(green('\nModels that DDF can create using the default settings.\n'))
for i, (ref, _) in enumerate(succeeded.items()):
i = str(i).zfill(3)
print(white('{}. {}: '.format(i, ref)) + green('succeeded'))

print(red('\nModels that requires some customisation.\n'))
for i, (ref, error) in enumerate(errors.items(), start=1):
i = str(i).zfill(3)
print(white('{}. {}: '.format(i, ref)) + red(error))

return succeeded, errors
42 changes: 42 additions & 0 deletions django_dynamic_fixture/tests/test_ddf_checkings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-

from django.test import TestCase

from django_dynamic_fixture.ddf import DDFLibrary
from django_dynamic_fixture import ddf_check_models, teach


class DDFTestCase(TestCase):
def setUp(self):
DDFLibrary.get_instance().clear()


class TestCheckCompatibility(DDFTestCase):
def test_default(self):
succeeded, errors = ddf_check_models()

compatible_models = [
'django_dynamic_fixture.EmptyModel',
'django_dynamic_fixture.ModelWithNumbers',
]
for model in compatible_models:
assert model in succeeded.keys(), model

incompatible_models = [
'django_dynamic_fixture.ModelWithUnsupportedField',
]
for model in incompatible_models:
assert model in errors.keys(), model
# TODO: Consider the RelatedObjectDoesNotExist errors
# https://stackoverflow.com/questions/26270042/how-do-you-catch-this-exception

def test_teaching_ddf(self):
teach('django_dynamic_fixture.ModelWithUnsupportedField', z='z')
succeeded, errors = ddf_check_models()

compatible_models = [
'django_dynamic_fixture.ModelWithUnsupportedField',
]
for model in compatible_models:
assert model in succeeded.keys(), model
assert model not in errors.keys(), model
4 changes: 3 additions & 1 deletion django_dynamic_fixture/tests/test_ddf_custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_unsupported_field_is_filled_with_null_if_it_is_possible(self):
assert instance.y is None

def test_unsupported_field_raise_an_error_if_it_does_not_accept_null_value(self):
DDFLibrary.get_instance().clear()
with pytest.raises(UnsupportedFieldError):
self.ddf.new(ModelWithUnsupportedField)

Expand All @@ -45,8 +46,9 @@ def test_new_field_that_double_inherits_django_field_must_be_supported_with_cust

class NewFullFillAttributesUsingPluginsTest(DDFTestCase):
def test_custom_field_not_registered_must_raise_an_unsupported_field_exception(self):
DDFLibrary.get_instance().clear()
with pytest.raises(UnsupportedFieldError):
self.ddf.new(ModelWithUnsupportedField)
self.ddf.get(ModelWithUnsupportedField)

def test_new_fill_field_with_data_generated_by_plugins_with_dict(self):
data_fixture.plugins = settings.DDF_FIELD_FIXTURES
Expand Down

0 comments on commit 837731e

Please sign in to comment.