Skip to content

Commit 3367b04

Browse files
committed
Testcases for subscribing a single file.
1 parent 1ee35f1 commit 3367b04

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

tests/test_single_file_watched.py

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
"""Test cases when a single file is subscribed."""
2+
3+
import time
4+
from os import mkdir
5+
6+
from .utils import P, StartWatching, TestEventQueue
7+
8+
SLEEP = 0.1
9+
10+
11+
def test_selftest(p: P, start_watching: StartWatching, event_queue: TestEventQueue) -> None:
12+
"""Check if all events are catched in SLEEP time."""
13+
14+
emitter = start_watching(path=p()) # Pretty sure this should work
15+
16+
with open(p("file1.bak"), "w") as fp:
17+
fp.write("test1")
18+
19+
with open(p("file2.bak"), "w") as fp:
20+
fp.write("test2")
21+
22+
time.sleep(SLEEP)
23+
24+
found_files = set()
25+
try:
26+
while event_queue.qsize():
27+
event, _ = event_queue.get()
28+
if event.is_directory: # Not catched on Windows
29+
assert event.src_path == p()
30+
else:
31+
found_files.add(event.src_path)
32+
finally:
33+
emitter.stop()
34+
35+
assert len(found_files) == 2, "Number of expected files differ. Increase sleep time."
36+
37+
38+
def test_file_access(p: P, start_watching: StartWatching, event_queue: TestEventQueue) -> None:
39+
"""Check if file fires events."""
40+
41+
file1 = "file1.bak"
42+
tmpfile = p(file1)
43+
44+
with open(tmpfile, "w") as fp:
45+
fp.write("init1")
46+
47+
emitter = start_watching(path=tmpfile)
48+
49+
# This is what we want to see
50+
with open(tmpfile, "w") as fp:
51+
fp.write("test1")
52+
53+
time.sleep(SLEEP)
54+
55+
try:
56+
while event_queue.qsize():
57+
event, _ = event_queue.get()
58+
if event.is_directory:
59+
assert event.src_path == p()
60+
continue
61+
assert event.src_path.endswith(file1)
62+
break
63+
else:
64+
raise AssertionError # No event catched
65+
finally:
66+
emitter.stop()
67+
68+
69+
def test_file_access_multiple(p: P, start_watching: StartWatching, event_queue: TestEventQueue) -> None:
70+
"""Check if file fires events multiple times."""
71+
72+
file1 = "file1.bak"
73+
tmpfile = p(file1)
74+
75+
with open(tmpfile, "w") as fp:
76+
fp.write("init1")
77+
78+
emitter = start_watching(path=tmpfile)
79+
80+
try:
81+
for _i in range(5):
82+
# This is what we want to see multiple times
83+
with open(tmpfile, "w") as fp:
84+
fp.write("test1")
85+
86+
time.sleep(SLEEP)
87+
88+
while event_queue.qsize():
89+
event, _ = event_queue.get()
90+
if event.is_directory:
91+
assert event.src_path == p()
92+
continue
93+
assert event.src_path.endswith(file1)
94+
break
95+
else:
96+
raise AssertionError # No event catched
97+
98+
finally:
99+
emitter.stop()
100+
101+
102+
def test_file_access_other_file(p: P, start_watching: StartWatching, event_queue: TestEventQueue) -> None:
103+
"""Check if other files doesn't fires events."""
104+
105+
file1 = "file1.bak"
106+
tmpfile = p(file1)
107+
108+
with open(tmpfile, "w") as fp:
109+
fp.write("init1")
110+
111+
emitter = start_watching(path=tmpfile)
112+
113+
# Don't wanted
114+
with open(p("file2.bak"), "w") as fp:
115+
fp.write("test2")
116+
117+
# but this
118+
with open(tmpfile, "w") as fp:
119+
fp.write("test1")
120+
121+
time.sleep(SLEEP)
122+
123+
found_files = set()
124+
try:
125+
while event_queue.qsize():
126+
event, _ = event_queue.get()
127+
if event.is_directory:
128+
assert event.src_path == p()
129+
else:
130+
found_files.add(event.src_path)
131+
assert event.src_path.endswith(file1)
132+
finally:
133+
emitter.stop()
134+
135+
assert len(found_files) == 1, "Number of expected files differ. Wrong file catched."
136+
137+
138+
def test_create_folder(p: P, start_watching: StartWatching, event_queue: TestEventQueue) -> None:
139+
"""Check if creation of a directory and inside files doesn't fires events."""
140+
141+
file1 = "file1.bak"
142+
tmpfile = p(file1)
143+
144+
with open(tmpfile, "w") as fp:
145+
fp.write("init1")
146+
147+
emitter = start_watching(path=tmpfile)
148+
149+
# Don't wanted
150+
mkdir(p("myfolder"))
151+
with open(p("myfolder/file2.bak"), "w") as fp:
152+
fp.write("test2")
153+
154+
# but this
155+
with open(tmpfile, "w") as fp:
156+
fp.write("test1")
157+
158+
time.sleep(SLEEP)
159+
160+
found_files = set()
161+
try:
162+
while event_queue.qsize():
163+
event, _ = event_queue.get()
164+
if event.is_directory:
165+
assert event.src_path == p()
166+
else:
167+
found_files.add(event.src_path)
168+
assert event.src_path.endswith(file1)
169+
finally:
170+
emitter.stop()
171+
172+
assert len(found_files) == 1, "Number of expected files differ. Wrong file catched."

0 commit comments

Comments
 (0)