-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from ambitioninc/master
Merge master -> develop
- Loading branch information
Showing
4 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import contextlib | ||
import json | ||
import psycopg2 | ||
|
||
|
||
@contextlib.contextmanager | ||
def json_cursor(django_database_connection): | ||
""" | ||
Cast json fields into their specific types to account for django bugs | ||
https://code.djangoproject.com/ticket/31956 | ||
https://code.djangoproject.com/ticket/31973 | ||
https://www.psycopg.org/docs/extras.html#psycopg2.extras.register_default_jsonb | ||
""" | ||
with django_database_connection.cursor() as cursor: | ||
psycopg2.extras.register_default_jsonb(conn_or_curs=cursor.cursor, loads=json.loads) | ||
yield cursor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.db import connection | ||
|
||
from querybuilder.cursor import json_cursor | ||
from querybuilder.tests.base import QuerybuilderTestCase | ||
from querybuilder.tests.models import MetricRecord | ||
|
||
|
||
class JsonCursorTests(QuerybuilderTestCase): | ||
def test_json_cursor(self): | ||
data = {'one': 1, 'two': 2} | ||
MetricRecord.objects.create(data=data) | ||
with json_cursor(connection) as cursor: | ||
cursor.execute(f'select data from {MetricRecord._meta.db_table}') | ||
record = cursor.fetchone() | ||
self.assertEqual(record[0], data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '3.0.1' | ||
__version__ = '3.0.2' |