def __init__(self, script_path, command_line): """Constructor. Parameters ---------- script_path : str Path of the Python file from which this app is generated. command_line : string Command line as input by the user """ basename = os.path.basename(script_path) self.script_path = os.path.abspath(script_path) self.script_folder = os.path.dirname(self.script_path) self.name = os.path.splitext(basename)[0] # The master queue contains all messages that comprise the report. # If the user chooses to share a saved version of the report, # we serialize the contents of the master queue. self._master_queue = ReportQueue() # The browser queue contains messages that haven't yet been # delivered to the browser. Periodically, the server flushes # this queue and delivers its contents to the browser. self._browser_queue = ReportQueue() self.generate_new_id() self.command_line = command_line
def test_add_rows_rerun(self): rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(INIT_MSG) # Simulate rerun for i in range(2): TEXT_DELTA_MSG1.metadata.delta_id = 0 rq.enqueue(TEXT_DELTA_MSG1) DF_DELTA_MSG.metadata.delta_id = 1 rq.enqueue(DF_DELTA_MSG) ADD_ROWS_MSG.metadata.delta_id = 1 rq.enqueue(ADD_ROWS_MSG) queue = rq.flush() self.assertEqual(len(queue), 3) self.assertTrue(queue[0].initialize.config.sharing_enabled) self.assertEqual(queue[1].metadata.delta_id, 0) self.assertEqual(queue[1].delta.new_element.text.body, "text1") self.assertEqual(queue[2].metadata.delta_id, 1) col0 = queue[2].delta.new_element.data_frame.data.cols[0].int64s.data col1 = queue[2].delta.new_element.data_frame.data.cols[1].int64s.data self.assertEqual(col0, [0, 1, 2, 3, 4, 5]) self.assertEqual(col1, [10, 11, 12, 13, 14, 15])
def __init__(self, script_name): """Initializes the ScriptRunner for the given script_name""" # DeltaGenerator deltas will be enqueued into self.report_queue. self.report_queue = ReportQueue() def enqueue_fn(msg): self.report_queue.enqueue(msg) self.maybe_handle_execution_control_request() self.script_request_queue = ScriptRequestQueue() script_path = os.path.join(os.path.dirname(__file__), "test_data", script_name) super(TestScriptRunner, self).__init__( session_id="test session id", report=Report(script_path, "test command line"), enqueue_forward_msg=enqueue_fn, client_state=ClientState(), request_queue=self.script_request_queue, ) # Accumulates uncaught exceptions thrown by our run thread. self.script_thread_exceptions = [] # Accumulates all ScriptRunnerEvents emitted by us. self.events = [] def record_event(event, **kwargs): self.events.append(event) self.on_event.connect(record_event, weak=False)
def test_enqueue_three(self): rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(NEW_REPORT_MSG) TEXT_DELTA_MSG1.metadata.delta_path[:] = make_delta_path( RootContainer.MAIN, (), 0 ) rq.enqueue(TEXT_DELTA_MSG1) TEXT_DELTA_MSG2.metadata.delta_path[:] = make_delta_path( RootContainer.MAIN, (), 1 ) rq.enqueue(TEXT_DELTA_MSG2) queue = rq.flush() self.assertEqual(len(queue), 3) self.assertTrue(queue[0].new_report.config.sharing_enabled) self.assertEqual( make_delta_path(RootContainer.MAIN, (), 0), queue[1].metadata.delta_path ) self.assertEqual(queue[1].delta.new_element.text.body, "text1") self.assertEqual( make_delta_path(RootContainer.MAIN, (), 1), queue[2].metadata.delta_path ) self.assertEqual(queue[2].delta.new_element.text.body, "text2")
def test_simple_add_rows(self): rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(NEW_REPORT_MSG) TEXT_DELTA_MSG1.metadata.delta_path[:] = make_delta_path( RootContainer.MAIN, (), 0 ) rq.enqueue(TEXT_DELTA_MSG1) DF_DELTA_MSG.metadata.delta_path[:] = make_delta_path(RootContainer.MAIN, (), 1) rq.enqueue(DF_DELTA_MSG) ADD_ROWS_MSG.metadata.delta_path[:] = make_delta_path(RootContainer.MAIN, (), 1) rq.enqueue(ADD_ROWS_MSG) queue = rq.flush() self.assertEqual(len(queue), 3) self.assertTrue(queue[0].new_report.config.sharing_enabled) self.assertEqual( make_delta_path(RootContainer.MAIN, (), 0), queue[1].metadata.delta_path ) self.assertEqual(queue[1].delta.new_element.text.body, "text1") self.assertEqual( make_delta_path(RootContainer.MAIN, (), 1), queue[2].metadata.delta_path ) col0 = queue[2].delta.new_element.data_frame.data.cols[0].int64s.data col1 = queue[2].delta.new_element.data_frame.data.cols[1].int64s.data self.assertEqual(col0, [0, 1, 2, 3, 4, 5]) self.assertEqual(col1, [10, 11, 12, 13, 14, 15])
def test_multiple_containers(self): """Deltas should only be coalesced if they're in the same container""" rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(INIT_MSG) def enqueue_deltas(container, path): # We deep-copy the protos because we mutate each one # multiple times. msg = copy.deepcopy(TEXT_DELTA_MSG1) msg.metadata.delta_id = 0 msg.metadata.parent_block.container = container msg.metadata.parent_block.path[:] = path rq.enqueue(msg) msg = copy.deepcopy(DF_DELTA_MSG) msg.metadata.delta_id = 1 msg.metadata.parent_block.container = container msg.metadata.parent_block.path[:] = path rq.enqueue(msg) msg = copy.deepcopy(ADD_ROWS_MSG) msg.metadata.delta_id = 1 msg.metadata.parent_block.container = container msg.metadata.parent_block.path[:] = path rq.enqueue(msg) enqueue_deltas(BlockPath.MAIN, []) enqueue_deltas(BlockPath.SIDEBAR, [0, 0, 1]) def assert_deltas(container, path, idx): self.assertEqual(0, queue[idx].metadata.delta_id) self.assertEqual(container, queue[idx].metadata.parent_block.container) self.assertEqual(path, queue[idx].metadata.parent_block.path) self.assertEqual("text1", queue[idx].delta.new_element.text.body) self.assertEqual(1, queue[idx + 1].metadata.delta_id) self.assertEqual(container, queue[idx + 1].metadata.parent_block.container) self.assertEqual(path, queue[idx + 1].metadata.parent_block.path) col0 = queue[ idx + 1].delta.new_element.data_frame.data.cols[0].int64s.data col1 = queue[ idx + 1].delta.new_element.data_frame.data.cols[1].int64s.data self.assertEqual([0, 1, 2, 3, 4, 5], col0) self.assertEqual([10, 11, 12, 13, 14, 15], col1) queue = rq.flush() self.assertEqual(5, len(queue)) self.assertTrue(queue[0].initialize.config.sharing_enabled) assert_deltas(BlockPath.MAIN, [], 1) assert_deltas(BlockPath.SIDEBAR, [0, 0, 1], 3)
def test_simple_enqueue(self): rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(INIT_MSG) self.assertFalse(rq.is_empty()) queue = rq.flush() self.assertTrue(rq.is_empty()) self.assertEqual(len(queue), 1) self.assertTrue(queue[0].initialize.config.sharing_enabled)
def test_simple_enqueue(self): rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(NEW_REPORT_MSG) self.assertFalse(rq.is_empty()) queue = rq.flush() self.assertTrue(rq.is_empty()) self.assertEqual(len(queue), 1) self.assertTrue(queue[0].new_report.config.sharing_enabled) self.assertTrue(queue[0].new_report.config.allow_run_on_save)
def test_enqueue_two(self): rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(INIT_MSG) TEXT_DELTA_MSG1.metadata.delta_id = 0 rq.enqueue(TEXT_DELTA_MSG1) queue = rq.flush() self.assertEqual(len(queue), 2) self.assertTrue(queue[0].initialize.config.sharing_enabled) self.assertEqual(queue[1].metadata.delta_id, 0) self.assertEqual(queue[1].delta.new_element.text.body, "text1")
def test_enqueue_two(self): rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(INIT_MSG) TEXT_DELTA_MSG1.metadata.delta_path[:] = make_delta_path( RootContainer.MAIN, (), 0) rq.enqueue(TEXT_DELTA_MSG1) queue = rq.flush() self.assertEqual(len(queue), 2) self.assertTrue(queue[0].initialize.config.sharing_enabled) self.assertEqual(make_delta_path(RootContainer.MAIN, (), 0), queue[1].metadata.delta_path) self.assertEqual(queue[1].delta.new_element.text.body, "text1")
def setUp(self, override_root=True): self.report_queue = ReportQueue() self.override_root = override_root self.orig_report_ctx = None if self.override_root: self.orig_report_ctx = get_report_ctx() add_report_ctx( threading.current_thread(), ReportContext( session_id="test session id", enqueue=self.report_queue.enqueue, query_string="", widgets=Widgets(), uploaded_file_mgr=UploadedFileManager(), ), )
def test_multiple_containers(self): """Deltas should only be coalesced if they're in the same container""" rq = ReportQueue() self.assertTrue(rq.is_empty()) rq.enqueue(NEW_REPORT_MSG) def enqueue_deltas(container: RootContainer, path: Tuple[int, ...]): # We deep-copy the protos because we mutate each one # multiple times. msg = copy.deepcopy(TEXT_DELTA_MSG1) msg.metadata.delta_path[:] = make_delta_path(container, path, 0) rq.enqueue(msg) msg = copy.deepcopy(DF_DELTA_MSG) msg.metadata.delta_path[:] = make_delta_path(container, path, 1) rq.enqueue(msg) msg = copy.deepcopy(ADD_ROWS_MSG) msg.metadata.delta_path[:] = make_delta_path(container, path, 1) rq.enqueue(msg) enqueue_deltas(RootContainer.MAIN, ()) enqueue_deltas(RootContainer.SIDEBAR, (0, 0, 1)) def assert_deltas(container: RootContainer, path: Tuple[int, ...], idx: int): self.assertEqual(make_delta_path(container, path, 0), queue[idx].metadata.delta_path) self.assertEqual("text1", queue[idx].delta.new_element.text.body) self.assertEqual(make_delta_path(container, path, 1), queue[idx + 1].metadata.delta_path) col0 = queue[ idx + 1].delta.new_element.data_frame.data.cols[0].int64s.data col1 = queue[ idx + 1].delta.new_element.data_frame.data.cols[1].int64s.data self.assertEqual([0, 1, 2, 3, 4, 5], col0) self.assertEqual([10, 11, 12, 13, 14, 15], col1) queue = rq.flush() self.assertEqual(5, len(queue)) self.assertTrue(queue[0].new_report.config.sharing_enabled) assert_deltas(RootContainer.MAIN, (), 1) assert_deltas(RootContainer.SIDEBAR, (0, 0, 1), 3)
def test_dont_replace_block(self, other_msg: ForwardMsg): """add_block deltas should never be replaced/composed because they can have dependent deltas later in the queue.""" rq = ReportQueue() self.assertTrue(rq.is_empty()) ADD_BLOCK_MSG.metadata.delta_path[:] = make_delta_path( RootContainer.MAIN, (), 0) other_msg.metadata.delta_path[:] = make_delta_path( RootContainer.MAIN, (), 0) # Delta messages should not replace `add_block` deltas with the # same delta_path. rq.enqueue(ADD_BLOCK_MSG) rq.enqueue(other_msg) queue = rq.flush() self.assertEqual(len(queue), 2) self.assertEqual(queue[0], ADD_BLOCK_MSG) self.assertEqual(queue[1], other_msg)