def test_open_event(self): module_pb = ProgressBar() module_pb.emit_init = Mock() self.ws.bind(module_pb) ws_connection = yield self.ws_connect('/ws/module/pb') module_pb.emit_init.assert_called_with() self.close(ws_connection)
def test_initialize(self, add_handler): ws = WebSocket('pb') module_pb = ProgressBar() module_pb.initialize = Mock() self.assertEqual(ws.modules, []) ws.bind(module_pb) self.assertEqual(ws.modules, [module_pb]) module_pb.initialize.assert_called_with()
def test_emit_init_indeterminate(self, add_handler): ws = WebSocket('pb') module_pb = ProgressBar(indeterminate=True) ws.bind(module_pb) ws.emit = Mock() module_pb.emit_init() call1 = call('module_progressbar_before_init', None) call2 = call('module_progressbar_init', {'indeterminate': True}) call3 = call('module_progressbar_after_init', None) ws.emit.assert_has_calls([call1, call2, call3])
def test_bind_module(self, add_handler): ws = WebSocket('path') module = ProgressBar('progress') module.initialize = Mock() self.assertListEqual(ws.modules, []) self.assertIsNone(module._websocket) module.initialize.assert_not_called() ws.bind(module) self.assertListEqual(ws.modules, [module]) self.assertEqual(module._websocket, ws) module.initialize.assert_called_with()
def test_reset(self): module_pb = ProgressBar() module_pb.emit_update = Mock() module_pb.emit_done = Mock() self.assertEqual(module_pb.min, 0) self.assertEqual(module_pb.current, module_pb.min) module_pb.tick() self.assertEqual(module_pb.current, 1) module_pb.tick() self.assertEqual(module_pb.current, 2) module_pb.reset() self.assertEqual(module_pb.current, module_pb.min)
def test_construct_default_values(self): module_pb = ProgressBar() self.assertEqual(module_pb.name, 'module_progressbar') self.assertEqual(module_pb.min, 0) self.assertEqual(module_pb.max, 100) self.assertEqual(module_pb.current, module_pb.min) self.assertFalse(module_pb.indeterminate)
def test_emit_init_determinate(self, add_handler): ws = WebSocket('pb') module_pb = ProgressBar() ws.bind(module_pb) ws.emit = Mock() module_pb.emit_init() call1 = call('module_progressbar_before_init', None) call2 = call('module_progressbar_init', { 'indeterminate': False, 'min': 0, 'max': 100, 'current': 0 }) call3 = call('module_progressbar_after_init', None) ws.emit.assert_has_calls([call1, call2, call3])
def test_emit_update_indeterminate(self, add_handler): ws = WebSocket('pb') module_pb = ProgressBar(indeterminate=True) ws.bind(module_pb) ws.emit = Mock() # 1st try, without label module_pb.tick() call1 = call('module_progressbar_before_update', None) call2 = call('module_progressbar_update', {}) call3 = call('module_progressbar_after_update', None) ws.emit.assert_has_calls([call1, call2, call3]) ws.emit.reset_mock() # 2nd and last try, with label module_pb.tick('My label') call1 = call('module_progressbar_before_update', None) call2 = call('module_progressbar_update', {'label': 'My label'}) call3 = call('module_progressbar_after_update', None) ws.emit.assert_has_calls([call1, call2, call3])
def test_open_event(self): @self.ws.on def open(): self.ws.emit('opened') module_pb = ProgressBar() module_pb.emit_init = Mock() self.ws.bind(module_pb) ws_connection = yield self.ws_connect('/ws/module/pb') # 1st: clasical websocket on open event response = yield ws_connection.read_message() response = json_decode(response) self.assertDictEqual(response, { 'event': 'opened', 'data': {} }) # 2nd: ProgressBar module on open event module_pb.emit_init.assert_called_with() self.close(ws_connection)
def test_emit_done(self, add_handler): ws = WebSocket('pb') module_pb = ProgressBar(min=0, max=2) ws.bind(module_pb) ws.emit = Mock() module_pb.emit_init = Mock() module_pb.emit_update = Mock() # 1st try module_pb.tick() self.assertEqual(module_pb.current, 1) ws.emit.assert_not_called() # 2nd and last try module_pb.tick() self.assertEqual(module_pb.current, 2) self.assertEqual(module_pb.current, module_pb.max) ws.emit.assert_called_with('module_progressbar_done', None)
# coding: utf-8 """ Example of module « Progress Bar » by using `tornado_websocket.modules.ProgressBar` to handle communications, and Django's TemplateView for rendering. """ from django.views.generic import TemplateView from tornado import gen from tornado_websockets.modules import ProgressBar from tornado_websockets.websocket import WebSocket ws = WebSocket('module_progressbar') progressbar = ProgressBar('foo', min=0, max=100) ws.bind(progressbar) @progressbar.on def reset(): progressbar.reset() @progressbar.on @gen.engine # Make this function asynchronous for Tornado's IOLoop def start(): for value in range(0, progressbar.max): yield gen.sleep(.1) # like time.sleep(), but asynchronous progressbar.tick(label="[%d/%d] Tâche %d terminée" % (progress_bar.current + 1, progress_bar.max, value))
def test_tick_and_is_done(self): module_pb = ProgressBar(min=0, max=3) module_pb.emit_update = Mock() module_pb.emit_done = Mock() self.assertEqual(module_pb.current, 0) self.assertFalse(module_pb.is_done()) # 1st tick module_pb.tick() self.assertEqual(module_pb.current, 1) module_pb.emit_update.assert_called_with(None) self.assertFalse(module_pb.is_done()) module_pb.emit_done.assert_not_called() # 2nd tick module_pb.tick('My label') self.assertEqual(module_pb.current, 2) module_pb.emit_update.assert_called_with('My label') self.assertFalse(module_pb.is_done()) module_pb.emit_done.assert_not_called() # 3rd tick module_pb.tick('My other label') self.assertEqual(module_pb.current, 3) module_pb.emit_update.assert_called_with('My other label') self.assertTrue(module_pb.is_done()) module_pb.emit_done.assert_called_with()
def test_construct_should_be_indeterminate(self): module_pb = ProgressBar(indeterminate=True) self.assertTrue(module_pb.indeterminate)
def test_construct_min_gt_max(self): with self.assertRaisesRegexp( ValueError, '« min » .* not be greater or equal .* « max »'): ProgressBar(min=1, max=0)
def test_is_done(self): module_pb = ProgressBar(min=0, max=100) self.assertFalse(module_pb.is_done()) module_pb = ProgressBar(indeterminate=False) self.assertFalse(module_pb.is_done()) module_pb = ProgressBar(min=0, max=1) module_pb.emit_update = Mock() module_pb.emit_done = Mock() module_pb.tick() self.assertEqual(module_pb.current, 1) self.assertTrue(module_pb.is_done())