Beispiel #1
0
    def test_get_param_from_local_config_value(self):
        from zzpy import get_param
        from zzpy import write_file
        import os
        import json

        key = "UNITTEST_KEY"

        local_config_path = os.path.join(self.root_dir, "local-config.json")
        local_config_value = "UNITTEST_LOCAL_CONF"
        write_file(json.dumps({key: local_config_value}), local_config_path)

        default_config_path = os.path.join(self.root_dir,
                                           "default-config.json")
        default_config_value = "UNITTEST_DEFAULT_CONF"
        write_file(json.dumps({key: default_config_value}),
                   default_config_path)

        default_value = "UNITTEST_DEFAULT"

        value = get_param(key,
                          default_value=default_value,
                          default_config_path=default_config_path,
                          local_config_path=local_config_path)
        self.assertEqual(value, local_config_value)
Beispiel #2
0
    def test_get_param_from_env(self):
        from zzpy import get_param
        from zzpy import write_file
        import os
        import json

        # init
        key = "UNITTEST_KEY"
        env_value = "UNITTEST_ENV"
        local_config_path = os.path.join(self.root_dir, "local-config.json")
        local_config_value = "UNITTEST_LOCAL_CONF"
        write_file(json.dumps({key: local_config_value}), local_config_path)

        default_config_path = os.path.join(self.root_dir,
                                           "default-config.json")
        default_config_value = "UNITTEST_DEFAULT_CONF"
        write_file(json.dumps({key: default_config_value}),
                   default_config_path)

        default_value = "UNITTEST_DEFAULT"

        os.environ[key] = env_value
        self.assertIn(key, os.environ)

        value = get_param(key,
                          default_value=default_value,
                          default_config_path=default_config_path,
                          local_config_path=local_config_path)
        self.assertEqual(value, env_value)

        # clean
        del os.environ[key]
        self.assertNotIn(key, os.environ)
Beispiel #3
0
    def test_get_one_file_path_from_dir_when_single_file(self):
        import os
        from zzpy import write_file
        from zzpy import get_one_file_path_from_dir

        write_file("CONTENT", os.path.join(self.root_dir, "a", "a1.txt"))
        self.assertEqual(
            get_one_file_path_from_dir(os.path.join(self.root_dir, "a")),
            os.path.join(self.root_dir, "a", "a1.txt"))
Beispiel #4
0
    def test_get_one_file_path_from_dir_when_multi_files(self):
        import os
        from zzpy import write_file
        from zzpy import get_one_file_path_from_dir

        write_file("CONTENT", os.path.join(self.root_dir, "a", "a1.txt"))
        write_file("CONTENT", os.path.join(self.root_dir, "a", "a2.txt"))
        self.assertIn(
            get_one_file_path_from_dir(os.path.join(self.root_dir, "a")),
            (os.path.join(self.root_dir, "a", "a1.txt"),
             os.path.join(self.root_dir, "a", "a2.txt")))
Beispiel #5
0
    def test_write_file_and_read_file_when_dir_not_exist(self):
        from zzpy import write_file
        from zzpy import read_file
        from zzpy import read_bin_file
        import os

        write_content = "abc"
        self.assertFalse(os.path.exists(self.root_dir))
        file_path = os.path.join(self.root_dir, f"1.txt")
        write_file(content=write_content, file_path=file_path)
        self.assertTrue(os.path.exists(file_path))
        read_content = read_file(file_path)
        self.assertEqual(read_content, write_content)
Beispiel #6
0
    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")])
Beispiel #7
0
    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")
        ])
Beispiel #8
0
    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")
Beispiel #9
0
    def test_get_one_file_path_from_dir_when_having_dirs_before_file(self):
        import os
        from zzpy import write_file
        from zzpy import get_one_file_path_from_dir

        write_file("CONTENT", os.path.join(self.root_dir, "c", "4.txt"))
        write_file("CONTENT", os.path.join(self.root_dir, "c", "3", "0.txt"))
        write_file("CONTENT", os.path.join(self.root_dir, "c", "1", "0.txt"))
        self.assertTrue(
            get_one_file_path_from_dir(os.path.join(self.root_dir, "c")),
            os.path.join(self.root_dir, "c", "4.txt"))
Beispiel #10
0
    def test_write_file_and_read_file(self):
        from zzpy import write_file
        from zzpy import read_file
        from zzpy import read_bin_file
        import os

        # 校验当文件目录不存在时,是否可以写文件
        self.assertFalse(os.path.exists(self.root_dir))

        for i, write_content in enumerate(
            ("abc", "你好", "abc你好", "你好abc", "こんにちは", "こんにちはzero", "zeroこんにちは",
             "你好zeroこんにちは", "こんにちはzero你好", "")):
            # default encoding
            file_path = os.path.join(self.root_dir, f"{i}-default.txt")
            write_file(content=write_content, file_path=file_path)
            self.assertTrue(os.path.exists(file_path))
            read_content = read_file(file_path)
            self.assertEqual(read_content, write_content)

            # encoding utf8
            file_path = os.path.join(self.root_dir, f"{i}-utf8.txt")
            write_file(content=write_content,
                       file_path=file_path,
                       encoding="utf8")
            self.assertTrue(os.path.exists(file_path))
            read_content = read_file(file_path)
            self.assertEqual(read_content, write_content)

            # encoding gbk
            file_path = os.path.join(self.root_dir, f"{i}-gbk.txt")
            write_file(content=write_content,
                       file_path=file_path,
                       encoding="gbk")
            self.assertTrue(os.path.exists(file_path))
            read_content = read_file(file_path)
            self.assertEqual(read_content, write_content)
Beispiel #11
0
    def test_get_file_name_list_from_dir(self):
        from zzpy import get_file_name_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_name_list_from_dir(self.root_dir)
        self.assertSetEqual(
            set(file_path_list),
            {"r.txt", "10.txt", "11.txt", "20.txt", "21.txt", "30.txt"})