Skip to content

Commit

Permalink
Make TextField generator respect max_length (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeiJp authored Aug 20, 2024
1 parent 0123af2 commit ef21347
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions model_bakery/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def gen_integer():
DecimalField: random_gen.gen_decimal,
BinaryField: random_gen.gen_byte_string,
CharField: random_gen.gen_string,
TextField: random_gen.gen_text,
TextField: random_gen.gen_string,
SlugField: random_gen.gen_slug,
UUIDField: random_gen.gen_uuid,
DateField: random_gen.gen_date,
Expand All @@ -133,7 +133,7 @@ def gen_integer():
if CIEmailField:
default_mapping[CIEmailField] = random_gen.gen_email
if CITextField:
default_mapping[CITextField] = random_gen.gen_text
default_mapping[CITextField] = random_gen.gen_string
if DecimalRangeField:
default_mapping[DecimalRangeField] = random_gen.gen_pg_numbers_range(Decimal)
if IntegerRangeField:
Expand Down
5 changes: 5 additions & 0 deletions model_bakery/random_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ def gen_slug(max_length: int) -> str:


def gen_text() -> str:
warnings.warn(
"gen_text is deprecated, use gen_string instead",
DeprecationWarning,
stacklevel=2,
)
return gen_string(MAX_LENGTH)


Expand Down
2 changes: 2 additions & 0 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Person(models.Model):
name = models.CharField(max_length=30)
nickname = models.SlugField(max_length=36)
age = models.IntegerField()
bio_summary = models.TextField(max_length=120)
bio = models.TextField()
birthday = models.DateField()
birth_time = models.TimeField()
Expand Down Expand Up @@ -129,6 +130,7 @@ class Person(models.Model):
ci_char = CICharField(max_length=30)
ci_email = CIEmailField()
ci_text = CITextField()
ci_text_max_length = CITextField(max_length=120)
int_range = IntegerRangeField()
bigint_range = BigIntegerRangeField()
date_range = DateRangeField()
Expand Down
17 changes: 16 additions & 1 deletion tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from model_bakery import baker
from model_bakery.content_types import BAKER_CONTENTTYPES
from model_bakery.gis import BAKER_GIS
from model_bakery.random_gen import gen_related
from model_bakery.random_gen import MAX_LENGTH, gen_related
from tests.generic import generators, models

try:
Expand Down Expand Up @@ -103,6 +103,14 @@ def test_fill_TextField_with_a_random_str(self, person):
assert isinstance(person_bio_field, fields.TextField)

assert isinstance(person.bio, str)
assert len(person.bio) == MAX_LENGTH

def test_fill_TextField_with_max_length_str(self, person):
person_bio_summary_field = models.Person._meta.get_field("bio_summary")
assert isinstance(person_bio_summary_field, fields.TextField)

assert isinstance(person.bio_summary, str)
assert len(person.bio_summary) == person_bio_summary_field.max_length


class TestBinaryFieldsFilling:
Expand Down Expand Up @@ -559,6 +567,13 @@ def test_filling_citextfield(self, person):
ci_text_field = models.Person._meta.get_field("ci_text")
assert isinstance(ci_text_field, CITextField)
assert isinstance(person.ci_text, str)
assert len(person.ci_text) == MAX_LENGTH

def test_filling_citextfield_with_max_length(self, person):
ci_text_max_length_field = models.Person._meta.get_field("ci_text_max_length")
assert isinstance(ci_text_max_length_field, CITextField)
assert isinstance(person.ci_text_max_length, str)
assert len(person.ci_text_max_length) == ci_text_max_length_field.max_length

def test_filling_decimal_range_field(self, person):
try:
Expand Down

0 comments on commit ef21347

Please sign in to comment.