Skip to content

Commit

Permalink
Add test for postgresql specific JSONField
Browse files Browse the repository at this point in the history
  • Loading branch information
romgar committed Aug 17, 2016
1 parent 7e01580 commit ae09c8e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/models.py
@@ -1,8 +1,12 @@
import django

from django.db import models
from django.db.models.signals import pre_save
from django.utils import timezone

from dirtyfields import DirtyFieldsMixin
from dirtyfields.compare import timezone_support_compare
from tests.utils import is_postgresql_env_with_json_field


class TestModel(DirtyFieldsMixin, models.Model):
Expand Down Expand Up @@ -101,3 +105,10 @@ def pre_save(instance, *args, **kwargs):
class TestModelWithoutM2MCheck(DirtyFieldsMixin, models.Model):
characters = models.CharField(blank=True, max_length=80)
ENABLE_M2M_CHECK = False


if is_postgresql_env_with_json_field():
from django.contrib.postgres.fields import JSONField

class TestModelWithJSONField(DirtyFieldsMixin, models.Model):
json_field = JSONField()
16 changes: 16 additions & 0 deletions tests/test_postgresql_specific.py
@@ -0,0 +1,16 @@
import pytest

from tests.utils import is_postgresql_env_with_json_field


@pytest.mark.skipif(not is_postgresql_env_with_json_field(),
reason="requires postgresql and Django 1.9+")
@pytest.mark.django_db
def test_dirty_json_field():
from tests.models import TestModelWithJSONField

tm = TestModelWithJSONField.objects.create(json_field={'data': 'dummy_data'})
assert tm.get_dirty_fields() == {}

tm.json_field = {'data': 'foo'}
assert tm.get_dirty_fields() == {'json_field': {'data': 'dummy_data'}}
11 changes: 11 additions & 0 deletions tests/utils.py
@@ -1,5 +1,7 @@
import re

import django

from django.conf import settings
from django.db import connection

Expand Down Expand Up @@ -55,3 +57,12 @@ def __init__(self, model_class, number):
regex = r'^.*SELECT.*FROM "tests_%s".*$' % model_name

super(assert_select_number_queries_on_model, self).__init__(regex, number)


def is_postgresql_env_with_json_field():
try:
PG_VERSION = connection.pg_version
except AttributeError:
PG_VERSION = 0

return PG_VERSION >= 90400 and django.VERSION >= (1, 9)

0 comments on commit ae09c8e

Please sign in to comment.