Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
senpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
6
Issues
6
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
1
Merge Requests
1
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
senpy
senpy
Commits
dcaaa591
Commit
dcaaa591
authored
Jan 18, 2018
by
J. Fernando Sánchez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve requests patching
parent
15ab5f4c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
3 deletions
+41
-3
senpy/test.py
senpy/test.py
+9
-3
tests/test_test.py
tests/test_test.py
+32
-0
No files found.
senpy/test.py
View file @
dcaaa591
...
...
@@ -3,6 +3,8 @@ try:
except
ImportError
:
from
mock
import
patch
,
MagicMock
from
past.builtins
import
basestring
import
json
from
contextlib
import
contextmanager
...
...
@@ -15,13 +17,17 @@ def patch_requests(value, code=200):
success
=
MagicMock
()
if
isinstance
(
value
,
BaseModel
):
value
=
value
.
jsonld
()
data
=
json
.
dumps
(
value
)
if
not
isinstance
(
value
,
basestring
):
data
=
json
.
dumps
(
value
)
else
:
data
=
value
success
.
json
.
return_value
=
value
success
.
data
.
return_value
=
data
success
.
status_code
=
code
success
.
content
=
data
success
.
text
=
data
success
.
content
=
json
.
dumps
(
value
)
method_mocker
=
MagicMock
()
method_mocker
.
return_value
=
success
with
patch
.
multiple
(
'requests'
,
request
=
method_mocker
,
...
...
tests/test_test.py
0 → 100644
View file @
dcaaa591
from
unittest
import
TestCase
import
requests
import
json
from
senpy.test
import
patch_requests
from
senpy.models
import
Results
class
TestTest
(
TestCase
):
def
test_patch_text
(
self
):
with
patch_requests
(
'hello'
):
r
=
requests
.
get
(
'http://example.com'
)
assert
r
.
text
==
'hello'
assert
r
.
content
==
'hello'
def
test_patch_json
(
self
):
r
=
Results
()
with
patch_requests
(
r
):
res
=
requests
.
get
(
'http://example.com'
)
assert
res
.
content
==
json
.
dumps
(
r
.
jsonld
())
js
=
res
.
json
()
assert
js
assert
js
[
'@type'
]
==
r
[
'@type'
]
def
test_patch_dict
(
self
):
r
=
{
'nothing'
:
'new'
}
with
patch_requests
(
r
):
res
=
requests
.
get
(
'http://example.com'
)
assert
res
.
content
==
json
.
dumps
(
r
)
js
=
res
.
json
()
assert
js
assert
js
[
'nothing'
]
==
'new'
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