Skip to content

Commit

Permalink
Merge pull request #130 from paulocheque/teach-flex
Browse files Browse the repository at this point in the history
Accept override an Lesson
  • Loading branch information
paulocheque committed Mar 29, 2020
2 parents c1dfd9b + c820485 commit bc354a0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
14 changes: 6 additions & 8 deletions django_dynamic_fixture/ddf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ class PendingField(Exception):
"Internal exception to control pending fields when using Copier."


class CantOverrideLesson(Exception):
"Override a lesson is an anti-pattern and will turn your test suite very hard to understand."


def _validate_model(model_class):
if not is_model_class(model_class):
raise InvalidReceiverError(model_class, 'Invalid model')
Expand Down Expand Up @@ -249,10 +245,15 @@ def get_instance(cls):
return cls.instance

def add_configuration(self, model_class, kwargs, name=None):
import os
import warnings
if name in [None, True]:
name = self.DEFAULT_KEY
if model_class in self.configs and name in self.configs[model_class]:
raise CantOverrideLesson('A lesson {} has already been saved for the model {}'.format(name, model_class))
if not os.getenv('DDF_SHELL_MODE'):
msg = "Override a lesson is an anti-pattern and will turn your test suite very hard to understand."
msg = 'A lesson {} has already been saved for the model {}. {}'.format(name, model_class, msg)
warnings.warn(msg, RuntimeWarning)
model_class_config = self.configs.setdefault(model_class, {})
model_class_config[name] = kwargs

Expand Down Expand Up @@ -676,9 +677,6 @@ def get(self, model_class, ddf_lesson=None, **kwargs):
return instance

def teach(self, model_class, ddf_lesson=None, **kwargs):
'''
@raise an CantOverrideLesson error if the same model/lesson were called twice.
'''
library = DDFLibrary.get_instance()
for field_name in kwargs.keys():
if field_name in self._DDF_CONFIGS:
Expand Down
9 changes: 6 additions & 3 deletions django_dynamic_fixture/tests/test_ddf_teaching_and_lessons.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ def test_teach_a_default_lesson_for_a_model(self):
instance = self.ddf.get(ModelForLibrary)
assert instance.integer == 1000

def test_default_lesson_must_NOT_be_overrided(self):
def test_default_lesson_may_be_overrided_although_it_is_an_anti_pattern(self):
self.ddf.teach(ModelForLibrary, integer=1000)
with pytest.raises(CantOverrideLesson):
self.ddf.teach(ModelForLibrary, integer=1001)
instance = self.ddf.get(ModelForLibrary)
assert instance.integer == 1000
self.ddf.teach(ModelForLibrary, integer=1001)
instance = self.ddf.get(ModelForLibrary)
assert instance.integer == 1001

def test_it_must_NOT_raise_an_error_if_user_try_to_use_a_not_saved_default_configuration(self):
self.ddf.get(ModelForLibrary)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/ddf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,4 @@ You can have **many custom lessons** too, giving names to them::
instance = G(Model, ddf_lesson='my custom lesson 2')
assert instance.field_x == 99

ps: Just be aware that lessons can not be overriden, since this is an anti-pattern and may let your test suite very hard to understand.
ps: Just be aware that overriding lessons is an anti-pattern and may let your test suite very hard to understand.
1 change: 0 additions & 1 deletion docs/source/more.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ List of Exceptions
* *InvalidCopierExpressionError*: The specified expression used in a Copier is invalid.
* *InvalidModelError*: Invalid Model: The class is not a model or it is abstract.
* *InvalidDDFSetupError*: ddf_setup.py has execution errors
* *CantOverrideLesson*: Override a lesson is an anti-pattern and will turn your test suite very hard to understand.


Decorators (New in 1.4.0)
Expand Down
3 changes: 3 additions & 0 deletions docs/source/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ You can configure DDF in ``settings.py`` file. You can also override the global
# You can override the global config for one case:
G(Model, debug_mode=True)
G(Model, debug_mode=False)


* **DDF_SHELL_MODE** (Default = False): To disable some DDF warnings so DDF can be used better in Python shell: to populate the DB, for exampel.

0 comments on commit bc354a0

Please sign in to comment.