From 0903b18d1e4213cb88aa8cfcd0eb473ae54aa40b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fleschenberg?= Date: Thu, 22 Sep 2016 14:47:15 +0200 Subject: [PATCH 1/2] Fix and simplify the JSONfield wrapper code --- shop/models/fields.py | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/shop/models/fields.py b/shop/models/fields.py index 3459eec01..acbe81ef2 100644 --- a/shop/models/fields.py +++ b/shop/models/fields.py @@ -1,29 +1,12 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from django.db import connection +from django.conf import settings -from shop.apps import get_tuple_version -try: - if str(connection.vendor) == 'postgresql': - import psycopg2 - - psycopg2_version = get_tuple_version(psycopg2.__version__[:5]) - with connection.cursor() as cursor: - cursor.execute("SELECT version()") - row = cursor.fetchone()[:17] - postgres_version = get_tuple_version(str(row[0][:17].split(' ')[1])) - # To be able to use the Django version of JSONField, it requires to have - # PostgreSQL ≥ 9.4 and psycopg2 ≥ 2.5.4, otherwise some issues could be faced. - if (psycopg2_version) >= (2, 5, 4) and (postgres_version >= (9, 4)): - from django.contrib.postgres.fields import JSONField as _JSONField - else: - raise ImportError - else: - raise ImportError - -except ImportError: +if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql': + from django.contrib.postgres.fields import JSONField as _JSONField +else: from jsonfield.fields import JSONField as _JSONField From ad5b0bf9243e4925016e06891635812ca6928fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fleschenberg?= Date: Thu, 22 Sep 2016 14:48:37 +0200 Subject: [PATCH 2/2] Make this test postgres-compatible We don't run the testsuite on postgres yet, but we probably should. --- example/tests/test_fields.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/example/tests/test_fields.py b/example/tests/test_fields.py index 8c13e7488..d3ad4ce18 100644 --- a/example/tests/test_fields.py +++ b/example/tests/test_fields.py @@ -1,12 +1,18 @@ # -*- coding: utf-8 from __future__ import unicode_literals +import decimal + +from django.contrib.auth import get_user_model from django.db import models from django.test import TestCase -from shop.models.defaults.cart_item import CartItem from shop.models.fields import JSONField +from myshop.models import Cart, CartItem, Customer +from myshop.models.manufacturer import Manufacturer +from myshop.models.polymorphic.commodity import Commodity + class JsonModel(models.Model): @@ -14,21 +20,33 @@ class Meta: app_label = 'JsonModel' json = JSONField() - default_json = JSONField(default={"check": 12}) + default_json = JSONField(default=lambda: {"check": 12}) class JSONFieldTest(TestCase): """JSONField Tests""" - fixtures = ['myshop-polymorphic.json'] - def setUp(self): super(JSONFieldTest, self).setUp() - self.sample = CartItem.objects.get(id=8) + user = get_user_model().objects.create() + customer = Customer.objects.create(number=1, user=user) + manufacturer = Manufacturer.objects.create() + product = Commodity.objects.create( + product_code="testproduct", + unit_price=decimal.Decimal("0"), + order=1, + manufacturer=manufacturer + ) + cart = Cart.objects.create(customer=customer) + self.sample = CartItem.objects.create( + quantity=1, + cart=cart, + product=product, + extra={'product_code': 'foo'}, + ) self.assertIsNotNone(self.sample) def test_json_field_create(self): """Test saving a JSON object in our JSONField""" - extra = {"product_code":"1121"} - + extra = {'product_code': 'foo'} self.assertEqual(self.sample.extra, extra)