44from pathlib import Path
55
66import pytest
7- from pydantic import BaseModel , ConfigDict
87
98from openbb_core .app .model .system_settings import SystemSettings
109
1110
12- class MockSystemSettings (BaseModel ):
13- """Mock SystemSettings."""
14-
15- model_config = ConfigDict (extra = "allow" , populate_by_name = True )
16-
17-
1811def test_system_settings ():
1912 """Test the SystemSettings class."""
2013 sys = SystemSettings ()
@@ -29,74 +22,78 @@ def test_system_settings_repr():
2922 assert "openbb_directory" in result
3023
3124
32- def test_create_openbb_directory_directory_and_files_not_exist (tmpdir ):
25+ def test_create_openbb_directory_directory_and_files_not_exist (tmpdir , monkeypatch ):
3326 """Test the create_openbb_directory method."""
3427 # Arrange
35- values = MockSystemSettings (
36- ** {
37- "openbb_directory" : str (tmpdir .join ("openbb" )),
38- "user_settings_path" : str (tmpdir .join ("user_settings.json" )),
39- "system_settings_path" : str (tmpdir .join ("system_settings.json" )),
40- }
28+ obb_dir = str (tmpdir .join ("openbb" ))
29+ user_settings = str (tmpdir .join ("user_settings.json" ))
30+ system_settings = str (tmpdir .join ("system_settings.json" ))
31+
32+ monkeypatch .setenv ("OPENBB_DIRECTORY" , obb_dir )
33+ monkeypatch .setenv ("USER_SETTINGS_PATH" , user_settings )
34+ monkeypatch .setenv ("SYSTEM_SETTINGS_PATH" , system_settings )
35+
36+ # Act - The validator runs automatically during instantiation
37+ sys = SystemSettings (
38+ openbb_directory = obb_dir ,
39+ user_settings_path = user_settings ,
40+ system_settings_path = system_settings ,
4141 )
4242
43- # Act
44- SystemSettings .create_openbb_directory (values ) # type: ignore[operator]
45-
4643 # Assert
47- assert os .path .exists (values .openbb_directory ) # type: ignore[attr-defined]
48- assert os .path .exists (values .user_settings_path ) # type: ignore[attr-defined]
49- assert os .path .exists (values .system_settings_path ) # type: ignore[attr-defined]
44+ assert os .path .exists (sys .openbb_directory )
45+ assert os .path .exists (sys .user_settings_path )
46+ assert os .path .exists (sys .system_settings_path )
5047
5148
5249def test_create_openbb_directory_directory_exists_user_settings_missing (tmpdir ):
5350 """Test the create_openbb_directory method."""
5451 # Arrange
55- values = MockSystemSettings (
56- ** {
57- "openbb_directory" : str (tmpdir .join ("openbb" )),
58- "user_settings_path" : str (tmpdir .join ("user_settings.json" )),
59- "system_settings_path" : str (tmpdir .join ("system_settings.json" )),
60- }
61- )
52+ obb_dir = str (tmpdir .join ("openbb" ))
53+ user_settings = str (tmpdir .join ("user_settings.json" ))
54+ system_settings = str (tmpdir .join ("system_settings.json" ))
6255
6356 # Create the openbb directory
64- Path (values . openbb_directory ).mkdir (parents = True , exist_ok = True ) # type: ignore[attr-defined]
57+ Path (obb_dir ).mkdir (parents = True , exist_ok = True )
6558
66- # Act
67- SystemSettings .create_openbb_directory (values ) # type: ignore[operator]
59+ # Act - The validator runs automatically during instantiation
60+ sys = SystemSettings (
61+ openbb_directory = obb_dir ,
62+ user_settings_path = user_settings ,
63+ system_settings_path = system_settings ,
64+ )
6865
6966 # Assert
70- assert os .path .exists (values .openbb_directory ) # type: ignore[attr-defined]
71- assert os .path .exists (values .user_settings_path ) # type: ignore[attr-defined]
72- assert os .path .exists (values .system_settings_path ) # type: ignore[attr-defined]
67+ assert os .path .exists (sys .openbb_directory )
68+ assert os .path .exists (sys .user_settings_path )
69+ assert os .path .exists (sys .system_settings_path )
7370
7471
7572def test_create_openbb_directory_directory_exists_system_settings_missing (tmpdir ):
7673 """Test the create_openbb_directory method."""
7774 # Arrange
78- values = MockSystemSettings (
79- ** {
80- "openbb_directory" : str (tmpdir .join ("openbb" )),
81- "user_settings_path" : str (tmpdir .join ("user_settings.json" )),
82- "system_settings_path" : str (tmpdir .join ("system_settings.json" )),
83- }
84- )
75+ obb_dir = str (tmpdir .join ("openbb" ))
76+ user_settings = str (tmpdir .join ("user_settings.json" ))
77+ system_settings = str (tmpdir .join ("system_settings.json" ))
8578
8679 # Create the openbb directory
87- Path (values . openbb_directory ).mkdir (parents = True , exist_ok = True ) # type: ignore[attr-defined]
80+ Path (obb_dir ).mkdir (parents = True , exist_ok = True )
8881
8982 # Create the user_settings.json file
90- with open (values . user_settings_path , "w" ) as f : # type: ignore[attr-defined]
83+ with open (user_settings , "w" ) as f :
9184 f .write ("{}" )
9285
93- # Act
94- SystemSettings .create_openbb_directory (values ) # type: ignore[operator]
86+ # Act - The validator runs automatically during instantiation
87+ sys = SystemSettings (
88+ openbb_directory = obb_dir ,
89+ user_settings_path = user_settings ,
90+ system_settings_path = system_settings ,
91+ )
9592
9693 # Assert
97- assert os .path .exists (values .openbb_directory ) # type: ignore[attr-defined]
98- assert os .path .exists (values .user_settings_path ) # type: ignore[attr-defined]
99- assert os .path .exists (values .system_settings_path ) # type: ignore[attr-defined]
94+ assert os .path .exists (sys .openbb_directory )
95+ assert os .path .exists (sys .user_settings_path )
96+ assert os .path .exists (sys .system_settings_path )
10097
10198
10299@pytest .mark .parametrize (
0 commit comments