def test_move_file_from_src_dir_when_multi_in_src(self): """当src有多个文件时""" import os from zzpy import write_file from zzpy import get_file_path_list_from_dir from zzpy import move_file_from_src_dir from zzpy import create_dir write_file("1", os.path.join(self.root_dir, "src", "1.txt")) write_file("2", os.path.join(self.root_dir, "src", "2.txt")) write_file("2", os.path.join(self.root_dir, "dst", "3.txt")) move_file_from_src_dir(dst_file_path=os.path.join( self.root_dir, "dst", "0.txt"), src_dir_path=os.path.join(self.root_dir, "src")) src_pathes = get_file_path_list_from_dir( os.path.join(self.root_dir, "src")) self.assertEqual(len(src_pathes), 1) self.assertIn(src_pathes[0], (os.path.join(self.root_dir, "src", "1.txt"), os.path.join(self.root_dir, "src", "2.txt"))) dst_pathes = get_file_path_list_from_dir( os.path.join(self.root_dir, "dst")) self.assertEqual(len(dst_pathes), 2) self.assertListEqual(dst_pathes, [ os.path.join(self.root_dir, "dst", "0.txt"), os.path.join(self.root_dir, "dst", "3.txt") ])
def test_move_file_from_src_dir_when_no_file_in_src(self): """当src没有文件时""" import os from zzpy import write_file from zzpy import get_file_path_list_from_dir from zzpy import move_file_from_src_dir move_file_from_src_dir(dst_file_path=os.path.join( self.root_dir, "dst", "1.txt"), src_dir_path=os.path.join(self.root_dir, "src")) self.assertListEqual( get_file_path_list_from_dir(os.path.join(self.root_dir, "src")), []) self.assertListEqual( get_file_path_list_from_dir(os.path.join(self.root_dir, "dst")), [])
def test_move_file_from_src_dir_when_single_in_src_and_dst_not_exist(self): """当src只有一个文件,dst不存在时""" import os from zzpy import write_file from zzpy import get_file_path_list_from_dir from zzpy import move_file_from_src_dir write_file("1", os.path.join(self.root_dir, "src", "1.txt")) move_file_from_src_dir(dst_file_path=os.path.join( self.root_dir, "dst", "1.txt"), src_dir_path=os.path.join(self.root_dir, "src")) self.assertListEqual( get_file_path_list_from_dir(os.path.join(self.root_dir, "src")), []) self.assertListEqual( get_file_path_list_from_dir(os.path.join(self.root_dir, "dst")), [os.path.join(self.root_dir, "dst", "1.txt")])
def test_move_file_from_src_dir_when_src_and_dst_have_same_file(self): """当src有多个文件时""" import os from zzpy import write_file from zzpy import get_file_path_list_from_dir from zzpy import move_file_from_src_dir from zzpy import read_file write_file("1", os.path.join(self.root_dir, "src", "1.txt")) write_file("2", os.path.join(self.root_dir, "dst", "1.txt")) move_file_from_src_dir(dst_file_path=os.path.join( self.root_dir, "dst", "1.txt"), src_dir_path=os.path.join(self.root_dir, "src")) src_pathes = get_file_path_list_from_dir( os.path.join(self.root_dir, "src")) self.assertEqual(len(src_pathes), 0) dst_pathes = get_file_path_list_from_dir( os.path.join(self.root_dir, "dst")) self.assertEqual(len(dst_pathes), 1) dst_path = dst_pathes[0] self.assertEqual(dst_path, os.path.join(self.root_dir, "dst", "1.txt")) self.assertEqual(read_file(dst_path), "1")
def test_get_file_path_list_from_dir(self): from zzpy import get_file_path_list_from_dir from zzpy import write_file from zzpy import init_dir import os # init a_dir = os.path.join(self.root_dir, "a") aa_dir = os.path.join(a_dir, "aa") init_dir(aa_dir) a10_path = os.path.join(a_dir, "10.txt") write_file("10.txt", a10_path) aa11_path = os.path.join(aa_dir, "11.txt") write_file("11.txt", aa11_path) b_dir = os.path.join(self.root_dir, "b") bb_dir = os.path.join(b_dir, "bb") init_dir(bb_dir) bb20_path = os.path.join(bb_dir, "20.txt") write_file("20.txt", bb20_path) bb21_path = os.path.join(bb_dir, "21.txt") write_file("21.txt", bb21_path) c_dir = os.path.join(self.root_dir, "c") init_dir(c_dir) c30_path = os.path.join(c_dir, "30.txt") write_file("30.txt", c30_path) r_path = os.path.join(self.root_dir, "r.txt") write_file("r.txt", r_path) # test file_path_list = get_file_path_list_from_dir(self.root_dir) self.assertSetEqual( set(file_path_list), {r_path, a10_path, aa11_path, bb20_path, bb21_path, c30_path})