From fb748f788fe10f44af6d1b6569b3f94425acbaa0 Mon Sep 17 00:00:00 2001 From: SergeiJp Date: Fri, 16 Aug 2024 13:29:41 +0900 Subject: [PATCH 1/4] make textfield respect max_length --- model_bakery/generators.py | 4 ++-- model_bakery/random_gen.py | 4 ---- tests/generic/models.py | 2 ++ tests/test_filling_fields.py | 17 ++++++++++++++++- 4 files changed, 20 insertions(+), 7 deletions(-) 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..8bd439ee 100644 --- a/model_bakery/random_gen.py +++ b/model_bakery/random_gen.py @@ -133,10 +133,6 @@ def gen_slug(max_length: int) -> str: gen_slug.required = ["max_length"] # type: ignore[attr-defined] -def gen_text() -> str: - return gen_string(MAX_LENGTH) - - def gen_boolean() -> bool: return baker_random.choice((True, False)) 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..98d6d6df 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 gen_related, MAX_LENGTH 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, str) + assert len(person.ci_text) == ci_text_max_length_field.max_length def test_filling_decimal_range_field(self, person): try: From de467ba1da9343054536de304ca40668abb450aa Mon Sep 17 00:00:00 2001 From: SergeiJp Date: Fri, 16 Aug 2024 21:35:29 +0900 Subject: [PATCH 2/4] optimize imports --- tests/test_filling_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_filling_fields.py b/tests/test_filling_fields.py index 98d6d6df..df5f365a 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, MAX_LENGTH +from model_bakery.random_gen import MAX_LENGTH, gen_related from tests.generic import generators, models try: From 481db426389cb2c2de083db555096f467f2f1481 Mon Sep 17 00:00:00 2001 From: SergeiJp Date: Sat, 17 Aug 2024 10:57:45 +0900 Subject: [PATCH 3/4] Rollback gen_text, adds deprecation warning --- model_bakery/random_gen.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/model_bakery/random_gen.py b/model_bakery/random_gen.py index 8bd439ee..d356ef61 100644 --- a/model_bakery/random_gen.py +++ b/model_bakery/random_gen.py @@ -133,6 +133,15 @@ def gen_slug(max_length: int) -> str: gen_slug.required = ["max_length"] # type: ignore[attr-defined] +def gen_text() -> str: + warnings.warn( + "gen_text is deprecated, use gen_string instead", + DeprecationWarning, + stacklevel=2, + ) + return gen_string(MAX_LENGTH) + + def gen_boolean() -> bool: return baker_random.choice((True, False)) From b723de2127c6b40b10aa6d3fa6ebd4476d69ae60 Mon Sep 17 00:00:00 2001 From: SergeiJp Date: Sat, 17 Aug 2024 17:39:24 +0900 Subject: [PATCH 4/4] fixes ci_text_max_length test --- tests/test_filling_fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_filling_fields.py b/tests/test_filling_fields.py index df5f365a..97aec04d 100644 --- a/tests/test_filling_fields.py +++ b/tests/test_filling_fields.py @@ -523,8 +523,8 @@ def test_filling_citextfield(self, person): 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, str) - assert len(person.ci_text) == ci_text_max_length_field.max_length + 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: