Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse lessons for base models when creating proxy models #161

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions django_dynamic_fixture/ddf.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def add_configuration(self, model_class, kwargs, name=None):
import warnings
if name in [None, True]:
name = self.DEFAULT_KEY
model_class = self._get_concrete_model(model_class)
if model_class in self.configs and name in self.configs[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."
Expand All @@ -250,12 +251,19 @@ def add_configuration(self, model_class, kwargs, name=None):
def get_configuration(self, model_class, name=None):
if name is None:
name = self.DEFAULT_KEY
model_class = self._get_concrete_model(model_class)
# copy is important because this dict will be updated every time in the algorithm.
config = self.configs.get(model_class, {})
if name != self.DEFAULT_KEY and name not in config.keys():
raise InvalidConfigurationError('There is no lesson for model {} with the name "{}"'.format(get_unique_model_name(model_class), name))
return config.get(name, {}).copy() # default configuration never raises an error

def _get_concrete_model(self, model_class):
if hasattr(model_class, '_meta') and model_class._meta.proxy:
return model_class._meta.concrete_model or model_class
else:
return model_class

def clear(self):
'''Remove all lessons of the library. Util for the DDF tests.'''
self.configs = {}
Expand Down
7 changes: 7 additions & 0 deletions django_dynamic_fixture/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ class Meta:
app_label = 'django_dynamic_fixture'


class ProxyModelForLibrary(ModelForLibrary):
class Meta:
proxy = True
verbose_name = 'Proxy Library'
app_label = 'django_dynamic_fixture'


class ModelWithUniqueCharField(models.Model):
text_unique = models.CharField(max_length=20, unique=True)

Expand Down
18 changes: 18 additions & 0 deletions django_dynamic_fixture/tests/test_ddf_teaching_and_lessons.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ def test_it_must_use_lessons_for_internal_dependencies(self):
assert instance.integer == 1000
assert instance.foreignkey.integer == 1001

def test_it_uses_lessons_for_base_model_when_creating_a_proxy_model(self):
self.ddf.teach(ModelForLibrary, integer=123)
instance = self.ddf.get(ProxyModelForLibrary)
assert instance.__class__ is ProxyModelForLibrary
assert instance.integer == 123

def test_it_uses_lessons_for_proxy_models_when_creating_the_base_model(self):
self.ddf.teach(ProxyModelForLibrary, integer=456)
instance = self.ddf.get(ModelForLibrary)
assert instance.__class__ is ModelForLibrary
assert instance.integer == 456

def test_it_uses_lessons_for_proxy_models_when_creating_the_proxy_model(self):
self.ddf.teach(ProxyModelForLibrary, integer=789)
instance = self.ddf.get(ProxyModelForLibrary)
assert instance.__class__ is ProxyModelForLibrary
assert instance.integer == 789

# Not implemented yet
# def test_teaching_must_store_ddf_configs_too(self):
# self.ddf.teach(ModelForLibrary, fill_nullable_fields=False)
Expand Down