From a73a9747b8a16b72c45414a75bf833fd4779eefc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20L=C3=B3pez=20Calvo?= Date: Wed, 22 Jan 2020 15:56:06 +0100 Subject: [PATCH] Increase default randomness for integer generators (#59) * Increase default MAX_INT random generator I think 10000 is just too low. Wwe have been having flakyness on our tests with UNIQUE constraints on Integer columns because of conflicting values we this generator. * Add dynamic integer ranges from Django's bounds * Formatting * Add more generator ranges for other types of integer fields * Update changelog Co-authored-by: Bernardo Fontes --- CHANGELOG.md | 3 ++- model_bakery/generators.py | 43 +++++++++++++++++++++++++++++++++----- model_bakery/random_gen.py | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d9754db..1b2c9a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Changed -<<<<<<< HEAD + - Improve code comments [PR #31](https://github.com/model-bakers/model_bakery/pull/31) - Switch to tox-travis [PR #43](https://github.com/model-bakers/model_bakery/pull/43) - Add black job [PR #42](https://github.com/model-bakers/model_bakery/pull/42) @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Add Django 3.0 and Python 3.8 to CI [PR #48](https://github.com/model-bakers/model_bakery/pull/48/) - Add `start` argument to `baker.seq` [PR #56](https://github.com/model-bakers/model_bakery/pull/56) - Fixes bug when registering custom fields generator via `settings.py` [PR #58](https://github.com/model-bakers/model_bakery/pull/58) +- The different IntegerField types now will generate values on their min/max range [PR #59](https://github.com/model-bakers/model_bakery/pull/59) ### Removed diff --git a/model_bakery/generators.py b/model_bakery/generators.py index 6b3b7528..679eab59 100644 --- a/model_bakery/generators.py +++ b/model_bakery/generators.py @@ -1,3 +1,4 @@ +from django.db.backends.base.operations import BaseDatabaseOperations from django.db.models import ( BigIntegerField, BinaryField, @@ -31,6 +32,18 @@ from . import random_gen from .utils import import_from_str +try: + from django.db.models import AutoField, BigAutoField, SmallAutoField +except ImportError: + AutoField = None + BigAutoField = None + SmallAutoField = None + +try: + from django.db.models import PositiveBigIntegerField +except ImportError: + PositiveBigIntegerField = None + try: from django.contrib.postgres.fields import ArrayField except ImportError: @@ -58,17 +71,26 @@ CITextField = None +def _make_integer_gen_by_range(field_type): + min_int, max_int = BaseDatabaseOperations.integer_field_ranges[field_type.__name__] + + def gen_integer(): + return random_gen.gen_integer(min_int=min_int, max_int=max_int) + + return gen_integer + + default_mapping = { ForeignKey: random_gen.gen_related, OneToOneField: random_gen.gen_related, ManyToManyField: random_gen.gen_m2m, BooleanField: random_gen.gen_boolean, NullBooleanField: random_gen.gen_boolean, - IntegerField: random_gen.gen_integer, - BigIntegerField: random_gen.gen_integer, - SmallIntegerField: random_gen.gen_integer, - PositiveIntegerField: lambda: random_gen.gen_integer(min_int=0), - PositiveSmallIntegerField: lambda: random_gen.gen_integer(min_int=0), + IntegerField: _make_integer_gen_by_range(IntegerField), + BigIntegerField: _make_integer_gen_by_range(BigIntegerField), + SmallIntegerField: _make_integer_gen_by_range(SmallIntegerField), + PositiveIntegerField: _make_integer_gen_by_range(PositiveIntegerField), + PositiveSmallIntegerField: _make_integer_gen_by_range(PositiveSmallIntegerField), FloatField: random_gen.gen_float, DecimalField: random_gen.gen_decimal, BinaryField: random_gen.gen_byte_string, @@ -100,6 +122,17 @@ default_mapping[CIEmailField] = random_gen.gen_email if CITextField: default_mapping[CITextField] = random_gen.gen_text +if AutoField: + default_mapping[AutoField] = _make_integer_gen_by_range(AutoField) +if BigAutoField: + default_mapping[BigAutoField] = _make_integer_gen_by_range(BigAutoField) +if SmallAutoField: + default_mapping[SmallAutoField] = _make_integer_gen_by_range(SmallAutoField) +if PositiveBigIntegerField: + default_mapping[PositiveBigIntegerField] = _make_integer_gen_by_range( + PositiveBigIntegerField + ) + # Add GIS fields diff --git a/model_bakery/random_gen.py b/model_bakery/random_gen.py index 6078fdf8..433c38e2 100644 --- a/model_bakery/random_gen.py +++ b/model_bakery/random_gen.py @@ -21,7 +21,7 @@ MAX_LENGTH = 300 # Using sys.maxint here breaks a bunch of tests when running against a # Postgres database. -MAX_INT = 10000 +MAX_INT = 100000000000 def get_content_file(content, name):