Beispiel #1
0
    def setUpClass(cls) -> None:
        """"""
        from yzrpc.commands import CommandUtility
        cmd = CommandUtility()
        cmd.run_from_argv(['yzrpc', 'createproject', 'myproject'])
        cmd.run_from_argv(
            ['yzrpc', 'createapp', 'my_app1', "-D", "./myproject/src"])
        cmd.run_from_argv(
            ['yzrpc', 'generateproto', '-D', "./myproject/src/protos"])
        cmd.run_from_argv([
            'yzrpc', 'generatemodule', '-I', "./myproject/src/protos", '-O',
            "./myproject/src/services"
        ])

        # self.middleware_func = Mock()  # yzrpc.exceptions.InvalidMiddleware

        def middle_func(*args):
            return args

        cls.middleware_func = middle_func
        cls.server = Server(
            host='localhost',
            port=4444,
            max_workers=1,
            # middlewares=[self.middleware_func]
        )
        cls.server.run()
Beispiel #2
0
 def setUp(self) -> None:
     self.cmd = CommandUtility()
     self.project_name = 'myproject'
     self.app_name = 'myapp1'
     self.target_path = f'{self.project_name}/src'
     self.default_target = './src'
     self.template_path = './app_tmp'
     self.cmd.run_from_argv(['yzrpc', 'createproject', self.project_name])
Beispiel #3
0
 def setUp(self) -> None:
     self.cmd = CommandUtility()
     self.project_name = 'myproject'
     self.app_name = 'myapp1'
     self.target_path = join(self.project_name, 'src')
     self.destination = join(self.target_path, 'protos')
     self.output = join(self.target_path, 'services')
     self.default_target = './src'
     # self.template_path = './app_tmp'
     self.cmd.run_from_argv(['yzrpc', 'createproject', self.project_name])
     self.cmd.run_from_argv(
         ['yzrpc', 'createapp', self.app_name, '-D', self.target_path])
     self.cmd.run_from_argv(
         ['yzrpc', 'generateproto', '-D', self.destination])
Beispiel #4
0
 def setUpClass(cls) -> None:
     cls.cmd = CommandUtility()
     cls.project_name = 'myproject'
     cls.app_name = 'myapp1'
     cls.target_path = join(cls.project_name, 'src')
     cls.destination = join(cls.target_path, 'protos')
     cls.output = join(cls.target_path, 'services')
     cls.default_target = './src'
     # cls.template_path = './app_tmp'
     cls.cmd.run_from_argv(['yzrpc', 'createproject', cls.project_name])
     cls.cmd.run_from_argv(
         ['yzrpc', 'createapp', cls.app_name, '-D', cls.target_path])
     cls.cmd.run_from_argv(
         ['yzrpc', 'generateproto', '-D', cls.destination])
     cls.cmd.run_from_argv([
         'yzrpc', 'generatemodule', '-I', cls.destination, '-O', cls.output
     ])
Beispiel #5
0
def execute_from_command_line(argv=None):
    """Run a ManagementUtility."""
    # utility = ManagementUtility(argv)
    # utility.execute()
    from yzrpc.commands import CommandUtility
    if argv[0] == 'yzrpc':
        utility = CommandUtility()
    else:
        utility = CommandUtility(command_dir=os.path.join(
            os.path.dirname(__file__), "commands"),
                                 module_path="yzcore.core.management.commands")
    utility.run_from_argv(argv)
