|
45 | 45 | add_path_test/1, |
46 | 46 | %% Immediate application tests |
47 | 47 | import_applies_to_running_interpreter_test/1, |
48 | | - path_applies_to_running_interpreter_test/1 |
| 48 | + path_applies_to_running_interpreter_test/1, |
| 49 | + %% Config initialization tests |
| 50 | + init_from_config_test/1 |
49 | 51 | ]). |
50 | 52 |
|
51 | 53 | all() -> |
@@ -82,7 +84,9 @@ groups() -> |
82 | 84 | add_path_test, |
83 | 85 | %% Immediate application tests |
84 | 86 | import_applies_to_running_interpreter_test, |
85 | | - path_applies_to_running_interpreter_test |
| 87 | + path_applies_to_running_interpreter_test, |
| 88 | + %% Config initialization tests |
| 89 | + init_from_config_test |
86 | 90 | ]}]. |
87 | 91 |
|
88 | 92 | init_per_suite(Config) -> |
@@ -845,3 +849,57 @@ path_applies_to_running_interpreter_test(Config) -> |
845 | 849 | ok = py_import:clear_paths(), |
846 | 850 |
|
847 | 851 | ct:pal("add_path applies immediately to running interpreter"). |
| 852 | + |
| 853 | +%% ============================================================================ |
| 854 | +%% Config Initialization Tests |
| 855 | +%% ============================================================================ |
| 856 | + |
| 857 | +%% @doc Test that imports and paths are loaded from application config |
| 858 | +init_from_config_test(Config) -> |
| 859 | + %% Clear existing state |
| 860 | + ok = py_import:clear_imports(), |
| 861 | + ok = py_import:clear_paths(), |
| 862 | + |
| 863 | + %% Create test module in priv_dir |
| 864 | + PrivDir = ?config(priv_dir, Config), |
| 865 | + ModuleDir = filename:join(PrivDir, "config_test"), |
| 866 | + ok = filelib:ensure_dir(filename:join(ModuleDir, "dummy")), |
| 867 | + ModulePath = filename:join(ModuleDir, "config_test_mod.py"), |
| 868 | + ok = file:write_file(ModulePath, <<"CONFIG_VALUE = 123\n">>), |
| 869 | + |
| 870 | + %% Set application config |
| 871 | + ok = application:set_env(erlang_python, imports, [{json, dumps}, {base64, b64encode}]), |
| 872 | + ok = application:set_env(erlang_python, paths, [ModuleDir]), |
| 873 | + |
| 874 | + %% Re-run init to load config |
| 875 | + ok = py_import:init(), |
| 876 | + |
| 877 | + %% Verify imports were loaded in registry |
| 878 | + Imports = py_import:all_imports(), |
| 879 | + ?assert(lists:member({<<"json">>, <<"dumps">>}, Imports)), |
| 880 | + ?assert(lists:member({<<"base64">>, <<"b64encode">>}, Imports)), |
| 881 | + |
| 882 | + %% Verify paths were loaded in registry |
| 883 | + Paths = py_import:all_paths(), |
| 884 | + ModuleDirBin = list_to_binary(ModuleDir), |
| 885 | + ?assert(lists:member(ModuleDirBin, Paths)), |
| 886 | + |
| 887 | + %% Create a new context and verify imports/paths are applied |
| 888 | + {ok, Ctx} = py_context:new(#{mode => worker}), |
| 889 | + |
| 890 | + %% Verify json.dumps works (from config imports) |
| 891 | + {ok, JsonResult} = py_context:call(Ctx, json, dumps, [[1, 2, 3]], #{}), |
| 892 | + ?assertEqual(<<"[1, 2, 3]">>, JsonResult), |
| 893 | + |
| 894 | + %% Verify custom module from config path works |
| 895 | + {ok, ConfigValue} = py_context:eval(Ctx, <<"__import__('config_test_mod').CONFIG_VALUE">>), |
| 896 | + ?assertEqual(123, ConfigValue), |
| 897 | + |
| 898 | + %% Clean up |
| 899 | + py_context:destroy(Ctx), |
| 900 | + ok = application:unset_env(erlang_python, imports), |
| 901 | + ok = application:unset_env(erlang_python, paths), |
| 902 | + ok = py_import:clear_imports(), |
| 903 | + ok = py_import:clear_paths(), |
| 904 | + |
| 905 | + ct:pal("init loads imports and paths from config and applies to new contexts"). |
0 commit comments