Skip to content

Commit cad5a73

Browse files
committed
Add test cases for ExpectedResults classes.
(cherry picked from commit f6de4c2)
1 parent 2185fdc commit cad5a73

5 files changed

Lines changed: 114 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ tools/ola_trigger/ola_trigger
225225
tools/ola_trigger/ola_trigger.exe
226226
tools/ola_trigger/FileValidateTest.sh
227227
tools/rdm/DataLocation.py
228+
tools/rdm/ExpectedResultsTest.sh
228229
tools/rdm/ResponderTestTest.sh
229230
tools/rdm/TestHelpersTest.sh
230231
tools/rdm/TestStateTest.sh

tools/rdm/ExpectedResults.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
# Expected result classes are broken down as follows:
1919
#
2020
# BaseExpectedResult - the base class
21-
# BroadcastResult - expects the request to be broadcast
21+
# BroadcastResult - expects the request to be broadcast, hence no response
22+
# TimeoutResult - expects the response to be a timeout
23+
# InvalidResult - expects the response to be invalid
24+
# UnsupportedResult - expects RDM Discovery to be unsupported
25+
# DUBResult - expects an RDM DUB response
2226
# SuccessfulResult - expects a well formed response from the device
2327
# NackResult - parent NACK class
28+
# NackDiscoveryResult - expects DISCOVERY_COMMAND_RESPONSE with a NACK
2429
# NackGetResult - expects GET_COMMAND_RESPONSE with a NACK
2530
# NackSetResult - expects SET_COMMAND_RESPONSE with a NACK
2631
# AckResult - parent ACK class
32+
# AckDiscoveryResult - expects DISCOVERY_COMMAND_RESPONSE with an ACK
2733
# AckGetResult - expects GET_COMMAND_RESPONSE with an ACK
2834
# AckSetResult - expects SET_COMMAND_RESPONSE with an ACK
2935
# QueuedMessageResult - expects an ACK or NACK for any PID other than
@@ -134,8 +140,8 @@ class SuccessfulResult(BaseExpectedResult):
134140
"""This checks that we received a valid response from the device.
135141
136142
This doesn't check that the response was a certain type, but simply that the
137-
message was formed correctly. Other classes inherit from this an perform more
138-
specific checking.
143+
message was formed correctly. Other classes inherit from this and perform
144+
more specific checking.
139145
"""
140146
def __str__(self):
141147
return 'RDM_COMPLETED_OK'

tools/rdm/ExpectedResultsTest.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
# This program is free software; you can redistribute it and/or modify
3+
# it under the terms of the GNU General Public License as published by
4+
# the Free Software Foundation; either version 2 of the License, or
5+
# (at your option) any later version.
6+
#
7+
# This program is distributed in the hope that it will be useful,
8+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
# GNU Library General Public License for more details.
11+
#
12+
# You should have received a copy of the GNU General Public License
13+
# along with this program; if not, write to the Free Software
14+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15+
#
16+
# ExpectedResultsTest.py
17+
# Copyright (C) 2021 Peter Newman
18+
19+
import unittest
20+
from ExpectedResults import (BroadcastResult, DUBResult, InvalidResponse,
21+
TimeoutResult, SuccessfulResult, UnsupportedResult)
22+
from ola.OlaClient import OlaClient
23+
from collections import namedtuple
24+
25+
"""Test cases for ExpectedResults classes."""
26+
27+
__author__ = 'nomis52@gmail.com (Simon Newton)'
28+
29+
30+
class ExpectedResultsTest(unittest.TestCase):
31+
MockResponse = namedtuple('MockResponse', ['response_code'])
32+
MockBroadcastResponse = MockResponse(OlaClient.RDM_WAS_BROADCAST)
33+
MockTimeoutResponse = MockResponse(OlaClient.RDM_TIMEOUT)
34+
MockInvalidResponse = MockResponse(OlaClient.RDM_INVALID_RESPONSE)
35+
MockUnsupportedResult = MockResponse(
36+
OlaClient.RDM_PLUGIN_DISCOVERY_NOT_SUPPORTED)
37+
MockDUBResult = MockResponse(OlaClient.RDM_DUB_RESPONSE)
38+
MockSuccessfulResult = MockResponse(OlaClient.RDM_COMPLETED_OK)
39+
40+
def testBroadcastResult(self):
41+
br = BroadcastResult()
42+
self.assertTrue(br.Matches(self.MockBroadcastResponse, {}))
43+
self.assertFalse(br.Matches(self.MockTimeoutResponse, {}))
44+
self.assertFalse(br.Matches(self.MockInvalidResponse, {}))
45+
self.assertFalse(br.Matches(self.MockUnsupportedResult, {}))
46+
self.assertFalse(br.Matches(self.MockDUBResult, {}))
47+
self.assertFalse(br.Matches(self.MockSuccessfulResult, {}))
48+
49+
def testTimeoutResult(self):
50+
tr = TimeoutResult()
51+
self.assertFalse(tr.Matches(self.MockBroadcastResponse, {}))
52+
self.assertTrue(tr.Matches(self.MockTimeoutResponse, {}))
53+
self.assertFalse(tr.Matches(self.MockInvalidResponse, {}))
54+
self.assertFalse(tr.Matches(self.MockUnsupportedResult, {}))
55+
self.assertFalse(tr.Matches(self.MockDUBResult, {}))
56+
self.assertFalse(tr.Matches(self.MockSuccessfulResult, {}))
57+
58+
def testInvalidResponse(self):
59+
ir = InvalidResponse()
60+
self.assertFalse(ir.Matches(self.MockBroadcastResponse, {}))
61+
self.assertFalse(ir.Matches(self.MockTimeoutResponse, {}))
62+
self.assertTrue(ir.Matches(self.MockInvalidResponse, {}))
63+
self.assertFalse(ir.Matches(self.MockUnsupportedResult, {}))
64+
self.assertFalse(ir.Matches(self.MockDUBResult, {}))
65+
self.assertFalse(ir.Matches(self.MockSuccessfulResult, {}))
66+
67+
def testUnsupportedResult(self):
68+
ur = UnsupportedResult()
69+
self.assertFalse(ur.Matches(self.MockBroadcastResponse, {}))
70+
self.assertFalse(ur.Matches(self.MockTimeoutResponse, {}))
71+
self.assertFalse(ur.Matches(self.MockInvalidResponse, {}))
72+
self.assertTrue(ur.Matches(self.MockUnsupportedResult, {}))
73+
self.assertFalse(ur.Matches(self.MockDUBResult, {}))
74+
self.assertFalse(ur.Matches(self.MockSuccessfulResult, {}))
75+
76+
def testDUBResult(self):
77+
dr = DUBResult()
78+
self.assertFalse(dr.Matches(self.MockBroadcastResponse, {}))
79+
self.assertFalse(dr.Matches(self.MockTimeoutResponse, {}))
80+
self.assertFalse(dr.Matches(self.MockInvalidResponse, {}))
81+
self.assertFalse(dr.Matches(self.MockUnsupportedResult, {}))
82+
self.assertTrue(dr.Matches(self.MockDUBResult, {}))
83+
self.assertFalse(dr.Matches(self.MockSuccessfulResult, {}))
84+
85+
def testSuccessfulResult(self):
86+
sr = SuccessfulResult()
87+
self.assertFalse(sr.Matches(self.MockBroadcastResponse, {}))
88+
self.assertFalse(sr.Matches(self.MockTimeoutResponse, {}))
89+
self.assertFalse(sr.Matches(self.MockInvalidResponse, {}))
90+
self.assertFalse(sr.Matches(self.MockUnsupportedResult, {}))
91+
self.assertFalse(sr.Matches(self.MockDUBResult, {}))
92+
self.assertTrue(sr.Matches(self.MockSuccessfulResult, {}))
93+
94+
95+
if __name__ == '__main__':
96+
unittest.main()

