def to_clientside(self) -> clientside.TabUpdate: return clientside.TabUpdate( slug=self.slug, name=self.name, selected_step_index=self.selected_step_position, step_ids=list(self.live_steps.values_list("id", flat=True)), )
def test_clientside_update(self, send_delta): workflow = Workflow.create_and_init() tab = workflow.tabs.first() tab.name = "foo" tab.save(update_fields=["name"]) send_delta.side_effect = async_noop self.run_with_async_db( commands.do(SetTabName, workflow_id=workflow.id, tab=tab, new_name="bar") ) send_delta.assert_called() delta1 = send_delta.call_args[0][1] self.assertEqual(delta1.tabs[tab.slug], clientside.TabUpdate(name="bar")) self.run_with_async_db(commands.undo(workflow.id)) delta2 = send_delta.call_args[0][1] self.assertEqual(delta2.tabs[tab.slug], clientside.TabUpdate(name="foo"))
def test_duplicate_empty_tab(self, send_update, queue_render): send_update.side_effect = async_noop workflow = Workflow.create_and_init() tab = workflow.tabs.first() cmd = self.run_with_async_db( commands.do( DuplicateTab, workflow_id=workflow.id, from_tab=tab, slug="tab-2", name="Tab 2", )) # Adds new tab cmd.tab.refresh_from_db() self.assertFalse(cmd.tab.is_deleted) self.assertEqual(cmd.tab.slug, "tab-2") self.assertEqual(cmd.tab.name, "Tab 2") workflow.refresh_from_db() send_update.assert_called_with( workflow.id, clientside.Update( workflow=clientside.WorkflowUpdate( updated_at=workflow.updated_at, tab_slugs=["tab-1", "tab-2"]), tabs={ "tab-2": clientside.TabUpdate( slug="tab-2", name="Tab 2", step_ids=[], selected_step_index=None, ) }, ), ) # Backward: should delete tab self.run_with_async_db(commands.undo(workflow.id)) cmd.tab.refresh_from_db() self.assertTrue(cmd.tab.is_deleted) workflow.refresh_from_db() send_update.assert_called_with( workflow.id, clientside.Update( workflow=clientside.WorkflowUpdate( updated_at=workflow.updated_at, tab_slugs=["tab-1"]), clear_tab_slugs=frozenset(["tab-2"]), ), ) # Forward: should bring us back self.run_with_async_db(commands.redo(workflow.id)) cmd.tab.refresh_from_db() self.assertFalse(cmd.tab.is_deleted) workflow.refresh_from_db() send_update.assert_called_with( workflow.id, clientside.Update( workflow=clientside.WorkflowUpdate( updated_at=workflow.updated_at, tab_slugs=["tab-1", "tab-2"]), tabs={ "tab-2": clientside.TabUpdate( slug="tab-2", name="Tab 2", step_ids=[], selected_step_index=None, ) }, ), ) # There should never be a render: we aren't changing any module # outputs. queue_render.assert_not_called()
def test_delete_custom_report_blocks(self, send_update): future_none = asyncio.Future() future_none.set_result(None) send_update.return_value = future_none workflow = Workflow.create_and_init(has_custom_report=True) # tab-1 tab1 = workflow.tabs.first() step1 = tab1.steps.create( order=0, slug="step-1", last_relevant_delta_id=workflow.last_delta_id, params={"url": ""}, ) step2 = tab1.steps.create( order=0, slug="step-2", last_relevant_delta_id=workflow.last_delta_id, params={"url": ""}, ) # Report will include the step twice, and have another step elsewhere # that should not be touched block1 = workflow.blocks.create(position=0, slug="block-step-1-1", block_type="Chart", step=step1) block2 = workflow.blocks.create(position=1, slug="block-step-2", block_type="Chart", step=step2) block3 = workflow.blocks.create(position=2, slug="block-step-1-2", block_type="Chart", step=step1) self.run_with_async_db( commands.do(DeleteStep, workflow_id=workflow.id, step=step1)) with self.assertRaises(Block.DoesNotExist): block1.refresh_from_db() with self.assertRaises(Block.DoesNotExist): block3.refresh_from_db() block2.refresh_from_db() self.assertEqual(block2.position, 0) send_update.assert_called() update = send_update.call_args[0][1] self.assertEqual(update.workflow.block_slugs, ["block-step-2"]) self.assertEqual(update.tabs, {"tab-1": clientside.TabUpdate(step_ids=[step2.id])}) self.assertEqual(update.clear_step_ids, frozenset([step1.id])) self.assertEqual(update.blocks, {}) self.run_with_async_db(commands.undo(workflow.id)) # The old blocks are deleted. We expect new blocks with new IDs. with self.assertRaises(Block.DoesNotExist): block1.refresh_from_db() with self.assertRaises(Block.DoesNotExist): block3.refresh_from_db() new_block1 = workflow.blocks.get(slug=block1.slug) new_block3 = workflow.blocks.get(slug=block3.slug) self.assertEqual(new_block1.step_id, step1.id) self.assertEqual(new_block3.step_id, step1.id) block2.refresh_from_db() self.assertEqual(new_block1.position, 0) self.assertEqual(block2.position, 1) self.assertEqual(new_block3.position, 2) send_update.assert_called() update = send_update.call_args[0][1] self.assertEqual( update.workflow.block_slugs, ["block-step-1-1", "block-step-2", "block-step-1-2"], ) self.assertEqual( update.tabs, {"tab-1": clientside.TabUpdate(step_ids=[step1.id, step2.id])}) self.assertEqual( update.blocks, { "block-step-1-1": clientside.ChartBlock("step-1"), "block-step-1-2": clientside.ChartBlock("step-1"), }, ) self.run_with_async_db(commands.redo(workflow.id)) block2.refresh_from_db() self.assertEqual(block2.position, 0)