Skip to content

Commit

Permalink
Merge pull request #103 from paulocheque/json_field
Browse files Browse the repository at this point in the history
Native support for Postgres JSON field
  • Loading branch information
paulocheque committed Jan 4, 2020
2 parents fba3905 + 06603da commit 8968fff
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 76 deletions.
4 changes: 4 additions & 0 deletions django_dynamic_fixture/fixture_algorithms/default_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def uuidfield_config(self, field, key):
def genericipaddressfield_config(self, field, key):
return self.ipaddressfield_config(field, key)

# POSTGRES
def jsonfield_config(self, field, key):
return {}


# GIS/GeoDjango
class GeoDjangoFixtureMixin(object):
Expand Down
10 changes: 10 additions & 0 deletions django_dynamic_fixture/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ class Meta:
app_label = 'django_dynamic_fixture'


try:
from django.contrib.postgres.fields import JSONField
class ModelForPostgresFields(models.Model):
nullable_json_field = JSONField(null=True)
json_field = JSONField(null=False)
class Meta:
app_label = 'django_dynamic_fixture'
except ImportError:
pass

try:
from jsonfield import JSONField
from jsonfield import JSONCharField
Expand Down
75 changes: 0 additions & 75 deletions django_dynamic_fixture/tests/test_ddf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from decimal import Decimal
import uuid

from django.conf import settings

from django.test import TestCase
import pytest

Expand Down Expand Up @@ -149,61 +147,6 @@ def test_bad_data_raise_an_error(self):
self.ddf.get(ModelWithNumbers, integer=50000)


class NewFullFillAttributesUsingPluginsTest(DDFTestCase):
def test_custom_field_not_registered_must_raise_an_unsupported_field_exception(self):
with pytest.raises(UnsupportedFieldError):
self.ddf.new(ModelWithUnsupportedField)

def test_new_fill_field_with_data_generated_by_plugins_with_dict(self):
data_fixture.plugins = settings.DDF_FIELD_FIXTURES
try:
instance = self.ddf.get(ModelForFieldPlugins)
# assert instance.aaa == 123456789
# assert instance.bbb == 123456789
assert instance.custom_field_custom_fixture == 123456789
finally:
data_fixture.plugins = {}

def test_new_fill_field_with_data_generated_by_plugins_with_direct_fuction(self):
data_fixture.plugins = settings.DDF_FIELD_FIXTURES
try:
instance = self.ddf.get(ModelForFieldPlugins)
assert instance.custom_field_custom_fixture2 == 987654321
finally:
data_fixture.plugins = {}

# Real Custom Field
def test_json_field_not_registered_must_raise_an_unsupported_field_exception(self):
# jsonfield requires Django 1.4+
try:
from jsonfield import JSONCharField, JSONField
instance = self.ddf.new(ModelForPlugins1)
assert False, 'JSON fields must not be supported by default'
except ImportError:
pass
except UnsupportedFieldError as e:
pass

def test_new_fill_json_field_with_data_generated_by_plugins(self):
# jsonfield requires Django 1.4+
try:
import json
from jsonfield import JSONCharField, JSONField
data_fixture.plugins = settings.DDF_FIELD_FIXTURES
try:
instance = self.ddf.new(ModelForPlugins1)
assert isinstance(instance.json_field1, str), type(instance.json_field1)
assert isinstance(instance.json_field2, str), type(instance.json_field2)
assert isinstance(json.loads(instance.json_field1), dict)
assert isinstance(json.loads(instance.json_field2), list)
assert instance.json_field1 == '{"some random value": "c"}'
assert instance.json_field2 == '[1, 2, 3]'
finally:
data_fixture.plugins = {}
except ImportError:
pass


class NewIgnoringNullableFieldsTest(DDFTestCase):
def test_new_do_not_fill_nullable_fields_if_we_do_not_want_to(self):
self.ddf = DynamicFixture(data_fixture, fill_nullable_fields=False)
Expand Down Expand Up @@ -408,24 +351,6 @@ def test_get_2(self):
assert isinstance(instance.parent, ModelChildWithCustomParentLink)


class CustomFieldsTest(DDFTestCase):
def test_new_field_that_extends_django_field_must_be_supported(self):
instance = self.ddf.new(ModelWithCustomFields)
assert instance.x == 1

def test_unsupported_field_is_filled_with_null_if_it_is_possible(self):
instance = self.ddf.new(ModelWithCustomFields)
assert instance.y is None

def test_unsupported_field_raise_an_error_if_it_does_not_accept_null_value(self):
with pytest.raises(UnsupportedFieldError):
self.ddf.new(ModelWithUnsupportedField)

def test_new_field_that_double_inherits_django_field_must_be_supported(self):
instance = self.ddf.new(ModelWithCustomFieldsMultipleInheritance)
assert instance.x == 1


