-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First prototype for integration tests
Working on #32
- Loading branch information
1 parent
40c4912
commit 12e50ec
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pyparsing==2.0.3 | |
python2-pythondialog==3.0.1 | ||
configobj==5.0.6 | ||
six==1.10.0 | ||
mock==2.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from contextlib import contextmanager | ||
from functools import partial | ||
|
||
import mock | ||
import dialog | ||
|
||
PATCH_FUNCTIONS = ('yesno',) | ||
|
||
class UserBehavior(object): | ||
def __init__(self): | ||
self._handlers = [] | ||
|
||
def expect(self, | ||
function_name, | ||
return_value, | ||
callback=None): | ||
def handler(actual_function_name, *args, **kwargs): | ||
assert actual_function_name == function_name,\ | ||
"Expected call of {}, got {}!".format(function_name, actual_function_name) | ||
if callback is not None: | ||
callback(*args, **kwargs) | ||
return return_value | ||
self._handlers.append(handler) | ||
|
||
def expect_yesno(self, answer): | ||
return_value = dialog.Dialog.OK if answer else dialog.Dialog.CANCEL | ||
self.expect('yesno', return_value) | ||
|
||
def _side_effect_handler(self, function_name, *args, **kwargs): | ||
assert self._handlers, "Got call of {}, but no handlers are defined!".format(function_name) | ||
handler = self._handlers.pop() | ||
return handler(function_name, *args, **kwargs) | ||
|
||
def _start_mock(self, function_name): | ||
target = 'dialog.Dialog.{}'.format(function_name) | ||
patcher = mock.patch(target, side_effect=partial(self._side_effect_handler, function_name)) | ||
patcher.start() | ||
return patcher | ||
|
||
@contextmanager | ||
def simulate(self): | ||
patchers = [self._start_mock(function) for function in PATCH_FUNCTIONS] | ||
try: | ||
yield | ||
finally: | ||
for patcher in patchers: | ||
patcher.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import unittest | ||
from dialog import Dialog | ||
|
||
import mock | ||
|
||
from mockdialog import UserBehavior | ||
|
||
class TestMenuPrivacyIDEA(unittest.TestCase): | ||
def test_invocation(self): | ||
user = UserBehavior() | ||
user.expect('yesno', Dialog.CANCEL) | ||
with user.simulate(): | ||
d = Dialog() | ||
code = d.yesno('hello!', width=70) | ||
print 'code is ', code |