forked from TadLeonard/tfatool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_functional.py
142 lines (113 loc) · 4.25 KB
/
test_functional.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import logging
import os
import threading
import time
logging.basicConfig(level=logging.DEBUG, style="{",
format="{asctime} | {levelname} | {name} | {message}")
from tfatool import command, upload, sync
fns = [
command.count_files,
command.memory_changed,
command.get_ssid,
command.get_password,
command.get_mac,
command.get_browser_lang,
command.get_fw_version,
command.get_ctrl_image,
command.get_wifi_mode,
]
def test_upload_delete():
upload.delete_file("/DCIM/README.md")
files = command.list_files(remote_dir="/DCIM")
assert not any(f.filename == "README.md" for f in files)
upload.upload_file("README.md", remote_dir="/DCIM")
files = command.list_files(remote_dir="/DCIM")
assert any(f.filename == "README.md" for f in files)
upload.delete_file("/DCIM/README.md")
assert not any(f.filename == "README.md" for f in files)
def test_list_files():
files = command.list_files()
raw_files = command.list_files()
same_attrs = "filename directory size".split()
for n, (f, r) in enumerate(zip(files, raw_files)):
assert all(getattr(f, n) == getattr(r, n) for n in same_attrs)
assert n > 3, "too few files to test"
def test_smoke():
"""Smoke test: just runs every command.cgi function"""
for fn in fns:
val = fn()
print("\n\n*** Command {}:\n{} (type: {})".format(
fn.__name__, val, type(val)))
def _prepare_test_files(local_only=False):
test_names = ["__testfile{0}".format(n) for n in range(2, -1, -1)]
name_filter = lambda f: f.filename.startswith("__testfile")
for f in test_names:
os.system("touch {}".format(f))
if not local_only:
upload.delete_file("/DCIM/{}".format(f))
return test_names, name_filter
def _teardown_test_files(names):
for name in names:
try:
os.remove(name)
except OSError:
pass
def test_watch_local():
watcher = sync.watch_local_files()
new_files, all_files = next(watcher)
assert not new_files
names, name_filter = _prepare_test_files(local_only=True)
new_files, all_files = next(watcher)
assert {f.filename for f in new_files} == set(names)
new_files, all_files = next(watcher)
assert not new_files
_teardown_test_files(names)
def test_sync_up_by_time():
names, name_filter = _prepare_test_files()
sync.up_by_time(name_filter, remote_dir="/DCIM", count=1)
files = command.list_files(name_filter, remote_dir="/DCIM")
files = list(files)
assert len(files) == 1
assert files[0].path == "/DCIM/__testfile0"
upload.delete_file(files[0].path)
_teardown_test_files(names)
def test_sync_up_by_name():
names, name_filter = _prepare_test_files()
sync.up_by_name(name_filter, remote_dir="/DCIM", count=2)
files = command.list_files(name_filter, remote_dir="/DCIM")
files = list(files)
assert len(files) == 2
assert [f.filename for f in files] == ["__testfile2", "__testfile1"]
for f in files:
upload.delete_file(f.path)
_teardown_test_files(names)
def test_sync_up_by_arrival_threaded():
names, name_filter = _prepare_test_files()
_teardown_test_files(names)
monitor = sync.Monitor(name_filter, remote_dir="/DCIM")
monitor.sync_up()
time.sleep(0.2)
_prepare_test_files() # files get `touch`ed
time.sleep(2)
monitor.stop()
files = list(command.list_files(name_filter, remote_dir="/DCIM"))
for f in files:
upload.delete_file(f.path)
_teardown_test_files(names)
assert len(files) == len(names)
def test_sync_up_by_arrival():
names, name_filter = _prepare_test_files()
_teardown_test_files(names)
to_upload = sync.up_by_arrival(name_filter, remote_dir="/DCIM")
_, new = next(to_upload) # nothing new yet
assert not new # empty set
_prepare_test_files() # files get `touch`ed
_, new = next(to_upload) # should be something new to upload
assert len(new) == len(names)
_, new = next(to_upload) # triggers upload, then yields an empty set again
assert not new
files = list(command.list_files(name_filter, remote_dir="/DCIM"))
for f in files:
upload.delete_file(f.path)
_teardown_test_files(names)
assert len(files) == len(names)