Beispiel #6
0
class TestCreateAppCommand(unittest.TestCase):
    def setUp(self) -> None:
        self.cmd = CommandUtility()
        self.project_name = 'myproject'
        self.app_name = 'myapp1'
        self.target_path = f'{self.project_name}/src'
        self.default_target = './src'
        self.template_path = './app_tmp'
        self.cmd.run_from_argv(['yzrpc', 'createproject', self.project_name])

    def tearDown(self) -> None:
        del self.cmd
        import shutil
        # 删除创建的工程
        try:
            shutil.rmtree(self.project_name)
        except FileNotFoundError:
            pass
        try:
            shutil.rmtree(self.default_target)
        except FileNotFoundError:
            pass
        try:
            shutil.rmtree(self.template_path)
        except FileNotFoundError:
            pass

    def _judge_file_exist(self, src_path):
        self.assertTrue(exists(join(src_path, '__init__.py')))
        self.assertTrue(exists(join(src_path, 'controllers.py')))
        self.assertTrue(exists(join(src_path, 'models.py')))
        self.assertTrue(exists(join(src_path, 'schemas.py')))
        self.assertTrue(exists(join(src_path, 'views.py')))
        self.assertTrue(exists(join(src_path, 'tests.py')))

    def test_no_app_name(self):
        try:
            self.cmd.run_from_argv(['yzrpc', 'createapp'])
        except SystemExit as e:
            self.assertEqual(e.args[0], 2)

    def test_app_name_with_path(self):
        expect_raise = "The app name can't have a path; " \
                       "please use the '-D' parameter for the path."
        with self.assertRaises(CommandError) as e:
            self.cmd.run_from_argv(['yzrpc', 'createapp', './my/app1'])
        exp = e.exception
        self.assertEqual(exp.args[0], expect_raise)

    def test_create_project_with_success(self):
        os.mkdir(self.default_target)
        self.cmd.run_from_argv(['yzrpc', 'createapp', self.app_name])
        src_path = join('src', 'apps', self.app_name)
        self._judge_file_exist(src_path)

    def test_target_path_with_not_exist(self):
        expect_raise = "Destination directory {} does not exist, " \
                       "please create it first.".format(
            os.path.abspath(os.path.expanduser('./src'))
        )
        with self.assertRaises(CommandError) as e:
            self.cmd.run_from_argv(['yzrpc', 'createapp', self.app_name])
        exp = e.exception
        self.assertEqual(exp.args[0], expect_raise)

    def test_target_path_with_exist(self):
        self.cmd.run_from_argv(
            ['yzrpc', 'createapp', self.app_name, '-D', self.target_path])
        src_path = join(self.project_name, 'src', 'apps', self.app_name)
        self._judge_file_exist(src_path)

    def test_template_with_path_not_exist(self):
        expect_raise = "The template dir is not exist."
        with self.assertRaises(CommandError) as e:
            self.cmd.run_from_argv([
                'yzrpc', 'createapp', self.app_name, '-D', self.target_path,
                '-T', self.template_path
            ])
        exp = e.exception
        self.assertEqual(exp.args[0], expect_raise)

    def test_template_with_path_exist(self):
        expect_txt = "This is template test."
        os.mkdir(self.template_path)
        file_path = join(self.template_path, 'temp_test.txt')
        with open(file_path, 'w') as f:
            f.write(expect_txt)
            f.flush()
        self.cmd.run_from_argv([
            'yzrpc', 'createapp', self.app_name, '-D', self.target_path, '-T',
            self.template_path
        ])
        proto_path = join(self.project_name, 'src', 'temp_test.txt')
        self.assertTrue(exists(proto_path))
        with open(proto_path, 'r') as f:
            content = f.read()
        self.assertEqual(content, expect_txt)
Beispiel #7
0
 def setUp(self) -> None:
     self.cmd = CommandUtility()
     self.project_name = 'myproject'
     self.target_path = './tgpath'
     self.template_path = './proj_tmp'
Beispiel #8
0
def main():
    cmd = CommandUtility()
    cmd.run_from_argv(sys.argv)
