def _test_tree_node(self, node_to_test: TreeNode, test_node: TreeNode): self.assertEqual(node_to_test.get_name(), test_node.get_name()) payload_to_test = node_to_test.get_payload() test_payload = test_node.get_payload() self.assertEqual(payload_to_test.id, test_payload.id) self.assertEqual(payload_to_test.type, test_payload.type) self.assertEqual(payload_to_test.updated_at, test_payload.updated_at) self.assertEqual(payload_to_test.created_at, test_payload.created_at) self.assertEqual(payload_to_test.path, test_payload.path)
def upload_node(self, node: TreeNode, parent_id: str = "LOCAL_ROOT"): """Carica un nodo, all'interno del parent passato""" headers = {"cookie": self.cookie} name = node.get_name() content = open(node.get_payload().path, "rb") updated_at = node.get_updated_at() created_at = node.get_payload().created_at multipart_form = { "command": "upload", "name": name, "content": content, "parent": self.get_user_id() + "/" + parent_id, "updated-at": updated_at, "created-at": created_at } response = requests.post(self.url_files, headers=headers, files=multipart_form) if response.ok: self.logger.info( f"Upload del file {name}, completato con successo") else: self.logger.info(f"Upload del file {name}, fallito") # alzo le eccezioni del caso self.check_status_code(response)
def __init__(self, tree: TreeNode, override_name: str = None): self._name = tree.get_name( ) if override_name is None else override_name self._node = tree self._files = [] self._dirs = [] self._path = tree.get_payload().path self.update_list_of_content()
def upload_folder(node: TreeNode, parent_folder_id: str = "LOCAL_ROOT") -> None: """Il nodo rappresenta la cartella che non esiste nel server""" parent_folder_id = create_folder(node.get_name(), parent_folder_id) for _node in node.get_children(): if _node.is_directory(): upload_folder(_node, parent_folder_id) else: upload_file(_node, parent_folder_id)
def download_node(self, node: TreeNode, path_folder: str, quota_libera: float) -> dict: remote_node = self.api_implementation.get_content_from_node( node.get_payload().id) file_size = remote_node["getNode"]["size"] # Se ho spazio procedo al download if quota_libera > file_size: result = self.api_implementation.download_node(node, path_folder) return { "node_name": node.get_name(), "result": result, "type": "network_error" if not result else "" } return { "node_name": node.get_name(), "result": False, "type": "space_error" }
def download_folder(node: TreeNode, path: str) -> list[dict]: """Il nodo rappresenta la cartella che non esiste, ritorna i risultati dei download che sono stati fatti""" # Controlliamo se la cartella ha dei nodi da scaricare file_node_list = settings_model.get_sync_list() res = check_node_in_nodelist(node, file_node_list) download_operations_list = [] if res: path_folder = os.path.join(path, node.get_name()) os.mkdir(path_folder) for _node in node.get_children(): if _node.is_directory(): result = download_folder(_node, path_folder) download_operations_list.extend(result) else: result = download_file(_node, path_folder) if result is not None: download_operations_list.append(result) return download_operations_list
class TestTreeNode(unittest.TestCase): def setUp(self): """Metodo che viene chiamato prima di ogni metodo""" self.files: list[Node] = [ Node("CLIENT_NODE", "Test", Type.File, 1000, 1022, "root/file.txt"), Node("CLIENT_NODE", "Test2", Type.File, 1000, 1024, "root/file2.txt"), Node("CLIENT_NODE", "Test3", Type.File, 1000, 1026, "root/root2/file3.txt"), ] self.folders: list[Node] = [ Node("CLIENT_NODE", "Test Folder", Type.Folder, 1000, 1022, "root"), Node("CLIENT_NODE", "Test Folder2", Type.Folder, 1000, 1025, "root/root2"), ] self.tree_node = TreeNode(self.folders[0]) self.tree_node.add_node(TreeNode(self.files[0])) self.tree_node.add_node(TreeNode(self.files[1])) self.tree_node_sub = TreeNode(self.folders[1]) self.tree_node_sub.add_node(TreeNode(self.files[2])) self.tree_node.add_node(self.tree_node_sub) def test_is_directory(self) -> None: self.assertTrue(self.tree_node.is_directory()) self.assertFalse(self.tree_node._children[0].is_directory()) self.assertFalse(self.tree_node._children[1].is_directory()) self.assertTrue(self.tree_node._children[2].is_directory()) self.assertFalse( self.tree_node._children[2]._children[0].is_directory()) def test_get_name(self) -> None: tests = { "Test Folder": self.tree_node.get_name(), "Test": self.tree_node._children[0].get_name(), "Test2": self.tree_node._children[1].get_name(), "Test Folder2": self.tree_node._children[2].get_name(), "Test3": self.tree_node._children[2]._children[0].get_name() } for expected, values in tests.items(): self.assertEqual(expected, values) def test_get_updated_at(self) -> None: self.assertEqual(1022, self.tree_node.get_updated_at()) self.assertEqual(1022, self.tree_node._children[0].get_updated_at()) self.assertEqual(1024, self.tree_node._children[1].get_updated_at()) self.assertEqual(1025, self.tree_node._children[2].get_updated_at()) self.assertEqual( 1026, self.tree_node._children[2]._children[0].get_updated_at()) def test_get_children(self) -> None: self.assertEqual(self.folders[0], self.tree_node.get_payload()) self.assertEqual(self.files[0], self.tree_node._children[0].get_payload()) self.assertEqual(self.files[1], self.tree_node._children[1].get_payload()) self.assertEqual(self.folders[1], self.tree_node._children[2].get_payload()) self.assertEqual( self.files[2], self.tree_node._children[2]._children[0].get_payload()) def test_get_payload(self) -> None: self.assertEqual(3, len(self.tree_node.get_children())) self.assertEqual( self.files[2], self.tree_node._children[2]._children[0].get_payload()) def test_parent(self) -> None: self.assertEqual(self.tree_node, self.tree_node._children[0]._parent) self.assertEqual(self.tree_node, self.tree_node._children[1]._parent) self.assertEqual(self.tree_node, self.tree_node._children[2]._parent) self.assertEqual(self.tree_node._children[2], self.tree_node._children[2]._children[0]._parent) def test_add_node_exception(self) -> None: root = TreeNode(self.files[0]) with self.assertRaises(ValueError): root.add_node(TreeNode(self.files[1])) def test_str(self) -> None: res = ("Test Folder - Folder (1022 -> 01/01/1970 00:17:02)\n" " Test (1022 -> 01/01/1970 00:17:02)\n" " Test2 (1024 -> 01/01/1970 00:17:04)\n" " Test Folder2 - Folder (1025 -> 01/01/1970 00:17:05)\n" " Test3 (1026 -> 01/01/1970 00:17:06)\n") self.assertEqual(res, str(self.tree_node))
def __init__(self, node: TreeNode): self._name = node.get_name() self._creation_date = self._convert_int_to_date( node.get_payload().created_at) self._last_modified_date = self._convert_int_to_date( node.get_updated_at())