Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
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
edx-platform
Commits
1422db5c
Commit
1422db5c
authored
Jun 09, 2015
by
Calen Pennington
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow check_sum_of_calls to measure methods as well as pure functions
parent
5962c4eb
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
1 deletions
+19
-1
common/lib/xmodule/xmodule/modulestore/tests/factories.py
+19
-1
No files found.
common/lib/xmodule/xmodule/modulestore/tests/factories.py
View file @
1422db5c
"""
Factories for use in tests of XBlocks.
"""
import
inspect
import
pprint
import
threading
from
uuid
import
uuid4
...
...
@@ -321,12 +326,25 @@ def check_sum_of_calls(object_, methods, maximum_calls, minimum_calls=1):
Instruments the given methods on the given object to verify that the total sum of calls made to the
methods falls between minumum_calls and maximum_calls.
"""
mocks
=
{
method
:
Mock
(
wraps
=
getattr
(
object_
,
method
))
for
method
in
methods
}
with
patch
.
multiple
(
object_
,
**
mocks
):
if
inspect
.
isclass
(
object_
):
# If the object that we're intercepting methods on is a class, rather than a module,
# then we need to set the method to a real function, so that self gets passed to it,
# and then explicitly pass that self into the call to the mock
# pylint: disable=unnecessary-lambda,cell-var-from-loop
mock_kwargs
=
{
method
:
lambda
self
,
*
args
,
**
kwargs
:
mocks
[
method
](
self
,
*
args
,
**
kwargs
)
for
method
in
methods
}
else
:
mock_kwargs
=
mocks
with
patch
.
multiple
(
object_
,
**
mock_kwargs
):
yield
call_count
=
sum
(
mock
.
call_count
for
mock
in
mocks
.
values
())
...
...
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