~gandelman-a/+junk/python-flask-restful

« back to all changes in this revision

Viewing changes to build/lib.linux-x86_64-2.7/tests/test_fields.py

  • Committer: Adam Gandelman
  • Date: 2013-05-26 22:21:00 UTC
  • Revision ID: adamg@canonical.com-20130526222100-vtzmfbauoc61x3me
Initial release (Closes: #nnnn)  <nnnn is the bug number of your ITP>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from decimal import Decimal
 
2
import unittest
 
3
from mock import Mock
 
4
from flask.ext.restful.fields import MarshallingException
 
5
from flask_restful import fields
 
6
from datetime import datetime
 
7
from flask import Flask
 
8
#noinspection PyUnresolvedReferences
 
9
from nose.tools import assert_equals # you need it for tests in form of continuations
 
10
 
 
11
 
 
12
class Foo(object):
 
13
    def __init__(self):
 
14
        self.hey = 3
 
15
class Bar(object):
 
16
    def __marshallable__(self):
 
17
        return {"hey": 3}
 
18
 
 
19
def check_field(expected, field, value):
 
20
    assert_equals(expected, field.output('a', {'a': value}))
 
21
 
 
22
def test_float():
 
23
    values = [
 
24
        ("-3.13", "-3.13"),
 
25
        (str(-3.13), "-3.13"),
 
26
        (3, "3"),
 
27
    ]
 
28
    for value, expected in values:
 
29
        yield check_field, expected, fields.Arbitrary(), value
 
30
 
 
31
 
 
32
def test_boolean():
 
33
    values = [
 
34
        (True, True),
 
35
        (False, False),
 
36
        ({}, False),
 
37
        ("false", True),  # These are different from php
 
38
        ("0", True),      # Will this be a problem?
 
39
        ]
 
40
    for value, expected in values:
 
41
        yield check_field, expected, fields.Boolean(), value
 
42
 
 
43
 
 
44
class FieldsTestCase(unittest.TestCase):
 
45
 
 
46
    def test_decimal_trash(self):
 
47
        self.assertRaises(MarshallingException, lambda: fields.Float().output('a', {'a': 'Foo'}))
 
48
 
 
49
    def test_basic_dictionary(self):
 
50
        obj = {"foo": 3}
 
51
        field = fields.String()
 
52
        self.assertEquals(field.output("foo", obj), "3")
 
53
 
 
54
 
 
55
    def test_no_attribute(self):
 
56
        obj = {"bar": 3}
 
57
        field = fields.String()
 
58
        self.assertEquals(field.output("foo", obj), None)
 
59
 
 
60
 
 
61
    def test_date_field_invalid(self):
 
62
        obj = {"bar": 3}
 
63
        field = fields.DateTime()
 
64
        self.assertRaises(MarshallingException, lambda: field.output("bar", obj))
 
65
 
 
66
 
 
67
    def test_attribute(self):
 
68
        obj = {"bar": 3}
 
69
        field = fields.String(attribute="bar")
 
70
        self.assertEquals(field.output("foo", obj), "3")
 
71
 
 
72
 
 
73
    def test_formatting_field_none(self):
 
74
        obj = {}
 
75
        field = fields.FormattedString("/foo/{0[account_sid]}/{0[sid]}/")
 
76
        self.assertRaises(MarshallingException, lambda: field.output("foo", obj))
 
77
 
 
78
 
 
79
    def test_formatting_field_tuple(self):
 
80
        obj = (3, 4)
 
81
        field = fields.FormattedString("/foo/{0[account_sid]}/{0[sid]}/")
 
82
        self.assertRaises(MarshallingException, lambda: field.output("foo", obj))
 
83
 
 
84
 
 
85
    def test_formatting_field_dict(self):
 
86
        obj = {
 
87
            "sid": 3,
 
88
            "account_sid": 4,
 
89
            }
 
90
        field = fields.FormattedString("/foo/{account_sid}/{sid}/")
 
91
        self.assertEquals(field.output("foo", obj), "/foo/4/3/")
 
92
 
 
93
 
 
94
    def test_formatting_field(self):
 
95
        obj = Mock()
 
96
        obj.sid = 3
 
97
        obj.account_sid = 4
 
98
        field = fields.FormattedString("/foo/{account_sid}/{sid}/")
 
99
        self.assertEquals(field.output("foo", obj), "/foo/4/3/")
 
100
 
 
101
 
 
102
    def test_basic_field(self):
 
103
        obj = Mock()
 
104
        obj.foo = 3
 
105
        field = fields.Raw()
 
106
        self.assertEquals(field.output("foo", obj), 3)
 
107
 
 
108
 
 
109
    def test_raw_field(self):
 
110
        obj = Mock()
 
111
        obj.foo = 3
 
112
        field = fields.Raw()
 
113
        self.assertEquals(field.output("foo", obj), 3)
 
114
 
 
115
 
 
116
    def test_formatted_string_invalid_obj(self):
 
117
        field = fields.FormattedString("{hey}")
 
118
        self.assertRaises(MarshallingException, lambda: field.output("hey", None))
 
119
 
 
120
 
 
121
    def test_formatted_string(self):
 
122
        field = fields.FormattedString("{hey}")
 
123
        self.assertEquals("3", field.output("hey", Foo()))
 
124
 
 
125
 
 
126
    def test_string_with_attribute(self):
 
127
        field = fields.String(attribute="hey")
 
128
        self.assertEquals("3", field.output("foo", Foo()))
 
129
 
 
130
 
 
131
    def test_url_invalid_object(self):
 
132
        app = Flask(__name__)
 
133
        app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
 
134
        field = fields.Url("foobar")
 
135
 
 
136
        with app.test_request_context("/"):
 
137
            self.assertRaises(MarshallingException, lambda: field.output("hey", None))
 
138
 
 
139
 
 
140
    def test_url(self):
 
141
        app = Flask(__name__)
 
142
        app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
 
143
        field = fields.Url("foobar")
 
144
 
 
145
        with app.test_request_context("/"):
 
146
            self.assertEquals("/3", field.output("hey", Foo()))
 
147
 
 
148
 
 
149
    def test_int(self):
 
150
        field = fields.Integer()
 
151
        self.assertEquals(3, field.output("hey", {'hey': 3}))
 
152
 
 
153
 
 
154
    def test_int_default(self):
 
155
        field = fields.Integer(default=1)
 
156
        self.assertEquals(1, field.output("hey", {'hey': None}))
 
157
 
 
158
 
 
159
    def test_no_int(self):
 
160
        field = fields.Integer()
 
161
        self.assertEquals(0, field.output("hey", {'hey': None}))
 
162
 
 
163
    def test_int_decode_error(self):
 
164
        field = fields.Integer()
 
165
        self.assertRaises(MarshallingException, lambda: field.output("hey", {'hey': 'Explode please I am nowhere looking like an int'}))
 
166
 
 
167
    def test_float(self):
 
168
        field = fields.Float()
 
169
        self.assertEquals('3.0', field.output("hey", {'hey': 3.0}))
 
170
 
 
171
    def test_float_decode_error(self):
 
172
        field = fields.Float()
 
173
        self.assertRaises(MarshallingException, lambda: field.output("hey", {'hey': 'Explode!'}))
 
174
 
 
175
    PI_STR = u'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861'
 
176
    PI = Decimal(PI_STR)
 
177
 
 
178
    def test_arbitrary(self):
 
179
        field = fields.Arbitrary()
 
180
        self.assertEquals(self.PI_STR, field.output("hey", {'hey': self.PI}))
 
181
 
 
182
    def test_fixed(self):
 
183
        field5 = fields.Fixed(5)
 
184
        field4 = fields.Fixed(4)
 
185
 
 
186
        self.assertEquals('3.14159', field5.output("hey", {'hey': self.PI}))
 
187
        self.assertEquals('3.1416', field4.output("hey", {'hey': self.PI}))
 
188
        self.assertEquals('3.0000', field4.output("hey", {'hey': '3'}))
 
189
        self.assertEquals('3.0000', field4.output("hey", {'hey': '03'}))
 
190
        self.assertEquals('3.0000', field4.output("hey", {'hey': '03.0'}))
 
191
 
 
192
    def test_zero_fixed(self):
 
193
        field = fields.Fixed()
 
194
        self.assertEquals('0.00000', field.output('hey', {'hey': 0}))
 
195
 
 
196
    def test_infinite_fixed(self):
 
197
        field = fields.Fixed()
 
198
        self.assertRaises(MarshallingException, lambda: field.output("hey", {'hey': '+inf'}))
 
199
        self.assertRaises(MarshallingException, lambda: field.output("hey", {'hey': '-inf'}))
 
200
 
 
201
 
 
202
    def test_advanced_fixed(self):
 
203
        field = fields.Fixed()
 
204
        self.assertRaises(MarshallingException, lambda: field.output("hey", {'hey': 'NaN'}))
 
205
 
 
206
 
 
207
    def test_string(self):
 
208
        field = fields.String()
 
209
        self.assertEquals("3", field.output("hey", Foo()))
 
210
 
 
211
 
 
212
    def test_string_no_value(self):
 
213
        field = fields.String()
 
214
        self.assertEquals(None, field.output("bar", Foo()))
 
215
 
 
216
 
 
217
    def test_string_none(self):
 
218
        field = fields.String()
 
219
        self.assertEquals(None, field.output("empty", {'empty': None}))
 
220
 
 
221
 
 
222
    def test_date_field_with_offset(self):
 
223
        obj = {"bar": datetime(2011, 8, 22, 20, 58, 45)}
 
224
        field = fields.DateTime()
 
225
        self.assertEquals("Mon, 22 Aug 2011 20:58:45 -0000", field.output("bar", obj))
 
226
 
 
227
 
 
228
    def test_to_dict(self):
 
229
        obj = {"hey": 3}
 
230
        self.assertEquals(obj, fields.to_marshallable_type(obj))
 
231
 
 
232
 
 
233
    def test_to_dict_obj(self):
 
234
        obj = {"hey": 3}
 
235
        self.assertEquals(obj, fields.to_marshallable_type(Foo()))
 
236
 
 
237
    def test_to_dict_custom_marshal(self):
 
238
        obj = {"hey": 3}
 
239
        self.assertEquals(obj, fields.to_marshallable_type(Bar()))
 
240
 
 
241
    def test_get_value(self):
 
242
        self.assertEquals(3, fields.get_value("hey", {"hey": 3}))
 
243
 
 
244
 
 
245
    def test_get_value_no_value(self):
 
246
        self.assertEquals(None, fields.get_value("foo", {"hey": 3}))
 
247
 
 
248
 
 
249
    def test_get_value_obj(self):
 
250
        self.assertEquals(3, fields.get_value("hey", Foo()))
 
251
 
 
252
if __name__ == '__main__':
 
253
    unittest.main()