def test_import(py_file, expect_exception): p = os.path.join(THIS_DIR, "assets") try: mod = pw.import_entrypoint(py_file, p) assert expect_exception == False except Exception: assert expect_exception == True
def test_import(py_file, expect_exception): p = os.path.join(THIS_DIR, "assets") try: _ = pw.import_entrypoint(py_file, p) assert not expect_exception except Exception: assert expect_exception
def append_entrypoint(app: typing.Generic, entrypoint: str, location: str) -> typing.Generic: """Add routes/functions defined in entrypoint.""" mod = pw.import_entrypoint(entrypoint, location) fm = pw.get_func_annotations(mod) if not any([e.endpoint for e in fm]): raise Exception("no endpoints defined") openapi_spec = po.base_spec(title=entrypoint) # iterate over annotations in usercode for f in fm: if f.endpoint: log.debug( f"adding endpoint for func: {f.__name__} (func_args: {f.comment_args})" ) app.add_url_rule(f.endpoint_path, f.endpoint_path, pw.func_wrapper(f), methods=[f.request_method.upper()], strict_slashes=False) # add info about endpoint to api spec po.path_spec(openapi_spec, f) app.add_url_rule( "/openapi.yaml", "/openapi.yaml", openapi_spec.to_yaml, methods=["GET"], ) return app
def test_annotated_scripts(py_file, expected): p = os.path.join(THIS_DIR, "assets") mod = pw.import_entrypoint(py_file, p) fas = pw.get_func_annotations(mod) flatten = lambda l: [item for sublist in l for item in sublist] for a in expected: assert a in flatten([ac.comment_args for ac in fas])
def test_func_exec(py_file, func, expected): p = os.path.join(THIS_DIR, "assets") mod = pw.import_entrypoint(py_file, p) assert mod.__getattribute__(func)() == expected