summaryrefslogtreecommitdiff
path: root/pecan/tests/test_jsonify.py
blob: 3c5d8b5d68b476cb5cf91e9a4a4c02f4be64ab6e (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from datetime import datetime, date
from decimal import Decimal
try:
    from simplejson import loads
except:
    from json import loads  # noqa
try:
    from sqlalchemy import orm, schema, types
    from sqlalchemy.engine import create_engine
except ImportError:
    create_engine = None  # noqa
from unittest import TestCase

from pecan.jsonify import jsonify, encode, ResultProxy, RowProxy
from pecan import Pecan, expose
from webtest import TestApp

from webob.multidict import MultiDict


def make_person():
    class Person(object):
        def __init__(self, first_name, last_name):
            self.first_name = first_name
            self.last_name = last_name

        @property
        def name(self):
            return '%s %s' % (self.first_name, self.last_name)
    return Person


def test_simple_rule():
    Person = make_person()

    # create a Person instance
    p = Person('Jonathan', 'LaCour')

    # register a generic JSON rule
    @jsonify.when_type(Person)
    def jsonify_person(obj):
        return dict(
            name=obj.name
        )

    # encode the object using our new rule
    result = loads(encode(p))
    assert result['name'] == 'Jonathan LaCour'
    assert len(result) == 1


class TestJsonify(TestCase):

    def test_simple_jsonify(self):
        Person = make_person()

        # register a generic JSON rule
        @jsonify.when_type(Person)
        def jsonify_person(obj):
            return dict(
                name=obj.name
            )

        class RootController(object):
            @expose('json')
            def index(self):
                # create a Person instance
                p = Person('Jonathan', 'LaCour')
                return p

        app = TestApp(Pecan(RootController()))

        r = app.get('/')
        assert r.status_int == 200
        assert loads(r.body) == {'name': 'Jonathan LaCour'}


class TestJsonifyGenericEncoder(TestCase):
    def test_json_callable(self):
        class JsonCallable(object):
            def __init__(self, arg):
                self.arg = arg

            def __json__(self):
                return {"arg": self.arg}

        result = encode(JsonCallable('foo'))
        assert loads(result) == {'arg': 'foo'}

    def test_datetime(self):
        today = date.today()
        now = datetime.now()

        result = encode(today)
        assert loads(result) == str(today)

        result = encode(now)
        assert loads(result) == str(now)

    def test_decimal(self):
        # XXX Testing for float match which is inexact

        d = Decimal('1.1')
        result = encode(d)
        assert loads(result) == float(d)

    def test_multidict(self):
        md = MultiDict()
        md.add('arg', 'foo')
        md.add('arg', 'bar')
        result = encode(md)
        assert loads(result) == {'arg': ['foo', 'bar']}

    def test_fallback_to_builtin_encoder(self):
        class Foo(object):
            pass

        self.assertRaises(TypeError, encode, Foo())


class TestJsonifySQLAlchemyGenericEncoder(TestCase):

    def setUp(self):
        if not create_engine:
            self.create_fake_proxies()
        else:
            self.create_sa_proxies()

    def create_fake_proxies(self):

        # create a fake SA object
        class FakeSAObject(object):
            def __init__(self):
                self._sa_class_manager = object()
                self._sa_instance_state = 'awesome'
                self.id = 1
                self.first_name = 'Jonathan'
                self.last_name = 'LaCour'

        # create a fake result proxy
        class FakeResultProxy(ResultProxy):
            def __init__(self):
                self.rowcount = -1
                self.rows = []

            def __iter__(self):
                return iter(self.rows)

            def append(self, row):
                self.rows.append(row)

        # create a fake row proxy
        class FakeRowProxy(RowProxy):
            def __init__(self, arg=None):
                self.row = dict(arg)

            def __getitem__(self, key):
                return self.row.__getitem__(key)

            def keys(self):
                return self.row.keys()

        # get the SA objects
        self.sa_object = FakeSAObject()
        self.result_proxy = FakeResultProxy()
        self.result_proxy.append(
            FakeRowProxy([
                ('id', 1),
                ('first_name', 'Jonathan'),
                ('last_name', 'LaCour')
            ])
        )
        self.result_proxy.append(
            FakeRowProxy([
                ('id', 2), ('first_name', 'Yoann'), ('last_name', 'Roman')
            ]))
        self.row_proxy = FakeRowProxy([
            ('id', 1), ('first_name', 'Jonathan'), ('last_name', 'LaCour')
        ])

    def create_sa_proxies(self):

        # create the table and mapper
        metadata = schema.MetaData()
        user_table = schema.Table(
            'user',
            metadata,
            schema.Column('id', types.Integer, primary_key=True),
            schema.Column('first_name', types.Unicode(25)),
            schema.Column('last_name', types.Unicode(25))
        )

        class User(object):
            pass
        orm.mapper(User, user_table)

        # create the session
        engine = create_engine('sqlite:///:memory:')
        metadata.bind = engine
        metadata.create_all()
        session = orm.sessionmaker(bind=engine)()

        # add some dummy data
        user_table.insert().execute([
            {'first_name': u'Jonathan', 'last_name': u'LaCour'},
            {'first_name': u'Yoann', 'last_name': u'Roman'}
        ])

        # get the SA objects
        self.sa_object = session.query(User).first()
        select = user_table.select()
        self.result_proxy = select.execute()
        self.row_proxy = select.execute().fetchone()

    def test_sa_object(self):
        result = encode(self.sa_object)
        assert loads(result) == {
            'id': 1, 'first_name': 'Jonathan', 'last_name': 'LaCour'
        }

    def test_result_proxy(self):
        result = encode(self.result_proxy)
        assert loads(result) == {'count': 2, 'rows': [
            {'id': 1, 'first_name': 'Jonathan', 'last_name': 'LaCour'},
            {'id': 2, 'first_name': 'Yoann', 'last_name': 'Roman'}
        ]}

    def test_row_proxy(self):
        result = encode(self.row_proxy)
        assert loads(result) == {
            'id': 1, 'first_name': 'Jonathan', 'last_name': 'LaCour'
        }