Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
4ff9e96b
Commit
4ff9e96b
authored
Oct 05, 2016
by
Alex Kahan
Committed by
Tom Christie
Oct 05, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding tests to encoder.py (#4536)
parent
915ac22a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
0 deletions
+81
-0
tests/test_encoders.py
+81
-0
No files found.
tests/test_encoders.py
0 → 100644
View file @
4ff9e96b
from
datetime
import
date
,
datetime
,
timedelta
,
tzinfo
from
decimal
import
Decimal
from
uuid
import
uuid4
from
django.test
import
TestCase
from
rest_framework.utils.encoders
import
JSONEncoder
class
JSONEncoderTests
(
TestCase
):
"""
Tests the JSONEncoder method
"""
def
setUp
(
self
):
self
.
encoder
=
JSONEncoder
()
def
test_encode_decimal
(
self
):
"""
Tests encoding a decimal
"""
d
=
Decimal
(
3.14
)
self
.
assertEqual
(
d
,
float
(
d
))
def
test_encode_datetime
(
self
):
"""
Tests encoding a datetime object
"""
current_time
=
datetime
.
now
()
self
.
assertEqual
(
self
.
encoder
.
default
(
current_time
),
current_time
.
isoformat
())
def
test_encode_time
(
self
):
"""
Tests encoding a timezone
"""
current_time
=
datetime
.
now
()
.
time
()
self
.
assertEqual
(
self
.
encoder
.
default
(
current_time
),
current_time
.
isoformat
()[:
12
])
def
test_encode_time_tz
(
self
):
"""
Tests encoding a timezone aware timestamp
"""
class
UTC
(
tzinfo
):
"""
Class extending tzinfo to mimic UTC time
"""
def
utcoffset
(
self
,
dt
):
return
timedelta
(
0
)
def
tzname
(
self
,
dt
):
return
"UTC"
def
dst
(
self
,
dt
):
return
timedelta
(
0
)
current_time
=
datetime
.
now
()
.
time
()
current_time
=
current_time
.
replace
(
tzinfo
=
UTC
())
with
self
.
assertRaises
(
ValueError
):
self
.
encoder
.
default
(
current_time
)
def
test_encode_date
(
self
):
"""
Tests encoding a date object
"""
current_date
=
date
.
today
()
self
.
assertEqual
(
self
.
encoder
.
default
(
current_date
),
current_date
.
isoformat
())
def
test_encode_timedelta
(
self
):
"""
Tests encoding a timedelta object
"""
delta
=
timedelta
(
hours
=
1
)
self
.
assertEqual
(
self
.
encoder
.
default
(
delta
),
str
(
delta
.
total_seconds
()))
def
test_encode_uuid
(
self
):
"""
Tests encoding a UUID object
"""
unique_id
=
uuid4
()
self
.
assertEqual
(
self
.
encoder
.
default
(
unique_id
),
str
(
unique_id
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment