def test_with_diff_value(self): node1 = DataNode("TestNode1") node2 = DataNode("TestNode2") self.assertFalse(node1 == node2, "TestEqual: test with diff values: " "nodes must not be equal") self.assertTrue(node1 != node2, "TestEqual: test with diff values: " "not equal operator must be true")
def enqueue(self, value): if self.size == 0: self.head = DataNode(value) self.tail = self.head else: newNode = DataNode(value) self.tail.set_next(newNode) self.tail = newNode self.size += 1
def test_with_same_value(self): node_value = "TestNode" node1 = DataNode(node_value) node2 = DataNode(node_value) self.assertFalse(node1 == node2, "TestEqual: test with same value: " "nodes must not be equal") self.assertTrue(node1 != node2, "TestEqual: test with same value: " "not equal operator must be true")
def test_append_incorrect_instance(self): node = DataNode("TestNode") child_value = "ChildNode" child = DataNode(child_value) try: node.append_child(child_value) self.assertTrue(False, "TestAppend: test incorrect instance appending: " "Exception must be raised") except DataNodeInstanceException: pass
def node_to_item(self, node: DataNode) -> QStandardItem: """ Create QStandardItem based on DataNode. :param node: data source node :return: QStandardItem with node data """ item = QStandardItem(node.get_value()) item.setData(node) item.setEnabled(node.is_enabled()) item.setEditable(False) for child in node.get_children(): item.appendRow(self.node_to_item(child)) return item
def _to_data_list(self, node: DataNode, list_: List[Data]) -> List[Data]: """ Recursive method for extracting Data from DataNode :param node: node for extracting :param list_: container for extracted Data :return: updated list with extracted Data """ list_.append(node.get_instance()) child_items = [] for child in node.get_children(): self._to_data_list(child, list_) list_.extend(child_items) return list_
def test_with_self(self): node1 = DataNode("TestNode") self.assertTrue(node1 == node1, "TestEqual: test with self: " "must be equal with self") self.assertFalse(node1 != node1, "TestEqual: test with self: " "result of not equal operator must be false")
def test_with_copy(self): node1 = DataNode("TestNode") node1_copy = deepcopy(node1) self.assertTrue(node1 == node1_copy, "TestEqual: test with copy: " "node with it copy must be equal") self.assertFalse(node1 != node1_copy, "TestEqual: test with copy: " "result of not equal operator must be false")
def _update_node_with_data(self, node: DataNode, data: Data) -> bool: """ Private method for attempting update node with data. Returns True if attempt was successfull. :param node: node for update :param data: update data :return: True if node successfully update """ if node == data: node.set_value(data.get_value()) if not data.is_enabled(): node.set_enabled(False) return True else: for child in node.get_children(): if self._update_node_with_data(child, data): return True return False
def test_with_only_value(self): try: node_value = "TestNode" node = DataNode(node_value) self.assertEqual(node.get_value() == node_value, True, "TestInit: test with value case: " "value set incorrectly") self.assertIsNone(node.get_parent_id(), "TestInit: test with value case: " "parent must be None") self.assertIsNotNone(node.get_id(), "TestInit: test with value case: " "id of the node was not set") except DataNodeInstanceException: self.assertTrue(False, "TestInit: test with value case: " "exception must not be raised")
def send_data_to_cache(self, json_data: str) -> None: """ Decodes data from json into Data, then appends it to the cache. If cache already has that element, nothing will be appended. :param json_data: received json format data :return: None """ data = self._data_decoder.decode(json_data) if not self._data_controller.node_list_has_data(self.data_cache, data): self.data_cache.append(DataNode(instance=data)) self._data_controller.update_node_hierarchy(self.data_cache, remove_from_list=True) self.sync_tree_cache()
def create_node_hierarchy(self, data_list: List[Data]) -> DataNode: """ Creates node based on list of Data. First :param data_list: list of Data :return: list of DataNode """ nodes = [] for data in data_list: nodes.append(DataNode(instance=data)) self.update_node_hierarchy(nodes) return nodes
def _search_parent(self, checked_node: DataNode, orphan_node: DataNode) -> bool: """ Searches parent for orphan node in node and it children :param checked_node: node for searching parent :param orphan_node: parentless node :return: True if parent found """ if orphan_node.get_parent_node() is not None: return True if checked_node.get_id() == orphan_node.get_parent_id(): checked_node.append_child(orphan_node) if not checked_node.is_enabled(): orphan_node.set_enabled(False) return True for child in checked_node.get_children(): if self._search_parent(child, orphan_node): return True return False
def test_append_first_child(self): node = DataNode("TestNode") child = DataNode("ChildNode") try: node.append_child(child) self.assertTrue(len(node.get_children()) == 1, "TestAppend: test appending first element: " "Children list has not single element") self.assertTrue(child in node.get_children(), "TestAppend: test appending first element: " "Different element created in children list") except DataNodeInstanceException: self.assertTrue(False, "TestAppend: test appending first element: " "Exception must not be raised")
def add_item(self) -> None: """ Show new element window for selected cache item. IF no item was selected, nothing will happens :return: None """ item = self.get_selected_item(self.tree_cache) if item is None: return text, ok = QInputDialog.getText(self, "Appending new data", "Data:") if ok: parent_id = item.data().get_id() data = Data(text, parent_id) data_node = DataNode(instance=data) self.data_cache.append(data_node) self._data_controller.update_node_hierarchy(self.data_cache, remove_from_list=True) self.sync_tree_cache()
def update_node_list_with_data_list(self, nodes_list, data_list, append_new=True) -> None: """ Method for applying update for used nodes. Applies value changing, delete effect. Also new elements will be appended to the tree. :param nodes_list: list of nodes for updating :param data_list: update data :param append_new: enabled by default, appends new nodes from data_list. Disable when just update required. :return: None """ # reserving list for removing from it updated elements process_list = data_list[:] for node in nodes_list: self._update_node_with_data_list(node, process_list) if append_new: nodes_list.extend([DataNode(instance=a) for a in process_list]) self.update_node_hierarchy(nodes_list, remove_from_list=True)
class Queue: def __init__(self): self.head = None self.tail = None self.size = 0 def enqueue(self, value): if self.size == 0: self.head = DataNode(value) self.tail = self.head else: newNode = DataNode(value) self.tail.set_next(newNode) self.tail = newNode self.size += 1 def dequeue(self): if self.empty(): return removed = self.head if self.size == 1: self.head = None self.tail = None else: self.head = self.head.get_next() self.size -= 1 return removed.get_data() def empty(self): return self.size == 0 def __str__(self): current = self.head output = '' while current: output += str(current.get_data()) + ' -> ' current = current.get_next() return output.rstrip(' -> ')
def push(self, data): current = self.head data_node = DataNode(data) self.head = data_node data_node.set_next(current)
def test_with_correct_parent(self): try: grand_node_value = "Grand" grand_node = DataNode(grand_node_value) node1_value = "TestNode1" node2_value = "TestNode2" node1 = DataNode(node1_value, parent=grand_node) node2 = DataNode(node2_value, parent=grand_node) node1_child1_value = "Node1Child1" node1_child2_value = "Node1Child2" node1_child3_value = "Node1Child3" node1_child1 = DataNode(node1_child1_value, parent=node1) node1_child2 = DataNode(node1_child2_value, parent=node1) node1_child3 = DataNode(node1_child3_value, parent=node1) self.assertIsNone(grand_node.get_parent_id(), "TestInit: test with correct parent: " "root node parent must be None") self.assertEqual(node1.get_parent_id(), grand_node.get_id(), "TestInit: test with correct parent: " "root node must be parent of the node1") self.assertEqual(node2.get_parent_id(), grand_node.get_id(), "TestInit: test with correct parent: " "root node must be parent of the node2") self.assertEqual(node1_child1.get_parent_id(), node1.get_id(), "TestInit: test with correct parent: " "node1 must be parent of the child1") self.assertEqual(node1_child2.get_parent_id(), node1.get_id(), "TestInit: test with correct parent: " "node1 must be parent of the child2") self.assertEqual(node1_child2.get_parent_id(), node1.get_id(), "TestInit: test with correct parent: " "node1 must be parent of the child3") except DataNodeInstanceException: self.assertTrue(False, "TestInit: exception was raised")
def test_append_multiple_children(self): node = DataNode("TestNode") child1 = DataNode("ChildNode1") child2 = DataNode("ChildNode2") child3 = DataNode("ChildNode3") try: node.append_child(child1) node.append_child(child2) node.append_child(child3) self.assertTrue(len(node.get_children()) == 3, "TestAppend: test appending multiple children: " "Incorrect count of the appended elements") self.assertTrue(child1 in node.get_children(), "TestAppend: test appending multiple children: " "Different element instead child 1 created in children list") self.assertTrue(child2 in node.get_children(), "TestAppend: test appending multiple children: " "Different element instead child 2 created in children list") self.assertTrue(child3 in node.get_children(), "TestAppend: test appending multiple children: " "Different element instead child 3 created in children list") except DataNodeInstanceException: self.assertTrue(False, "TestAppend: test appending first element: " "Exception must not be raised")
def test_with_not_instance(self): node1_value = "TestNode1" node1 = DataNode(node1_value) self.assertFalse(node1 == node1_value, "TestEqual: test with not instance: " "eq with not instance must be false")
def create_data_sample(self) -> DataNode: """ Create start sample structure :return: root DataNode """ root = DataNode("Grandpa") node1 = DataNode("Node1", parent=root) node2 = DataNode("Node2", parent=root) node3 = DataNode("Node3", parent=root) child1_1 = DataNode("Child1_1", parent=node1) child1_2 = DataNode("Child1_2", parent=node1) child2_1 = DataNode("Child2_1", parent=node2) child2_2 = DataNode("Child2_2", parent=node2) child2_3 = DataNode("Child2_3", parent=node2) child3_1 = DataNode("Child3_1", parent=node3) grandchild1_1_1 = DataNode("Grandchild1_1_1", parent=child1_1) grandchild1_1_2 = DataNode("Grandchild1_1_2", parent=child1_1) grandchild1_2_1 = DataNode("Grandchild1_2_1", parent=child1_2) grandchild2_1_1 = DataNode("Grandchild2_1_1", parent=child2_1) grandchild2_1_2 = DataNode("Grandchild2_1_2", parent=child2_1) grandchild2_1_3 = DataNode("Grandchild2_1_3", parent=child2_1) grandchild2_2_1 = DataNode("Grandchild2_2_1", parent=child2_2) grandchild2_2_2 = DataNode("Grandchild2_2_2", parent=child2_2) grandchild2_3_1 = DataNode("Grandchild2_3_1", parent=child2_3) grandchild2_3_2 = DataNode("Grandchild2_3_2", parent=child2_3) grandchild2_3_3 = DataNode("Grandchild2_3_3", parent=child2_3) grandchild2_3_4 = DataNode("Grandchild2_3_4", parent=child2_3) grandchild3_1_1 = DataNode("Grandchild3_1_1", parent=child3_1) grandchild3_1_2 = DataNode("Grandchild3_1_2", parent=child3_1) grandchild3_1_3 = DataNode("Grandchild3_1_3", parent=child3_1) grandchild3_1_4 = DataNode("Grandchild3_1_4", parent=child3_1) grandchild3_1_5 = DataNode("Grandchild3_1_5", parent=child3_1) return root