def test_enable_copy_args(self): config = rv.get_configuration() class M(object): def m(self): self.x = 123 @rv.monitor(m=M.m) @rv.spec(when=rv.POST) def spec(event): if hasattr(event.fn.m.inputs[0], 'x'): raise ValueError("buffy") self.assertEquals(event.fn.m.outputs[0].x, 123) a = M() a.m() b = M() rv.configure(enable_copy_args=False) with self.assertRaises(ValueError) as e: b.m() self.assertEquals(e.exception.message, "buffy") rv.configure(**config)
def test_configuration(self): config = rv.get_configuration() self.assertEquals(rv.get_configuration()['error_handler'], rv.DEFAULT_ERROR_HANDLER) self.assertEquals(rv.get_configuration()['enable_copy_args'], True) rv.configure(error_handler=1234, enable_copy_args=False) self.assertEquals(rv.get_configuration()['error_handler'], 1234) self.assertEquals(rv.get_configuration()['enable_copy_args'], False) rv.configure(**config) self.assertEquals(rv.get_configuration()['error_handler'], rv.DEFAULT_ERROR_HANDLER) self.assertEquals(rv.get_configuration()['enable_copy_args'], True)
def test_log(self): rv.configure(error_handler=rv.LoggingErrorHandler()) for i, t in enumerate(error_levels): class M(object): def m(self): pass @rv.monitor(m=M.m) @rv.spec(level=t) def spec(event): assert False, "of time" a = M() a.m() self.assertLog(t, "of time")
def test_raise(self): for level_i, level in enumerate(error_levels): rv.configure(error_handler=rv.RaiseExceptionErrorHandler(level=level)) for i, t in enumerate(error_levels): class M(object): def m(self): pass @rv.monitor(m=M.m) @rv.spec(level=t) def spec(event): assert False, "of time" a = M() if i < level_i: a.m() else: with self.assertRaises(AssertionError) as e: a.m() self.assertEquals(e.exception.message, 'of time')
def tearDown(self): super(TestSpecErrorLevels, self).tearDown() rv.configure(**self.old_rv_config)
def tearDown(self): super(TestRVWithLoggingErrorHandler, self).tearDown() rv.configure(**self.old_rv_config)
def setUp(self): super(TestRVWithLoggingErrorHandler, self).setUp() self.old_rv_config = rv.get_configuration() rv.configure(error_handler=rv.LoggingErrorHandler())
from pythonrv import rv class CustomErrorHandler(object): def handle(self, level, errors): if level == rv.CRITICAL: # raise only the first error raise errors[0] rv.configure(error_handler=CustomErrorHandler()) @rv.monitor(...) @rv.spec(level=rv.CRITICAL) def spec(event): assert False
from pythonrv import rv from django.core.handlers.base import BaseHandler # disable copying to work around bug in django rv.configure(enable_copy_args=False) # monitor the main request-processing method that # has access to both the request and the response @rv.monitor(bh=BaseHandler.get_response) @rv.spec(when=rv.POST) def ensure_auth(event): request = event.called_function.inputs[1] response = event.called_function.result if requires_auth(request, response): assert (request.user.is_authenticated(), "The current user is not authenticated") assert (request.user.is_active, "The current user is not active") # a helper function for when authentication is # required def requires_auth(req, res): # only ok responses need authentication if res.status_code != 200: return False # requests to /login does not require auth nor # does /appmedia, which is only css and images if (req.path.startswith("/login") or req.path.startswith("/appmedia")): return False return True
#need import of thread_safe to get rv it turns out import thread_safe_specs import threading import logging import time from pythonrv import rv rv.configure(error_handler=rv.LoggingErrorHandler()) logging.basicConfig(filename="log", level=logging.ERROR, filemode="w", format="%(asctime)s - %(levelname)s - %(message)s") from testmod import lock_constr, incr i = 0 i_lock = threading.Lock() def nsfw_test(): global i for x in range(10000): i = incr(i) def sfw_test(): #use global variable i global i i_lock.acquire() lock_constr() try:
#!/usr/bin/env python import sys import os sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) import fib_rvspecs import logging from pythonrv import rv rv.configure(error_handler=rv.LoggingErrorHandler()) logging.basicConfig(filename="log", level=logging.WARNING, filemode="w", format="%(asctime)s - %(levelname)s - %(message)s") from fib import fib def main(): print "Fibonacci numbers:" for i in range(1, 20): print "%d -> %d" %(i, fib(i)) if __name__ == '__main__': main()