Skip to content

Commit 7fbca1b

Browse files
committed
Update test_path.py
1 parent 11667ee commit 7fbca1b

File tree

1 file changed

+123
-18
lines changed

1 file changed

+123
-18
lines changed

qtapputils/widgets/tests/test_path.py

Lines changed: 123 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# ---- Third party imports
1818
import pytest
1919
from qtpy.QtCore import Qt
20+
from qtpy.QtGui import QIcon
2021

2122
# ---- Local imports
2223
from qtapputils.widgets.path import PathBoxWidget, QFileDialog
@@ -29,8 +30,6 @@
2930
def pathbox(qtbot):
3031
pathbox = PathBoxWidget(
3132
parent=None,
32-
path='',
33-
directory='',
3433
path_type='getSaveFileName',
3534
filters=None
3635
)
@@ -42,32 +41,138 @@ def pathbox(qtbot):
4241
# =============================================================================
4342
# ---- Tests for the PathBoxWidget
4443
# =============================================================================
45-
def test_getopen_filename(qtbot, pathbox, mocker, tmp_path):
46-
"""Test that getting a file name is working as expected."""
44+
def test_initialization_no_icon(qtbot):
45+
pathbox = PathBoxWidget()
46+
qtbot.addWidget(pathbox)
47+
48+
assert pathbox.is_empty()
4749
assert not pathbox.is_valid()
50+
assert pathbox.path() == ""
51+
assert pathbox.directory()
52+
53+
54+
def test_initialization_with_icon(qtbot):
55+
icon = QIcon()
56+
pathbox = PathBoxWidget(browse_icon=icon)
57+
qtbot.addWidget(pathbox)
58+
4859
assert pathbox.is_empty()
49-
assert pathbox.path() == ''
50-
assert osp.samefile(pathbox.directory(), osp.expanduser('~'))
60+
assert not pathbox.is_valid()
5161

52-
# Create an empty file.
53-
selectedfilter = 'Text File (*.txt)'
54-
selectedfilename = osp.join(tmp_path, 'pathbox_testfile.txt')
55-
with open(selectedfilename, 'w') as txtfile:
56-
txtfile.write('test')
5762

58-
# Patch the open file dialog and select the test file.
63+
def test_set_and_get_path_valid(tmp_path, qtbot):
64+
# Create a valid file
65+
f = tmp_path / "myfile.txt"
66+
f.write_text("content")
67+
68+
pathbox = PathBoxWidget(path_type="getOpenFileName")
69+
qtbot.addWidget(pathbox)
70+
pathbox.set_path(str(f))
71+
72+
assert not pathbox.is_empty()
73+
assert pathbox.is_valid()
74+
assert pathbox.path() == str(f)
75+
assert pathbox.directory() == str(tmp_path)
76+
77+
78+
def test_set_and_get_path_invalid(qtbot):
79+
pathbox = PathBoxWidget()
80+
qtbot.addWidget(pathbox)
81+
pathbox.set_path("/tmp/nonexistent_file.txt")
82+
83+
assert pathbox.path() == "/tmp/nonexistent_file.txt"
84+
assert pathbox.directory() == osp.expanduser('~')
85+
assert not pathbox.is_valid()
86+
87+
88+
def test_set_directory_valid(tmp_path, qtbot):
89+
d = tmp_path / "dir"
90+
d.mkdir()
91+
92+
pathbox = PathBoxWidget()
93+
qtbot.addWidget(pathbox)
94+
pathbox.set_directory(str(d))
95+
96+
assert pathbox.directory() == str(d)
97+
98+
99+
def test_set_directory_invalid(qtbot):
100+
pathbox = PathBoxWidget()
101+
qtbot.addWidget(pathbox)
102+
pathbox.set_directory("/tmp/nonexistent_dir")
103+
104+
# Should fallback to home if directory is invalid
105+
assert pathbox.directory() == osp.expanduser('~')
106+
107+
108+
def test_signal_emitted_on_path_change(tmp_path, qtbot):
109+
f = tmp_path / "file.txt"
110+
f.write_text("abc")
111+
112+
pathbox = PathBoxWidget()
113+
qtbot.addWidget(pathbox)
114+
115+
with qtbot.waitSignal(pathbox.sig_path_changed) as blocker:
116+
pathbox.set_path(str(f))
117+
assert blocker.args == [str(f)]
118+
119+
120+
def test_browse_path_get_existing_directory(mocker, tmp_path, qtbot):
121+
d = tmp_path / "bro_dir"
122+
d.mkdir()
123+
124+
pathbox = PathBoxWidget(path_type="getExistingDirectory")
125+
qtbot.addWidget(pathbox)
126+
59127
qfdialog_patcher = mocker.patch.object(
60128
QFileDialog,
61-
'getSaveFileName',
62-
return_value=(selectedfilename, selectedfilter)
129+
'getExistingDirectory',
130+
return_value=str(d)
63131
)
64-
qtbot.mouseClick(pathbox.browse_btn, Qt.LeftButton)
65132

133+
pathbox.browse_path()
66134
assert qfdialog_patcher.call_count == 1
135+
assert pathbox.path() == str(d)
136+
assert pathbox.directory() == str(tmp_path)
67137
assert pathbox.is_valid()
68-
assert not pathbox.is_empty()
69-
assert pathbox.path() == selectedfilename
70-
assert osp.samefile(pathbox.directory(), tmp_path)
138+
139+
140+
def test_browse_path_get_open_file_name(mocker, tmp_path, qtbot):
141+
f = tmp_path / "bro_file.txt"
142+
f.write_text("abc")
143+
144+
pathbox = PathBoxWidget(path_type="getOpenFileName")
145+
qtbot.addWidget(pathbox)
146+
147+
qfdialog_patcher = mocker.patch.object(
148+
QFileDialog,
149+
'getOpenFileName',
150+
return_value=(str(f), "")
151+
)
152+
153+
pathbox.browse_path()
154+
assert qfdialog_patcher.call_count == 1
155+
assert pathbox.path() == str(f)
156+
assert pathbox.directory() == str(tmp_path)
157+
assert pathbox.is_valid()
158+
159+
160+
def test_browse_path_get_save_file_name(mocker, tmp_path, qtbot):
161+
f = tmp_path / "save_file.txt"
162+
163+
pathbox = PathBoxWidget(path_type="getSaveFileName")
164+
qtbot.addWidget(pathbox)
165+
166+
qfdialog_patcher = mocker.patch.object(
167+
QFileDialog,
168+
'getSaveFileName',
169+
return_value=(str(f), "")
170+
)
171+
172+
pathbox.browse_path()
173+
assert qfdialog_patcher.call_count == 1
174+
assert pathbox.path() == str(f)
175+
assert pathbox.directory() == str(tmp_path)
71176

72177

73178
if __name__ == "__main__":

0 commit comments

Comments
 (0)