Skip to content

Commit

Permalink
Fix JsonField from_db_value + better unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seljin committed May 16, 2024
1 parent 420fcca commit 1598e8f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions .venv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drm
7 changes: 7 additions & 0 deletions rest_models/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def message_from_response(response):
class JSONField(JSONFieldLegacy):
def get_prep_value(self, value):
return value

def from_db_value(self, value, expression, connection):
try:
return super().from_db_value(value, expression, connection)
except TypeError:
return value

except NameError:
def JSONField(*args, **kwargs):
return None
8 changes: 7 additions & 1 deletion rest_models/tests/tests_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ def test_many2many_clear(self):


@skipIf(settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3', 'no json in sqlite')
@skipIf('year' in DynamicFilterBackend.VALID_FILTER_OPERATORS, 'skip check not compatible with current drest')
class TestJsonField(TestCase):
fixtures = ['data.json']
databases = ["default", "api"]

def test_jsonfield_create(self):
t = client_models.Topping.objects.create(
Expand Down Expand Up @@ -277,6 +277,12 @@ def test_jsonfield_update(self):
{k: v for k, v in t2.__dict__.items() if k in ('name', 'cost', 'metadata')}
)


@skipIf(settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3', 'no json in sqlite')
@skipIf('year' in DynamicFilterBackend.VALID_FILTER_OPERATORS, 'skip check not compatible with current drest')
class TestJsonLookup(TestCase):
fixtures = ['data.json']

def test_jsonfield_lookup_isnull(self):
t = api_models.Topping.objects.create(
name='lardons lux',
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
Expand Down

0 comments on commit 1598e8f

Please sign in to comment.