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

Fix Recipe Import Bug from Non-Installed-App Module #201

Merged
merged 3 commits into from
Jun 13, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Fixed a bug (introduced in 1.2.1) that was breaking imports of recipes from non-installed-app modules [PR #201](https://github.com/model-bakers/model_bakery/pull/201)

### Removed

## [1.3.1](https://pypi.org/project/model-bakery/1.3.1/)
Expand Down
6 changes: 3 additions & 3 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def prepare(

def _recipe(name: str) -> Any:
app_name, recipe_name = name.rsplit(".", 1)
if "." in app_name: # probably full path, not app name
pkg = app_name
else:
try:
pkg = apps.get_app_config(app_name).module.__package__
except LookupError:
pkg = app_name
return import_from_str(".".join((pkg, "baker_recipes", recipe_name)))


Expand Down
2 changes: 1 addition & 1 deletion tests/generic/baker_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
Dog,
DummyDefaultFieldsModel,
DummyUniqueIntegerFieldModel,
Person,
LonelyPerson,
Person,
School,
)

Expand Down
27 changes: 27 additions & 0 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ def test_raise_model_not_found(self):
baker.Baker("NonExistingModel")


class TestRecipeFinder:
def test_from_app_module(self):
obj = baker.prepare_recipe("generic.person")
assert isinstance(obj, models.Person)
assert obj.name == "John Doe"

def test_full_path_from_app_module(self):
obj = baker.prepare_recipe("tests.generic.person")
assert isinstance(obj, models.Person)
assert obj.name == "John Doe"

def test_from_non_app_module(self):
obj = baker.prepare_recipe("uninstalled.person")
assert isinstance(obj, models.Person)
assert obj.name == "Uninstalled"

def test_full_path_from_non_app_module(self):
obj = baker.prepare_recipe("tests.uninstalled.person")
assert isinstance(obj, models.Person)
assert obj.name == "Uninstalled"

def test_raise_on_non_module_path(self):
# Error trying to parse "app_name" + "recipe_name" from provided string
with pytest.raises(ValueError):
baker.prepare_recipe("person")


@pytest.mark.django_db
class TestsBakerCreatesSimpleModel:
def test_consider_real_django_fields_only(self):
Expand Down
17 changes: 17 additions & 0 deletions tests/uninstalled/baker_recipes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.utils.timezone import now

from model_bakery.recipe import Recipe
from tests.generic.models import Person

person = Recipe(
Person,
name="Uninstalled",
nickname="uninstalled",
age=18,
bio="Uninstalled",
blog="http://uninstalled.com",
days_since_last_login=4,
birthday=now().date(),
appointment=now(),
birth_time=now(),
)