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

Add support for suffix in seq (fix #93) #111

Merged
merged 11 commits into from
Oct 6, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [dev] pre-commit to use local packages (so versions will match)
- [dev] consistent use of pydocstyle
- [dev] Updates to MANIFEST.in
- Support for `prefix` in `seq` values ([PR #111](https://github.com/model-bakers/model_bakery/pull/111) fixes [Issue #93](https://github.com/model-bakers/model_bakery/issues/93))

### Removed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pip install model_bakery
class Customer(models.Model):
enjoy_jards_macale = models.BooleanField()
name = models.CharField(max_length=30)
email = models.EmailField()
age = models.IntegerField()
bio = models.TextField()
days_since_last_login = models.BigIntegerField()
Expand Down
2 changes: 2 additions & 0 deletions docs/source/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ File: **models.py** ::
"""
enjoy_jards_macale = models.BooleanField()
name = models.CharField(max_length=30)
email = models.EmailField()
age = models.IntegerField()
bio = models.TextField()
days_since_last_login = models.BigIntegerField()
Expand Down Expand Up @@ -68,6 +69,7 @@ File: **models.py** ::
"""
enjoy_jards_macale = models.BooleanField()
name = models.CharField(max_length=30)
email = models.EmailField()
age = models.IntegerField()
bio = models.TextField()
days_since_last_login = models.BigIntegerField()
Expand Down
18 changes: 18 additions & 0 deletions docs/source/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,24 @@ Sometimes, you have a field with an unique value and using ``make`` can cause ra

This will append a counter to strings to avoid uniqueness problems and it will sum the counter with numerical values.

An optional ``suffix`` parameter can be supplied to augment the value for cases like generating emails
or other strings with common suffixes.

.. code-block:: python

>>> from model_bakery import.recipe import Recipe, seq
>>> from shop.models import Customer

>>> customer = Recipe(Customer, email=seq('user', suffix='@example.com'))

>>> customer = baker.make_recipe('shop.customer')
>>> customer.email
'user1@example.com'

>>> customer = baker.make_recipe('shop.customer')
>>> customer.email
'user2@example.com'

Sequences and iterables can be used not only for recipes, but with ``baker`` as well:

.. code-block:: python
Expand Down
4 changes: 2 additions & 2 deletions model_bakery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def import_from_str(import_string):
return import_string


def seq(value, increment_by=1, start=None):
def seq(value, increment_by=1, start=None, suffix=None):
timjklein36 marked this conversation as resolved.
Show resolved Hide resolved
if type(value) in [datetime.datetime, datetime.date, datetime.time]:
if start:
msg = "start parameter is ignored when using seq with date, time or datetime objects"
Expand All @@ -46,4 +46,4 @@ def seq(value, increment_by=1, start=None):
yield series_date
else:
for n in itertools.count(start or increment_by, increment_by):
yield value + type(value)(n)
yield value + type(value)(n) + (suffix or type(value)())
timjklein36 marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,17 @@ def test_increment_for_strings(self):
person = baker.make_recipe("tests.generic.serial_person")
assert person.name == "joe3"

def test_increment_for_strings_with_suffix(self):
timjklein36 marked this conversation as resolved.
Show resolved Hide resolved
from model_bakery.recipe import seq # NoQA

fred_person = person_recipe.extend(name=seq("fred", suffix="@example.com"))
person = fred_person.make()
assert person.name == "fred1@example.com"
person = fred_person.make()
assert person.name == "fred2@example.com"
person = fred_person.make()
assert person.name == "fred3@example.com"

def test_increment_for_numbers(self):
dummy = baker.make_recipe("tests.generic.serial_numbers")
assert dummy.default_int_field == 11
Expand Down Expand Up @@ -442,6 +453,20 @@ def test_increment_for_numbers_2(self):
assert dummy.default_decimal_field == Decimal("23.1")
assert dummy.default_float_field == 4.23

def test_increment_for_numbers_with_suffix(self):
from model_bakery.recipe import seq # NoQA

dummy = baker.make_recipe(
"tests.generic.serial_numbers", default_int_field=seq(1, suffix=1)
)
assert dummy.default_int_field == 3
timjklein36 marked this conversation as resolved.
Show resolved Hide resolved

with pytest.raises(TypeError):
dummy = baker.make_recipe(
"tests.generic.serial_numbers",
default_int_field=seq(1, suffix="this should fail"),
)

def test_creates_unique_field_recipe_using_for_iterator(self):
for i in range(1, 4):
dummy = baker.make_recipe("tests.generic.dummy_unique_field")
Expand Down