def test_timed(self): def too_slow(): time.sleep(.3) too_slow = timed(.2)(too_slow) def quick(): time.sleep(.1) quick = timed(.2)(quick) def check_result(): return 42 check_result = timed(.2)(check_result) assert 42 == check_result() quick() try: too_slow() except TimeExpired: pass else: self.fail("Slow test did not throw TimeExpired")
def test_decorator_func_sorting(self): from nose.tools import raises, timed, with_setup from nose.util import func_lineno def test1(): pass def test2(): pass def test3(): pass def foo(): pass test1_pos = func_lineno(test1) test2_pos = func_lineno(test2) test3_pos = func_lineno(test3) test1 = raises(TypeError)(test1) test2 = timed(1.0)(test2) test3 = with_setup(foo)(test3) self.assertEqual(func_lineno(test1), test1_pos) self.assertEqual(func_lineno(test2), test2_pos) self.assertEqual(func_lineno(test3), test3_pos)
def test_timed(self): def too_slow(): time.sleep(.3) too_slow = timed(.2)(too_slow) def quick(): time.sleep(.1) quick = timed(.2)(quick) quick() try: too_slow() except TimeExpired: pass else: self.fail("Slow test did not throw TimeExpired")
def test_Handler_redirect(): app = natrix.Application([ ("/0", lambda x: x.redirect("/2")), ("/1", lambda x: x.redirect("http://github.com/", delay=0.2)), ("/2", lambda x: x.redirect("http://github.com/", code=301)), ("/3", lambda x: x.redirect("http://github.com/", permanent=True)), ("/4", lambda x: x.redirect("http://github.com/юникод")), ("/5", lambda x: x.redirect(u"http://github.com/юникод")), ("/6-юникод", lambda x: x.redirect("ok")), ("/7#post", lambda x: x.redirect()), ]) testapp = webtest.TestApp(app) # 0. x.redirect(/2) r = testapp.get("/0") validate_response(r, status_int=302, location="/2") # 1. x.redirect(external, delay=0.2) r = timed(0.3)(lambda: testapp.get("/1"))() validate_response(r, status_int=302, location="http://github.com/") # 2. x.redirect(external, code=301) r = testapp.get("/2") validate_response(r, status_int=301, location="http://github.com/") # 3. x.redirect(external, permanent=True) r = testapp.get("/3") validate_response(r, status_int=301, location="http://github.com/") # 4. x.redirect(external/юникод) r = testapp.get("/4") validate_response(r, status_int=302, location="http://github.com/юникод") # 5. x.redirect(external/юникод) r = testapp.get("/5") validate_response(r, status_int=302, location="http://github.com/юникод") # 6. x.redirect(any) r = testapp.get("/6-юникод") validate_response(r, status_int=302, location="ok") # 7. x.redirect() r = testapp.post("/7") validate_response(r, status_int=302, location="/7")
def test_nested_decorators(self): from nose.tools import raises, timed, with_setup def test(): pass def foo(): pass test = with_setup(foo, foo)(test) test = timed(1.0)(test) test = raises(TypeError)(test) assert test.setup == foo assert test.teardown == foo
def test_that_fails(): timed(.30)
from pulsar.web.framework import file_response from unittest import TestCase, skip try: from nose.tools import nottest from nose.tools import timed except ImportError: def nottest(x): return x def timed(x): return lambda x: x INTEGRATION_MAXIMUM_TEST_TIME = 15 integration_test = timed(INTEGRATION_MAXIMUM_TEST_TIME) TEST_DIR = dirname(__file__) ROOT_DIR = join(TEST_DIR, pardir) class TempDirectoryTestCase(TestCase): def setUp(self): self.temp_directory = mkdtemp() def tearDown(self): rmtree(self.temp_directory) def get_test_toolbox():
def timed_class(cls): """Class decorator version of `nose.tools.timed`""" for key in cls.__dict__: if key.startswith('test_'): setattr(cls, key, timed(2)(getattr(cls, key))) return cls
class GraphTest(unittest.TestCase): def setUp(self): self.devices = ["localhost"] self.plugin = "snmp" self.data_source = "milliampere-0" self.time_from = 1380802176 self.time_to = 1380842640 self.width = 200 self.height = 100 self.graph = Graph(devices=self.devices, plugin=self.plugin, data_source=self.data_source, time_from=self.time_from, time_to=self.time_to, width=self.width, height=self.height) def test_init(self): self.assertTrue(self.graph) self.assertEqual(self.graph.devices, self.devices) self.assertEqual(self.graph.plugin, self.plugin) self.assertEqual(self.graph.data_source, self.data_source) self.assertEqual(self.graph.time_from, self.time_from) self.assertEqual(self.graph.time_to, self.time_to) self.assertEqual(self.graph.width, self.width) self.assertEqual(self.graph.height, self.height) self.assertEqual(str(self.graph.out_file.__class__), "tempfile._TemporaryFileWrapper") def test_generate_defs(self): self.graph._generate_defs() self.assertTrue(self.graph._defs) self.assertTrue(self.graph._def_map) self.assertTrue(self.graph._metadata) self.assertEqual(type(self.graph._metadata['max0']), dict, "metadata has key max0 with value dict of metadata") self.assertEqual(self.graph._def_map['def_mA_average_0'], "average0") def test_generate_defs_fails_with_unknown_host(self): self.graph.devices[0] = "unfug" self.graph._generate_defs() self.assertFalse(self.graph._defs) def test_generate_cdefs(self): self.graph._generate_defs() self.graph._generate_cdefs() self.assertTrue(self.graph._cdef_map) self.assertTrue(self.graph._cdefs) self.assertEqual(type(self.graph._cdefs[0]), pyrrd.graph.CalculationDefinition, "cdefs stores CalculationDefinition objects") self.assertEqual( self.graph._cdef_values['cdef_mA_max'][0], "def_mA_max_0", "cdef_values stores def names to calculate to get cdef") self.assertEqual(self.graph._cdef_map['cdef_mA_max'], "max0", "cdef_map stores metadata keys") self.graph.mode = MULTI_LINE self.graph._generate_cdefs() self.assertEqual( self.graph._cdef_values['cdef_mA_max_0'][0], "def_mA_max_0", "cdef_values stores def names to calculate to get cdef") self.assertEqual(self.graph._cdef_map['cdef_mA_max_0'], "max0", "cdef_map stores metadata keys") def test_generate_cdefs_fails_without_defs(self): self.graph._generate_cdefs() self.assertFalse(self.graph._cdef_map) self.assertFalse(self.graph._cdefs) def test_generate_lines(self): self.graph._generate_defs() self.graph._generate_cdefs() self.graph._generate_lines() self.assertEqual(len(self.graph._lines), 1) self.assertEqual(type(self.graph._lines[0]), pyrrd.graph.Line, "lines stores Line objects") def test_generate_multi_lines(self): self.graph.mode = MULTI_LINE self.graph.devices.append('testhost') self.graph._generate_defs() self.graph._generate_cdefs() self.graph._generate_lines() print self.graph._lines self.assertEqual(len(self.graph._lines), 2) def test_generate_lines_fails_without_defs_and_cdefs(self): self.graph._generate_lines() self.assertFalse(self.graph._lines) def test_generate_areas(self): self.graph._generate_defs() self.graph._generate_cdefs() self.graph._generate_areas() self.assertEqual(len(self.graph._areas), 2) self.assertEqual(type(self.graph._areas['min'][0]), pyrrd.graph.Area, "areas stores Area objects") def test_generate_areas_fails_without_defs_and_cdefs(self): self.graph._generate_areas() self.assertFalse(self.graph._areas) def test_generate_table(self): self.graph._generate_defs() self.graph._generate_cdefs() self.graph._generate_table() self.assertEqual(type(self.graph._vdefs[0]), pyrrd.graph.VariableDefinition, "vdefs stores VariableDefinition objects") self.assertEqual(type(self.graph._gprints[0]), pyrrd.graph.GraphPrint, "gprints stores gprint objects") def test_generate_table_fails_without_defs_and_cdefs(self): self.graph._generate_table() self.assertFalse(self.graph._vdefs) self.assertFalse(self.graph._gprints) def test_generate_rpn(self): self.assertEqual(self.graph._generate_rpn("C", "test_def"), "test_def,10,/,273.15,-,1,/", "automatically convert C data sources") self.assertEqual(self.graph._generate_rpn("unfug", "test_def"), "test_def,0,+", "dont convert unknown data source") def test_generate_title(self): self.graph._generate_title() self.assertEqual( self.graph.title, "test_translation_of_data_sources_at_a_specific_device") self.graph.devices = ["localhost, testhost"] self.graph._generate_title() self.assertEqual(self.graph.title, "milliampere_Port_1", "title for more than one device") def test_generate_Y_Axis_Label(self): self.graph._generate_y_axis_label() self.assertEqual(self.graph.y_label, "mA", "y label is mA") def test_get_label_for_data_source(self): self.assertEqual(rrdscout.Graph.get_label_for_data_source("Total_W"), "W", "test with Total_W") self.assertEqual(rrdscout.Graph.get_label_for_data_source(""), "") self.assertEqual(rrdscout.Graph.get_label_for_data_source("ETHZ"), "ETHZ", "unconvertable input") def test_generate_comment(self): self.assertEqual(rrdscout.Graph.generate_comment(self.graph.devices), "localhost") self.assertEqual(rrdscout.Graph.generate_comment("test me"), "test me") def test_generate_attachment_name(self): self.graph.generate_attachment_name("jpg") self.assertEqual(self.graph.attachment_name, "localhost_snmp_milliampere-0.jpg") self.graph.devices = ["localhost", "testhost"] self.graph.generate_attachment_name() self.assertEqual(self.graph.attachment_name, "snmp_milliampere-0.png") @not_on_travis(timed(0.1)) def test_generate_graph(self): self.graph.generate_graph() self.assertFalse(self.graph.generation_failed()) @not_on_travis(timed(0.1)) def test_generate_multi_line_graph(self): self.graph.devices = ["localhost", "testhost"] self.graph.mode = MULTI_LINE self.graph.generate_graph() self.assertFalse(self.graph.generation_failed()) @not_on_travis(timed(0.1)) def test_generate_aggregated_graph(self): self.graph.devices = ["localhost", "testhost"] self.graph.generate_graph() self.assertFalse(self.graph.generation_failed())