def test_upload_folder_with_folder(self, mocked_fun): updated = 200 created = 100 test_node = TreeNode(Node("CLIENT_NODE", self.folder_name, Type.Folder, created, updated, self.path)) test_node.add_node(TreeNode(Node("CLIENT_NODE", self.file_name, Type.Folder, created, updated, self.path))) os_handler.upload_folder(test_node, self.path) self.assertEqual(mocked_fun.call_count, 2)
def test_upload_folder_with_file(self, mocked_create, mocked_upload): updated = 200 created = 100 test_node = TreeNode(Node("CLIENT_NODE", self.folder_name, Type.Folder, created, updated, self.path)) test_node.add_node(TreeNode(Node("CLIENT_NODE", self.file_name, Type.File, created, updated, self.path))) os_handler.upload_folder(test_node, self.path) mocked_upload.assert_called_once() mocked_create.assert_called_once()
def test_download_folder_with_file(self, mock_1, mock_2, mock_3): updated = 200 created = 100 test_node = TreeNode(Node("CLIENT_NODE", self.folder_name, Type.Folder, created, updated, self.path)) test_node.add_node(TreeNode(Node("CLIENT_NODE", self.file_name, Type.File, created, updated, self.path))) x = os_handler.download_folder(test_node, self.path) self.assertEqual(x, [True]) mock_1.assert_called_once() mock_2.assert_called_once() mock_3.assert_called_once()
def test_download_folder_with_folder(self): created = 100 updated = 200 test_node = TreeNode(Node("CLIENT_NODE", self.folder_name, Type.Folder, created, updated, self.path)) test_node.add_node(TreeNode(Node("CLIENT_NODE", self.folder_name, Type.Folder, created, updated, self.path))) x = os_handler.download_folder(test_node, self.path) self.assertEqual(x, []) folder_path = os.path.join(self.path, self.folder_name) inner_folder_path = os.path.join(folder_path, self.folder_name) self.assertEqual(os.path.exists(folder_path), False) self.assertEqual(os.path.exists(inner_folder_path), False)
def test_create_node_from_dict(self): _dict = _get_default_dict() test_node = TreeNode( Node(_dict["id"], _dict["name"], Type.File, _dict["created_at"] / 1000, _dict["updated_at"] / 1000)) node_to_test = tree_builder._create_node_from_dict(_dict) self._test_tree_node(node_to_test, test_node)
def test_get_tree_from_node_id(self, mocked_function): _dict = _get_default_dict() test_node = TreeNode( Node(_dict["id"], _dict["name"], Type.Folder, _dict["created_at"] / 1000, _dict["updated_at"] / 1000)) node_to_test = tree_builder.get_tree_from_node_id("test") mocked_function.assert_called_once() self._test_tree_node(node_to_test, test_node)
def test_build_tree_node(self): node_name = "prova" self.tree = tree_builder._build_tree_node(self.path, node_name) updated = int(os.stat(self.path).st_mtime) created = int(os.stat(self.path).st_ctime) test_node = TreeNode( Node("CLIENT_NODE", node_name, Type.Folder, created, updated, self.path)) self._test_tree_node(self.tree, test_node)
def _build_tree_node(path: str, name: str) -> TreeNode: """Costruisce un TreeNode a partire dal path""" id = "CLIENT_NODE" # name = os.path.basename(path) if not os.path.exists(path): raise FileNotFoundError() type = Type.Folder if os.path.isdir(path) else Type.File created_at = math.trunc(os.stat(path).st_ctime) updated_at = math.trunc(os.stat(path).st_mtime) return TreeNode(Node(id, name, type, created_at, updated_at, path=path))
def test_common_strategy_client_new_file_id_not_exists(self, m1, m2): node_raw = { "action": Actions.CLIENT_NEW_FILE, "node": TreeNode(Node("a", "ciao.txt", Type.File, 100, 200, "a/ciao.txt")) } logger = default_code.FakeLogger() strategy.common_strategy(node_raw, logger) m1.assert_not_called() m2.assert_called_once()
def test_get_tree_from_system(self): self.tree = tree_builder.get_tree_from_system(self.path) updated = int(os.stat(self.path).st_mtime) created = int(os.stat(self.path).st_ctime) test_node = TreeNode( Node("CLIENT_NODE", "ROOT", Type.Folder, created, updated, self.path)) self._test_tree_node(self.tree, test_node) curr_dir_content = os.listdir(self.path) tree_childs = self.tree.get_children() tree_childs.reverse() for content in curr_dir_content: node = tree_childs.pop() file_path = os.path.join(self.path, content) created = int(os.stat(file_path).st_ctime) updated = int(os.stat(file_path).st_mtime) test_node = TreeNode( Node("CLIENT_NODE", content, Type.File, created, updated, file_path)) self._test_tree_node(node, test_node)
def test_download_node_from_server_error(self, mocked_response, mocked_get_id): updated = 200 created = 100 test_node = TreeNode( Node("CLIENT_NODE", self.file_name, Type.Folder, created, updated, self.path)) try: self.api.download_node(test_node, self.path) except APIException as e: mocked_response.assert_called_once() mocked_get_id.assert_called_once() self.assertEqual(str(e), str(APIException()))
def test_download_node_from_server_ok(self, mocked_1, mocked_2): updated = 200 created = 100 test_node = TreeNode( Node("CLIENT_NODE", self.file_name, Type.Folder, created, updated, self.path)) result = self.api.download_node(test_node, self.path) self.assertEqual(result, True) mocked_1.assert_called_once() mocked_2.assert_called_once() file_path = os.path.join(self.path, self.file_name) self.assertTrue(os.path.exists(file_path)) self.assertEqual(os.path.getmtime(file_path), updated)
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_check_node_in_nodelist(self): updated = 200 created = 100 test_node = TreeNode(Node("ROOT", self.folder_name, Type.Folder, created, updated, self.path)) test_node.add_node(TreeNode(Node("b", self.file_name, Type.File, created, updated, self.path))) sub_folder = TreeNode(Node("a", self.folder_name, Type.Folder, created, updated, self.path)) sub_folder.add_node(TreeNode(Node("f", self.folder_name, Type.File, created, updated, self.path))) test_node.add_node(sub_folder) res = os_handler.check_node_in_nodelist(test_node, ["a", "b"]) self.assertTrue(res) res = os_handler.check_node_in_nodelist(test_node, ["d", "e"]) self.assertFalse(res) res = os_handler.check_node_in_nodelist(test_node, ["f"]) self.assertTrue(res)
def test_upload_success(self, mocked_response, mocked_get_id): # creo file da leggere, potrei mockare anche la call # di lettura ma questo risulta piu facile ed indolore anche # se molto brutto full_path = os.path.join(self.path, self.file_name) with open(full_path, "w"): pass updated = 200 created = 100 test_node = TreeNode( Node("CLIENT_NODE", self.file_name, Type.Folder, created, updated, full_path)) self.assertIsNone(self.api.upload_node(test_node)) mocked_response.assert_called_once() mocked_get_id.assert_called_once()
def test_upload_error(self, mocked_response, mocked_get_id): # creo file da leggere, potrei mockare anche la call # di lettura ma questo risulta piu facile ed indolore anche # se molto brutto full_path = os.path.join(self.path, self.file_name) with open(full_path, "w"): pass updated = 200 created = 100 test_node = TreeNode( Node("CLIENT_NODE", self.file_name, Type.Folder, created, updated, full_path)) try: self.api.upload_node(test_node) except APIException as e: mocked_response.assert_called_once() mocked_get_id.assert_called_once() self.assertEqual(str(e), str(APIException()))
def _create_node_from_dict(dict: str) -> TreeNode: """Costruisce un TreeNode a partire dal dict""" id = dict["id"] name = dict["name"] type = Type.File if dict["type"] == "File" else Type.Folder created_at = math.trunc(dict["created_at"] / 1000) updated_at = math.trunc(dict["updated_at"] / 1000) size = 0 if type == Type.Folder else dict["size"] last_editor = None if dict["last_editor"] is None else dict["last_editor"][ "email"] node = Node(id, name, type, created_at, updated_at, size=size, last_editor=last_editor) return TreeNode(node)
def get_data(self) -> Tuple[list[RemoteFile], list[RemoteDirectory]]: list_of_files = [] list_of_dirs = [] try: for entry in self.get_current_tree().get_children(): if entry.get_payload().type == Type.File: list_of_files.append(RemoteFile(entry)) else: list_of_dirs.append(RemoteDirectory(entry)) if self.folder_queue[-1] != "LOCAL_ROOT": previous_folder = RemoteDirectory( TreeNode( Node(self.folder_queue[-1], '..', Type.Folder, None, None))) list_of_dirs.insert(0, previous_folder) except APIException: return None return list_of_files, list_of_dirs
def setUp(self): """Metodo che viene chiamato prima di ogni metodo""" self.node = Node("CLIENT_NODE", "Test", Type.File, 1000, 1022, "test/file.txt") self.folder = Node("CLIENT_NODE", "Test Folder", Type.Folder, 1000, 1022, "test")
def test_download_file_not_in_list(self, mocked_fun): test_node = TreeNode(Node("CLIENT_NODE", self.folder_name, Type.File, 100, 200, "ciao.txt")) res = os_handler.download_file(test_node, self.path) mocked_fun.assert_called_once() self.assertIsNone(res)