Beispiel #9
0
class TestGenerateModuleCommand(unittest.TestCase):
    def setUp(self) -> None:
        self.cmd = CommandUtility()
        self.project_name = 'myproject'
        self.app_name = 'myapp1'
        self.target_path = join(self.project_name, 'src')
        self.destination = join(self.target_path, 'protos')
        self.output = join(self.target_path, 'services')
        self.default_target = './src'
        # self.template_path = './app_tmp'
        self.cmd.run_from_argv(['yzrpc', 'createproject', self.project_name])
        self.cmd.run_from_argv(
            ['yzrpc', 'createapp', self.app_name, '-D', self.target_path])
        self.cmd.run_from_argv(
            ['yzrpc', 'generateproto', '-D', self.destination])

    def tearDown(self) -> None:
        del self.cmd
        import shutil
        # 删除创建的工程
        try:
            shutil.rmtree(self.project_name)
        except FileNotFoundError:
            pass
        try:
            shutil.rmtree(self.default_target)
        except FileNotFoundError:
            pass

    def test_generate_module_with_default(self):
        # expect_raise = "Not found any of proto file."
        with self.assertRaises(SystemExit) as e:
            self.cmd.run_from_argv(['yzrpc', 'generatemodule'])
        exp = e.exception
        self.assertEqual(exp.args[0], 1)

    def test_generate_module_with_success(self):
        os.mkdir(self.default_target)
        self.cmd.run_from_argv([
            'yzrpc', 'generatemodule', '-I', self.destination, '-O',
            self.output
        ])
        src_path = join(self.target_path, 'services')
        self.assertTrue(exists(join(src_path, f'{self.app_name}_pb2.py')))
        self.assertTrue(exists(join(src_path, f'{self.app_name}_pb2_grpc.py')))

    def test_proto_path_with_not_exist(self):
        # expect_raise = "Not found any of proto file."
        with self.assertRaises(SystemExit) as e:
            self.cmd.run_from_argv(
                ['yzrpc', 'generatemodule', '-I', self.default_target])
        exp = e.exception
        self.assertEqual(exp.args[0], 1)
Beispiel #10
0
class TestGenerateProtoCommand(unittest.TestCase):
    def setUp(self) -> None:
        self.cmd = CommandUtility()
        self.project_name = 'myproject'
        self.app_name = 'myapp1'
        self.target_path = f'{self.project_name}/src'
        self.destination = f'{self.target_path}/protos'
        self.default_target = './src'
        self.template_path = './proto_tmp'
        self.cmd.run_from_argv(['yzrpc', 'createproject', self.project_name])
        self.cmd.run_from_argv(
            ['yzrpc', 'createapp', self.app_name, '-D', self.target_path])

    def tearDown(self) -> None:
        del self.cmd
        import shutil
        # 删除创建的工程
        try:
            shutil.rmtree(self.project_name)
        except FileNotFoundError:
            pass
        try:
            shutil.rmtree(self.default_target)
        except FileNotFoundError:
            pass
        try:
            shutil.rmtree(self.template_path)
        except FileNotFoundError:
            pass

    def test_generate_proto_with_success(self):
        self.cmd.run_from_argv(
            ['yzrpc', 'generateproto', '-D', self.destination])
        src_path = join(self.project_name, 'src', 'protos',
                        f'{self.app_name}.proto')
        self.assertTrue(exists(src_path))
        with open(src_path, 'r') as f:
            self.assertIn(f'package {self.app_name};\n', f.readlines())

    def test_template_with_path_not_exist(self):
        expect_raise = "The template dir is not exist."
        with self.assertRaises(CommandError) as e:
            self.cmd.run_from_argv(
                ['yzrpc', 'generateproto', '-T', self.template_path])
        exp = e.exception
        self.assertEqual(exp.args[0], expect_raise)

    def test_template_with_path_exist(self):
        expect_txt = "This is template test."
        os.mkdir(self.template_path)
        file_path = join(self.template_path, 'proto.tmpl')
        with open(file_path, 'w') as f:
            f.write(expect_txt)
            f.flush()
        self.cmd.run_from_argv([
            'yzrpc', 'generateproto', '-D', self.destination, '-T',
            self.template_path
        ])
        proto_path = join(self.project_name, 'src', 'protos',
                          f"{self.app_name}.proto")
        self.assertTrue(exists(proto_path))
        with open(proto_path, 'r') as f:
            content = f.read()
        self.assertEqual(content, expect_txt)