diff --git a/model_bakery/generators.py b/model_bakery/generators.py index d609be0d..41a7a8fa 100644 --- a/model_bakery/generators.py +++ b/model_bakery/generators.py @@ -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, @@ -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: diff --git a/model_bakery/random_gen.py b/model_bakery/random_gen.py index a54516a6..d356ef61 100644 --- a/model_bakery/random_gen.py +++ b/model_bakery/random_gen.py @@ -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) diff --git a/tests/generic/models.py b/tests/generic/models.py index 789c7e7a..d56305ad 100755 --- a/tests/generic/models.py +++ b/tests/generic/models.py @@ -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() @@ -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() diff --git a/tests/test_filling_fields.py b/tests/test_filling_fields.py index d4297a0c..97aec04d 100644 --- a/tests/test_filling_fields.py +++ b/tests/test_filling_fields.py @@ -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: @@ -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: @@ -510,6 +518,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: