-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from paulocheque/ddf_check
New ddf_check_models method
- Loading branch information
Showing
6 changed files
with
103 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters