1
from decimal import Decimal
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
16
def __marshallable__(self):
19
def check_field(expected, field, value):
20
assert_equals(expected, field.output('a', {'a': value}))
25
(str(-3.13), "-3.13"),
28
for value, expected in values:
29
yield check_field, expected, fields.Arbitrary(), value
37
("false", True), # These are different from php
38
("0", True), # Will this be a problem?
40
for value, expected in values:
41
yield check_field, expected, fields.Boolean(), value
44
class FieldsTestCase(unittest.TestCase):
46
def test_decimal_trash(self):
47
self.assertRaises(MarshallingException, lambda: fields.Float().output('a', {'a': 'Foo'}))
49
def test_basic_dictionary(self):
51
field = fields.String()
52
self.assertEquals(field.output("foo", obj), "3")
55
def test_no_attribute(self):
57
field = fields.String()
58
self.assertEquals(field.output("foo", obj), None)
61
def test_date_field_invalid(self):
63
field = fields.DateTime()
64
self.assertRaises(MarshallingException, lambda: field.output("bar", obj))
67
def test_attribute(self):
69
field = fields.String(attribute="bar")
70
self.assertEquals(field.output("foo", obj), "3")
73
def test_formatting_field_none(self):
75
field = fields.FormattedString("/foo/{0[account_sid]}/{0[sid]}/")
76
self.assertRaises(MarshallingException, lambda: field.output("foo", obj))
79
def test_formatting_field_tuple(self):
81
field = fields.FormattedString("/foo/{0[account_sid]}/{0[sid]}/")
82
self.assertRaises(MarshallingException, lambda: field.output("foo", obj))
85
def test_formatting_field_dict(self):
90
field = fields.FormattedString("/foo/{account_sid}/{sid}/")
91
self.assertEquals(field.output("foo", obj), "/foo/4/3/")
94
def test_formatting_field(self):
98
field = fields.FormattedString("/foo/{account_sid}/{sid}/")
99
self.assertEquals(field.output("foo", obj), "/foo/4/3/")
102
def test_basic_field(self):
106
self.assertEquals(field.output("foo", obj), 3)
109
def test_raw_field(self):
113
self.assertEquals(field.output("foo", obj), 3)
116
def test_formatted_string_invalid_obj(self):
117
field = fields.FormattedString("{hey}")
118
self.assertRaises(MarshallingException, lambda: field.output("hey", None))
121
def test_formatted_string(self):
122
field = fields.FormattedString("{hey}")
123
self.assertEquals("3", field.output("hey", Foo()))
126
def test_string_with_attribute(self):
127
field = fields.String(attribute="hey")
128
self.assertEquals("3", field.output("foo", Foo()))
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")
136
with app.test_request_context("/"):
137
self.assertRaises(MarshallingException, lambda: field.output("hey", None))
141
app = Flask(__name__)
142
app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
143
field = fields.Url("foobar")
145
with app.test_request_context("/"):
146
self.assertEquals("/3", field.output("hey", Foo()))
150
field = fields.Integer()
151
self.assertEquals(3, field.output("hey", {'hey': 3}))
154
def test_int_default(self):
155
field = fields.Integer(default=1)
156
self.assertEquals(1, field.output("hey", {'hey': None}))
159
def test_no_int(self):
160
field = fields.Integer()
161
self.assertEquals(0, field.output("hey", {'hey': None}))
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'}))
167
def test_float(self):
168
field = fields.Float()
169
self.assertEquals('3.0', field.output("hey", {'hey': 3.0}))
171
def test_float_decode_error(self):
172
field = fields.Float()
173
self.assertRaises(MarshallingException, lambda: field.output("hey", {'hey': 'Explode!'}))
175
PI_STR = u'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861'
178
def test_arbitrary(self):
179
field = fields.Arbitrary()
180
self.assertEquals(self.PI_STR, field.output("hey", {'hey': self.PI}))
182
def test_fixed(self):
183
field5 = fields.Fixed(5)
184
field4 = fields.Fixed(4)
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'}))
192
def test_zero_fixed(self):
193
field = fields.Fixed()
194
self.assertEquals('0.00000', field.output('hey', {'hey': 0}))
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'}))
202
def test_advanced_fixed(self):
203
field = fields.Fixed()
204
self.assertRaises(MarshallingException, lambda: field.output("hey", {'hey': 'NaN'}))
207
def test_string(self):
208
field = fields.String()
209
self.assertEquals("3", field.output("hey", Foo()))
212
def test_string_no_value(self):
213
field = fields.String()
214
self.assertEquals(None, field.output("bar", Foo()))
217
def test_string_none(self):
218
field = fields.String()
219
self.assertEquals(None, field.output("empty", {'empty': None}))
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))
228
def test_to_dict(self):
230
self.assertEquals(obj, fields.to_marshallable_type(obj))
233
def test_to_dict_obj(self):
235
self.assertEquals(obj, fields.to_marshallable_type(Foo()))
237
def test_to_dict_custom_marshal(self):
239
self.assertEquals(obj, fields.to_marshallable_type(Bar()))
241
def test_get_value(self):
242
self.assertEquals(3, fields.get_value("hey", {"hey": 3}))
245
def test_get_value_no_value(self):
246
self.assertEquals(None, fields.get_value("foo", {"hey": 3}))
249
def test_get_value_obj(self):
250
self.assertEquals(3, fields.get_value("hey", Foo()))
252
if __name__ == '__main__':