class ComplexFieldsTest(DDFTestCase):
def test_x(self):
instance = self.ddf.new(ModelForUUID)
Expand Down
118 changes: 118 additions & 0 deletions django_dynamic_fixture/tests/test_ddf_custom_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# -*- coding: utf-8 -*-
from django.conf import settings

from django.test import TestCase
import pytest

from django_dynamic_fixture.models_test import *
from django_dynamic_fixture.ddf import *
from django_dynamic_fixture.decorators import only_for_database, POSTGRES
from django_dynamic_fixture.fixture_algorithms.sequential_fixture import SequentialDataFixture

data_fixture = SequentialDataFixture()


class DDFTestCase(TestCase):
def setUp(self):
self.ddf = DynamicFixture(data_fixture)


class CustomFieldsTest(DDFTestCase):
def test_new_field_that_extends_django_field_must_be_supported(self):
instance = self.ddf.new(ModelWithCustomFields)
assert instance.x == 1

def test_unsupported_field_is_filled_with_null_if_it_is_possible(self):
instance = self.ddf.new(ModelWithCustomFields)
assert instance.y is None

def test_unsupported_field_raise_an_error_if_it_does_not_accept_null_value(self):
with pytest.raises(UnsupportedFieldError):
self.ddf.new(ModelWithUnsupportedField)

def test_new_field_that_double_inherits_django_field_must_be_supported(self):
instance = self.ddf.new(ModelWithCustomFieldsMultipleInheritance)
assert instance.x == 1


class NewFullFillAttributesUsingPluginsTest(DDFTestCase):
def test_custom_field_not_registered_must_raise_an_unsupported_field_exception(self):
with pytest.raises(UnsupportedFieldError):
self.ddf.new(ModelWithUnsupportedField)

def test_new_fill_field_with_data_generated_by_plugins_with_dict(self):
data_fixture.plugins = settings.DDF_FIELD_FIXTURES
try:
instance = self.ddf.get(ModelForFieldPlugins)
# assert instance.aaa == 123456789
# assert instance.bbb == 123456789
assert instance.custom_field_custom_fixture == 123456789
finally:
data_fixture.plugins = {}

def test_new_fill_field_with_data_generated_by_plugins_with_direct_fuction(self):
data_fixture.plugins = settings.DDF_FIELD_FIXTURES
try:
instance = self.ddf.get(ModelForFieldPlugins)
assert instance.custom_field_custom_fixture2 == 987654321
finally:
data_fixture.plugins = {}

# Real Custom Field
def test_json_field_not_registered_must_raise_an_unsupported_field_exception(self):
# jsonfield requires Django 1.4+
try:
from jsonfield import JSONCharField, JSONField
instance = self.ddf.new(ModelForPlugins1)
assert False, 'JSON fields must not be supported by default'
except ImportError:
pass
except UnsupportedFieldError as e:
pass

def test_new_fill_json_field_with_data_generated_by_plugins(self):
# jsonfield requires Django 1.4+
try:
import json
from jsonfield import JSONCharField, JSONField
data_fixture.plugins = settings.DDF_FIELD_FIXTURES
try:
instance = self.ddf.new(ModelForPlugins1)
assert isinstance(instance.json_field1, str), type(instance.json_field1)
assert isinstance(instance.json_field2, str), type(instance.json_field2)
assert isinstance(json.loads(instance.json_field1), dict)
assert isinstance(json.loads(instance.json_field2), list)
assert instance.json_field1 == '{"some random value": "c"}'
assert instance.json_field2 == '[1, 2, 3]'
finally:
data_fixture.plugins = {}
except ImportError:
pass


class PostgresCustomFieldsTest(DDFTestCase):
@only_for_database(POSTGRES)
def test_json_field(self):
instance = self.ddf.get(ModelForPostgresFields)
assert instance.nullable_json_field == {}
assert instance.json_field == {}

@only_for_database(POSTGRES)
def test_json_field_as_null(self):
instance = self.ddf.get(ModelForPostgresFields, nullable_json_field=None)
assert instance.nullable_json_field is None

@only_for_database(POSTGRES)
def test_json_field_as_dicts(self):
instance = self.ddf.get(ModelForPostgresFields, json_field={'a': 1})
assert instance.json_field == {'a': 1}

@only_for_database(POSTGRES)
def test_json_field_as_lists(self):
instance = self.ddf.get(ModelForPostgresFields, json_field=['str1', 2, {'c': 3}])
assert instance.json_field == ['str1', 2, {'c': 3}]

@only_for_database(POSTGRES)
def test_json_field_as_strings(self):
instance = self.ddf.get(ModelForPostgresFields, json_field='str')
assert instance.json_field == 'str'
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
# https://github.com/bradjasper/django-jsonfield
'jsonfield.fields.JSONCharField': {'ddf_fixture': lambda: json.dumps({'some random value': 'c'})},
'jsonfield.fields.JSONField': {'ddf_fixture': lambda: json.dumps([1, 2, 3])},
}
}

0 comments on commit 8968fff

Please sign in to comment.