tools/rdm/Makefile.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ launcher_files = \
5555

5656
EXTRA_DIST += $(launcher_files)
5757

58+
tools/rdm/ExpectedResultsTest.sh: tools/rdm/Makefile.mk
59+
mkdir -p $(top_builddir)/python/ola
60+
echo "PYTHONPATH=${top_builddir}/python $(PYTHON) ${srcdir}/tools/rdm/ExpectedResultsTest.py; exit \$$?" > $(top_builddir)/tools/rdm/ExpectedResultsTest.sh
61+
chmod +x $(top_builddir)/tools/rdm/ExpectedResultsTest.sh
62+
5863
tools/rdm/ResponderTestTest.sh: tools/rdm/Makefile.mk
5964
mkdir -p $(top_builddir)/python/ola
6065
echo "PYTHONPATH=${top_builddir}/python $(PYTHON) ${srcdir}/tools/rdm/ResponderTestTest.py; exit \$$?" > $(top_builddir)/tools/rdm/ResponderTestTest.sh
@@ -71,19 +76,22 @@ tools/rdm/TestStateTest.sh: tools/rdm/Makefile.mk
7176
chmod +x $(top_builddir)/tools/rdm/TestStateTest.sh
7277

7378
dist_check_SCRIPTS += \
79+
tools/rdm/ExpectedResultsTest.py \
7480
tools/rdm/ResponderTestTest.py \
7581
tools/rdm/TestHelpersTest.py \
7682
tools/rdm/TestStateTest.py
7783

7884
if BUILD_PYTHON_LIBS
7985
test_scripts += \
86+
tools/rdm/ExpectedResultsTest.sh \
8087
tools/rdm/ResponderTestTest.sh \
8188
tools/rdm/TestHelpersTest.sh \
8289
tools/rdm/TestStateTest.sh
8390
endif
8491

8592
CLEANFILES += \
8693
tools/rdm/*.pyc \
94+
tools/rdm/ExpectedResultsTest.sh \
8795
tools/rdm/ResponderTestTest.sh \
8896
tools/rdm/TestHelpersTest.sh \
8997
tools/rdm/TestStateTest.sh \

tools/rdm/ResponderTestTest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class ZTestFixture(TestFixture):
3333

3434

3535
class TestFixtureTest(unittest.TestCase):
36-
3736
def testCmp(self):
3837
base = TestFixture({}, 2, 123, None)
3938
base2 = TestFixture({}, 3, 456, None)

0 commit comments

Comments
 (0)