1# -*- coding: utf-8 -*-
2import pytest
3import sqlalchemy as sa
4
5from sqlalchemy_utils.types import json
6
7
8@pytest.fixture
9def Document(Base):
10    class Document(Base):
11        __tablename__ = 'document'
12        id = sa.Column(sa.Integer, primary_key=True)
13        json = sa.Column(json.JSONType)
14    return Document
15
16
17@pytest.fixture
18def init_models(Document):
19    pass
20
21
22class JSONTestCase(object):
23    def test_list(self, session, Document):
24        document = Document(
25            json=[1, 2, 3]
26        )
27
28        session.add(document)
29        session.commit()
30
31        document = session.query(Document).first()
32        assert document.json == [1, 2, 3]
33
34    def test_parameter_processing(self, session, Document):
35        document = Document(
36            json={'something': 12}
37        )
38
39        session.add(document)
40        session.commit()
41
42        document = session.query(Document).first()
43        assert document.json == {'something': 12}
44
45    def test_non_ascii_chars(self, session, Document):
46        document = Document(
47            json={'something': u'äääööö'}
48        )
49
50        session.add(document)
51        session.commit()
52
53        document = session.query(Document).first()
54        assert document.json == {'something': u'äääööö'}
55
56    def test_compilation(self, Document, session):
57        query = sa.select([Document.json])
58        # the type should be cacheable and not throw exception
59        session.execute(query)
60
61
62@pytest.mark.skipif('json.json is None')
63@pytest.mark.usefixtures('sqlite_memory_dsn')
64class TestSqliteJSONType(JSONTestCase):
65    pass
66
67
68@pytest.mark.skipif('json.json is None')
69@pytest.mark.usefixtures('postgresql_dsn')
70class TestPostgresJSONType(JSONTestCase):
71    pass
72