Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for django 3.1 JSONField #85

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- Support to Postgres fields: `DecimalRangeField`, `FloatRangeField`, `IntegerRangeField`, `BigIntegerRangeField`, `DateRangeField`, `DateTimeRangeField` [PR #80](https://github.com/model-bakers/model_bakery/pull/80)
- Support to django 3.1 `JSONField` [PR #85](https://github.com/model-bakers/model_bakery/pull/85)

### Changed
- Add isort and fix imports [PR #77](https://github.com/model-bakers/model_bakery/pull/77)
Expand Down
11 changes: 9 additions & 2 deletions model_bakery/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@
except ImportError:
PositiveBigIntegerField = None

try:
from django.db.models import JSONField
except ImportError:
JSONField = None

try:
from django.contrib.postgres.fields import ArrayField
except ImportError:
ArrayField = None

try:
from django.contrib.postgres.fields import JSONField
from django.contrib.postgres.fields import JSONField as PostgresJSONField
except ImportError:
JSONField = None
PostgresJSONField = None

try:
from django.contrib.postgres.fields import HStoreField
Expand Down Expand Up @@ -135,6 +140,8 @@ def gen_integer():
default_mapping[ArrayField] = random_gen.gen_array
if JSONField:
default_mapping[JSONField] = random_gen.gen_json
if PostgresJSONField:
default_mapping[PostgresJSONField] = random_gen.gen_json
if HStoreField:
default_mapping[HStoreField] = random_gen.gen_hstore
if CICharField:
Expand Down
13 changes: 11 additions & 2 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ class Person(models.Model):
id_document = models.CharField(unique=True, max_length=10)

try:
from django.contrib.postgres.fields import ArrayField, HStoreField, JSONField
from django.models import JSONField
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the correct import is django.db.models, isn't it?


data = JSONField()
except ImportError:
# Skip JSONField-related fields
pass

try:
from django.contrib.postgres.fields import ArrayField, HStoreField
from django.contrib.postgres.fields import JSONField as PostgresJSONField
from django.contrib.postgres.fields.citext import (
CICharField,
CIEmailField,
Expand All @@ -105,7 +114,7 @@ class Person(models.Model):

if settings.USING_POSTGRES:
acquaintances = ArrayField(models.IntegerField())
data = JSONField()
postgres_data = PostgresJSONField()
hstore_data = HStoreField()
ci_char = CICharField(max_length=30)
ci_email = CIEmailField()
Expand Down
19 changes: 16 additions & 3 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
from model_bakery.random_gen import gen_related
from tests.generic import generators, models

try:
from django.models import JSONField
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the correct import is django.db.models, isn't it?

except ImportError:
JSONField = None

try:
from django.contrib.postgres.fields import (
ArrayField,
CICharField,
CIEmailField,
CITextField,
HStoreField,
JSONField,
JSONField as PostgresJSONField,
)
from django.contrib.postgres.fields.ranges import (
IntegerRangeField,
Expand All @@ -37,7 +42,7 @@
)
except ImportError:
ArrayField = None
JSONField = None
PostgresJSONField = None
HStoreField = None
CICharField = None
CIEmailField = None
Expand Down Expand Up @@ -164,6 +169,14 @@ def test_fill_UUIDField_with_uuid_object(self, person):
assert isinstance(person.uuid, uuid.UUID)


@pytest.mark.skipif(JSONField is None, reason="JSONField could not be imported")
class TestJSONFieldsFilling:
def test_fill_JSONField_with_uuid_object(self, person):
json_field = models.Person._meta.get_field("data")
assert isinstance(json_field, JSONField)
assert isinstance(person.data, dict)


@pytest.mark.django_db
class TestFillingIntFields:
def test_fill_IntegerField_with_a_random_number(self):
Expand Down Expand Up @@ -479,7 +492,7 @@ def test_fill_arrayfield_with_empty_array(self, person):
assert person.acquaintances == []

def test_fill_jsonfield_with_empty_dict(self, person):
assert person.data == {}
assert person.postgres_data == {}

def test_fill_hstorefield_with_empty_dict(self, person):
assert person.hstore_data == {}
Expand Down