1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
# -*- coding: utf-8 -*-
import sqlalchemy as sa
from pytest import mark
from sqlalchemy_utils.types import json
from tests import TestCase
class JSONTestCase(TestCase):
def create_models(self):
class Document(self.Base):
__tablename__ = 'document'
id = sa.Column(sa.Integer, primary_key=True)
json = sa.Column(json.JSONType)
self.Document = Document
def test_list(self):
document = self.Document(
json=[1, 2, 3]
)
self.session.add(document)
self.session.commit()
document = self.session.query(self.Document).first()
assert document.json == [1, 2, 3]
def test_parameter_processing(self):
document = self.Document(
json={'something': 12}
)
self.session.add(document)
self.session.commit()
document = self.session.query(self.Document).first()
assert document.json == {'something': 12}
def test_non_ascii_chars(self):
document = self.Document(
json={'something': u'äääööö'}
)
self.session.add(document)
self.session.commit()
document = self.session.query(self.Document).first()
assert document.json == {'something': u'äääööö'}
@mark.skipif('json.json is None')
class TestSqliteJSONType(JSONTestCase):
pass
@mark.skipif('json.json is None')
class TestPostgresJSONType(JSONTestCase):
dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'
|