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
cd469f61
Unverified
Commit
cd469f61
authored
Feb 15, 2017
by
Mariusz Felisiak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Increased utils/encoders.py test coverage.
parent
eb14f478
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
17 deletions
+18
-17
tests/test_encoders.py
+18
-17
No files found.
tests/test_encoders.py
View file @
cd469f61
from
datetime
import
date
,
datetime
,
timedelta
,
tzinfo
from
datetime
import
date
,
datetime
,
timedelta
from
decimal
import
Decimal
from
uuid
import
uuid4
import
pytest
from
django.test
import
TestCase
from
django.utils.timezone
import
utc
from
rest_framework.compat
import
coreapi
from
rest_framework.utils.encoders
import
JSONEncoder
class
MockList
(
object
):
def
tolist
(
self
):
return
[
1
,
2
,
3
]
class
JSONEncoderTests
(
TestCase
):
"""
Tests the JSONEncoder method
...
...
@@ -22,7 +28,7 @@ class JSONEncoderTests(TestCase):
Tests encoding a decimal
"""
d
=
Decimal
(
3.14
)
assert
d
==
float
(
d
)
assert
self
.
encoder
.
default
(
d
)
==
float
(
d
)
def
test_encode_datetime
(
self
):
"""
...
...
@@ -30,6 +36,8 @@ class JSONEncoderTests(TestCase):
"""
current_time
=
datetime
.
now
()
assert
self
.
encoder
.
default
(
current_time
)
==
current_time
.
isoformat
()
current_time_utc
=
current_time
.
replace
(
tzinfo
=
utc
)
assert
self
.
encoder
.
default
(
current_time_utc
)
==
current_time
.
isoformat
()
+
'Z'
def
test_encode_time
(
self
):
"""
...
...
@@ -42,22 +50,8 @@ class JSONEncoderTests(TestCase):
"""
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
()
)
current_time
=
current_time
.
replace
(
tzinfo
=
utc
)
with
pytest
.
raises
(
ValueError
):
self
.
encoder
.
default
(
current_time
)
...
...
@@ -91,3 +85,10 @@ class JSONEncoderTests(TestCase):
with
pytest
.
raises
(
RuntimeError
):
self
.
encoder
.
default
(
coreapi
.
Error
())
def
test_encode_object_with_tolist
(
self
):
"""
Tests encoding a object with tolist method
"""
foo
=
MockList
()
assert
self
.
encoder
.
default
(
foo
)
==
[
1
,
2
,
3
]
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