forked from dps/rpi-timelapse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_wrappers.py
133 lines (98 loc) · 4.64 KB
/
test_wrappers.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
import subprocess
import time
import unittest
from wrappers import GPhoto
from wrappers import Identify
from wrappers import NetworkInfo
class FakePopen(object):
def __init__(self, out, err, ret):
self._out = out
self._err = err
self.returncode = ret
def communicate(self):
return self._out, self._err
class FakeSubprocess(object):
PIPE = 'pipe'
def __init__(self):
self._fakes = {}
self._invocations = []
pass
def set_Popen_for_cmd(self, cmd, popen):
self._fakes[cmd] = popen
def Popen(self, cmd, shell, stdout, stderr):
self._invocations.append(cmd)
if type(cmd) == type([]):
cmd = cmd[0]
if cmd in self._fakes.keys():
return self._fakes[cmd]
return FakePopen("out", "err", 0)
def get_invocations(self):
return self._invocations
class NetworkInfoTestCase(unittest.TestCase):
def setUp(self):
self._test_subprocess = FakeSubprocess()
self._network_info = NetworkInfo(self._test_subprocess)
def tearDown(self):
pass
def test_no_network(self):
assert self._network_info.network_status() == 'No Network'
assert 'iwconfig' in self._test_subprocess.get_invocations()
assert 'ifconfig wlan0' in self._test_subprocess.get_invocations()
assert 'ifconfig eth0' in self._test_subprocess.get_invocations()
def test_ethernet(self):
data = ''.join(file('testdata/ifceth0', 'r').readlines())
popen = FakePopen(data, '', 0)
self._test_subprocess.set_Popen_for_cmd('ifconfig eth0', popen)
assert '10.1.10.15' in self._network_info.network_status()
assert 'iwconfig' in self._test_subprocess.get_invocations()
assert 'ifconfig wlan0' in self._test_subprocess.get_invocations()
assert 'ifconfig eth0' in self._test_subprocess.get_invocations()
def test_wlan(self):
data = ''.join(file('testdata/ifceth0_nc', 'r').readlines())
popen = FakePopen(data, '', 0)
self._test_subprocess.set_Popen_for_cmd('ifconfig eth0', popen)
data = ''.join(file('testdata/ifcwlan0', 'r').readlines())
popen = FakePopen(data, '', 0)
self._test_subprocess.set_Popen_for_cmd('ifconfig wlan0', popen)
data = ''.join(file('testdata/iwc', 'r').readlines())
popen = FakePopen(data, '', 0)
self._test_subprocess.set_Popen_for_cmd('iwconfig', popen)
assert '10.1.10.15' in self._network_info.network_status()
assert '146csbr' in self._network_info.network_status()
assert 'iwconfig' in self._test_subprocess.get_invocations()
assert 'ifconfig wlan0' in self._test_subprocess.get_invocations()
assert 'ifconfig eth0' in self._test_subprocess.get_invocations()
class IdentifyTestCase(unittest.TestCase):
def setUp(self):
self._test_subprocess = FakeSubprocess()
self._identify = Identify(self._test_subprocess)
def tearDown(self):
pass
def test_identify(self):
popen = FakePopen('test.jpg JPEG 1728x1152 1728x1152+0+0 8-bit DirectClass 652KB 0.000u 0:00.009', '', 0)
self._test_subprocess.set_Popen_for_cmd('identify test.jpg', popen)
assert 'JPEG 1728x1152' in self._identify.summary('test.jpg')
assert 'identify test.jpg' in self._test_subprocess.get_invocations()
def test_brightness(self):
self._identify.mean_brightness('test.jpg')
assert 'identify -format "%[mean]" test.jpg' in self._test_subprocess.get_invocations()
class GPhotoTestCase(unittest.TestCase):
def setUp(self):
self._test_subprocess = FakeSubprocess()
self._gphoto = GPhoto(self._test_subprocess)
def tearDown(self):
pass
def test_set_shutter_speed(self):
popen = FakePopen('Label: Shutter Speed\nType: MENU\nCurrent: 30\nChoice: 0 Bulb\nChoice: 4 2', '', 0)
self._test_subprocess.set_Popen_for_cmd('gphoto2 --get-config /main/settings/shutterspeed', popen)
self._gphoto.set_shutter_speed(secs="2")
assert ['gphoto2 --get-config /main/settings/shutterspeed'] in self._test_subprocess.get_invocations()
assert ['gphoto2 --set-config /main/settings/shutterspeed=4'] in self._test_subprocess.get_invocations()
def test_get_camera_time(self):
data = ''.join(file('testdata/datetime', 'r').readlines())
popen = FakePopen(data, '', 0)
self._test_subprocess.set_Popen_for_cmd('gphoto2 --get-config /main/status/datetime', popen)
tim = self._gphoto.get_camera_date_time()
assert time.strptime("2013-01-10 07:16:59", "%Y-%m-%d %H:%M:%S") == tim
if __name__ == '__main__':
unittest.main()