Skip to content

Commit 652071c

Browse files
Update tests for browser preferences refactor
1 parent b35cced commit 652071c

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

pydoll/browser/interfaces.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC, abstractmethod
22

3+
from pydoll.browser.preference_types import BrowserPreferences
34
from pydoll.constants import PageLoadState
45

56

@@ -25,7 +26,7 @@ def add_argument(self, argument: str):
2526

2627
@property
2728
@abstractmethod
28-
def browser_preferences(self) -> dict:
29+
def browser_preferences(self) -> BrowserPreferences:
2930
pass
3031

3132
@property

pydoll/browser/options.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from contextlib import suppress
2-
from typing import Any, Optional
2+
from typing import Any, Dict, Optional, cast
33

44
from pydoll.browser.interfaces import Options
55
from pydoll.browser.preference_types import PREFERENCE_SCHEMA, BrowserPreferences
@@ -154,9 +154,11 @@ def _set_pref_path(self, path: list, value):
154154
self._validate_pref_path(path)
155155
self._validate_pref_value(path, value)
156156

157-
d = self._browser_preferences
157+
d: Dict[str, Any] = cast(Dict[str, Any], self._browser_preferences)
158158
for key in path[:-1]:
159-
d = d.setdefault(key, {})
159+
if key not in d or not isinstance(d[key], dict):
160+
d[key] = {}
161+
d = cast(Dict[str, Any], d[key])
160162
d[path[-1]] = value
161163

162164
@staticmethod
@@ -217,10 +219,10 @@ def _get_pref_path(self, path: list):
217219
except InvalidPreferencePath:
218220
raise
219221

220-
nested_preferences = self._browser_preferences
222+
nested_preferences: Dict[str, Any] = cast(Dict[str, Any], self._browser_preferences)
221223
with suppress(KeyError, TypeError):
222224
for key in path:
223-
nested_preferences = nested_preferences[key]
225+
nested_preferences = cast(Dict[str, Any], nested_preferences[key])
224226
return nested_preferences
225227
return None
226228

pydoll/browser/preference_types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import NotRequired, TypedDict
1+
from typing import TypedDict
2+
3+
from typing_extensions import NotRequired
24

35

46
class DownloadPreferences(TypedDict):

tests/test_browser/test_browser_preferences.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,15 @@ def test_set_pref_path_creates_structure():
153153
}
154154
})
155155
options._set_pref_path(['profile', 'default_content_setting_values', 'popups'], 0)
156-
assert cast(Any, options.browser_preferences)['profile']['default_content_setting_values']['popups'] == 0
156+
assert cast(Any, options.browser_preferences)['profile']['default_content_setting_values']['popups'] == 0
157+
158+
159+
def test_validate_pref_value_dict_validation():
160+
"""Test validation when expected type is dict."""
161+
options = ChromiumOptions()
162+
# Test valid dict
163+
options._validate_pref_value(['intl'], {'accept_languages': 'pt-BR'})
164+
165+
# Test invalid dict (should raise)
166+
with pytest.raises(InvalidPreferenceValue):
167+
options._validate_pref_value(['intl'], 'not_a_dict')

tests/test_managers/test_browser_managers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,14 @@ def test_initialize_options_preserves_custom_arguments():
245245
assert '--custom-flag' in result.arguments
246246
assert '--no-first-run' in result.arguments
247247
assert '--no-default-browser-check' in result.arguments
248+
249+
250+
def test_handle_cleanup_error_cache_file_retry_fails_raises(temp_manager):
251+
"""Test that non-cache files raise PermissionError when retry fails."""
252+
func_mock = Mock()
253+
temp_manager.retry_process_file = Mock(side_effect=PermissionError)
254+
255+
# Non-cache file should raise after retry fails
256+
path = "/tmp/some_file.txt"
257+
with pytest.raises(PermissionError):
258+
temp_manager.handle_cleanup_error(func_mock, path, (PermissionError, PermissionError(), None))

0 commit comments

Comments
 (0)