Skip to content

Commit

Permalink
First prototype for integration tests
Browse files Browse the repository at this point in the history
Working on #32
  • Loading branch information
Friedrich Weber authored and cornelinux committed Jan 9, 2018
1 parent 40c4912 commit 12e50ec
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 47 additions & 0 deletions test/mockdialog.py
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()
15 changes: 15 additions & 0 deletions test/test_menu_privacyidea.py
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

0 comments on commit 12e50ec

Please sign in to comment.