def test_deep_copy_should_deep_copy_child_data_groups(self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_group = DataGroup.DataGroup() data_item1 = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item1) data_group.append_display_item(document_model.get_display_item_for_data_item(data_item1)) data_group2 = DataGroup.DataGroup() data_group.append_data_group(data_group2) # attempt to copy data_group_copy = copy.deepcopy(data_group) # make sure data_groups are not shared self.assertNotEqual(data_group.data_groups[0], data_group_copy.data_groups[0])
def test_deep_copy_should_deep_copy_child_data_groups(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_group = DataGroup.DataGroup() data_item1 = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item1) data_group.append_display_item(document_model.get_display_item_for_data_item(data_item1)) data_group2 = DataGroup.DataGroup() data_group.append_data_group(data_group2) # attempt to copy data_group_copy = copy.deepcopy(data_group) # make sure data_groups are not shared self.assertNotEqual(data_group.data_groups[0], data_group_copy.data_groups[0])
def test_counted_display_items(self): # TODO: split test_counted_display_items into separate tests with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_group = DataGroup.DataGroup() document_model.append_data_group(data_group) self.assertEqual(len(data_group.counted_display_items), 0) self.assertEqual(len(document_model.data_items), 0) data_item1 = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item1) display_item1 = document_model.get_display_item_for_data_item(data_item1) data_group.append_display_item(display_item1) # make sure that both top level and data_group see the data item self.assertEqual(len(document_model.data_items), 1) self.assertEqual(len(data_group.counted_display_items), 1) self.assertEqual(list(document_model.display_items), list(data_group.counted_display_items)) self.assertIn(display_item1, list(data_group.counted_display_items.keys())) # add a child data item and make sure top level and data_group see it # also check data item. data_item1a = document_model.get_resample_new(display_item1, display_item1.data_item) display_item1a = document_model.get_display_item_for_data_item(data_item1a) data_group.append_display_item(display_item1a) self.assertEqual(len(document_model.data_items), 2) self.assertEqual(len(data_group.counted_display_items), 2) self.assertIn(display_item1, list(data_group.counted_display_items.keys())) self.assertIn(display_item1a, list(data_group.counted_display_items.keys())) # add a child data item to the child and make sure top level and data_group match. # also check data items. data_item1a1 = document_model.get_resample_new(display_item1a, display_item1a.data_item) display_item1a1 = document_model.get_display_item_for_data_item(data_item1a1) data_group.append_display_item(display_item1a1) self.assertEqual(len(document_model.data_items), 3) self.assertEqual(len(data_group.counted_display_items), 3) self.assertIn(display_item1, list(data_group.counted_display_items.keys())) self.assertIn(display_item1a, list(data_group.counted_display_items.keys())) self.assertIn(display_item1a1, list(data_group.counted_display_items.keys())) # now add a data item that already has children data_item2 = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item2) display_item2 = document_model.get_display_item_for_data_item(data_item2) data_item2a = document_model.get_resample_new(display_item2, display_item2.data_item) display_item2a = document_model.get_display_item_for_data_item(data_item2a) data_group.append_display_item(display_item2) data_group.append_display_item(display_item2a) self.assertEqual(len(document_model.data_items), 5) self.assertEqual(len(data_group.counted_display_items), 5) self.assertIn(data_item2a, document_model.data_items) self.assertIn(display_item2a, list(data_group.counted_display_items.keys())) # remove data item without children document_model.remove_data_item(data_item1a1) self.assertEqual(len(document_model.data_items), 4) self.assertEqual(len(data_group.counted_display_items), 4) # now remove data item with children document_model.remove_data_item(data_item2) self.assertEqual(len(document_model.data_items), 2) self.assertEqual(len(data_group.counted_display_items), 2)
def test_deep_copy_should_not_deep_copy_data_items(self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_group = DataGroup.DataGroup() data_item = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item(data_item) data_group.append_display_item(display_item) data_group_copy = copy.deepcopy(data_group) self.assertEqual(display_item.uuid, data_group_copy.display_item_references[0])
def test_insert_data_group_undo_redo(self): app = Application.Application(TestUI.UserInterface(), set_global=False) document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): # setup three data items and put two in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item3) data_group1 = DataGroup.DataGroup() data_group1.append_display_item( document_model.get_display_item_for_data_item(data_item1)) data_group2 = DataGroup.DataGroup() data_group3 = DataGroup.DataGroup() data_group2_uuid = data_group2.uuid data_group3.append_display_item( document_model.get_display_item_for_data_item(data_item3)) document_model.append_data_group(data_group1) document_model.append_data_group(data_group3) # insert the middle data group command = DocumentController.DocumentController.InsertDataGroupCommand( document_model, document_model, 1, data_group2) command.perform() document_controller.push_undo_command(command) self.assertEqual(3, len(document_model.data_groups)) self.assertEqual(data_group1, document_model.data_groups[0]) self.assertEqual(data_group3, document_model.data_groups[2]) self.assertEqual(data_group2_uuid, document_model.data_groups[1].uuid) # undo and check document_controller.handle_undo() self.assertEqual(2, len(document_model.data_groups)) self.assertEqual(data_group1, document_model.data_groups[0]) self.assertEqual(data_group3, document_model.data_groups[1]) # redo and check document_controller.handle_redo() self.assertEqual(3, len(document_model.data_groups)) self.assertEqual(data_group1, document_model.data_groups[0]) self.assertEqual(data_group3, document_model.data_groups[2]) self.assertEqual(data_group2_uuid, document_model.data_groups[1].uuid)
def test_deep_copy_should_not_deep_copy_data_items(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_group = DataGroup.DataGroup() data_item = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item(data_item) data_group.append_display_item(display_item) data_group_copy = copy.deepcopy(data_group) display_item_specifier = Persistence.PersistentObjectSpecifier.read(data_group_copy.display_item_specifiers[0]) self.assertEqual(display_item, document_model.resolve_item_specifier(display_item_specifier))
def test_insert_data_group_undo_redo(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model # setup three data items and put two in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item3) data_group1 = DataGroup.DataGroup() data_group1.append_display_item( document_model.get_display_item_for_data_item(data_item1)) data_group2 = DataGroup.DataGroup() data_group3 = DataGroup.DataGroup() data_group2_uuid = data_group2.uuid data_group3.append_display_item( document_model.get_display_item_for_data_item(data_item3)) document_model.append_data_group(data_group1) document_model.append_data_group(data_group3) # insert the middle data group command = DocumentController.DocumentController.InsertDataGroupCommand( document_model, document_model._project, 1, data_group2) command.perform() document_controller.push_undo_command(command) self.assertEqual(3, len(document_model.data_groups)) self.assertEqual(data_group1, document_model.data_groups[0]) self.assertEqual(data_group3, document_model.data_groups[2]) self.assertEqual(data_group2_uuid, document_model.data_groups[1].uuid) # undo and check document_controller.handle_undo() self.assertEqual(2, len(document_model.data_groups)) self.assertEqual(data_group1, document_model.data_groups[0]) self.assertEqual(data_group3, document_model.data_groups[1]) # redo and check document_controller.handle_redo() self.assertEqual(3, len(document_model.data_groups)) self.assertEqual(data_group1, document_model.data_groups[0]) self.assertEqual(data_group3, document_model.data_groups[2]) self.assertEqual(data_group2_uuid, document_model.data_groups[1].uuid)
def test_data_item_removed_implicitly_from_data_group_undo_redo(self): app = Application.Application(TestUI.UserInterface(), set_global=False) document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): # setup three data items and put in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item2 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) document_model.append_data_item(data_item3) data_group = DataGroup.DataGroup() display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) display_item3 = document_model.get_display_item_for_data_item( data_item3) data_group.append_display_item(display_item1) data_group.append_display_item(display_item2) data_group.append_display_item(display_item3) document_model.append_data_group(data_group) # remove the 2nd data item command = document_controller.create_remove_data_items_command( [data_item2]) command.perform() document_controller.push_undo_command(command) self.assertEqual(2, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertEqual(document_model.display_items[0], data_group.display_items[0]) self.assertEqual(document_model.display_items[1], data_group.display_items[1]) # undo and check document_controller.handle_undo() self.assertEqual(3, len(document_model.data_items)) self.assertEqual(3, len(data_group.display_items)) self.assertEqual(document_model.display_items[0], data_group.display_items[0]) self.assertEqual(document_model.display_items[1], data_group.display_items[1]) self.assertEqual(document_model.display_items[2], data_group.display_items[2]) # redo and check document_controller.handle_redo() self.assertEqual(2, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertEqual(document_model.display_items[0], data_group.display_items[0]) self.assertEqual(document_model.display_items[1], data_group.display_items[1])
def test_deleting_data_item_removes_it_from_data_group(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item) data_group = DataGroup.DataGroup() data_group.append_display_item(document_model.get_display_item_for_data_item(data_item)) document_model.append_data_group(data_group) self.assertEqual(1, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) document_model.remove_data_item(data_item) self.assertEqual(0, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups))
def test_deleting_data_item_removes_it_from_data_group(self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_item = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item) data_group = DataGroup.DataGroup() data_group.append_display_item(document_model.get_display_item_for_data_item(data_item)) document_model.append_data_group(data_group) self.assertEqual(1, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) document_model.remove_data_item(data_item) self.assertEqual(0, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups))
def test_data_item_removed_implicitly_from_data_group_undo_redo(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model # setup three data items and put in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item2 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) document_model.append_data_item(data_item3) data_group = DataGroup.DataGroup() display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) display_item3 = document_model.get_display_item_for_data_item( data_item3) data_group.append_display_item(display_item1) data_group.append_display_item(display_item2) data_group.append_display_item(display_item3) document_model.append_data_group(data_group) # remove the 2nd data item command = document_controller.create_remove_data_items_command( [data_item2]) command.perform() document_controller.push_undo_command(command) self.assertEqual(2, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertEqual(document_model.display_items[0], data_group.display_items[0]) self.assertEqual(document_model.display_items[1], data_group.display_items[1]) # undo and check document_controller.handle_undo() self.assertEqual(3, len(document_model.data_items)) self.assertEqual(3, len(data_group.display_items)) self.assertEqual(document_model.display_items[0], data_group.display_items[0]) self.assertEqual(document_model.display_items[1], data_group.display_items[1]) self.assertEqual(document_model.display_items[2], data_group.display_items[2]) # redo and check document_controller.handle_redo() self.assertEqual(2, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertEqual(document_model.display_items[0], data_group.display_items[0]) self.assertEqual(document_model.display_items[1], data_group.display_items[1])
def test_deep_copy_should_not_deep_copy_data_items(self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_group = DataGroup.DataGroup() data_item = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) data_group.append_display_item(display_item) data_group_copy = copy.deepcopy(data_group) display_item_specifier = Persistence.PersistentObjectSpecifier.read( data_group_copy.display_item_specifiers[0]) self.assertEqual( display_item, document_model.resolve_item_specifier(display_item_specifier))
def test_inserting_items_data_group_undo_redo(self): app = Application.Application(TestUI.UserInterface(), set_global=False) document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController(app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): # setup three data items and put in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item4 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item3) document_model.append_data_item(data_item4) data_group = DataGroup.DataGroup() display_item1 = document_model.get_display_item_for_data_item(data_item1) display_item3 = document_model.get_display_item_for_data_item(data_item3) display_item4 = document_model.get_display_item_for_data_item(data_item4) data_group.append_display_item(display_item1) data_group.append_display_item(display_item3) document_model.append_data_group(data_group) self.assertEqual(3, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4], list(document_model.data_items)) self.assertListEqual([display_item1, display_item3], list(data_group.display_items)) # insert new items data_item2 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item2) display_item2 = document_model.get_display_item_for_data_item(data_item2) command = DocumentController.DocumentController.InsertDataGroupDataItemsCommand(document_controller, data_group, [data_item2, data_item4], 1) command.perform() document_controller.push_undo_command(command) self.assertEqual(4, len(document_model.data_items)) self.assertEqual(4, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items)) self.assertListEqual([display_item1, display_item2, display_item4, display_item3], list(data_group.display_items)) # undo and check document_controller.handle_undo() self.assertEqual(4, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items)) self.assertListEqual([display_item1, display_item3], list(data_group.display_items)) # redo and check document_controller.handle_redo() data_item2 = document_model.data_items[3] self.assertEqual(4, len(document_model.data_items)) self.assertEqual(4, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items)) self.assertListEqual([display_item1, display_item2, display_item4, display_item3], list(data_group.display_items))
def test_inserting_items_data_group_undo_redo(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model # setup three data items and put in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item4 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item3) document_model.append_data_item(data_item4) data_group = DataGroup.DataGroup() display_item1 = document_model.get_display_item_for_data_item(data_item1) display_item3 = document_model.get_display_item_for_data_item(data_item3) display_item4 = document_model.get_display_item_for_data_item(data_item4) data_group.append_display_item(display_item1) data_group.append_display_item(display_item3) document_model.append_data_group(data_group) self.assertEqual(3, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4], list(document_model.data_items)) self.assertListEqual([display_item1, display_item3], list(data_group.display_items)) # insert new items data_item2 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item2) display_item2 = document_model.get_display_item_for_data_item(data_item2) # hack until InsertDataGroupDisplayItemsCommand handles multiple display items, call it twice command = DocumentController.DocumentController.InsertDataGroupDisplayItemsCommand(document_controller.document_model, data_group, 1, [display_item2, display_item4]) command.perform() document_controller.push_undo_command(command) self.assertEqual(4, len(document_model.data_items)) self.assertEqual(4, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items)) self.assertListEqual([display_item1, display_item2, display_item4, display_item3], list(data_group.display_items)) # undo and check document_controller.handle_undo() self.assertEqual(4, len(document_model.data_items)) self.assertEqual(2, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items)) self.assertListEqual([display_item1, display_item3], list(data_group.display_items)) # redo and check document_controller.handle_redo() data_item2 = document_model.data_items[3] self.assertEqual(4, len(document_model.data_items)) self.assertEqual(4, len(data_group.display_items)) self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items)) self.assertListEqual([display_item1, display_item2, display_item4, display_item3], list(data_group.display_items))
def test_insert_data_item_into_data_group_undo_redo(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model # setup three data items and put two in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item2 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) document_model.append_data_item(data_item3) data_group = DataGroup.DataGroup() display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) display_item3 = document_model.get_display_item_for_data_item( data_item3) data_group.append_display_item(display_item1) data_group.append_display_item(display_item3) document_model.append_data_group(data_group) # insert a new one display_item2 = document_model.get_display_item_for_data_item( data_item2) command = document_controller.create_insert_data_group_display_items_command( data_group, 1, [display_item2]) command.perform() document_controller.push_undo_command(command) self.assertEqual(3, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item1, data_group.display_items[0]) self.assertEqual(display_item2, data_group.display_items[1]) self.assertEqual(display_item3, data_group.display_items[2]) # undo and check document_controller.handle_undo() self.assertEqual(2, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item1, data_group.display_items[0]) self.assertEqual(display_item3, data_group.display_items[1]) # redo and check document_controller.handle_redo() self.assertEqual(3, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item1, data_group.display_items[0]) self.assertEqual(display_item2, data_group.display_items[1]) self.assertEqual(display_item3, data_group.display_items[2])
def test_deleting_data_items_out_of_order_from_data_group_undo_redo(self): app = Application.Application(TestUI.UserInterface(), set_global=False) document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): # setup three data items in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item2 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) document_model.append_data_item(data_item3) data_group = DataGroup.DataGroup() display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) display_item3 = document_model.get_display_item_for_data_item( data_item3) data_group.append_display_item(display_item1) data_group.append_display_item(display_item2) data_group.append_display_item(display_item3) document_model.append_data_group(data_group) # remove two of the three command = DocumentController.DocumentController.RemoveDataGroupDisplayItemsCommand( document_model, data_group, [display_item3, display_item1]) command.perform() document_controller.push_undo_command(command) self.assertEqual(1, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item2, data_group.display_items[0]) # undo and check document_controller.handle_undo() self.assertEqual(3, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item1, data_group.display_items[0]) self.assertEqual(display_item2, data_group.display_items[1]) self.assertEqual(display_item3, data_group.display_items[2]) # redo and check document_controller.handle_redo() self.assertEqual(1, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item2, data_group.display_items[0])
def test_data_group_rename_undo_redo(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model # setup data group data_group = DataGroup.DataGroup() data_group.title = "ethel" document_model.append_data_group(data_group) # rename command = document_controller.create_rename_data_group_command(data_group, "fred") command.perform() document_controller.push_undo_command(command) self.assertEqual(data_group.title, "fred") # undo and check document_controller.handle_undo() self.assertEqual(data_group.title, "ethel") # redo and check document_controller.handle_redo() self.assertEqual(data_group.title, "fred")
def test_deleting_data_items_out_of_order_from_data_group_undo_redo(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model # setup three data items in a group data_item1 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item2 = DataItem.DataItem(numpy.random.randn(4, 4)) data_item3 = DataItem.DataItem(numpy.random.randn(4, 4)) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) document_model.append_data_item(data_item3) data_group = DataGroup.DataGroup() display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) display_item3 = document_model.get_display_item_for_data_item( data_item3) data_group.append_display_item(display_item1) data_group.append_display_item(display_item2) data_group.append_display_item(display_item3) document_model.append_data_group(data_group) # remove two of the three command = DocumentController.DocumentController.RemoveDataGroupDisplayItemsCommand( document_model, data_group, [display_item3, display_item1]) command.perform() document_controller.push_undo_command(command) self.assertEqual(1, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item2, data_group.display_items[0]) # undo and check document_controller.handle_undo() self.assertEqual(3, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item1, data_group.display_items[0]) self.assertEqual(display_item2, data_group.display_items[1]) self.assertEqual(display_item3, data_group.display_items[2]) # redo and check document_controller.handle_redo() self.assertEqual(1, len(data_group.display_items)) self.assertEqual(0, len(data_group.data_groups)) self.assertEqual(display_item2, data_group.display_items[0])
def test_data_group_rename_undo_redo(self): app = Application.Application(TestUI.UserInterface(), set_global=False) document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController(app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): # setup data group data_group = DataGroup.DataGroup() data_group.title = "ethel" document_model.append_data_group(data_group) # rename command = document_controller.create_rename_data_group_command(data_group, "fred") command.perform() document_controller.push_undo_command(command) self.assertEqual(data_group.title, "fred") # undo and check document_controller.handle_undo() self.assertEqual(data_group.title, "ethel") # redo and check document_controller.handle_redo() self.assertEqual(data_group.title, "fred")
def read_project(self) -> None: if callable(self.handle_start_read): self.handle_start_read() properties = self._raw_properties if properties: project_version = properties.get("version", None) if project_version is not None and project_version == FileStorageSystem.PROJECT_VERSION: for item_d in properties.get("data_items", list()): data_item = DataItem.DataItem() data_item.begin_reading() data_item.read_from_dict(item_d) data_item.finish_reading() if not self.get_item_by_uuid("data_items", data_item.uuid): self.load_item("data_items", len(self.data_items), data_item) else: data_item.close() for item_d in properties.get("display_items", list()): display_item = DisplayItem.DisplayItem() display_item.begin_reading() display_item.read_from_dict(item_d) display_item.finish_reading() if not self.get_item_by_uuid("display_items", display_item.uuid): self.load_item("display_items", len(self.display_items), display_item) else: display_item.close() for item_d in properties.get("data_structures", list()): data_structure = DataStructure.DataStructure() data_structure.begin_reading() data_structure.read_from_dict(item_d) data_structure.finish_reading() if not self.get_item_by_uuid("data_structures", data_structure.uuid): self.load_item("data_structures", len(self.data_structures), data_structure) else: data_structure.close() for item_d in properties.get("computations", list()): computation = Symbolic.Computation() computation.begin_reading() computation.read_from_dict(item_d) computation.finish_reading() if not self.get_item_by_uuid("computations", computation.uuid): self.load_item("computations", len(self.computations), computation) # TODO: handle update script and bind after reload in document model computation.update_script( Project._processing_descriptions) computation.reset() else: computation.close() for item_d in properties.get("connections", list()): connection = Connection.connection_factory(item_d.get) connection.begin_reading() connection.read_from_dict(item_d) connection.finish_reading() if not self.get_item_by_uuid("connections", connection.uuid): self.load_item("connections", len(self.connections), connection) else: connection.close() for item_d in properties.get("data_groups", list()): data_group = DataGroup.data_group_factory(item_d.get) data_group.begin_reading() data_group.read_from_dict(item_d) data_group.finish_reading() if not self.get_item_by_uuid("data_groups", data_group.uuid): self.load_item("data_groups", len(self.data_groups), data_group) else: data_group.close() for item_d in properties.get("workspaces", list()): workspace = WorkspaceLayout.factory(item_d.get) workspace.begin_reading() workspace.read_from_dict(item_d) workspace.finish_reading() if not self.get_item_by_uuid("workspaces", workspace.uuid): self.load_item("workspaces", len(self.workspaces), workspace) else: workspace.close() workspace_uuid_str = properties.get("workspace_uuid", None) if workspace_uuid_str: self._set_persistent_property_value( "workspace_uuid", uuid.UUID(workspace_uuid_str)) self._set_persistent_property_value( "data_item_references", properties.get("data_item_references", dict())) self._set_persistent_property_value( "mapped_items", properties.get("mapped_items", list())) self.__has_been_read = True if callable(self.handle_finish_read): self.handle_finish_read()