forked from spec-first/connexion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flask_encoder.py
38 lines (24 loc) · 943 Bytes
/
test_flask_encoder.py
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
import datetime
import json
import math
from decimal import Decimal
from connexion.apps.flask_app import FlaskJSONEncoder
def test_json_encoder():
s = json.dumps({1: 2}, cls=FlaskJSONEncoder)
assert '{"1": 2}' == s
s = json.dumps(datetime.date.today(), cls=FlaskJSONEncoder)
assert len(s) == 12
s = json.dumps(datetime.datetime.utcnow(), cls=FlaskJSONEncoder)
assert s.endswith('Z"')
s = json.dumps(Decimal(1.01), cls=FlaskJSONEncoder)
assert s == '1.01'
s = json.dumps(math.expm1(1e-10), cls=FlaskJSONEncoder)
assert s == '1.00000000005e-10'
def test_json_encoder_datetime_with_timezone():
class DummyTimezone(datetime.tzinfo):
def utcoffset(self, dt):
return datetime.timedelta(0)
def dst(self, dt):
return datetime.timedelta(0)
s = json.dumps(datetime.datetime.now(DummyTimezone()), cls=FlaskJSONEncoder)
assert s.endswith('+00:00"')