test_module_import_helper.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. from core.helper.module_import_helper import import_module_from_source, load_single_subclass_from_source
  3. from tests.integration_tests.utils.parent_class import ParentClass
  4. def test_loading_subclass_from_source():
  5. current_path = os.getcwd()
  6. module = load_single_subclass_from_source(
  7. module_name="ChildClass", script_path=os.path.join(current_path, "child_class.py"), parent_type=ParentClass
  8. )
  9. assert module
  10. assert module.__name__ == "ChildClass"
  11. def test_load_import_module_from_source():
  12. current_path = os.getcwd()
  13. module = import_module_from_source(
  14. module_name="ChildClass", py_file_path=os.path.join(current_path, "child_class.py")
  15. )
  16. assert module
  17. assert module.__name__ == "ChildClass"
  18. def test_lazy_loading_subclass_from_source():
  19. current_path = os.getcwd()
  20. clz = load_single_subclass_from_source(
  21. module_name="LazyLoadChildClass",
  22. script_path=os.path.join(current_path, "lazy_load_class.py"),
  23. parent_type=ParentClass,
  24. use_lazy_loader=True,
  25. )
  26. instance = clz("dify")
  27. assert instance.get_name() == "dify"