Skip to content

Commit

Permalink
Merge pull request #104 from paulocheque/model_string
Browse files Browse the repository at this point in the history
[feature] G, N, T now supports <app_label>.<model_name> strings inste…
  • Loading branch information
paulocheque committed Jan 4, 2020
2 parents dc8a369 + 0f418f3 commit 8e8aa06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions django_dynamic_fixture/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import warnings
import six

from django.apps import apps

from django_dynamic_fixture.ddf import DynamicFixture, Copier, DDFLibrary, \
set_pre_save_receiver, set_post_save_receiver
from django_dynamic_fixture.django_helper import print_field_values, django_greater_than
Expand Down Expand Up @@ -64,7 +66,7 @@ def _new(model, n=1, lesson=None, persist_dependencies=True, **kwargs):
Return one or many valid instances of Django Models with fields filled with auto generated or customized data.
All instances will NOT be persisted in the database, except its dependencies, in case @persist_dependencies is True.
@model: The class of the Django model.
@model: The class of the Django model. It can be a string `<app_label>.<model_name>`
@n: number of instances to be created with the given configuration. Default is 1.
@lesson: use a custom lesson to build the model object.
@persist_dependencies: If True, save internal dependencies, otherwise just instantiate them. Default is True.
Expand All @@ -79,6 +81,8 @@ def _new(model, n=1, lesson=None, persist_dependencies=True, **kwargs):
Wrapper for the method DynamicFixture.new
"""
if isinstance(model, str):
model = apps.get_model(model)
kwargs = look_up_alias(**kwargs)
d = fixture(**kwargs)
if n == 1:
Expand All @@ -94,7 +98,7 @@ def _get(model, n=1, lesson=None, **kwargs):
Return one or many valid instances of Django Models with fields filled with auto generated or customized data.
All instances will be persisted in the database.
@model: The class of the Django model.
@model: The class of the Django model. It can be a string `<app_label>.<model_name>`
@n: number of instances to be created with the given configuration. Default is 1.
@lesson: use a custom lesson to build the model object.
Expand All @@ -108,6 +112,8 @@ def _get(model, n=1, lesson=None, **kwargs):
Wrapper for the method DynamicFixture.get
"""
if isinstance(model, str):
model = apps.get_model(model)
kwargs = look_up_alias(**kwargs)
d = fixture(**kwargs)
if n == 1:
Expand All @@ -120,7 +126,7 @@ def _get(model, n=1, lesson=None, **kwargs):

def _teach(model, lesson=None, **kwargs):
'''
@model: The class of the Django model.
@model: The class of the Django model. It can be a string `<app_label>.<model_name>`
@lesson: Name of custom lesson to be created.
@raise an CantOverrideLesson error if the same model/lesson were called twice.
Expand All @@ -136,6 +142,8 @@ def _teach(model, lesson=None, **kwargs):
`Shelve` becomes `Teach`
`Library` becomes `Lessons`
'''
if isinstance(model, str):
model = apps.get_model(model)
kwargs = look_up_alias(**kwargs)
d = fixture(**kwargs)
return d.teach(model, lesson=lesson, **kwargs)
Expand Down
14 changes: 14 additions & 0 deletions django_dynamic_fixture/tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,17 @@ def test_pre_save(self):
def test_post_save(self):
POST_SAVE(EmptyModel, lambda x: x)


class UsingModelNameInsteadTest(TestCase):
def test_compatibility_for_new(self):
instance = N('django_dynamic_fixture.ModelWithNumbers', integer=5)
assert instance.integer == 5

def test_compatibility_for_get(self):
instance = G('django_dynamic_fixture.ModelWithNumbers', integer=5)
assert instance.integer == 5

def test_compatibility_for_teach(self):
teach('django_dynamic_fixture.ModelWithDefaultValues', integer_with_default=5)
instance = G('django_dynamic_fixture.ModelWithDefaultValues')
assert instance.integer_with_default == 5

0 comments on commit 8e8aa06

Please sign in to comment.