-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·159 lines (130 loc) · 6.21 KB
/
test.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import unittest
from DmxOscServer import *
from pythonosc.osc_message_builder import OscMessageBuilder
def _(fix, address, *args): return
class TestDmxOscServer(unittest.TestCase):
def test_fixture_string(self):
"""
Test the Fixture.__str__()
"""
fixture1 = Fixture(0, 1, 2, _)
self.assertEqual(fixture1.__str__(), "Fixture(universe=0, starting_addr=1, channels=2, channel_as_array=False, values=[0, 0])")
fixture2 = Fixture(universe=1, starting_addr=3, channels=2, handler=_, channel_as_array=False)
self.assertEqual(fixture2.__str__(), "Fixture(universe=1, starting_addr=3, channels=2, channel_as_array=False, values=[0, 0])")
def test_fixture_call(self):
"""
Test the Fixture.__call__()
"""
fixture = Fixture(universe=0, starting_addr=1, channels=5, handler=_)
fixture(1, 0.1) # Call at address 1 which is the first channel
fixture(2, 0.6) # Call at address 2
self.assertEqual(fixture.values, [0.1, 0.6, 0, 0, 0])
def test_fixture_call_array(self):
"""
Test the Fixture.__call__() with array
"""
fixture = Fixture(universe=0, starting_addr=1, channels=2, handler=_, channel_as_array=False)
fixture(1, [0.1, 0.6])
self.assertEqual(fixture.values, [0.1, 0.6])
fixture = Fixture(universe=0, starting_addr=1, channels=2, handler=_, channel_as_array=True)
fixture(1, [0.1, 0.6])
fixture(2, [0.4, 0.8])
self.assertEqual(fixture.values, [[0.1, 0.6], [0.4, 0.8]])
def test_fixture_call_arguments(self):
"""
Test the Fixture.__call__() with multiple arguments
"""
fixture = Fixture(universe=0, starting_addr=1, channels=2, handler=_, channel_as_array=True)
fixture(1, 0.1, 0.6)
self.assertEqual(fixture.values, [[0.1, 0.6],[0]])
def test_server_define_fixture(self):
"""
Test the @server.define_fixture()
"""
server = DmxOscServer()
@server.define_fixture(0, 1, 2)
def handle(): return
@server.define_fixture(universe=0, starting_addr=3, channels=2)
def handle(): return
@server.define_fixture(0, 5, 2, True)
def handle(): return
@server.define_fixture(universe=0, starting_addr=7, channels=2, channel_as_array=True)
def handle(): return
self.assertEqual(server.list_fixtures()[0].__str__(), "Fixture(universe=0, starting_addr=1, channels=2, channel_as_array=False, values=[0, 0])")
self.assertEqual(server.list_fixtures()[1].__str__(), "Fixture(universe=0, starting_addr=3, channels=2, channel_as_array=False, values=[0, 0])")
self.assertEqual(server.list_fixtures()[2].__str__(), "Fixture(universe=0, starting_addr=5, channels=2, channel_as_array=True, values=[[0], [0]])")
self.assertEqual(server.list_fixtures()[3].__str__(), "Fixture(universe=0, starting_addr=7, channels=2, channel_as_array=True, values=[[0], [0]])")
def test_server_add_fixture(self):
"""
Test the server.add_fixture()
"""
fix = Fixture(0, 1, 2, _)
server = DmxOscServer()
server.add_fixture(fix)
self.assertEqual(server.list_fixtures()[0], fix)
def test_server_add_fixtures(self):
"""
Test the server.add_fixtures()
"""
fix1 = Fixture(0, 1, 2, _)
fix2 = Fixture(0, 3, 4, _)
server = DmxOscServer()
server.add_fixtures(fix1, fix2)
self.assertEqual(server.list_fixtures(), [fix1, fix2])
def test_server_list_fixtures(self):
"""
Test the server.list_fixtures()
"""
fix1 = Fixture(0, 1, 2, _)
fix2 = Fixture(0, 3, 4, _)
fix3 = Fixture(1, 1, 2, _)
fix4 = Fixture(2, 3, 4, _)
server = DmxOscServer()
server.add_fixtures(fix1, fix2, fix3, fix4)
self.assertEqual(server.list_fixtures(), [fix1, fix2, fix3, fix4])
self.assertEqual(server.list_fixtures(0), [fix1, fix2])
self.assertEqual(server.list_fixtures(1), [fix3])
self.assertEqual(server.list_fixtures(2), [fix4])
def test_server_raises_fixtures(self):
"""
Test the exceptions of server.add_fixtures()
"""
class obj:
def __init__(self, uni, st, ch, hd):
self.universe=uni
self.starting_addr=st
self.channels=ch
self.handler=hd
notfix = obj(0, 1, 3, _)
fix1 = Fixture(0, 1, 3, _)
fix2 = Fixture(1, 1, 4, _)
fix3 = Fixture(0, 2, 4, _)
server = DmxOscServer()
# Test non-fixture
with self.assertRaises(TypeError) as context: server.add_fixture(notfix)
self.assertTrue('Not a Fixture!' in str(context.exception))
# Test duplicates
server.add_fixture(fix1)
server.add_fixture(fix2) # check if universes don't matter by adding to universe 1
with self.assertRaises(ValueError) as context: server.add_fixture(fix3)
self.assertTrue('There is already a Fixture within that address!' in str(context.exception))
# Test call with out of range
with self.assertRaises(ValueError) as context: fix1(5, 0.5) # address 5 is out of the range
self.assertTrue('Address 5 is not within the address range!' in str(context.exception))
# Test read-only properties
for prop in ["universe","starting_addr","channels","end_addr","address_range","channel_as_array"]:
with self.assertRaises(AttributeError) as context: fix1.__setattr__(prop, 1)
self.assertTrue("Can't modify {}".format(prop) in str(context.exception))
def test_server_dispatcher(self):
"""
Test the dmx_handler using the OSC Dispatcher to make sure the code works
"""
server = DmxOscServer()
@server.define_fixture(0, 1, 2)
def handler(fix, addr, *args): self.assertEqual(fix.__str__(), "Fixture(universe=0, starting_addr=1, channels=2, channel_as_array=False, values=[0.5, 0])")
msg = OscMessageBuilder("/0/dmx/1")
msg.add_arg(0.5)
for handler in server._dispatcher.handlers_for_address("/0/dmx/1"): handler.invoke("", msg.build())
if __name__ == '__main__':
unittest.main()