Skip to content

Commit

Permalink
Increase default randomness for integer generators (#59)
Browse files Browse the repository at this point in the history
* 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 <bernardoxhc@gmail.com>
  • Loading branch information
AdrianLC and berinhard committed Jan 22, 2020
1 parent 9ae2b27 commit a73a974
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ 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)
- README.md instead of rst [PR #44](https://github.com/model-bakers/model_bakery/pull/44)
- 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

Expand Down
43 changes: 38 additions & 5 deletions model_bakery/generators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.models import (
BigIntegerField,
BinaryField,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion model_bakery/random_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a73a974

Please sign in to comment.