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

Conversation

timjklein36
Copy link
Collaborator

Add support for suffix in seq (fix #93)

What

This change introduces the ability to append a common suffix on to values generated by seq.

Fixes #93

Why

Some fields may represent data that generally contains a common suffix, like in the case of emails. Mock data is therefore differentiated in the first portion of the field's value. An example of this:

fake.user1@example.com
fake.user2@example.com
...

By allowing an optionally supplied suffix, the seq function is able to append some common string onto its generated values.

How

A suffix keyword argument was added to the seq function to allow the concatenation of a common suffix string onto a generated value. This will not work with types of value other than str. Tests have been added to cover the "happy" and some "sad" path use of the new keyword argument.

Example:

from django.db import models
from model_bakery.recipe import Recipe, seq


class Person(models.Model):
    email = models.CharField(max_length=50)


person_recipe = Recipe(Person, email=seq('fake.user', suffix='example.com')

person_recipe.make().email  # 'fake.user1@example.com'
person_recipe.make().email  # 'fake.user2@example.com'
person_recipe.make().email  # 'fake.user3@example.com'

This PR also adds a section under "Sequences in recipes" to the documentation to provide example usage of the new keyword argument. Additionally, an email field is added to the Customer example model in the README.md to go along with the new section in the documentation.

Note: the line altered in seq (shown below) takes into account that if no suffix argument is supplied, it will essentially append an un-initialized type of the same as the value input. For number types, this is equivalent to 0.

yield value + type(value)(n) + (suffix or type(value)())

- This adds a new `suffix` keyword arg to the `seq` function to allow
  the concatenation of a common suffix string onto a generated `seq`
  value.
- This adds a section under "Sequences in recipes" to the documentation
  to provide example usage of the new keyword argument.
  - Additionally, an `email` field is added to the `Customer` example
    model in the README.md to go along with the new section in the
    documentation.
- This also adds unit tests for the new keyword argument behavior.
- This also adds that example field to the other `Customer`
  class examples throughout the docs.
Copy link
Member

@berinhard berinhard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for such a clean, testable and documented PR @timjklein36! I left 2 suggestion on how we can improve the error handling for this new parameter. Besides that, I'm all good with the code and the PR.

I'll wait for @anapaulagomes or @amureki to review it and as soon as we have an extra approval we can merge this.

model_bakery/utils.py Outdated Show resolved Hide resolved
tests/test_recipes.py Outdated Show resolved Hide resolved
… text sequence

- Also, change the str test case to use Person.email field (to better match
  the example mock data).
@timjklein36
Copy link
Collaborator Author

@berinhard I updated the code based on your suggestions. Take a look at your earliest convenience. Thanks!

- Removes unused variable in test method
Copy link
Member

@berinhard berinhard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks =)

Copy link
Contributor

@anapaulagomes anapaulagomes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution, @timjklein36! This is a really nice feature for seq. 🌷

I left small comments but I'm not sure about the naming. I think end would fit better than suffix, only because we already have start as a parameter of seq. I'd love to hear your and @berinhard thoughts about it.

model_bakery/utils.py Show resolved Hide resolved
model_bakery/utils.py Outdated Show resolved Hide resolved
model_bakery/utils.py Outdated Show resolved Hide resolved
tests/test_recipes.py Show resolved Hide resolved
Copy link
Member

@berinhard berinhard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstrings =)

@timjklein36
Copy link
Collaborator Author

@berinhard @anapaulagomes I added a seq docstring. Let me know if it suits your expectations. Thanks!

Copy link
Contributor

@anapaulagomes anapaulagomes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🎉

Copy link
Member

@berinhard berinhard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks =)

@berinhard berinhard merged commit 7e98728 into model-bakers:master Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use seq with suffix str
4 participants